Tool to fix error in story file
This commit is contained in:
parent
09dd9d52f4
commit
ea5c8acb2f
2 changed files with 66 additions and 1 deletions
|
@ -6,6 +6,28 @@ from hugvey.voice import VoiceStorage
|
||||||
|
|
||||||
logger = logging.getLogger('toolbox')
|
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:
|
class Toolbox:
|
||||||
def __init__(self, configFile):
|
def __init__(self, configFile):
|
||||||
self.languageFiles = {}
|
self.languageFiles = {}
|
||||||
|
@ -96,4 +118,39 @@ class Toolbox:
|
||||||
logger.info("{} files missing".format(len(missingFiles)))
|
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}")
|
||||||
|
|
||||||
|
|
||||||
|
|
10
tools.py
10
tools.py
|
@ -20,6 +20,13 @@ if __name__ == '__main__':
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help="Add/remove new & unused file"
|
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()
|
args = argParser.parse_args()
|
||||||
|
|
||||||
|
@ -35,4 +42,5 @@ if __name__ == '__main__':
|
||||||
if args.files:
|
if args.files:
|
||||||
logger.info("Filenames")
|
logger.info("Filenames")
|
||||||
tools.clean_audio_files()
|
tools.clean_audio_files()
|
||||||
|
if args.story:
|
||||||
|
tools.fix_story_file(args.story)
|
Loading…
Reference in a new issue