-
Notifications
You must be signed in to change notification settings - Fork 0
/
wizard.py
123 lines (115 loc) · 3.86 KB
/
wizard.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
import json
import os
import shutil
import sys
from subprocess import run
import questionary
import requests
from rich import print
from yaml import Dumper, Loader, dump, load
SYSTEMS = [
"nes",
"snes",
"n64",
"nds",
"megadrive",
"gamegear",
"html5",
"flash",
"scratch",
]
with open("config.yml") as configfile:
config = load(configfile.read(), Loader=Loader)
def projectidvalidator(projectid):
try:
int(projectid)
except ValueError:
return False
return True
while True:
print("[bold] Welcome to the EmuWeb management wizard![/]")
action = questionary.select(
"What would you like to do?",
[
"Copy a game file",
"Add a Scratch Game",
"Remove a Game",
"Enable/Disable Systems",
"Regenerate Output",
"Quit",
],
).ask()
if action == "Copy a game file":
gamespath = os.path.join(os.getcwd(), "games")
os.chdir(os.path.expanduser("~"))
file = questionary.path("Please select a file to copy:").ask()
if file == None:
continue
system = questionary.select(
"Please select the system that the game is for:", SYSTEMS
).ask()
if system == None:
continue
print("[bold lime]Copying file...[/]")
shutil.copy(file, os.path.join(gamespath, system))
elif action == "Add a Scratch Game":
projectid = questionary.text(
"Please enter the scratch project id:", validate=projectidvalidator
).ask()
if projectid == None:
continue
print("Fetching project name...")
projectdata = json.loads(
requests.get(
"https://api.scratch.mit.edu/projects/" + projectid, timeout=60
).content
)
projecttitle = projectdata["title"].replace("/", "|")
print("Adding project " + projecttitle + "...")
with open(
os.path.join("games", "scratch", f"{projecttitle}.txt"), "w"
) as projectfile:
projectfile.write(projectid)
print("Project added, regenerate output to see your changes")
elif action == "Remove a Game":
os.chdir("games")
gamepath = questionary.path(
"Please enter the path of the game to remove (press tab):"
).ask()
if gamepath == None:
continue
if "info.txt" not in gamepath:
if os.path.isdir(gamepath):
print("[bold red]Please do not select a directory.[/]")
else:
artworkpath = os.path.join(
"artwork", f"{os.path.basename(gamepath)}.png"
)
print(artworkpath)
if questionary.confirm(
f"Are you sure you want to remove {gamepath}?", False
).ask():
os.remove(gamepath)
if os.path.isfile(artworkpath):
os.remove(artworkpath)
print("Game removed, regenerate output to see your changes")
else:
print("[bold red]Cancelling...[/]")
else:
print("[bold red]info.txt is not a game[/]")
elif action == "Enable/Disable Systems":
enabledsystems = questionary.checkbox(
"Please select the systems to enable:", SYSTEMS
).ask()
config["enabled-systems"] = enabledsystems
with open("config.yml", "w") as configfile:
dump(config, configfile, Dumper=Dumper)
elif action == "Regenerate Output":
if questionary.confirm(
"Would you like to attempt to automatically download artwork?"
).ask():
run([sys.executable, "main.py", "--download_artwork"])
else:
run([sys.executable, "main.py"])
elif action == "Quit":
quit()