From ea5c8acb2f64b43a3101c05e0773e5cb175a9bfd Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Fri, 8 Nov 2019 11:30:49 +0100 Subject: [PATCH] Tool to fix error in story file --- hugvey/tools.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ tools.py | 10 ++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/hugvey/tools.py b/hugvey/tools.py index 36fa711..9afe7af 100644 --- a/hugvey/tools.py +++ b/hugvey/tools.py @@ -6,6 +6,28 @@ from hugvey.voice import VoiceStorage logger = logging.getLogger('toolbox') +# From https://stackoverflow.com/a/1165552 +class DictDiffer(object): + """ + Calculate the difference between two dictionaries as: + (1) items added + (2) items removed + (3) keys same in both but changed values + (4) keys same in both and unchanged values + """ + def __init__(self, current_dict, past_dict): + self.current_dict, self.past_dict = current_dict, past_dict + self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys()) + self.intersect = self.set_current.intersection(self.set_past) + def added(self): + return self.set_current - self.intersect + def removed(self): + return self.set_past - self.intersect + def changed(self): + return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o]) + def unchanged(self): + return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o]) + class Toolbox: def __init__(self, configFile): self.languageFiles = {} @@ -96,4 +118,39 @@ class Toolbox: logger.info("{} files missing".format(len(missingFiles))) + def fix_story_file(self, lang_code): + if lang_code not in self.languages.keys(): + logger.critical("Invalid langauge code") + logger.warn(f"Valid codes are {' '.join(self.languages.keys())}") + return + + + filename = os.path.join(self.config['web']['files_dir'],self.languageFiles[lang_code]) + story = self.languages[lang_code] + + beginnings = [item for item in story if 'beginning' in item and item['beginning'] is True] + if len(beginnings) < 1: + logger.critical("No beginning set") + if len(beginnings) > 1: + logger.critical(f"{len(beginnings)} beginning messages configured. Set only one") + + itemsPerId = {item['@id']: item for item in story} + + for i, item in enumerate(story): + if item['@type'] == 'Direction': + if type(item['source']) == dict: + logger.warn(f"Fixing broken direction {item['@id']}, please check if everything still works!") + validMsg = itemsPerId[item['source']['@id']] + diff = DictDiffer(item['source'], validMsg) + if diff.changed() or diff.added() or diff.removed(): + logger.warn("Changes found between messages") + logger.warn(f"Changed: {list(diff.changed())} Keys that will be remove: {list(diff.added())} Keys that will be added: {list(diff.removed())}") + logger.info(f"Direction pointed to {item['source']}") + logger.info(f"Will now point to {validMsg}") + item['source'] = item['source']['@id'] + + with open(filename, 'w') as fp: + json.dump(story, fp, indent=2) + logger.info(f"Wrote to {filename}") + diff --git a/tools.py b/tools.py index 7c26bfa..db712fc 100644 --- a/tools.py +++ b/tools.py @@ -20,6 +20,13 @@ if __name__ == '__main__': action='store_true', help="Add/remove new & unused file" ) + + argParser.add_argument( + '--story', + default=None, + help="Find/restore issues with the story file", + metavar="LANG_CODE" + ) args = argParser.parse_args() @@ -35,4 +42,5 @@ if __name__ == '__main__': if args.files: logger.info("Filenames") tools.clean_audio_files() - \ No newline at end of file + if args.story: + tools.fix_story_file(args.story) \ No newline at end of file