Created
April 14, 2016 10:55
-
-
Save sdague/6bca299c2f34da6c5a0153b317db61ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import argparse | |
import yaml | |
parser = argparse.ArgumentParser(description='Merge parameter yaml files') | |
parser.add_argument('files', metavar='F', nargs='+', | |
help='files to process') | |
args = parser.parse_args() | |
SECTIONS = ('header', 'path', 'query', 'body') | |
all_params = {} | |
params = {} | |
for sec in SECTIONS: | |
params[sec] = {} | |
def check_collisions(name, properties): | |
global all_params | |
n = name | |
counter = 1 | |
while n in all_params and all_params[n] != properties: | |
# print "WARNING: mismatch on %s" % name | |
# print "%s != %s" % (all_params[n], properties) | |
n = "%s_%s" % (name, counter) | |
counter += 1 | |
all_params[n] = properties | |
return n | |
for fname in args.files: | |
parameters = yaml.load(file(fname, 'r')) | |
for name, properties in parameters.items(): | |
where = properties['in'] | |
name = check_collisions(name, properties) | |
params[where][name] = properties | |
def should_use_block(value): | |
for c in u"\u000a\u000d\u001c\u001d\u001e\u0085\u2028\u2029": | |
if c in value: | |
return True | |
return False | |
def my_represent_scalar(self, tag, value, style=None): | |
if style is None: | |
if should_use_block(value): | |
style='>' | |
else: | |
style = self.default_style | |
node = yaml.representer.ScalarNode(tag, value, style=style) | |
if self.alias_key is not None: | |
self.represented_objects[self.alias_key] = node | |
return node | |
def str_presenter(dumper, data): | |
if len(data.splitlines()) > 1: # check for multiline string | |
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='>') | |
return dumper.represent_scalar('tag:yaml.org,2002:str', data) | |
yaml.representer.BaseRepresenter.represent_scalar = my_represent_scalar | |
for sec in SECTIONS: | |
print "# variables in %s" % sec | |
secdict = params[sec] | |
print yaml.dump(secdict, default_flow_style=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment