From efedbd2bdbb1444f42248c44fb777ce237b92d6c Mon Sep 17 00:00:00 2001 From: Michael Schappacher Date: Thu, 29 Sep 2022 15:19:43 -0400 Subject: [PATCH] fix(remove external library): Using upip to install dependencies --- .python-version | 1 + firmware/picowebrouter/__init__.py | 67 ----------------------- firmware/picowebrouter/objects/request.py | 34 ------------ firmware/picowebrouter/utils.py | 28 ---------- 4 files changed, 1 insertion(+), 129 deletions(-) create mode 100644 .python-version delete mode 100644 firmware/picowebrouter/__init__.py delete mode 100644 firmware/picowebrouter/objects/request.py delete mode 100644 firmware/picowebrouter/utils.py diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..6f80e59 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +micropython-1.19.1 diff --git a/firmware/picowebrouter/__init__.py b/firmware/picowebrouter/__init__.py deleted file mode 100644 index b977df3..0000000 --- a/firmware/picowebrouter/__init__.py +++ /dev/null @@ -1,67 +0,0 @@ -from .objects.request import * -from .utils import walk_directories -import socket -import uos as os - - -class WebRouter: - def __init__(self, ip, port, default, static_location="static"): - self.routes = {"default": default} - self.socket = self.open_socket(ip, port) - self.static_location = static_location - self.static_files = [] - self.add_static() - - def route(self, path): - def router(function): - def wrapper(*args, **kwargs): - self.routes[path] = function - - return wrapper - - return router - - def add_static(self): - static_files = walk_directories(self.static_location) - if static_files: - for directory in static_files.keys(): - for file in static_files[directory]: - self.static_files.append(f"{directory}/{file}") - - @staticmethod - def open_socket(ip, port): - address = (ip, port) - new_socket = socket.socket() - new_socket.bind((ip, port)) - new_socket.listen(1) - return new_socket - - @staticmethod - def determine_mimetype(path): - mimetype = "text/html" - if path.endswith(".js"): - mimetype = "text/javascript" - if path.endswith(".css"): - mimetype = "text/css" - return mimetype - - def serve(self): - while True: - client = self.socket.accept()[0] - request = client.recv(1024) - if request: - request = Request(request.decode("utf-8")) - path = request.path - header = "HTTP/1.1 200 OK\n" - mimetype = self.determine_mimetype(path) - if path in self.routes.keys(): - webpage = self.routes[path](request=request) - elif path in self.static_files: - with open(path, "rb") as static: - webpage = static.read() - else: - webpage = self.routes["default"](request=request) - header += f"Content-Type: {mimetype}\n\n" - client.sendall(webpage) - client.close() - diff --git a/firmware/picowebrouter/objects/request.py b/firmware/picowebrouter/objects/request.py deleted file mode 100644 index 17c18eb..0000000 --- a/firmware/picowebrouter/objects/request.py +++ /dev/null @@ -1,34 +0,0 @@ - -class Request: - def __init__(self, request): - self.raw = request - self.method = None - self.path = None - self.query_strings = None - self.protocol = None - self.headers = None - self.body = None - self.parse_request() - - def parse_request(self): - try: - request, self.body = self.raw.split("\r\n\r\n") - except ValueError: - request = self.raw - split_request = request.split("\r\n") - request_line = split_request.pop(0) - self.method, self.path, self.protocol = request_line.split(" ") - if "?" in self.path: - query_strings = {} - self.path, raw_query = self.path.split("?") - raw_query = raw_query.split("&") - for query in raw_query: - key, value = query.split("=") - query_strings[key] = value - self.query_strings = query_strings - - headers = {} - for header in split_request: - key, value = header.split(": ") - headers[key] = value - self.headers = headers diff --git a/firmware/picowebrouter/utils.py b/firmware/picowebrouter/utils.py deleted file mode 100644 index f840d2d..0000000 --- a/firmware/picowebrouter/utils.py +++ /dev/null @@ -1,28 +0,0 @@ -import uos as os - - -def walk_directories(initial_directory="/"): - if not initial_directory.startswith("/"): - initial_directory = f"/{initial_directory}" - directories = [initial_directory] - paths = {} - while directories: - for directory in directories: - new_directories, files = recursive_walk(directory) - paths[directory] = files - directories = new_directories - return paths - -def recursive_walk(initial_directory): - files = [] - directories = [] - unprocessed_directory = os.ilistdir(initial_directory) - if unprocessed_directory: - for item in unprocessed_directory: - if item[1] == 16384: - if initial_directory == "/": - initial_directory = "" - directories.append(f"{initial_directory}/{item[0]}") - else: - files.append(item[0]) - return (directories, files) \ No newline at end of file