Skip to content

Commit 5cd91ef

Browse files
committed
Cleanup helper script
1 parent c8544e4 commit 5cd91ef

File tree

1 file changed

+49
-43
lines changed

1 file changed

+49
-43
lines changed

twitchio/__main__.py

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
import pathlib
2929
import platform
3030
import re
31-
import sys
3231
import subprocess
33-
from typing import Callable
32+
import sys
33+
from collections.abc import Callable
3434

3535
import aiohttp
3636

@@ -91,7 +91,7 @@ async def teardown(bot: Bot) -> None: ...
9191
MAIN = """"""
9292

9393
BOOLS = {
94-
"y": True,
94+
"y": True,
9595
"yes": True,
9696
"n": False,
9797
"no": False,
@@ -114,23 +114,23 @@ def bool_validate(inp: str) -> bool:
114114

115115
def validate_input(inp: str, check: Callable[[str], bool] | None = None, *, error_msg: str | None = None) -> str:
116116
error_msg = error_msg or "Invalid input, please try again!"
117-
117+
118118
while True:
119119
response = input(inp)
120120
if not check:
121121
break
122-
122+
123123
try:
124124
result = check(response)
125125
except Exception:
126126
result = False
127-
127+
128128
if result is False:
129129
print(error_msg, end="\n\n")
130130
continue
131-
131+
132132
break
133-
133+
134134
return response
135135

136136

@@ -183,102 +183,108 @@ def version_info() -> None:
183183

184184

185185
def install_packages(exe: pathlib.Path, starlette: bool | None = False) -> None:
186-
package = "twitchio" if not starlette else "twitchio[starlette]"
186+
package = "twitchio[starlette]" if starlette else "twitchio"
187187
subprocess.call([exe, "-m", "pip", "install", package, "--upgrade", "--no-cache"])
188188

189189

190190
def generate_venv() -> None:
191191
# Create the venv...
192192
subprocess.call([sys.executable, "-m", "venv", ".venv"])
193-
193+
194194
system = platform.system()
195-
195+
196196
if system == "Windows":
197197
exe = pathlib.Path(".venv") / "Scripts" / "python.exe"
198198
elif system in ["Darwin", "Linux"]:
199199
exe = pathlib.Path(".venv") / "bin" / "python"
200-
else:
200+
else:
201201
print("Unsupported operating system... Skipping package installation. Please manually install required packages.")
202202
return
203-
204-
starlette = bool_check(validate_input("Would you like to install the optional Starlette and Uvicorn packages? (y/N): ", bool_validate,))
205-
install_packages(exe, starlette)
203+
204+
starlette = bool_check(
205+
validate_input(
206+
"Would you like to install the optional Starlette and Uvicorn packages? (y/N): ",
207+
bool_validate,
208+
)
209+
)
210+
install_packages(exe, starlette)
206211

207212

208213
def generate_bot() -> ...:
209214
name = validate_input("Project name? (Leave blank to generate files in this directory): ")
210215
if name:
211-
dir = pathlib.Path(name)
212-
dir.mkdir(exist_ok=True)
213-
os.chdir(dir)
216+
_dir = pathlib.Path(name)
217+
_dir.mkdir(exist_ok=True)
218+
os.chdir(_dir)
214219
else:
215-
dir = pathlib.Path.cwd()
216-
220+
_dir = pathlib.Path.cwd()
221+
217222
if sys.prefix != sys.base_prefix:
218-
resp = bool_check(validate_input("No virtual environment used. Would you like to create one? (y/N): ", bool_validate,))
219-
223+
resp = bool_check(
224+
validate_input(
225+
"No virtual environment used. Would you like to create one? (y/N): ",
226+
bool_validate,
227+
)
228+
)
229+
220230
if resp:
221231
generate_venv()
222-
223-
components = bool_check(validate_input("Would you like to setup commands.Components? (y/N): ", bool_validate))
232+
233+
components = bool_check(validate_input("Would you like to setup commands.Components? (y/N): ", bool_validate))
224234
if components:
225235
comp_dir = pathlib.Path("components")
226236
comp_dir.mkdir(exist_ok=True)
227-
237+
228238
with open(comp_dir / "general.py", "w") as fp:
229239
fp.write(COMPONENT)
230-
240+
231241
client_id = None
232242
client_sec = None
233-
config = bool_check(validate_input("Would you like to create a config? (y/N): ", bool_validate))
234-
243+
config = bool_check(validate_input("Would you like to create a config? (y/N): ", bool_validate))
244+
235245
if config:
236-
237246
while True:
238-
239247
client_id = validate_input("Please enter your Client-ID: ")
240248
cid_reenter = validate_input("Please re-enter your Client-ID: ")
241-
249+
242250
if client_id != cid_reenter:
243251
print("Client-ID does not match, please try again...", end="\n\n")
244252
continue
245-
253+
246254
break
247-
248-
while True:
255+
256+
while True:
249257
client_sec = getpass.getpass("Please enter your Client-Secret: ")
250258
csec_reenter = getpass.getpass("Please re-enter your Client-Secret: ")
251259

252260
if client_sec != csec_reenter:
253261
print("Client-Secret does not match, please try again...", end="\n\n")
254262
continue
255-
263+
256264
break
257-
265+
258266
config_data = f"""[secrets]\nclient_id = \"{client_id}\"\nclient_secret = \"{client_sec}\""""
259267
with open("config.toml", "w") as fp:
260268
fp.write(config_data)
261-
269+
262270
if client_id and client_sec:
263271
while True:
264272
owner_name = validate_input("Please enter the Twitch username of the owner of this Bot (E.g. chillymosh): ")
265273
bot_name = validate_input("Please enter the Twitch username of the Bot Account (E.g. chillybot): ")
266274
names = f"Owner Name: '{owner_name}'\nBot Name: '{bot_name}'"
267-
275+
268276
correct = bool_check(validate_input(f"Is this information correct? (y/N)\n\n{names}\n", bool_validate))
269277
if not correct:
270278
continue
271-
279+
272280
break
273-
274-
275-
281+
276282
# TODO: .env
277283
# TODO: client details
278284
# TODO: fetch owner/bot IDs
279285
# with open(dir / "main.py", "w") as fp:
280286
# ...
281-
287+
282288
# with open(dir / "bot.py", "w") as fp:
283289
# ...
284290

0 commit comments

Comments
 (0)