This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
update_types.py
133 lines (102 loc) · 3.49 KB
/
update_types.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import json
from typing import Optional
ALLOWED_TYPES = [
"AI_ML",
"GENERATORS",
"VISUALIZERS",
"LOADERS",
"EXTRACTORS",
"TRANSFORMERS",
"ARITHMETIC",
"IO",
"LOGIC_GATES",
"CONDITIONALS",
"NUMPY",
"SCIPY",
]
OVERRIDES = {
"LOOP_INDEX": "default",
"MATRIX": "default",
"BASIC_OSCILLATOR": "default",
"PERIODOGRAM": "default",
"WELCH": "default",
"STFT": "default",
"SKIMAGE": "default",
"LINSPACE": "default",
}
Path = os.path
NODES_DIR = Path.join("docs", "nodes")
FULL_PATH = Path.abspath(Path.join(Path.curdir, NODES_DIR))
type_map = {}
def contains_directory(path, dir_name):
# Check if the main directory exists
if not os.path.exists(path) or not os.path.isdir(path):
return False
# Get a list of entries in the main directory
with os.scandir(path) as entries:
# Iterate over the entries to find the specified directory
for entry in entries:
if entry.is_dir() and entry.name == dir_name:
return True
# If the specified directory is not found, return False
return False
def get_type_map(dir_path: str, cur_type: Optional[str] = None):
basename = Path.basename(dir_path)
cur_type = basename if basename in ALLOWED_TYPES else cur_type
if contains_directory(dir_path, "a1-[autogen]"):
type_map[basename] = cur_type
return
with os.scandir(dir_path) as entries:
# Sort entries alphabetically
entries = sorted(entries, key=lambda e: e.name)
for entry in entries:
if not entry.is_dir():
continue
get_type_map(entry.path, cur_type)
def browse_directories(dir_path: str):
if contains_directory(dir_path, "examples"):
print("updating ", Path.basename(dir_path))
app_path = Path.join(dir_path, "examples", "EX1", "app.json")
update_app(app_path)
return
with os.scandir(dir_path) as entries:
# Sort entries alphabetically
entries = sorted(entries, key=lambda e: e.name)
for entry in entries:
if not entry.is_dir():
continue
browse_directories(entry.path)
def update_app(path: str):
with open(path, "r") as f:
app = json.load(f)
nodes = app["rfInstance"]["nodes"]
edges = app["rfInstance"]["edges"]
def should_keep_node(node):
return node["data"]["func"] != "END" and node["data"]["func"] != "GOTO"
def should_keep_edge(edge):
return (
not edge["target"].startswith("END-")
and not edge["target"].startswith("GOTO-")
and not edge["source"].startswith("END-")
and not edge["source"].startswith("GOTO-")
)
for node in nodes:
if not should_keep_node(node):
continue
func = node["data"]["func"]
if func in OVERRIDES:
mapped = OVERRIDES[func]
else:
mapped = type_map[func]
node["type"] = mapped
node["data"]["type"] = mapped
app["rfInstance"]["nodes"] = [node for node in nodes if should_keep_node(node)]
app["rfInstance"]["edges"] = [edge for edge in edges if should_keep_edge(edge)]
with open(path, "w") as f:
json.dump(app, f, indent=2)
get_type_map(FULL_PATH)
type_map["DF_2_OrderedTriple"] = "TRANSFORMERS"
example_apps_path = Path.join("..", "studio", "src", "utils", "app-gallery-apps")
for path in os.listdir(example_apps_path):
update_app(Path.join(example_apps_path, path))