diff --git a/.flake8 b/.flake8 new file mode 100644 index 000000000..77a35f7cd --- /dev/null +++ b/.flake8 @@ -0,0 +1,8 @@ +[flake8] + +max-line-length = 160 +exclude = src/* , .git , docs +max-complexity = 10 +ignore = + # empty file at eof + W391 diff --git a/README.md b/README.md index b089f8dee..4e3957f08 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,10 @@ [![Gitter](https://badges.gitter.im/robyn_/community.svg)](https://gitter.im/robyn_/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) [![Downloads](https://static.pepy.tech/personalized-badge/robyn?period=total&units=international_system&left_color=grey&right_color=blue&left_text=Downloads)](https://pepy.tech/project/robyn) +[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) -[Docs](https://sansyrox.github.io/robyn/#/) +API Docs: [Docs](https://sansyrox.github.io/robyn/#/) Robyn is an async Python backend server with a runtime written in Rust, btw. diff --git a/dev-requirements.txt b/dev-requirements.txt new file mode 100644 index 000000000..591458162 --- /dev/null +++ b/dev-requirements.txt @@ -0,0 +1,2 @@ +flake8==4.0.1 +black==21.12b0 diff --git a/robyn/__init__.py b/robyn/__init__.py index 6113c708b..d86a03a25 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -3,7 +3,6 @@ import asyncio from inspect import signature import multiprocessing as mp -mp.allow_connection_pickling() # custom imports and exports from .robyn import Server, SocketHeld @@ -14,11 +13,11 @@ from .log_colors import Colors from .ws import WS - # 3rd party imports and exports from multiprocess import Process from watchdog.observers import Observer +mp.allow_connection_pickling() class Robyn: @@ -31,7 +30,7 @@ def __init__(self, file_object): self.server = Server(directory_path) self.parser = ArgumentParser() self.dev = self.parser.is_dev() - self.processes = self.parser.num_processes() + self.processes = self.parser.num_processes() self.workers = self.parser.workers() self.routes = [] self.headers = [] @@ -39,7 +38,6 @@ def __init__(self, file_object): self.directories = [] self.web_sockets = {} - def add_route(self, route_type, endpoint, handler): """ [This is base handler for all the decorators] @@ -53,22 +51,24 @@ def add_route(self, route_type, endpoint, handler): """ number_of_params = len(signature(handler).parameters) self.routes.append( - ( route_type, endpoint, handler, asyncio.iscoroutinefunction(handler), number_of_params) + (route_type, + endpoint, + handler, + asyncio.iscoroutinefunction(handler), number_of_params) ) def add_directory(self, route, directory_path, index_file=None, show_files_listing=False): - self.directories.append(( route, directory_path, index_file, show_files_listing )) + self.directories.append((route, directory_path, index_file, show_files_listing)) def add_header(self, key, value): - self.headers.append(( key, value )) + self.headers.append((key, value)) def remove_header(self, key): self.server.remove_header(key) def add_web_socket(self, endpoint, ws): self.web_sockets[endpoint] = ws - - + def start(self, url="127.0.0.1", port=5000): """ [Starts the server] @@ -82,7 +82,9 @@ def start(self, url="127.0.0.1", port=5000): copied = socket.try_clone() p = Process( target=spawn_process, - args=(url, port, self.directories, self.headers, self.routes, self.web_sockets, copied, f"Process {process_number}", workers), + args=(url, port, self.directories, self.headers, + self.routes, self.web_sockets, copied, + f"Process {process_number}", workers), ) p.start() @@ -92,7 +94,9 @@ def start(self, url="127.0.0.1", port=5000): event_handler.start_server_first_time() print(f"{Colors.OKBLUE}Dev server initialised with the directory_path : {self.directory_path}{Colors.ENDC}") observer = Observer() - observer.schedule(event_handler, path=self.directory_path, recursive=True) + observer.schedule(event_handler, + path=self.directory_path, + recursive=True) observer.start() try: while True: @@ -101,7 +105,6 @@ def start(self, url="127.0.0.1", port=5000): observer.stop() observer.join() - def get(self, endpoint): """ [The @app.get decorator to add a get route] @@ -154,7 +157,7 @@ def patch(self, endpoint): """ def inner(handler): self.add_route("PATCH", endpoint, handler) - + return inner def head(self, endpoint): @@ -179,7 +182,6 @@ def inner(handler): return inner - def connect(self, endpoint): """ [The @app.connect decorator to add a get route] @@ -202,4 +204,3 @@ def inner(handler): return inner - diff --git a/robyn/dev_event_handler.py b/robyn/dev_event_handler.py index 0eca9d393..eadca5280 100644 --- a/robyn/dev_event_handler.py +++ b/robyn/dev_event_handler.py @@ -8,7 +8,6 @@ from watchdog.events import FileSystemEventHandler - class EventHandler(FileSystemEventHandler): def __init__(self, file_name): self.file_name = file_name @@ -27,7 +26,7 @@ def on_any_event(self, event): :param event [FSEvent]: [a data structure with info about the events] """ - if len(self.processes)>0: + if len(self.processes) > 0: for process in self.processes: process.terminate() print(f"{Colors.OKGREEN}Starting the server in dev mode{Colors.ENDC}") diff --git a/robyn/processpool.py b/robyn/processpool.py index 729ff1c8c..bb3cf2211 100644 --- a/robyn/processpool.py +++ b/robyn/processpool.py @@ -23,7 +23,7 @@ def spawn_process(url, port, directories, headers, routes, web_sockets, socket, :param process_name string: This is the name given to the process to identify the process :param workers number: This is the name given to the process to identify the process """ - # platform_name = platform.machine() + # platform_name = platform.machine() if sys.platform.startswith("win32") or sys.platform.startswith("linux-cross"): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) @@ -46,7 +46,6 @@ def spawn_process(url, port, directories, headers, routes, web_sockets, socket, for key, val in headers: server.add_header(key, val) - for route in routes: route_type, endpoint, handler, is_async, number_of_params = route server.add_route(route_type, endpoint, handler, is_async, number_of_params) @@ -56,6 +55,5 @@ def spawn_process(url, port, directories, headers, routes, web_sockets, socket, print(web_socket.methods) server.add_web_socket_route(endpoint, web_socket.methods["connect"], web_socket.methods["close"], web_socket.methods["message"]) - server.start(url, port, socket, process_name, workers) asyncio.get_event_loop().run_forever() diff --git a/robyn/responses.py b/robyn/responses.py index b261a06cc..a754b7562 100644 --- a/robyn/responses.py +++ b/robyn/responses.py @@ -1,5 +1,6 @@ import json + def static_file(file_path): """ [This function will help in serving a static_file] @@ -12,6 +13,7 @@ def static_file(file_path): "file_path": file_path } + def jsonify(input_dict): """ [This function serializes input dict to a json string] @@ -19,4 +21,4 @@ def jsonify(input_dict): :param input_dict [dict]: [response of the function] """ return json.dumps(input_dict) - + diff --git a/robyn/test_robyn.py b/robyn/test_robyn.py index 6b895a9c6..1f28f17e9 100644 --- a/robyn/test_robyn.py +++ b/robyn/test_robyn.py @@ -3,9 +3,11 @@ app = Robyn(__file__) + def test_directory_path(): assert app.directory_path == os.path.dirname(os.path.abspath(__file__)) + def test_file_path(): assert app.file_path == __file__ diff --git a/robyn/ws.py b/robyn/ws.py index ffc590462..23c7c0abf 100644 --- a/robyn/ws.py +++ b/robyn/ws.py @@ -1,6 +1,7 @@ import asyncio from inspect import signature + class WS: """This is the python wrapper for the web socket that will be used here. """ @@ -14,8 +15,8 @@ def inner(handler): if type not in ["connect", "close", "message"]: raise Exception(f"Socket method {type} does not exist") else: - self.methods[type] = ( handler, self._is_async(handler), self._num_params(handler) ) - self.robyn_object.add_web_socket(self.endpoint, self) + self.methods[type] = (handler, self._is_async(handler), self._num_params(handler)) + self.robyn_object.add_web_socket(self.endpoint, self) return inner diff --git a/src/server.rs b/src/server.rs index a7c70c4bc..a88e8aeb3 100644 --- a/src/server.rs +++ b/src/server.rs @@ -49,10 +49,10 @@ impl Server { pub fn start( &mut self, py: Python, - url: String, - port: u16, + _url: String, + _port: u16, socket: &PyCell, - name: String, + _name: String, workers: usize, ) -> PyResult<()> { if STARTED diff --git a/src/web_socket_connection.rs b/src/web_socket_connection.rs index 3a77e2f89..da7172a1f 100644 --- a/src/web_socket_connection.rs +++ b/src/web_socket_connection.rs @@ -95,7 +95,7 @@ impl StreamHandler> for MyWs { ctx.pong(&msg) } - Ok(ws::Message::Text(text)) => { + Ok(ws::Message::Text(_text)) => { // need to also passs this text as a param let handler_function = &self.router.get("message").unwrap().0; let _number_of_params = &self.router.get("message").unwrap().1;