-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·201 lines (152 loc) · 5.32 KB
/
build.py
File metadata and controls
executable file
·201 lines (152 loc) · 5.32 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/python3
import os, sys, time, datetime
import http.server
import socketserver
def read_file(path: str) -> str:
try:
with open(path, "r") as file:
return file.read()
except Exception as err:
# print(f"Warning! {err}")
return ""
def create_app(app_name: str):
base_path = f"src/apps/{app_name}"
files = [
f"{base_path}/{app_name}.html",
f"{base_path}/scripts/app.{app_name}.js",
f"{base_path}/styles/app.{app_name}.css",
]
for file_path in files:
directory = os.path.dirname(file_path)
os.makedirs(directory, exist_ok=True)
with open(file_path, "w") as file:
file.write("")
print("Done!")
def read_files_paths(path: str) -> list:
try:
files = os.listdir(path)
return files
except FileNotFoundError as err:
print(f"error {err} {path}")
return []
def build_apps() -> str:
# List all apps
app_list = read_files_paths("src/apps/")
# Open app boxed container
app_container = read_file("src/system/app.html")
final_app_list = ""
for app in app_list:
## Build JS tags
html_script = ""
scripts = read_files_paths(f"src/apps/{app}/scripts")
for sc in scripts:
file_content = read_file(f"src/apps/{app}/scripts/{sc}")
html_script = f"{html_script}<script>\n{file_content}\n</script>\n"
## Build window buttons
html_script = f"{html_script}<script>\n{read_file('src/system/window_toggle.js')}\n</script>\n"
## Build CSS tags
html_css = ""
styles = read_files_paths(f"src/apps/{app}/styles")
for st in styles:
file_content = read_file(f"src/apps/{app}/styles/{st}")
html_css = f"{html_css}<style>\n{file_content}\n</style>\n"
app_content = read_file(f"src/apps/{app}/{app}.html")
app_toolbar = read_file(f"src/apps/{app}/toolbar.html")
final_app = app_container.replace("@@APP_CONTAINER", app_content)
final_app = final_app.replace("@@APP_SCRIPTS", html_script)
final_app = final_app.replace("@@APP_STYLES", html_css)
final_app = final_app.replace("@@APP_NAME", app)
final_app = final_app.replace("@@APP_TOOLBAR", app_toolbar)
final_app_list = f"{final_app_list}\n{final_app}\n"
return final_app_list
def build_base_window() -> str:
window = read_file("src/system/ui.html")
fixed_items = [
f'<script>\n{read_file("src/scripts/jquery.js")}\n</script>\n',
f'<script>\n{read_file("src/scripts/ui.js")}\n</script>\n',
f'<style>\n{read_file("src/styles/ui.css")}\n</style>\n',
f'<style>\n{read_file("src/styles/apps.css")}\n</style>\n',
]
final_fixed_items = ""
for item in fixed_items:
final_fixed_items = f"{final_fixed_items}{item}\n"
return window.replace("@@FIXED_ITEMS", final_fixed_items)
def build_desktop() -> str:
app_list = read_files_paths("src/apps/")
desktop = read_file("src/system/ui.html")
## Build start menu
start_menu = read_file("src/system/start.html")
start_menu_item = read_file("src/system/start_items.html")
menu_items = ""
for app_name in app_list:
menu_btn = start_menu_item.replace("@@APP_NAME", app_name)
menu_items = f"{menu_items}{menu_btn}\n"
## Build start_menu
final_start_menu = start_menu.replace("@@MENU_ITEMS", menu_items)
desktop = desktop.replace("@@START", final_start_menu)
## Build taskbar
taskbar = read_file("src/system/taskbar.html")
desktop = desktop.replace("@@TASKBAR", taskbar)
## Build apps
apps = build_apps()
desktop = desktop.replace("@@APPS", apps)
return desktop
def build_cyberpunk_desktop() -> str:
try:
desktop = build_desktop()
with open("src/index.html", "w+") as file:
file.write(desktop)
file.close()
return "done"
except Exception as err:
print(f"$$$ ERROR :: {err}")
return "error"
def http_server():
PORT = 8000
web_dir = os.path.join(os.path.dirname(__file__), 'src')
os.chdir(web_dir)
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()
def loop_server():
while True:
build_cyberpunk_desktop()
now = datetime.datetime.now()
print("Building ...")
print(f"{now} :: Done")
time.sleep(5)
import threading
def cli():
options = sys.argv
help = '''
Valid options:
help
build
new app-name
'''
if (len(options)) < 2:
print(help)
return
match options[1]:
case 'build':
print(build_cyberpunk_desktop())
return
case 'loop':
while True:
build_cyberpunk_desktop()
now = datetime.datetime.now()
print("Building ...")
print(f"{now} :: Done")
time.sleep(5)
case 'new':
if(len(options)) < 3:
print("Missing app name")
create_app(options[2])
print(help)
case 'help':
print(help)
case 'loop_server':
threading.Thread(http_server())
threading.Thread(loop_server())
cli()