-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrouter.py
33 lines (25 loc) · 984 Bytes
/
router.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import importlib.util
import os
import sys
from types import ModuleType
from sanic import Sanic
from sanic.log import logger
def import_from_dir(name, path) -> ModuleType:
spec = importlib.util.spec_from_file_location(name, path)
mod = importlib.util.module_from_spec(spec)
sys.modules[name] = mod
spec.loader.exec_module(mod)
return mod
path = os.path.dirname(os.path.abspath(__file__)) + "/"
routes = []
def add_all_routes(app: Sanic) -> None:
def import_dir(directory) -> None:
sys.path.append(directory)
for f in os.listdir(directory):
if os.path.isdir(directory + f) and f not in ["__pycache__"]:
import_dir(directory + f + "/")
elif os.path.isfile(directory + f) and f not in ["__init__.py"]:
logger.info(f"Importing {directory}{f}")
mod = import_from_dir(f.rstrip(".py"), directory + f)
routes.append(mod)
import_dir(path + "handlers/")