Example #1
0
            >>> import yaml
            >>> yaml.dump(b, default_flow_style=True)
            '!munch.Munch {foo: [bar, !munch.Munch {lol: true}], hello: 42}\\n'
        """
        return dumper.represent_mapping(u('!munch.Munch'), data)

    for loader_name in ("BaseLoader", "FullLoader", "SafeLoader", "Loader", "UnsafeLoader", "DangerLoader"):
        LoaderCls = getattr(yaml, loader_name, None)
        if LoaderCls is None:
            # This code supports both PyYAML 4.x and 5.x versions
            continue
        yaml.add_constructor(u('!munch'), from_yaml, Loader=LoaderCls)
        yaml.add_constructor(u('!munch.Munch'), from_yaml, Loader=LoaderCls)

    SafeRepresenter.add_representer(Munch, to_yaml_safe)
    SafeRepresenter.add_multi_representer(Munch, to_yaml_safe)

    Representer.add_representer(Munch, to_yaml)
    Representer.add_multi_representer(Munch, to_yaml)

    # Instance methods for YAML conversion
    def toYAML(self, **options):
        """ Serializes this Munch to YAML, using `yaml.safe_dump()` if
            no `Dumper` is provided. See the PyYAML documentation for more info.

            >>> b = Munch(foo=['bar', Munch(lol=True)], hello=42)
            >>> import yaml
            >>> yaml.safe_dump(b, default_flow_style=True)
            '{foo: [bar, {lol: true}], hello: 42}\\n'
            >>> b.toYAML(default_flow_style=True)
            '{foo: [bar, {lol: true}], hello: 42}\\n'
    def to_yaml(dumper, data):
        """ Converts OrderedBunch to a representation node.
            
            >>> b = OrderedBunch(foo=['bar', OrderedBunch(lol=True)], hello=42)
            >>> import yaml
            >>> yaml.dump(b, default_flow_style=True)
            '!bunch.OrderedBunch {foo: [bar, !bunch.OrderedBunch {lol: true}], hello: 42}\\n'
        """
        return dumper.represent_mapping(u'!orderedbunch.OrderedBunch', data)

    yaml.add_constructor(u'!orderedbunch', from_yaml)
    yaml.add_constructor(u'!orderedbunch.OrderedBunch', from_yaml)

    SafeRepresenter.add_representer(OrderedBunch, to_yaml_safe)
    SafeRepresenter.add_multi_representer(OrderedBunch, to_yaml_safe)

    Representer.add_representer(OrderedBunch, to_yaml)
    Representer.add_multi_representer(OrderedBunch, to_yaml)

    # Instance methods for YAML conversion
    def toYAML(self, **options):
        """ Serializes this OrderedBunch to YAML, using `yaml.safe_dump()` if 
            no `Dumper` is provided. See the PyYAML documentation for more info.
            
            >>> b = OrderedBunch(foo=['bar', OrderedBunch(lol=True)], hello=42)
            >>> import yaml
            >>> yaml.safe_dump(b, default_flow_style=True)
            '{foo: [bar, {lol: true}], hello: 42}\\n'
            >>> b.toYAML(default_flow_style=True)
            '{foo: [bar, {lol: true}], hello: 42}\\n'
Example #3
0
 def to_yaml(dumper, data):
     """ Converts OrderedBunch to a representation node.
         
         >>> b = OrderedBunch(foo=['bar', OrderedBunch(lol=True)], hello=42)
         >>> import yaml
         >>> yaml.dump(b, default_flow_style=True)
         '!bunch.OrderedBunch {foo: [bar, !bunch.OrderedBunch {lol: true}], hello: 42}\\n'
     """
     return dumper.represent_mapping(u'!orderedbunch.OrderedBunch', data)
 
 
 yaml.add_constructor(u'!orderedbunch', from_yaml)
 yaml.add_constructor(u'!orderedbunch.OrderedBunch', from_yaml)
 
 SafeRepresenter.add_representer(OrderedBunch, to_yaml_safe)
 SafeRepresenter.add_multi_representer(OrderedBunch, to_yaml_safe)
 
 Representer.add_representer(OrderedBunch, to_yaml)
 Representer.add_multi_representer(OrderedBunch, to_yaml)
 
 
 # Instance methods for YAML conversion
 def toYAML(self, **options):
     """ Serializes this OrderedBunch to YAML, using `yaml.safe_dump()` if 
         no `Dumper` is provided. See the PyYAML documentation for more info.
         
         >>> b = OrderedBunch(foo=['bar', OrderedBunch(lol=True)], hello=42)
         >>> import yaml
         >>> yaml.safe_dump(b, default_flow_style=True)
         '{foo: [bar, {lol: true}], hello: 42}\\n'
         >>> b.toYAML(default_flow_style=True)
