Changeset 138:366b334a7018


Ignore:
Timestamp:
Sep 2, 2013, 4:12:42 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
rebase_source:
fed7e5a2d4f0c3a61f92e0d51daecd54fbeba081
Message:

Render tuples to lists

Location:
nagslang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/tests/test_yamlish.py

    r137 r138  
    3535
    3636class TestRoundTrip(TestCase):
     37    from_pyyaml = False
     38
    3739    def roundtrip(self, data):
    3840        text = self.dump_s(data)
     
    107109        })
    108110
     111    def test_tuples(self):
     112        if self.from_pyyaml:
     113            raise SkipTest("Can't parse PyYAML tuples")
     114        orig = {
     115            'polygons': {
     116                1: [
     117                    (0, 1),
     118                    (2, 3),
     119                ],
     120            },
     121        }
     122        text = self.dump_s(orig)
     123        result = self.load_s(text)
     124        self.assertEqual(orig['polygons'][1][0],
     125                         tuple(result['polygons'][1][0]))
     126        self.assertEqual(orig['polygons'][1][1],
     127                         tuple(result['polygons'][1][1]))
     128
    109129    def test_quoted(self):
    110130        # a literal true is True, but 'true' is a string
     
    122142
    123143class TestFromPyYAML(TestRoundTrip):
     144    from_pyyaml = True
     145
    124146    def dump_s(self, data):
    125147        if yaml is None:
     
    129151
    130152class TestFromPyYAMLInlineLists(TestRoundTrip):
     153    from_pyyaml = True
     154
    131155    def dump_s(self, data):
    132156        if yaml is None:
  • nagslang/yamlish.py

    r137 r138  
    3535
    3636    def _dump_block(self, data, indent=0):
    37         for type_ in (list, dict):
     37        for type_ in (list, tuple, dict):
    3838            if isinstance(data, type_):
    3939                f = getattr(self, '_dump_%s_block' % type_.__name__)
     
    4242
    4343    def _dump_inline(self, data):
    44         if data in (True, False, None):
     44        if data is True or data is False or data is None:
    4545            return self._dump_literal(data)
    46         for type_ in (list, dict, basestring, int, float):
     46        for type_ in (list, tuple, dict, basestring, int, float):
    4747            if isinstance(data, type_):
    4848                f = getattr(self, '_dump_%s' % type_.__name__)
     
    6161                output += dumped
    6262        return output
     63
     64    _dump_tuple_block = _dump_list_block
    6365
    6466    def _dump_dict_block(self, data, indent):
     
    7779
    7880    def _inlineable(self, data):
    79         if isinstance(data, list):
    80             return all(not isinstance(item, (list, dict)) for item in data)
     81        if isinstance(data, (list, tuple)):
     82            return all(not isinstance(item, (list, dict, tuple))
     83                       for item in data)
    8184        elif isinstance(data, dict):
    82             return all(not isinstance(item, (list, dict))
     85            return all(not isinstance(item, (list, dict, tuple))
    8386                       for item in data.itervalues())
    8487        else:
     
    8891        return '[%s]' % ', '.join(self._dump_inline(item) for item in data)
    8992
     93    _dump_tuple = _dump_list
     94
    9095    def _dump_dict(self, data):
    9196        return '{%s}' % ', '.join(
    92                 '%s: %s' % (self._dump_inline(key), self._dump_inline(value))
    93                 for key, value in data.iteritems())
     97            '%s: %s' % (self._dump_inline(key), self._dump_inline(value))
     98            for key, value in data.iteritems())
    9499
    95100    def _dump_basestring(self, data):
Note: See TracChangeset for help on using the changeset viewer.