Skip to content

Commit

Permalink
Add project wide flake8 settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sansyrox committed Dec 26, 2021
1 parent 8e1e64f commit fc2fda0
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 23 deletions.
8 changes: 8 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[flake8]

max-line-length = 160
exclude = src/* , .git , docs
max-complexity = 10
ignore =
# empty file at eof
W391
2 changes: 2 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flake8==4.0.1
black==21.12b0
31 changes: 16 additions & 15 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -31,15 +30,14 @@ 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 = []
self.routes = []
self.directories = []
self.web_sockets = {}


def add_route(self, route_type, endpoint, handler):
"""
[This is base handler for all the decorators]
Expand All @@ -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]
Expand All @@ -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()

Expand All @@ -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:
Expand All @@ -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]
Expand Down Expand Up @@ -154,7 +157,7 @@ def patch(self, endpoint):
"""
def inner(handler):
self.add_route("PATCH", endpoint, handler)

return inner

def head(self, endpoint):
Expand All @@ -179,7 +182,6 @@ def inner(handler):

return inner


def connect(self, endpoint):
"""
[The @app.connect decorator to add a get route]
Expand All @@ -202,4 +204,3 @@ def inner(handler):

return inner


3 changes: 1 addition & 2 deletions robyn/dev_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from watchdog.events import FileSystemEventHandler



class EventHandler(FileSystemEventHandler):
def __init__(self, file_name):
self.file_name = file_name
Expand All @@ -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}")
Expand Down
4 changes: 1 addition & 3 deletions robyn/processpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
4 changes: 3 additions & 1 deletion robyn/responses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json


def static_file(file_path):
"""
[This function will help in serving a static_file]
Expand All @@ -12,11 +13,12 @@ def static_file(file_path):
"file_path": file_path
}


def jsonify(input_dict):
"""
[This function serializes input dict to a json string]
:param input_dict [dict]: [response of the function]
"""
return json.dumps(input_dict)

2 changes: 2 additions & 0 deletions robyn/test_robyn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__

Expand Down
5 changes: 3 additions & 2 deletions robyn/ws.py
Original file line number Diff line number Diff line change
@@ -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.
"""
Expand All @@ -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

Expand Down

0 comments on commit fc2fda0

Please sign in to comment.