Skip to content

Commit

Permalink
Merge pull request #5 from ATRS7391/master
Browse files Browse the repository at this point in the history
fix for error with dynamic routing
  • Loading branch information
JarriqTheTechie authored Dec 9, 2022
2 parents b9dbf13 + 2143fd0 commit 46d4e5c
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/flask_fs_router/__init__.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)

Expand All @@ -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'))
[
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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

0 comments on commit 46d4e5c

Please sign in to comment.