Ignore:
Timestamp:
09/02/13 00:18:50 (8 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
Phase:
public
Amend:
63393330313363613231316330613166373636623937663033313537396430393532623165393632
Message:

Dump our YAML subset too

File:
1 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()
Note: See TracChangeset for help on using the changeset viewer.