mirror of
https://github.com/github/octodns.git
synced 2024-05-11 05:55:00 +00:00
Rework Yaml constructor/representer to match PyYaml's setup
They apparently should be called on class objects rather than on instances. I ran into thread-safety problems (eating data) before this change. /cc http://pyyaml.org/ticket/36 which mentions those methods not being thread-safe, but that PyYaml itself should be.
This commit is contained in:
@@ -30,11 +30,7 @@ def _zero_padded_numbers(s):
|
||||
# here
|
||||
class SortEnforcingLoader(SafeLoader):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SortEnforcingLoader, self).__init__(*args, **kwargs)
|
||||
self.add_constructor(self.DEFAULT_MAPPING_TAG, self._construct)
|
||||
|
||||
def _construct(self, _, node):
|
||||
def _construct(self, node):
|
||||
self.flatten_mapping(node)
|
||||
ret = self.construct_pairs(node)
|
||||
keys = [d[0] for d in ret]
|
||||
@@ -44,6 +40,10 @@ class SortEnforcingLoader(SafeLoader):
|
||||
return dict(ret)
|
||||
|
||||
|
||||
SortEnforcingLoader.add_constructor(SortEnforcingLoader.DEFAULT_MAPPING_TAG,
|
||||
SortEnforcingLoader._construct)
|
||||
|
||||
|
||||
def safe_load(stream, enforce_order=True):
|
||||
return load(stream, SortEnforcingLoader if enforce_order else SafeLoader)
|
||||
|
||||
@@ -57,16 +57,15 @@ class SortingDumper(SafeDumper):
|
||||
more info
|
||||
'''
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SortingDumper, self).__init__(*args, **kwargs)
|
||||
self.add_representer(dict, self._representer)
|
||||
|
||||
def _representer(self, _, data):
|
||||
def _representer(self, data):
|
||||
data = data.items()
|
||||
data.sort(key=lambda d: _zero_padded_numbers(d[0]))
|
||||
return self.represent_mapping(self.DEFAULT_MAPPING_TAG, data)
|
||||
|
||||
|
||||
SortingDumper.add_representer(dict, SortingDumper._representer)
|
||||
|
||||
|
||||
def safe_dump(data, fh, **options):
|
||||
kwargs = {
|
||||
'canonical': False,
|
||||
|
Reference in New Issue
Block a user