diff --git a/src/flask_fs_router/__init__.py b/src/flask_fs_router/__init__.py index 0c81826..37aa841 100644 --- a/src/flask_fs_router/__init__.py +++ b/src/flask_fs_router/__init__.py @@ -1,9 +1,7 @@ -import fnmatch -import os import secrets +from pathlib import Path from pydoc import locate from typing import Any -from pathlib import Path def to_class(path: str) -> Any: @@ -22,6 +20,10 @@ def to_class(path: str) -> Any: class FlaskFSRouter: def __init__(self, app=None): + self.possible_routes = [] + self.fqdns = [] + self.route_paths = [] + self.route_map = [] if app is not None: self.init_app(app) @@ -35,11 +37,11 @@ def init_app(self, app): methods=[route.get('method')], websocket=route.get("ws") ) - ) for route in self.routes_export() + ) for route in FlaskFSRouter().routes_export() ] def find_routes_files(self): - self.possible_routes = [] + pages_path = Path('pages') pages = list(pages_path.glob('**/*.py')) [ @@ -49,23 +51,17 @@ def find_routes_files(self): ] return self - def generate_fqns(self): - self.fqdns = [] + def generate_fqdns(self): [ self.fqdns.append(f"pages.{route.rstrip('.py')}.default") for route in self.possible_routes ] return self def fqdns_to_route_path(self): - self.route_paths = [] - self.route_map = [] for path in self.fqdns: fqdn = path - path = path.replace("default", "") - path = path.replace("pages.", "/") - path = path.replace(".", "/") - path = path.replace("index/", "") - path = path.replace("[", "<").replace("]", ">") + path = path.replace("default", "").replace("pages.", "/").replace(".", "/").replace("index/", "").replace( + "[", "<").replace("]", ">") method = fqdn.split("(")[-1].split(')')[0] if method.upper() in ["GET", "POST", "PUT", "DELETE", "PATCH"]: method = method @@ -76,7 +72,7 @@ def fqdns_to_route_path(self): else: path = path.rstrip("/") fqdn = fqdn.replace("//", '').replace("/", '.').replace("..", '.') - path = path.replace('_', '-').replace(f'({method})', '').replace("//", '/') or '/' + path = path.replace(f'({method})', '').replace("//", '/') or '/' self.route_map.append({ "path": path, "fqdn": fqdn, @@ -88,4 +84,4 @@ def fqdns_to_route_path(self): return self def routes_export(self): - return self.find_routes_files().generate_fqns().fqdns_to_route_path().route_map + return self.find_routes_files().generate_fqdns().fqdns_to_route_path().route_map