aboutsummaryrefslogtreecommitdiff
path: root/api/dynamic_import.py
blob: 76281c2143f06b8130001430686bad65aadc41bc (plain)
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
from app import app
from flask import url_for
import importlib
import os
import log
import glob

files = glob.glob(os.path.dirname(__file__) + "/**/*.py", recursive=True)
files.remove(__file__)
files = [str(filename)
        .replace(os.path.dirname(__file__) + "/", '')
        .replace("/", ".")
        .replace(".py", '')
        for filename in files]

def route(dynamic_route):
    app.register_blueprint(dynamic_route[1], url_prefix=dynamic_route[0])
    path = (dynamic_route[0] + "/" + dynamic_route[1].name).replace('//', '/')
    log.info(f"dynamically routing {path}")

for file in files:
    mod = importlib.import_module(file)
    if hasattr(mod, "dynamic_route"):
        route(mod.dynamic_route)
    elif hasattr(mod, "dynamic_routes"):
        for dynamic_route in mod.dynamic_routes:
            route(dynamic_route)