forked from Daxexs/flet-easy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_route.py
34 lines (27 loc) · 1.22 KB
/
auto_route.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
34
from importlib.util import module_from_spec, spec_from_file_location
from inspect import getmembers
from os import listdir, path
from typing import List
from flet_easy.pagesy import AddPagesy
def automatic_routing(dir: str) -> List[AddPagesy]:
"""
A function that automatically routes through a directory to find Python files, extract AddPagesy objects, and return a list of them.
Parameters:
- dir (str): The directory path to search for Python files.
Returns:
- List[AddPagesy]: A list of AddPagesy objects found in the specified directory.
"""
pages = []
for file in listdir(dir):
if (file.endswith(".py") or file.endswith(".pyc")) and file != "__init__.py":
spec = spec_from_file_location(path.splitext(file)[0], path.join(dir, file))
module = module_from_spec(spec)
spec.loader.exec_module(module)
for _, object_page in getmembers(module):
if isinstance(object_page, AddPagesy):
pages.append(object_page)
if len(pages) == 0:
raise ValueError(
"No instances of AddPagesy found. Check the assigned path of the 'path_views' parameter of the class (FletEasy)."
)
return pages