Created
January 7, 2015 21:01
-
-
Save AmandaCameron/a761313a6461ff1069f2 to your computer and use it in GitHub Desktop.
Starbound __merge - > JSON .patch conversion
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
# Converts the old __merge: [] syntax of Starbound file modifying to the new .patch format. | |
import json | |
import sys | |
def convert_file(file): | |
data = None | |
with open(file, "r") as f: | |
data = json.load(f) | |
ops = convert_data("", data) | |
with open(file + ".patch", "w") as f: | |
json.dump(ops, f) | |
def convert_data(path, data): | |
ops = [] | |
do_merge = False | |
if "__merge" in data and isinstance(data["__merge"], list): | |
for k, v in list(data.items()): | |
if k == "__merge": | |
continue | |
if isinstance(v, dict) and "__merge" in v: | |
ops.append(*convert_data(path + "/" + k, v)) | |
ops.append({ | |
"op": "add", | |
"path": path + "/" + k, | |
"value": v | |
}) | |
return ops | |
for file in sys.argv[1:]: | |
print("Converting", file) | |
convert_file(file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment