mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-10 08:04:16 +00:00
add toml convertor
This commit is contained in:
55
scripts/yaml-to-toml.py
Executable file
55
scripts/yaml-to-toml.py
Executable file
@@ -0,0 +1,55 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
"""
|
||||||
|
author: deadc0de6 (https://github.com/deadc0de6)
|
||||||
|
Copyright (c) 2022, deadc0de6
|
||||||
|
|
||||||
|
convert yaml config file to toml
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
# pip3 install ruamel.yaml
|
||||||
|
from ruamel.yaml import YAML as yaml
|
||||||
|
# pip3 install toml
|
||||||
|
import toml
|
||||||
|
|
||||||
|
|
||||||
|
def yaml_load(path):
|
||||||
|
"""load from yaml"""
|
||||||
|
with open(path, 'r', encoding='utf8') as file:
|
||||||
|
data = yaml()
|
||||||
|
data.typ = 'rt'
|
||||||
|
content = data.load(file)
|
||||||
|
return content
|
||||||
|
|
||||||
|
|
||||||
|
def replace_None(content):
|
||||||
|
"""replace any occurence of None with empty string"""
|
||||||
|
n = {}
|
||||||
|
for k in content:
|
||||||
|
if not content[k]:
|
||||||
|
n[k] = ""
|
||||||
|
continue
|
||||||
|
if isinstance(content[k], dict):
|
||||||
|
n[k] = replace_None(content[k])
|
||||||
|
continue
|
||||||
|
n[k] = content[k]
|
||||||
|
return n
|
||||||
|
|
||||||
|
|
||||||
|
def toml_dump(content):
|
||||||
|
"""dump toml to stdout"""
|
||||||
|
return toml.dumps(content)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("usage: {} <yaml-config-path>".format(sys.argv[0]))
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
path = sys.argv[1]
|
||||||
|
content = yaml_load(path)
|
||||||
|
content = replace_None(content)
|
||||||
|
out = toml_dump(content)
|
||||||
|
print(out)
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
from ruamel.yaml import YAML as yaml
|
|
||||||
from deepdiff import DeepDiff
|
|
||||||
import toml
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import pprint
|
|
||||||
import json
|
|
||||||
|
|
||||||
# pip install toml deepdiff
|
|
||||||
|
|
||||||
|
|
||||||
def _yaml_load(path):
|
|
||||||
"""load from yaml"""
|
|
||||||
with open(path, 'r', encoding='utf8') as file:
|
|
||||||
data = yaml()
|
|
||||||
data.typ = 'rt'
|
|
||||||
content = data.load(file)
|
|
||||||
return content
|
|
||||||
|
|
||||||
|
|
||||||
def _yaml_dump(content, where):
|
|
||||||
"""dump to yaml"""
|
|
||||||
data = yaml()
|
|
||||||
data.default_flow_style = False
|
|
||||||
data.indent = 2
|
|
||||||
data.typ = 'rt'
|
|
||||||
data.dump(content, where)
|
|
||||||
|
|
||||||
|
|
||||||
def _toml_load(path):
|
|
||||||
"""load from toml"""
|
|
||||||
with open(path, 'r', encoding='utf8') as file:
|
|
||||||
data = file.read()
|
|
||||||
content = toml.loads(data)
|
|
||||||
return content
|
|
||||||
|
|
||||||
|
|
||||||
def _toml_dump(content, where):
|
|
||||||
with open(where, 'w') as f:
|
|
||||||
toml.dump(content, f)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
if len(sys.argv) < 3:
|
|
||||||
print("usage:")
|
|
||||||
print(" {} to-toml <yaml-path>".format(sys.argv[0]))
|
|
||||||
print(" {} to-yaml <toml-path>".format(sys.argv[0]))
|
|
||||||
print(" {} compare <yaml-path> <toml-path>".format(sys.argv[0]))
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
act = sys.argv[1]
|
|
||||||
arg = sys.argv[2]
|
|
||||||
if len(sys.argv) > 3:
|
|
||||||
arg2 = sys.argv[3]
|
|
||||||
out = '/tmp/res'
|
|
||||||
|
|
||||||
if act == "to-toml":
|
|
||||||
|
|
||||||
content = _yaml_load(arg)
|
|
||||||
o = out + '.toml'
|
|
||||||
_toml_dump(content, o)
|
|
||||||
print("saved to {}".format(o))
|
|
||||||
|
|
||||||
elif act == "to-yaml":
|
|
||||||
|
|
||||||
content = _toml_load(arg)
|
|
||||||
o = out + '.yaml'
|
|
||||||
_yaml_dump(content, o)
|
|
||||||
print("saved to {}".format(o))
|
|
||||||
|
|
||||||
elif act == "compare":
|
|
||||||
a = _yaml_load(arg)
|
|
||||||
|
|
||||||
print("YAML dict:")
|
|
||||||
print(json.dumps(a, indent=4))
|
|
||||||
b = _toml_load(arg2)
|
|
||||||
|
|
||||||
print("TOML dict:")
|
|
||||||
print(json.dumps(b, indent=4))
|
|
||||||
|
|
||||||
diff = DeepDiff(a, b, ignore_order=True, view='tree')
|
|
||||||
print(diff)
|
|
||||||
Reference in New Issue
Block a user