From a436ddb0badf1a56de76fe03ad343bc1c8cd2701 Mon Sep 17 00:00:00 2001 From: Dominik Schmid Date: Thu, 21 Mar 2019 20:54:31 +1100 Subject: [PATCH 1/4] Allow bot creation to a differnt drive than $tmp --- rlbot_gui/bot_management/bot_creation.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rlbot_gui/bot_management/bot_creation.py b/rlbot_gui/bot_management/bot_creation.py index 84c23174..fc47d896 100644 --- a/rlbot_gui/bot_management/bot_creation.py +++ b/rlbot_gui/bot_management/bot_creation.py @@ -1,5 +1,6 @@ import configparser import os +import shutil import string import tempfile @@ -33,7 +34,7 @@ def bootstrap_python_bot(bot_name, directory): local_zip_path=f"{tmpdirname}/RLBotPythonExample.zip", local_folder_path=tmpdirname) try: - os.rename(f"{tmpdirname}/RLBotPythonExample-master", f"{bot_directory}/{sanitized_name}") + shutil.move(f"{tmpdirname}/RLBotPythonExample-master", f"{bot_directory}/{sanitized_name}") except FileExistsError: return {'error': f'There is already a bot named {sanitized_name}, please choose a different name!'} @@ -43,9 +44,9 @@ def bootstrap_python_bot(bot_name, directory): config_file = f"{code_dir}/{sanitized_name}.cfg" # We're making some big assumptions here that the file structure / names in RLBotPythonExample will not change. - os.rename(f"{bot_directory}/{sanitized_name}/python_example/", code_dir) - os.rename(f"{code_dir}/python_example.py", python_file) - os.rename(f"{code_dir}/python_example.cfg", config_file) + shutil.move(f"{bot_directory}/{sanitized_name}/python_example/", code_dir) + shutil.move(f"{code_dir}/python_example.py", python_file) + shutil.move(f"{code_dir}/python_example.cfg", config_file) # Update the config file to point to the renamed files, and show the correct bot name. From 75a7ec480c53490d60bdc4be87521c1f5d5e47d6 Mon Sep 17 00:00:00 2001 From: Dominik Schmid Date: Thu, 21 Mar 2019 20:54:54 +1100 Subject: [PATCH 2/4] Make string quoting consistent. --- rlbot_gui/bot_management/bot_creation.py | 26 ++++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/rlbot_gui/bot_management/bot_creation.py b/rlbot_gui/bot_management/bot_creation.py index fc47d896..fe13db9f 100644 --- a/rlbot_gui/bot_management/bot_creation.py +++ b/rlbot_gui/bot_management/bot_creation.py @@ -16,37 +16,37 @@ def convert_to_filename(text): """ import unicodedata normalized = unicodedata.normalize('NFKD', text).encode('ascii', 'ignore').decode() - valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits) + valid_chars = '-_.() %s%s' % (string.ascii_letters, string.digits) filename = ''.join(c for c in normalized if c in valid_chars) filename = filename.replace(' ', '_') # Replace spaces with underscores return filename def bootstrap_python_bot(bot_name, directory): - bot_directory = directory or "." + bot_directory = directory or '.' sanitized_name = convert_to_filename(bot_name) with tempfile.TemporaryDirectory() as tmpdirname: print('created temporary directory', tmpdirname) download_and_extract_zip( - download_url="https://github.com/RLBot/RLBotPythonExample/archive/master.zip", - local_zip_path=f"{tmpdirname}/RLBotPythonExample.zip", local_folder_path=tmpdirname) + download_url='https://github.com/RLBot/RLBotPythonExample/archive/master.zip', + local_zip_path=f'{tmpdirname}/RLBotPythonExample.zip', local_folder_path=tmpdirname) try: - shutil.move(f"{tmpdirname}/RLBotPythonExample-master", f"{bot_directory}/{sanitized_name}") + shutil.move(f'{tmpdirname}/RLBotPythonExample-master', f'{bot_directory}/{sanitized_name}') except FileExistsError: return {'error': f'There is already a bot named {sanitized_name}, please choose a different name!'} # Choose appropriate file names based on the bot name - code_dir = f"{bot_directory}/{sanitized_name}/{sanitized_name}" - python_file = f"{code_dir}/{sanitized_name}.py" - config_file = f"{code_dir}/{sanitized_name}.cfg" + code_dir = f'{bot_directory}/{sanitized_name}/{sanitized_name}' + python_file = f'{code_dir}/{sanitized_name}.py' + config_file = f'{code_dir}/{sanitized_name}.cfg' # We're making some big assumptions here that the file structure / names in RLBotPythonExample will not change. - shutil.move(f"{bot_directory}/{sanitized_name}/python_example/", code_dir) - shutil.move(f"{code_dir}/python_example.py", python_file) - shutil.move(f"{code_dir}/python_example.cfg", config_file) + shutil.move(f'{bot_directory}/{sanitized_name}/python_example/', code_dir) + shutil.move(f'{code_dir}/python_example.py', python_file) + shutil.move(f'{code_dir}/python_example.cfg', config_file) # Update the config file to point to the renamed files, and show the correct bot name. @@ -80,8 +80,8 @@ def bootstrap_python_bot(bot_name, directory): agent_config = BaseAgent.base_create_agent_configurations() agent_config.parse_file(raw_bot_config) agent_config.set_value(BOT_CONFIG_MODULE_HEADER, BOT_NAME_KEY, bot_name) - agent_config.set_value(BOT_CONFIG_MODULE_HEADER, PYTHON_FILE_KEY, f"{sanitized_name}.py") - with open(config_file, "w", encoding='utf8') as f: + agent_config.set_value(BOT_CONFIG_MODULE_HEADER, PYTHON_FILE_KEY, f'{sanitized_name}.py') + with open(config_file, 'w', encoding='utf8') as f: f.write(str(agent_config)) # This is intended to open the example python file in the default system editor for .py files. From e8e55919bf92fa56d82773aface40786a5f3a08a Mon Sep 17 00:00:00 2001 From: Dominik Schmid Date: Thu, 21 Mar 2019 21:57:46 +1100 Subject: [PATCH 3/4] Use Paths instead of string hacking. --- rlbot_gui/bot_management/bot_creation.py | 24 +++++++++++++----------- rlbot_gui/bot_management/downloader.py | 3 ++- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/rlbot_gui/bot_management/bot_creation.py b/rlbot_gui/bot_management/bot_creation.py index fe13db9f..18987f34 100644 --- a/rlbot_gui/bot_management/bot_creation.py +++ b/rlbot_gui/bot_management/bot_creation.py @@ -1,6 +1,7 @@ +from pathlib import Path +from shutil import move import configparser import os -import shutil import string import tempfile @@ -23,30 +24,31 @@ def convert_to_filename(text): def bootstrap_python_bot(bot_name, directory): - bot_directory = directory or '.' sanitized_name = convert_to_filename(bot_name) + bot_directory = Path(directory or '.') with tempfile.TemporaryDirectory() as tmpdirname: - print('created temporary directory', tmpdirname) + tmpdir = Path(tmpdirname) + print('created temporary directory', tmpdir) download_and_extract_zip( download_url='https://github.com/RLBot/RLBotPythonExample/archive/master.zip', - local_zip_path=f'{tmpdirname}/RLBotPythonExample.zip', local_folder_path=tmpdirname) + local_zip_path=tmpdir / 'RLBotPythonExample.zip', local_folder_path=tmpdir) try: - shutil.move(f'{tmpdirname}/RLBotPythonExample-master', f'{bot_directory}/{sanitized_name}') + move(tmpdir / 'RLBotPythonExample-master', bot_directory / sanitized_name) except FileExistsError: return {'error': f'There is already a bot named {sanitized_name}, please choose a different name!'} # Choose appropriate file names based on the bot name - code_dir = f'{bot_directory}/{sanitized_name}/{sanitized_name}' - python_file = f'{code_dir}/{sanitized_name}.py' - config_file = f'{code_dir}/{sanitized_name}.cfg' + code_dir = bot_directory / sanitized_name / sanitized_name + python_file = code_dir / f'{sanitized_name}.py' + config_file = code_dir / f'{sanitized_name}.cfg' # We're making some big assumptions here that the file structure / names in RLBotPythonExample will not change. - shutil.move(f'{bot_directory}/{sanitized_name}/python_example/', code_dir) - shutil.move(f'{code_dir}/python_example.py', python_file) - shutil.move(f'{code_dir}/python_example.cfg', config_file) + move(bot_directory / sanitized_name / 'python_example', code_dir) + move(code_dir / 'python_example.py', python_file) + move(code_dir / 'python_example.cfg', config_file) # Update the config file to point to the renamed files, and show the correct bot name. diff --git a/rlbot_gui/bot_management/downloader.py b/rlbot_gui/bot_management/downloader.py index a3016135..e728a754 100644 --- a/rlbot_gui/bot_management/downloader.py +++ b/rlbot_gui/bot_management/downloader.py @@ -1,8 +1,9 @@ +from pathlib import Path import urllib import zipfile -def download_and_extract_zip(download_url, local_zip_path, local_folder_path): +def download_and_extract_zip(download_url: str, local_zip_path: Path, local_folder_path: Path): urllib.request.urlretrieve(download_url, local_zip_path) with zipfile.ZipFile(local_zip_path, 'r') as zip_ref: From 04f3a3f8a54f08cf14ba76f71332116b329c22e8 Mon Sep 17 00:00:00 2001 From: Dominik Schmid Date: Sun, 24 Mar 2019 15:11:43 +1100 Subject: [PATCH 4/4] Modernize string formatting. --- rlbot_gui/bot_management/bot_creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rlbot_gui/bot_management/bot_creation.py b/rlbot_gui/bot_management/bot_creation.py index 18987f34..ecb80bb5 100644 --- a/rlbot_gui/bot_management/bot_creation.py +++ b/rlbot_gui/bot_management/bot_creation.py @@ -17,7 +17,7 @@ def convert_to_filename(text): """ import unicodedata normalized = unicodedata.normalize('NFKD', text).encode('ascii', 'ignore').decode() - valid_chars = '-_.() %s%s' % (string.ascii_letters, string.digits) + valid_chars = f'-_.() {string.ascii_letters}{string.digits}' filename = ''.join(c for c in normalized if c in valid_chars) filename = filename.replace(' ', '_') # Replace spaces with underscores return filename