diff options
-rwxr-xr-x | prepare-agenda.py | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/prepare-agenda.py b/prepare-agenda.py index 061be7f..a44ef7d 100755 --- a/prepare-agenda.py +++ b/prepare-agenda.py @@ -21,6 +21,7 @@ # Author: Milosz Wasilewski <milosz.wasilewski@linaro.org> # +import collections import yaml from optparse import OptionParser @@ -29,6 +30,23 @@ SQLITE_DB_GLOBAL_PATH = "/root/db_results/" SQLITE_DB = 'sqlite_database' CONFIG = 'config' + +# Modifying the yaml parser to use an OrderedDict, rather then regular Python +# dict for mappings. This preservers the order in which the items are +# specified. See +# http://stackoverflow.com/a/21048064 + +_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG + +def dict_representer(dumper, data): + return dumper.represent_dict(data.iteritems()) + +def dict_constructor(loader, node): + return collections.OrderedDict(loader.construct_pairs(node)) + +yaml.add_representer(collections.OrderedDict, dict_representer) +yaml.add_constructor(_mapping_tag, dict_constructor) + if __name__ == '__main__': usage = "usage: %prog [OPTIONS]" @@ -49,4 +67,4 @@ if __name__ == '__main__': agenda_yaml[CONFIG][SQLITE_DB] = SQLITE_DB_GLOBAL_PATH + options.job_name + ".db" agenda.close() with open(options.agenda, 'w') as outfile: - outfile.write( yaml.dump(agenda_yaml, default_flow_style=True) ) + outfile.write( yaml.dump(agenda_yaml, default_flow_style=False) ) |