Skip to content

Commit

Permalink
Merge pull request #3 from JarriqTheTechie/refactor/pathlib+list_comp…
Browse files Browse the repository at this point in the history
…reshensions

Refactored library to use Pathlib and list comprehensions instead of …
  • Loading branch information
JarriqTheTechie authored Dec 4, 2022
2 parents 5ce8bd6 + 69f6ebe commit ac8b806
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version="1.0",
version="1.1.1",
packages=[
"flask_fs_router",
],
Expand Down
35 changes: 14 additions & 21 deletions src/flask_fs_router/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import secrets
from pydoc import locate
from typing import Any
from pathlib import Path


def to_class(path: str) -> Any:
Expand All @@ -25,7 +26,7 @@ def __init__(self, app=None):
self.init_app(app)

def init_app(self, app):
for route in FlaskFSRouter().routes_export():
[
app.add_url_rule(
route.get('path'),
**dict(
Expand All @@ -34,22 +35,25 @@ def init_app(self, app):
methods=[route.get('method')],
websocket=route.get("ws")
)
)
) for route in FlaskFSRouter().routes_export()
]

def find_routes_files(self):
self.possible_routes = []
for root, dirnames, filenames in os.walk('pages'):
for filename in fnmatch.filter(filenames, '*.py'):
if filename:
self.possible_routes.append(
os.path.join( root, filename ).lstrip( "pages" ).lstrip( "\\" ).replace( "\\", "." ) )
pages_path = Path('pages')
pages = list(pages_path.glob('**/*.py'))
[
self.possible_routes.append(
str(page).lstrip("pages").lstrip("\\").replace("\\", "."))
for page in pages
]
return self

def generate_fqns(self):
self.fqdns = []
for route in self.possible_routes:
fqdn = f"pages.{route.rstrip('.py')}.default"
self.fqdns.append(fqdn)
[
self.fqdns.append(f"pages.{route.rstrip('.py')}.default") for route in self.possible_routes
]
return self

def fqdns_to_route_path(self):
Expand All @@ -60,11 +64,8 @@ def fqdns_to_route_path(self):
path = path.replace("default", "")
path = path.replace("pages.", "/")
path = path.replace(".", "/")

path = path.replace("index/", "")
path = path.replace("[", "<").replace("]", ">")


method = fqdn.split("(")[-1].split(')')[0]
if method.upper() in ["GET", "POST", "PUT", "DELETE", "PATCH"]:
method = method
Expand All @@ -84,14 +85,6 @@ def fqdns_to_route_path(self):
"method": method,
"ws": to_class(fqdn.replace("default", "ws")) or False
})
#print({
# "path": path,
# "fqdn": fqdn,
# "view_func": to_class(fqdn),
# "endpoint": to_class(fqdn.replace("default", "endpoint")) or secrets.token_urlsafe(8),
# "method": method,
# "ws": to_class(fqdn.replace("default", "ws")) or False
#})
return self

def routes_export(self):
Expand Down

0 comments on commit ac8b806

Please sign in to comment.