Changeset 90:a8d83de5b460


Ignore:
Timestamp:
Sep 2, 2013, 12:18:50 AM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
amend_source:
c93013ca211c0a1f766b97f031579d0952b1e962
Message:

Dump our YAML subset too

Location:
nagslang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/level_serializer.py

    r89 r90  
    66
    77import re
    8 # TODO: Dump pyyaml
    9 import yaml
    108
    119
    1210def dump(data, file_object):
    13     yaml.dump(data, file_object, default_flow_style=False)
     11    file_object.write('\n'.join(_dump(data, 0)))
     12
     13
     14def _dump(obj, indent):
     15    if isinstance(obj, list):
     16        for item in obj:
     17            yield '%s- %s' % (' ' * indent,
     18                              '\n'.join(_dump(item, indent + 2)).strip())
     19    elif isinstance(obj, dict):
     20        for k, v in obj.iteritems():
     21            if isinstance(v, basestring):
     22                yield '%s%s: %s' % (' ' * indent, k, v)
     23            else:
     24                extra = 0 if isinstance(v, list) else 2
     25                yield '%s%s:\n%s' % (' ' * indent, k,
     26                                     '\n'.join(_dump(v, indent + extra)))
     27    elif isinstance(obj, basestring):
     28        yield '%s%s' % (' ' * indent, obj)
    1429
    1530
     
    5671            parent_key = None
    5772
    58         while lm and lm.group(2).startswith('-'):
     73        while lm and lm.group(2).startswith('- '):
    5974            # Nested lists
    6075            prefix, line = lm.groups()
  • nagslang/tests/test_level_serializer.py

    r89 r90  
    1 from unittest import TestCase
     1from unittest import TestCase, SkipTest
    22from StringIO import StringIO
     3
     4try:
     5    import yaml
     6except ImportError:
     7    yaml = None  # pyflakes:ignore
    38
    49from nagslang.level_serializer import load, dump
    510
    611
    7 class TestSimple(TestCase):
     12class TestRoundTrip(TestCase):
    813    def roundtrip(self, data):
    914        f = StringIO()
     15        self.dump(data, f)
     16        f.seek(0)
     17        print '\n=== Begin ===\n%s\n=== End ===' % f.buf.rstrip()
     18        self.assertEqual(self.load(f), data)
     19
     20    def dump(self, data, f):
    1021        dump(data, f)
    11         f.seek(0)
    12         #print '\n== Begin ==\n%s== End ==' % f.buf
    13         self.assertEqual(load(f), data)
     22
     23    def load(self, f):
     24        return load(f)
    1425
    1526    def test_simple_dict(self):
     
    4657            ],
    4758        })
     59
     60
     61class TestFromPyYAML(TestRoundTrip):
     62    def dump(self, data, f):
     63        if yaml is None:
     64            raise SkipTest('yaml module unavailable')
     65        yaml.dump(data, f, default_flow_style=False)
     66
     67
     68class TestToPyYAML(TestRoundTrip):
     69    def load(self, f):
     70        if yaml is None:
     71            raise SkipTest('yaml module unavailable')
     72        return yaml.load(f)
Note: See TracChangeset for help on using the changeset viewer.