diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2013-02-18 12:36:48 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2013-02-18 15:45:28 +0000 |
commit | 42829e9d9aee1592cbd5391cdb30ab24f43370d1 (patch) | |
tree | 54027553a9344735f947b83ac705c62b08a98b17 /scripts | |
parent | dec7732a7b39896cb56b1cf639eee26e5f3cdef4 (diff) | |
download | morph-42829e9d9aee1592cbd5391cdb30ab24f43370d1.tar.gz |
scripts/edit-morph: Add to-yaml and to-json commands
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/edit-morph | 42 |
1 files changed, 42 insertions, 0 deletions
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 |