From 2e4645310b2b8a893d75e508b82e3f0ba176754f Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 9 May 2022 19:16:46 +1200 Subject: [PATCH] Also rename yaml filename with rename command (#3447) --- esphome/__main__.py | 138 ++++++++++++++++++++++---------------------- 1 file changed, 70 insertions(+), 68 deletions(-) diff --git a/esphome/__main__.py b/esphome/__main__.py index 80e8455465..c336336f18 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -496,83 +496,85 @@ def command_rename(args, config): ) ) return 1 + # Load existing yaml file with open(CORE.config_path, mode="r+", encoding="utf-8") as raw_file: raw_contents = raw_file.read() - yaml = yaml_util.load_yaml(CORE.config_path) - if CONF_ESPHOME not in yaml or CONF_NAME not in yaml[CONF_ESPHOME]: - print( - color( - Fore.BOLD_RED, "Complex YAML files cannot be automatically renamed." + + yaml = yaml_util.load_yaml(CORE.config_path) + if CONF_ESPHOME not in yaml or CONF_NAME not in yaml[CONF_ESPHOME]: + print( + color(Fore.BOLD_RED, "Complex YAML files cannot be automatically renamed.") + ) + return 1 + old_name = yaml[CONF_ESPHOME][CONF_NAME] + match = re.match(r"^\$\{?([a-zA-Z0-9_]+)\}?$", old_name) + if match is None: + new_raw = re.sub( + rf"name:\s+[\"']?{old_name}[\"']?", + f'name: "{args.name}"', + raw_contents, + ) + else: + old_name = yaml[CONF_SUBSTITUTIONS][match.group(1)] + if ( + len( + re.findall( + rf"^\s+{match.group(1)}:\s+[\"']?{old_name}[\"']?", + raw_contents, + flags=re.MULTILINE, ) ) - return 1 - old_name = yaml[CONF_ESPHOME][CONF_NAME] - match = re.match(r"^\$\{?([a-zA-Z0-9_]+)\}?$", old_name) - if match is None: - new_raw = re.sub( - rf"name:\s+[\"']?{old_name}[\"']?", - f'name: "{args.name}"', - raw_contents, - ) - else: - old_name = yaml[CONF_SUBSTITUTIONS][match.group(1)] - if ( - len( - re.findall( - rf"^\s+{match.group(1)}:\s+[\"']?{old_name}[\"']?", - raw_contents, - flags=re.MULTILINE, - ) - ) - > 1 - ): - print(color(Fore.BOLD_RED, "Too many matches in YAML to safely rename")) - return 1 - - new_raw = re.sub( - rf"^(\s+{match.group(1)}):\s+[\"']?{old_name}[\"']?", - f'\\1: "{args.name}"', - raw_contents, - flags=re.MULTILINE, - ) - - raw_file.seek(0) - raw_file.write(new_raw) - raw_file.flush() - - print(f"Updating {color(Fore.CYAN, CORE.config_path)}") - print() - - rc = run_external_process("esphome", "config", CORE.config_path) - if rc != 0: - raw_file.seek(0) - raw_file.write(raw_contents) - print(color(Fore.BOLD_RED, "Rename failed. Reverting changes.")) + > 1 + ): + print(color(Fore.BOLD_RED, "Too many matches in YAML to safely rename")) return 1 - cli_args = [ - "run", - CORE.config_path, - "--no-logs", - "--device", - CORE.address, - ] + new_raw = re.sub( + rf"^(\s+{match.group(1)}):\s+[\"']?{old_name}[\"']?", + f'\\1: "{args.name}"', + raw_contents, + flags=re.MULTILINE, + ) - if args.dashboard: - cli_args.insert(0, "--dashboard") + new_path = os.path.join(CORE.config_dir, args.name + ".yaml") + print( + f"Updating {color(Fore.CYAN, CORE.config_path)} to {color(Fore.CYAN, new_path)}" + ) + print() - try: - rc = run_external_process("esphome", *cli_args) - except KeyboardInterrupt: - rc = 1 - if rc != 0: - raw_file.seek(0) - raw_file.write(raw_contents) - return 1 + with open(new_path, mode="w", encoding="utf-8") as new_file: + new_file.write(new_raw) - print(color(Fore.BOLD_GREEN, "SUCCESS")) - print() - return 0 + rc = run_external_process("esphome", "config", new_path) + if rc != 0: + print(color(Fore.BOLD_RED, "Rename failed. Reverting changes.")) + os.remove(new_path) + return 1 + + cli_args = [ + "run", + new_path, + "--no-logs", + "--device", + CORE.address, + ] + + if args.dashboard: + cli_args.insert(0, "--dashboard") + + try: + rc = run_external_process("esphome", *cli_args) + except KeyboardInterrupt: + rc = 1 + if rc != 0: + os.remove(new_path) + return 1 + + os.remove(CORE.config_path) + + print(color(Fore.BOLD_GREEN, "SUCCESS")) + print() + return 0 PRE_CONFIG_ACTIONS = {