Skip to content

Commit

Permalink
Add Type Stubs (sparckles#192)
Browse files Browse the repository at this point in the history
* Add type stubs

* Remove generic type stubs

* Remove inconsistent docstrings
  • Loading branch information
sansyrox authored May 8, 2022
1 parent 7b67905 commit 77064fb
Show file tree
Hide file tree
Showing 16 changed files with 356 additions and 47 deletions.
50 changes: 26 additions & 24 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from robyn.events import Events

# custom imports and exports
from .robyn import Server, SocketHeld
from .robyn import SocketHeld
from .argument_parser import ArgumentParser
from .responses import static_file, jsonify
from .dev_event_handler import EventHandler
Expand Down Expand Up @@ -55,18 +55,20 @@ def _add_route(self, route_type, endpoint, handler):

def before_request(self, endpoint):
"""
[The @app.before_request decorator to add a get route]
The @app.before_request decorator to add a get route
:param endpoint [str]: [endpoint to server the route]
:param endpoint str: endpoint to server the route
"""

return self.middleware_router.add_before_request(endpoint)

def after_request(self, endpoint):
"""
[The @app.after_request decorator to add a get route]
The @app.after_request decorator to add a get route
:param endpoint [str]: [endpoint to server the route]
:param endpoint str: endpoint to server the route
"""

return self.middleware_router.add_after_request(endpoint)

def add_directory(
Expand Down Expand Up @@ -96,10 +98,11 @@ def shutdown_handler(self, handler):

def start(self, url="127.0.0.1", port=5000):
"""
[Starts the server]
Starts the server
:param port [int]: [reperesents the port number at which the server is listening]
:param port int: reperesents the port number at which the server is listening
"""

if not self.dev:
workers = self.workers
socket = SocketHeld(url, port)
Expand Down Expand Up @@ -139,9 +142,9 @@ def start(self, url="127.0.0.1", port=5000):

def get(self, endpoint):
"""
[The @app.get decorator to add a get route]
The @app.get decorator to add a get route
:param endpoint [str]: [endpoint to server the route]
:param endpoint str: endpoint to server the route
"""

def inner(handler):
Expand All @@ -151,9 +154,9 @@ def inner(handler):

def post(self, endpoint):
"""
[The @app.post decorator to add a get route]
The @app.post decorator to add a get route
:param endpoint [str]: [endpoint to server the route]
:param endpoint str: endpoint to server the route
"""

def inner(handler):
Expand All @@ -163,21 +166,20 @@ def inner(handler):

def put(self, endpoint):
"""
[The @app.put decorator to add a get route]
The @app.put decorator to add a get route
:param endpoint [str]: [endpoint to server the route]
:param endpoint str: endpoint to server the route
"""

def inner(handler):
self._add_route("PUT", endpoint, handler)

return inner

def delete(self, endpoint):
"""
[The @app.delete decorator to add a get route]
The @app.delete decorator to add a get route
:param endpoint [str]: [endpoint to server the route]
:param endpoint str: endpoint to server the route
"""

def inner(handler):
Expand All @@ -199,9 +201,9 @@ def inner(handler):

def head(self, endpoint):
"""
[The @app.head decorator to add a get route]
The @app.head decorator to add a get route
:param endpoint [str]: [endpoint to server the route]
:param endpoint str: endpoint to server the route
"""

def inner(handler):
Expand All @@ -211,9 +213,9 @@ def inner(handler):

def options(self, endpoint):
"""
[The @app.options decorator to add a get route]
The @app.options decorator to add a get route
:param endpoint [str]: [endpoint to server the route]
:param endpoint str: endpoint to server the route
"""

def inner(handler):
Expand All @@ -223,9 +225,9 @@ def inner(handler):

def connect(self, endpoint):
"""
[The @app.connect decorator to add a get route]
The @app.connect decorator to add a get route
:param endpoint [str]: [endpoint to server the route]
:param endpoint str: endpoint to server the route
"""

def inner(handler):
Expand All @@ -235,9 +237,9 @@ def inner(handler):

def trace(self, endpoint):
"""
[The @app.trace decorator to add a get route]
The @app.trace decorator to add a get route
:param endpoint [str]: [endpoint to server the route]
:param endpoint str: endpoint to server the route
"""

def inner(handler):
Expand Down
130 changes: 130 additions & 0 deletions robyn/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import asyncio
import multiprocessing as mp
import os
from inspect import signature
from typing import Callable

from multiprocess import Process
from watchdog.observers import Observer

from robyn.events import Events

from .argument_parser import ArgumentParser
from .dev_event_handler import EventHandler
from .log_colors import Colors
from .processpool import spawn_process
from .responses import jsonify, static_file
from .robyn import Server, SocketHeld
from .router import MiddlewareRouter, Router, WebSocketRouter
from .ws import WS


class Robyn:
"""This is the python wrapper for the Robyn binaries."""

def __init__(self, file_object: str) -> None: ...
def before_request(self, endpoint: str) -> Callable[..., None]:
"""
The @app.before_request decorator to add a get route
:param endpoint str: endpoint to server the route
"""
...

def after_request(self, endpoint: str) -> Callable[..., None]:
"""
The @app.after_request decorator to add a get route
:param endpoint str: endpoint to server the route
"""
...

def add_directory(
self,
route: str,
directory_path: str,
index_file: str = ...,
show_files_listing: str = ...,
): ...
def add_header(self, key: str, value: str) -> None: ...
def add_web_socket(self, endpoint: str, ws: WS) -> None: ...
def startup_handler(self, handler: Callable) -> None: ...
def shutdown_handler(self, handler: Callable) -> None: ...
def start(self, url: str = ..., port: int = ...) -> None:
"""
Starts the server
:param port int: reperesents the port number at which the server is listening
"""

...
def get(self, endpoint: str) -> Callable[..., None]:
"""
The @app.get decorator to add a get route
:param endpoint str: endpoint to server the route
"""
...
def post(self, endpoint: str) -> Callable[..., None]:
"""
The @app.post decorator to add a get route
:param endpoint str: endpoint to server the route
"""
...
def put(self, endpoint: str) -> Callable[..., None]:
"""
The @app.put decorator to add a get route
:param endpoint str: endpoint to server the route
"""

...
def delete(self, endpoint: str) -> Callable[..., None]:
"""
The @app.delete decorator to add a get route
:param endpoint str: endpoint to server the route
"""

...
def patch(self, endpoint: str) -> Callable[..., None]:
"""
The @app.patch decorator to add a get route
:param endpoint str: endpoint to server the route
"""

...

def head(self, endpoint: str) -> Callable[..., None]:
"""
The @app.head decorator to add a get route
:param endpoint str: endpoint to server the route
"""

...
def options(self, endpoint: str) -> Callable[..., None]:
"""
The @app.options decorator to add a get route
:param endpoint str: endpoint to server the route
"""

...
def connect(self, endpoint: str) -> Callable[..., None]:
"""
The @app.connect decorator to add a get route
:param endpoint str: endpoint to server the route
"""
...
def trace(self, endpoint: str) -> Callable[..., None]:
"""
The @app.trace decorator to add a get route
:param endpoint str: endpoint to server the route
"""

...
7 changes: 7 additions & 0 deletions robyn/argument_parser.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import argparse

class ArgumentParser(argparse.ArgumentParser):
def __init__(self) -> None: ...
def num_processes(self): ...
def workers(self): ...
def is_dev(self): ...
5 changes: 3 additions & 2 deletions robyn/dev_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ def start_server_first_time(self):

def on_any_event(self, event):
"""
[This function is a callback that will start a new server on every even change]
This function is a callback that will start a new server on every even change
:param event [FSEvent]: [a data structure with info about the events]
:param event FSEvent: a data structure with info about the events
"""

if len(self.processes) > 0:
for process in self.processes:
process.terminate()
Expand Down
11 changes: 11 additions & 0 deletions robyn/dev_event_handler.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from watchdog.events import FileSystemEvent, FileSystemEventHandler

class EventHandler(FileSystemEventHandler):
def __init__(self, file_name: str) -> None: ...
def start_server_first_time(self) -> None: ...
def on_any_event(self, event: FileSystemEvent) -> None:
"""
This function is a callback that will start a new server on every even change
:param event FSEvent: a data structure with info about the events
"""
3 changes: 3 additions & 0 deletions robyn/events.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Events:
STARTUP = ...
SHUTDOWN = ...
10 changes: 10 additions & 0 deletions robyn/log_colors.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Colors:
HEADER = ...
OKBLUE = ...
OKCYAN = ...
OKGREEN = ...
WARNING = ...
FAIL = ...
ENDC = ...
BOLD = ...
UNDERLINE = ...
7 changes: 4 additions & 3 deletions robyn/processpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ def spawn_process(
:param directories tuple: the list of all the directories and related data in a tuple
:param headers tuple: All the global headers in a tuple
:param routes tuple: The routes touple, containing the description about every route.
:param middlewares tuple: The middleware router touple, containing the description about every route.
:param routes Tuple[Route]: The routes touple, containing the description about every route.
:param middlewares Tuple[Route]: The middleware router touple, containing the description about every route.
:param web_sockets list: This is a list of all the web socket routes
:param event_handlers Dict: This is an event dict that contains the event handlers
:param socket Socket: This is the main tcp socket, which is being shared across multiple processes.
:param socket SocketHeld: This is the main tcp socket, which is being shared across multiple processes.
: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()
if sys.platform.startswith("win32") or sys.platform.startswith("linux-cross"):
loop = asyncio.new_event_loop()
Expand Down
35 changes: 35 additions & 0 deletions robyn/processpool.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from numbers import Number
from typing import Dict, Tuple

from robyn.events import Events
from robyn.robyn import SocketHeld
from robyn.router import Route
from robyn.ws import WS

Directory = Tuple[str, str, str, str]
Header = Tuple[str, str]

def spawn_process(
directories: Tuple[Directory, ...],
headers: Tuple[Header, ...],
routes: Tuple[Route, ...],
middlewares: Tuple[Route, ...],
web_sockets: Dict[str, WS],
event_handlers: Dict[Events, list],
socket: SocketHeld,
workers: Number,
) -> None:
"""
This function is called by the main process handler to create a server runtime.
This functions allows one runtime per process.
:param directories tuple: the list of all the directories and related data in a tuple
:param headers tuple: All the global headers in a tuple
:param routes Tuple[Route]: The routes touple, containing the description about every route.
:param middlewares Tuple[Route]: The middleware router touple, containing the description about every route.
:param web_sockets list: This is a list of all the web socket routes
:param event_handlers Dict: This is an event dict that contains the event handlers
:param socket SocketHeld: This is the main tcp socket, which is being shared across multiple processes.
: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
"""
Empty file added robyn/py.typed
Empty file.
Loading

0 comments on commit 77064fb

Please sign in to comment.