Example #4
0
    def to_yaml(dumper, data):
        """ Converts Munch to a representation node.

            >>> b = Munch(foo=['bar', Munch(lol=True)], hello=42)
            >>> import yaml
            >>> yaml.dump(b, default_flow_style=True)
            '!munch.Munch {foo: [bar, !munch.Munch {lol: true}], hello: 42}\\n'
        """
        return dumper.represent_mapping(u('!munch.Munch'), data)

    yaml.add_constructor(u('!munch'), from_yaml)
    yaml.add_constructor(u('!munch.Munch'), from_yaml)

    SafeRepresenter.add_representer(Munch, to_yaml_safe)
    SafeRepresenter.add_multi_representer(Munch, to_yaml_safe)

    Representer.add_representer(Munch, to_yaml)
    Representer.add_multi_representer(Munch, to_yaml)

    # Instance methods for YAML conversion
    def toYAML(self, **options):
        """ Serializes this Munch to YAML, using `yaml.safe_dump()` if
            no `Dumper` is provided. See the PyYAML documentation for more info.

            >>> b = Munch(foo=['bar', Munch(lol=True)], hello=42)
            >>> import yaml
            >>> yaml.safe_dump(b, default_flow_style=True)
            '{foo: [bar, {lol: true}], hello: 42}\\n'
            >>> b.toYAML(default_flow_style=True)
            '{foo: [bar, {lol: true}], hello: 42}\\n'
Example #5
0
}

def toyaml(*records, **kw):
    if kw: records += (kw,)
    # SafeDumper will not emit python/foo tags for unicode or objects
    return yaml.safe_dump_all(records, **DEFAULT_OPTIONS)

def write_yaml(*records, **options):
    options = merge({}, DEFAULT_OPTIONS, options)
    return yaml.safe_dump_all(records, **options)


from path import path
from yaml.representer import Representer, SafeRepresenter
SafeRepresenter.add_representer(path, SafeRepresenter.represent_unicode)
SafeRepresenter.add_multi_representer(path, SafeRepresenter.represent_unicode)

Representer.add_representer(path, Representer.represent_unicode)
Representer.add_multi_representer(path, Representer.represent_unicode)


import types
from yaml import Dumper, SafeDumper

class PPDumper(SafeDumper, Dumper):
    pass

# PPDumper.add_representer(unicode, SafeDumper.represent_unicode)
# PPDumper.add_representer(types.ClassType, Dumper.represent_name)
# PPDumper.add_representer(types.FunctionType, Dumper.represent_name)
# PPDumper.add_representer(types.BuiltinFunctionType, Dumper.represent_name)
Example #6
0
    def dict_dump(self):
        return unstitch(self)


def from_yaml(loader, node):
    data = Threads()
    yield data

    value = loader.construct_mapping(node)
    data.update(value)


def to_yaml(dumper, data):
    return dumper.represent_mapping(six.u('!threads.Threads'), data)


def to_yaml_safe(dumper, data):
    return dumper.represent_dict(data)


yaml.add_constructor(six.u('!threads'), from_yaml)
yaml.add_constructor(six.u('!threads.Threads'), from_yaml)

Representer.add_representer(Threads, to_yaml)
Representer.add_multi_representer(Threads, to_yaml)

SafeRepresenter.add_representer(Threads, to_yaml_safe)
SafeRepresenter.add_multi_representer(Threads, to_yaml_safe)

items = Threads(a=0, b=1, c=[0, 2], d=Threads(x=0, y=1, z=2))
print items.yaml_dump()
            raise ConstructorError(
                'while constructing a mapping', node.start_mark,
                'found unacceptable key (%s)' % exc, key_node.start_mark)
        attrdict[key] = loader.construct_object(value_node, deep=False)


class AttrDictYAMLLoader(Loader):
    '''A YAML loader that loads mappings into ordered AttrDict.

    >>> attrdict = yaml.load('x: 1\ny: 2', Loader=AttrDictYAMLLoader)
    '''

    def __init__(self, *args, **kwargs):
        super(AttrDictYAMLLoader, self).__init__(*args, **kwargs)
        self.add_constructor(u'tag:yaml.org,2002:map', from_yaml)
        self.add_constructor(u'tag:yaml.org,2002:omap', from_yaml)


def to_yaml(dumper, data):
    'Convert AttrDict to dictionary, preserving order'
    # yaml.representer.BaseRepresenter.represent_mapping sorts keys if the
    # object has .items(). So instead, pass the items directly.
    return dumper.represent_mapping(u'tag:yaml.org,2002:map', data.items())


SafeRepresenter.add_representer(AttrDict, to_yaml)
SafeRepresenter.add_multi_representer(AttrDict, to_yaml)

Representer.add_representer(AttrDict, to_yaml)
Representer.add_multi_representer(AttrDict, to_yaml)
Example #8
0
    def to_yaml(dumper, data):
        """ Converts Chunk to a representation node.

            >>> b = Chunk(foo=['bar', Chunk(lol=True)], hello=42)
            >>> import yaml
            >>> yaml.dump(b, default_flow_style=True)
            '!chunk.Chunk {foo: [bar, !chunk.Chunk {lol: true}], hello: 42}\\n'
        """
        return dumper.represent_mapping(u('!chunk.Chunk'), data)

    yaml.add_constructor(u('!chunk'), from_yaml)
    yaml.add_constructor(u('!chunk.Chunk'), from_yaml)

    SafeRepresenter.add_representer(Chunk, to_yaml_safe)
    SafeRepresenter.add_multi_representer(Chunk, to_yaml_safe)

    Representer.add_representer(Chunk, to_yaml)
    Representer.add_multi_representer(Chunk, to_yaml)

    # Instance methods for YAML conversion
    def toYAML(self, **options):
        """ Serializes this Chunk to YAML, using `yaml.safe_dump()` if
            no `Dumper` is provided. See the PyYAML documentation for more info.

            >>> b = Chunk(foo=['bar', Chunk(lol=True)], hello=42)
            >>> import yaml
            >>> yaml.safe_dump(b, default_flow_style=True)
            '{foo: [bar, {lol: true}], hello: 42}\\n'
            >>> b.toYAML(default_flow_style=True)
            '{foo: [bar, {lol: true}], hello: 42}\\n'
