Created
August 16, 2016 22:52
-
-
Save markus-mnm/767fa5bc9463515170c33447046bbfd8 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
import yaml, os, sys, datetime | |
# slightly adapted from http://stackoverflow.com/a/44512 | |
def merge(d1, d2, merge_fn=lambda x,y:y): | |
result = dict(d1) | |
for k,v in d2.iteritems(): | |
if k in result: | |
if isinstance(result[k], dict): | |
print "going in for " + k | |
result[k] = merge_fn(result[k], v) | |
else: | |
print "setting " + k + " with " + str(v) | |
result[k] = v | |
else: | |
print "setting " + k + " with " + str(v) | |
result[k] = v | |
return result | |
if len(sys.argv) <= 2 : | |
print "fileToPath patch" | |
sys.exit(2) | |
inputFilename = sys.argv[1] | |
patchFilename = sys.argv[2] | |
print "Patching " + inputFilename + " with " + patchFilename | |
with open(inputFilename) as f: | |
originalCfg = yaml.load(f) | |
with open(inputFilename + ".reformatted", "w") as f: | |
yaml.dump(originalCfg, f, default_flow_style=False) | |
with open(patchFilename) as f: | |
patchCfg = yaml.load(f) | |
os.rename(inputFilename, inputFilename + datetime.datetime.now().isoformat()) | |
#for level1Keys in patchCfg.keys(): | |
#for level2Keys in level1Keys.keys(): | |
#if level1Keys not in originalCfg | |
#all[k].append(a[k]) | |
#else: | |
#all[k] = a[k] | |
rec_merge = lambda x, y: merge(x, y, rec_merge) | |
patchedConfig = merge (originalCfg, patchCfg, rec_merge) | |
with open(inputFilename, "w") as f: | |
yaml.dump(patchedConfig, f, default_flow_style=False) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment