-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgallerydata.py
113 lines (96 loc) · 3.79 KB
/
gallerydata.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import importlib.util
import os
import sys
from os.path import isfile, join
from pathlib import Path
import flet as ft
class GridItem:
def __init__(self, id):
self.id = id
self.name = None
self.items = []
self.description = None
self.parent = None
class Item:
def __init__(self):
self.name = None
self.file_name = None
self.icon = None
self.order = None
self.layout = None
self.source_code = None
class ControlGroup:
def __init__(self, name, label, icon, selected_icon):
self.name = name
self.label = label
self.icon = icon
self.selected_icon = selected_icon
self.grid_items = []
class GalleryData:
def __init__(self):
self.import_modules()
destinations_list = [
ControlGroup(
name="scripts",
label="Scripts",
icon=ft.icons.DESCRIPTION,
selected_icon=ft.icons.DESCRIPTION_SHARP,
),
]
def list_control_dirs(self, dir):
file_path = os.path.join(str(Path(__file__).parent), dir)
control_dirs = [
f
for f in os.listdir(file_path)
if not isfile(f)
and f not in ["index.py", "images", "__pycache__", ".venv", ".git"]
]
return control_dirs
def list_example_files(self, control_group_dir, control_dir):
file_path = os.path.join(
str(Path(__file__).parent), control_group_dir, control_dir
)
example_files = [f for f in os.listdir(
file_path) if not f.startswith("_")]
return example_files
def import_modules(self):
for control_group_dir in self.destinations_list:
for control_dir in self.list_control_dirs(control_group_dir.name):
grid_item = GridItem(control_dir)
for file in self.list_example_files(
control_group_dir.name, control_dir
):
file_name = os.path.join(
control_group_dir.name, control_dir, file)
module_name = file_name.replace(
"/", ".").replace(".py", "")
if module_name in sys.modules:
print(f"{module_name!r} already in sys.modules")
else:
file_path = os.path.join(
str(Path(__file__).parent), file_name)
spec = importlib.util.spec_from_file_location(
module_name, file_path
)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
print(f"{module_name!r} has been imported")
if file == "index.py":
grid_item.name = module.name
grid_item.description = module.description
grid_item.icon = module.icon
grid_item.parent = control_group_dir
else:
item = Item()
item.layout = module.layout
item.file_name = (
module_name.replace(".", "/") + ".py"
)
item.name = "See code on github"
item.order = file[
:2
] # first 2 characters of example file name (e.g. '01')
grid_item.items.append(item)
grid_item.items.sort(key=lambda x: x.order)
control_group_dir.grid_items.append(grid_item)