Example #9
0
                for item in result[c.name]:
                    if isinstance(item, SAModelMixin):
                        item = copy(item)
                        item._embedded_as = c
                    l.append(item)
                result[c.name] = l
        return result

    @staticmethod
    def yaml_safe_representer(dumper, data):
        return dumper.represent_mapping(
            u'tag:yaml.org,2002:map', data.as_dict().items(), flow_style=False)

try:
    from yaml.representer import SafeRepresenter
    SafeRepresenter.add_multi_representer(
        SAModelMixin, SAModelMixin.yaml_safe_representer)
    del SafeRepresenter
except ImportError: # pragma: no cover
    pass

def hasUserMixin(owner_id_field):
    """
    Quite specific helper, that was made to ease working with permissions
    for models referencing Django-like user objects.
    """
    class HasUserMixin(object):
        @classmethod
        def check_class_permissions(cls, user=None):
            if user is None and hasattr(g, "user"):
                user = g.user
Example #10
0
            raise ConstructorError('while constructing a mapping',
                                   node.start_mark,
                                   'found unacceptable key (%s)' % exc,
                                   key_node.start_mark)
        attrdict[key] = loader.construct_object(value_node, deep=False)


class AttrDictYAMLLoader(Loader):
    '''A YAML loader that loads mappings into ordered AttrDict.

    >>> attrdict = yaml.load('x: 1\ny: 2', Loader=AttrDictYAMLLoader)
    '''
    def __init__(self, *args, **kwargs):
        super(AttrDictYAMLLoader, self).__init__(*args, **kwargs)
        self.add_constructor(u'tag:yaml.org,2002:map', from_yaml)
        self.add_constructor(u'tag:yaml.org,2002:omap', from_yaml)


def to_yaml(dumper, data):
    'Convert AttrDict to dictionary, preserving order'
    # yaml.representer.BaseRepresenter.represent_mapping sorts keys if the
    # object has .items(). So instead, pass the items directly.
    return dumper.represent_mapping(u'tag:yaml.org,2002:map', data.items())


SafeRepresenter.add_representer(AttrDict, to_yaml)
SafeRepresenter.add_multi_representer(AttrDict, to_yaml)

Representer.add_representer(AttrDict, to_yaml)
Representer.add_multi_representer(AttrDict, to_yaml)
Example #11
0
    def dict_dump(self):
        return unstitch(self)


def from_yaml(loader, node):
    data = Threads()
    yield data

    value = loader.construct_mapping(node)
    data.update(value)


def to_yaml(dumper, data):
    return dumper.represent_mapping(six.u('!threads.Threads'), data)


def to_yaml_safe(dumper, data):
    return dumper.represent_dict(data)


yaml.add_constructor(six.u('!threads'), from_yaml)
yaml.add_constructor(six.u('!threads.Threads'), from_yaml)

Representer.add_representer(Threads, to_yaml)
Representer.add_multi_representer(Threads, to_yaml)

SafeRepresenter.add_representer(Threads, to_yaml_safe)
SafeRepresenter.add_multi_representer(Threads, to_yaml_safe)

items = Threads(a=0, b=1, c=[0, 2], d=Threads(x=0, y=1, z=2))
print items.yaml_dump()