Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/thirdpin/libopencm3.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Gamari <bgamari.foss@gmail.com>2013-07-02 15:23:27 +0400
committerPiotr Esden-Tempski <piotr@esden.net>2013-07-08 00:52:14 +0400
commit4d6a5d96064c9149c8989d2814a9ff8dfc9ed72e (patch)
tree9c08956f9dacbbf829c5ab6282120a4d99288fc2 /scripts
parent21e4d4600f90e807697fde3ba9392f8a6d964323 (diff)
lpc43xx: Move YAML OrderedDict code to separate module
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/data/lpc43xx/csv2yaml.py35
-rw-r--r--scripts/data/lpc43xx/yaml_odict.py81
2 files changed, 82 insertions, 34 deletions
diff --git a/scripts/data/lpc43xx/csv2yaml.py b/scripts/data/lpc43xx/csv2yaml.py
index c7b4ec72..1ec9a413 100755
--- a/scripts/data/lpc43xx/csv2yaml.py
+++ b/scripts/data/lpc43xx/csv2yaml.py
@@ -4,40 +4,7 @@ import sys
import yaml
import csv
from collections import OrderedDict
-
-def repr_odict(dumper, data):
- """
- >>> data = OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')])
- >>> yaml.dump(data, default_flow_style=False)
- '!!omap\\n- foo: bar\\n- mumble: quux\\n- baz: gorp\\n'
- >>> yaml.dump(data, default_flow_style=True)
- '!!omap [foo: bar, mumble: quux, baz: gorp]\\n'
- """
- return repr_pairs(dumper, u'tag:yaml.org,2002:omap', data.iteritems())
-
-yaml.add_representer(OrderedDict, repr_odict)
-
-def repr_pairs(dump, tag, sequence, flow_style=None):
- """This is the same code as BaseRepresenter.represent_sequence(),
- but the value passed to dump.represent_data() in the loop is a
- dictionary instead of a tuple."""
-
- value = []
- node = yaml.SequenceNode(tag, value, flow_style=flow_style)
- if dump.alias_key is not None:
- dump.represented_objects[dump.alias_key] = node
- best_style = True
- for (key, val) in sequence:
- item = dump.represent_data({key: val})
- if not (isinstance(item, yaml.ScalarNode) and not item.style):
- best_style = False
- value.append(item)
- if flow_style is None:
- if dump.default_flow_style is not None:
- node.flow_style = dump.default_flow_style
- else:
- node.flow_style = best_style
- return node
+import yaml_odict
def convert_file(fname):
reader = csv.reader(open(fname, 'r'))
diff --git a/scripts/data/lpc43xx/yaml_odict.py b/scripts/data/lpc43xx/yaml_odict.py
new file mode 100644
index 00000000..05aa2697
--- /dev/null
+++ b/scripts/data/lpc43xx/yaml_odict.py
@@ -0,0 +1,81 @@
+import yaml
+from collections import OrderedDict
+def construct_odict(load, node):
+ """This is the same as SafeConstructor.construct_yaml_omap(),
+ except the data type is changed to OrderedDict() and setitem is
+ used instead of append in the loop.
+
+ >>> yaml.load('''
+ ... !!omap
+ ... - foo: bar
+ ... - mumble: quux
+ ... - baz: gorp
+ ... ''')
+ OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')])
+
+ >>> yaml.load('''!!omap [ foo: bar, mumble: quux, baz : gorp ]''')
+ OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')])
+ """
+
+ omap = OrderedDict()
+ yield omap
+ if not isinstance(node, yaml.SequenceNode):
+ raise yaml.constructor.ConstructorError(
+ "while constructing an ordered map",
+ node.start_mark,
+ "expected a sequence, but found %s" % node.id, node.start_mark
+ )
+ for subnode in node.value:
+ if not isinstance(subnode, yaml.MappingNode):
+ raise yaml.constructor.ConstructorError(
+ "while constructing an ordered map", node.start_mark,
+ "expected a mapping of length 1, but found %s" % subnode.id,
+ subnode.start_mark
+ )
+ if len(subnode.value) != 1:
+ raise yaml.constructor.ConstructorError(
+ "while constructing an ordered map", node.start_mark,
+ "expected a single mapping item, but found %d items" % len(subnode.value),
+ subnode.start_mark
+ )
+ key_node, value_node = subnode.value[0]
+ key = load.construct_object(key_node)
+ value = load.construct_object(value_node)
+ omap[key] = value
+
+yaml.add_constructor(u'tag:yaml.org,2002:omap', construct_odict)
+
+def repr_pairs(dump, tag, sequence, flow_style=None):
+ """This is the same code as BaseRepresenter.represent_sequence(),
+ but the value passed to dump.represent_data() in the loop is a
+ dictionary instead of a tuple."""
+
+ value = []
+ node = yaml.SequenceNode(tag, value, flow_style=flow_style)
+ if dump.alias_key is not None:
+ dump.represented_objects[dump.alias_key] = node
+ best_style = True
+ for (key, val) in sequence:
+ item = dump.represent_data({key: val})
+ if not (isinstance(item, yaml.ScalarNode) and not item.style):
+ best_style = False
+ value.append(item)
+ if flow_style is None:
+ if dump.default_flow_style is not None:
+ node.flow_style = dump.default_flow_style
+ else:
+ node.flow_style = best_style
+ return node
+
+def repr_odict(dumper, data):
+ """
+ >>> data = OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')])
+ >>> yaml.dump(data, default_flow_style=False)
+ '!!omap\\n- foo: bar\\n- mumble: quux\\n- baz: gorp\\n'
+ >>> yaml.dump(data, default_flow_style=True)
+ '!!omap [foo: bar, mumble: quux, baz: gorp]\\n'
+ """
+ return repr_pairs(dumper, u'tag:yaml.org,2002:omap', data.iteritems())
+
+yaml.add_representer(OrderedDict, repr_odict)
+