Use the much faster C yaml loader/dumper
This commit is contained in:
parent
a890b9084e
commit
29b5a7b4da
@ -5,6 +5,14 @@ import yaml
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from pathvalidate import sanitize_filename
|
from pathvalidate import sanitize_filename
|
||||||
|
from yaml import CDumper as Dumper
|
||||||
|
|
||||||
|
|
||||||
|
class Loader(yaml.CLoader):
|
||||||
|
"""Hack to store the stream before passing it off to :class:`yaml.CLoader`"""
|
||||||
|
def __init__(self, stream):
|
||||||
|
self.stream = stream
|
||||||
|
super().__init__(stream)
|
||||||
|
|
||||||
|
|
||||||
class IncludeTag:
|
class IncludeTag:
|
||||||
@ -23,13 +31,13 @@ class IncludeTag:
|
|||||||
target = os.path.join(base_dir, node.value)
|
target = os.path.join(base_dir, node.value)
|
||||||
with open(target) as f:
|
with open(target) as f:
|
||||||
if target.endswith('.yaml'):
|
if target.endswith('.yaml'):
|
||||||
return yaml.load(f, Loader=yaml.Loader)
|
return yaml.load(f, Loader=Loader)
|
||||||
else:
|
else:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
|
||||||
|
|
||||||
yaml.add_constructor('!include', IncludeTag.from_yaml)
|
Loader.add_constructor('!include', IncludeTag.from_yaml)
|
||||||
yaml.add_representer(IncludeTag, IncludeTag.to_yaml)
|
Dumper.add_representer(IncludeTag, IncludeTag.to_yaml)
|
||||||
|
|
||||||
|
|
||||||
def uniqueName(obj):
|
def uniqueName(obj):
|
||||||
@ -88,7 +96,7 @@ def recursivelyUnpackObject(parent_dir, obj,
|
|||||||
obj[k] = recursively_normalize_values(obj[k])
|
obj[k] = recursively_normalize_values(obj[k])
|
||||||
|
|
||||||
with open(file_base_path + '.yaml', 'w') as f:
|
with open(file_base_path + '.yaml', 'w') as f:
|
||||||
yaml.dump(obj, f)
|
yaml.dump(obj, f, Dumper=Dumper)
|
||||||
|
|
||||||
return obj_base_name + '.yaml'
|
return obj_base_name + '.yaml'
|
||||||
|
|
||||||
@ -107,7 +115,7 @@ def unpackJson(json_file, output_name):
|
|||||||
|
|
||||||
def packYaml(yaml_file, output_json_file):
|
def packYaml(yaml_file, output_json_file):
|
||||||
with open(yaml_file) as f:
|
with open(yaml_file) as f:
|
||||||
data = yaml.load(f, Loader=yaml.Loader)
|
data = yaml.load(f, Loader=Loader)
|
||||||
|
|
||||||
with open(output_json_file, 'w') as f:
|
with open(output_json_file, 'w') as f:
|
||||||
json.dump(data, f, indent=2)
|
json.dump(data, f, indent=2)
|
||||||
|
Loading…
Reference in New Issue
Block a user