Skip to content

Commit

Permalink
fixes #319
Browse files Browse the repository at this point in the history
  • Loading branch information
advornic committed Jul 30, 2015
1 parent 1493ee7 commit 92004ab
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
5 changes: 4 additions & 1 deletion plugins/allocate
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,17 @@ Definition example:
import logging
import os

from collections import OrderedDict

from ztpserver.serializers import load, dump
from ztpserver.constants import CONTENT_TYPE_YAML
from ztpserver.config import runtime


log = logging.getLogger(__name__) #pylint: disable=C0103

def load_resource(node_id, filename):
data = dict()
data = OrderedDict()

contents = load(filename,
CONTENT_TYPE_YAML,
Expand Down
53 changes: 48 additions & 5 deletions ztpserver/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@
# pylint: disable=R0201
#

from ztpserver.constants import CONTENT_TYPE_OTHER
from ztpserver.constants import CONTENT_TYPE_JSON
from ztpserver.constants import CONTENT_TYPE_YAML

import collections
import logging
import json
import os
import threading
import yaml

from collections import OrderedDict

from ztpserver.constants import CONTENT_TYPE_OTHER
from ztpserver.constants import CONTENT_TYPE_JSON
from ztpserver.constants import CONTENT_TYPE_YAML

READ_WRITE_LOCK = {}
log = logging.getLogger(__name__) #pylint: disable=C0103

Expand Down Expand Up @@ -77,6 +79,47 @@ def serialize(self, data):
''' Serialize a dict object and return text '''
return str(data)

#------------------------------------------------------------------------------
# Source: Michael Elsdörfer (https://gist.github.com/miracle2k))
# https://gist.githubusercontent.com/miracle2k/
# 3184458/raw/ae89e23502f95c4555f0643dafae8a748e3fb382/
# odict.py

def represent_odict(dump_odict, tag, mapping, flow_style=None):
'''
Like BaseRepresenter.represent_mapping, but does not issue the sort().
'''
value = []
node = yaml.MappingNode(tag, value, flow_style=flow_style)
if dump_odict.alias_key is not None:
dump_odict.represented_objects[dump_odict.alias_key] = node
best_style = True
if hasattr(mapping, 'items'):
mapping = mapping.items()
for item_key, item_value in mapping:
node_key = dump_odict.represent_data(item_key)
node_value = dump_odict.represent_data(item_value)
if not (isinstance(node_key, yaml.ScalarNode) and
not node_key.style):
best_style = False
if not (isinstance(node_value, yaml.ScalarNode) and
not node_value.style):
best_style = False
value.append((node_key, node_value))
if flow_style is None:
if dump_odict.default_flow_style is not None:
node.flow_style = dump_odict.default_flow_style
else:
node.flow_style = best_style
return node

yaml.SafeDumper.add_representer(
OrderedDict,
lambda dumper,
value: represent_odict(dumper,
u'tag:yaml.org,2002:map',
value))
#------------------------------------------------------------------------------

class YAMLSerializer(BaseSerializer):

Expand All @@ -97,7 +140,7 @@ def serialize(self, data):
''' Serialize a dict object and return YAML '''

try:
return yaml.dump(data, default_flow_style=False)
return yaml.safe_dump(data, default_flow_style=False)
except yaml.YAMLError as err:
msg = '''%s: unable to serialize YAML data:
%s
Expand Down

0 comments on commit 92004ab

Please sign in to comment.