From feabf1a6ef03a0d5800f7cd2f20198639c0fe076 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Mon, 16 Nov 2015 12:27:32 -0800 Subject: [PATCH] Offer a chance to fix YAML parse errors An alternative to 749ef8563847ef25c1a2816e2097e4e90e69069f by @jmwatte. --- beetsplug/edit.py | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/beetsplug/edit.py b/beetsplug/edit.py index 8e7b0b8d3..16f770b1c 100644 --- a/beetsplug/edit.py +++ b/beetsplug/edit.py @@ -166,28 +166,38 @@ class EditPlugin(plugins.BeetsPlugin): If something goes wrong during editing, return None to indicate the process should abort. """ - # Ask the user to edit the data. + # Set up a temporary file with the initial data for editi new = NamedTemporaryFile(suffix='.yaml', delete=False) old_str = dump(data) new.write(old_str) new.close() - edit(new.name) - # Read the data back after editing and check whether anything - # changed. - with open(new.name) as f: - new_str = f.read() - os.remove(new.name) - if new_str == old_str: - ui.print_("No changes; aborting.") - return None - - # Parse the updated data. + # Loop until we have parseable data. try: - return load(new_str) - except yaml.YAMLError as e: - ui.print_("Invalid YAML: {}".format(e)) - return None + while True: + # Ask the user to edit the data. + edit(new.name) + + # Read the data back after editing and check whether anything + # changed. + with open(new.name) as f: + new_str = f.read() + if new_str == old_str: + ui.print_("No changes; aborting.") + return None + + # Parse the updated data. + try: + return load(new_str) + except yaml.YAMLError as e: + ui.print_("Invalid YAML: {}".format(e)) + if not ui.input_yn("Edit again to fix? (Y/n)", True): + return None + + # Remove the temporary file before returning. + finally: + os.remove(new.name) + def apply_data(self, objs, old_data, new_data): """Take potentially-updated data and apply it to a set of Model