From 42829e9d9aee1592cbd5391cdb30ab24f43370d1 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Mon, 18 Feb 2013 12:36:48 +0000 Subject: scripts/edit-morph: Add to-yaml and to-json commands --- scripts/edit-morph | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'scripts') diff --git a/scripts/edit-morph b/scripts/edit-morph index 116b9681..244bbd61 100755 --- a/scripts/edit-morph +++ b/scripts/edit-morph @@ -90,6 +90,48 @@ class EditMorph(cliapp.Application): with morphlib.savefile.SaveFile(file_name, 'w') as f: morphology.write_to_file(f) + def cmd_to_json(self, args): + """Convert one or more FILES to JSON. + + Assumes a .yaml extension, which will be removed in the output file. + """ + + if len(args) == 0: + raise cliapp.AppException("to-json expects one or more filenames") + + for file_name in args: + try: + with open(file_name, 'r') as f: + text = f.read() + morphology = morphlib.morph2.Morphology(text) + + if not file_name.endswith('.yaml'): + raise morphlib.Error('file name does not end with .yaml') + out_file_name = file_name[:-len('.yaml')] + + with morphlib.savefile.SaveFile(out_file_name, 'w') as f: + morphology.update_text(text, f, convert_to='json') + except Exception as e: + print '%s: %s' % (file_name, e) + + def cmd_to_yaml(self, args): + """Convert one or more FILES to YAML. + + Adds a .yaml extension for each input file.""" + + if len(args) == 0: + raise cliapp.AppException("to-yaml expects one or more filenames") + + for file_name in args: + try: + with open(file_name, 'r') as f: + text = f.read() + morphology = morphlib.morph2.Morphology(text) + with morphlib.savefile.SaveFile(file_name + '.yaml', 'w') as f: + morphology.update_text(text, f, convert_to='yaml') + except Exception as e: + print '%s: %s' % (file_name, e) + def sort_chunks(self, chunks_list): """Sort stratum chunks -- cgit v1.2.1