Skip to content

Instantly share code, notes, and snippets.

@Razikus
Created October 22, 2019 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Razikus/cc0d94db5336e2b9fa357b83622c0625 to your computer and use it in GitHub Desktop.
Save Razikus/cc0d94db5336e2b9fa357b83622c0625 to your computer and use it in GitHub Desktop.
For the situation, where you have 2 yaml files, one is older, but fits in 50% of bigger, and bigger have just new keys, then you don't want to do something from beginning, and consider the script that will just join 2 files, but take objects that exists from the bigger file, uff.
import ruamel.yaml as yamler
import ruamel
import sys
def main():
resultDict = dict()
if(len(sys.argv) < 4):
print("USAGE: script biggerYaml smallerYaml resultfile")
print("If entry is in smaller yaml - it will go to result")
print("If entry is not in smaller yaml - it will take it from biggerYaml")
sys.exit(1)
firstYAML = yamler.YAML()
a = open(sys.argv[1])
firsts = firstYAML.load(a)
a.close()
a = open(sys.argv[2])
second = firstYAML.load(a)
a.close()
result = process(firsts, second, resultDict)
with open(sys.argv[3], "w") as fp:
firstYAML.dump(result, fp)
def process(entry, entrysmaller, result):
types = type(entry)
if types == ruamel.yaml.comments.CommentedMap:
for item in entry:
typeOfItem = type(entry[item])
if(typeOfItem == str):
if item in entrysmaller:
result[item] = entrysmaller[item]
else:
result[item] = entry[item]
elif(typeOfItem == ruamel.yaml.comments.CommentedMap):
if item not in entrysmaller:
result[item] = entry[item]
else:
result[item] = dict()
process(entry[item], entrysmaller[item], result[item])
elif typeOfItem == ruamel.yaml.scalarstring.LiteralScalarString:
if item in entrysmaller:
result[item] = entrysmaller[item]
else:
result[item] = entry[item]
else:
print("WARNING...")
print("NOT SUPPORTED CLASS:", typeOfItem)
print("NOT SUPPORTED VALUE:", item)
print("...ENDWARNING")
return result
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment