Пример #1
0
 def __init__(self, stream, filename):
     Reader.__init__(self, stream)
     Scanner.__init__(self)
     if cyaml:
         Parser.__init__(self, stream)
     else:
         Parser.__init__(self)
     Composer.__init__(self)
     SafeConstructor.__init__(self)
     Representer.__init__(self)
     Resolver.__init__(self)
     NodeConstructor.__init__(self, filename)
Пример #2
0
 def __init__(self, stream,
         default_style=None, default_flow_style=False,
         canonical=None, indent=None, width=None,
         allow_unicode=None, line_break=None,
         encoding=None, explicit_start=None, explicit_end=None,
         version=None, tags=None):
     Emitter.__init__(self, stream, canonical=canonical,
             indent=indent, width=width,
             allow_unicode=allow_unicode, line_break=line_break)
     Serializer.__init__(self, encoding=encoding,
             explicit_start=explicit_start, explicit_end=explicit_end,
             version=version, tags=tags)
     Representer.__init__(self, default_style=default_style,
             default_flow_style=default_flow_style)
     Resolver.__init__(self)
Пример #3
0
 def __init__(self, stream,
              default_style=None, default_flow_style=False,
              canonical=None, indent=None, width=None,
              allow_unicode=None, line_break=None,
              encoding=None, explicit_start=None, explicit_end=None,
              version=None, tags=None, sort_keys=True):
     tags = tags or {}
     tags.update(UNITY_TAG)
     version = version or VERSION
     Emitter.__init__(self, stream, canonical=canonical,
                      indent=indent, width=width,
                      allow_unicode=allow_unicode, line_break=line_break)
     Serializer.__init__(self, encoding=encoding,
                         explicit_start=explicit_start, explicit_end=explicit_end,
                         version=version, tags=tags)
     Representer.__init__(self, default_style=default_style,
                          default_flow_style=default_flow_style, sort_keys=sort_keys)
     Resolver.__init__(self)
Пример #4
0
        """ 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'
            >>> yaml.dump(b, default_flow_style=True)
            '!munch.Munch {foo: [bar, !munch.Munch {lol: true}], hello: 42}\\n'
Пример #5
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()
Пример #6
0
         
         >>> 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'
         >>> yaml.dump(b, default_flow_style=True)
Пример #7
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)
Пример #8
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)
# PPDumper.add_representer(types.ModuleType, Dumper.represent_module)
# PPDumper.add_multi_representer(types.InstanceType, Dumper.represent_instance)
Пример #9
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)
Пример #10
0
import yaml

from insights.core.evaluators import SingleEvaluator
from insights.formats import EvaluatorFormatterAdapter
from yaml.representer import Representer
from insights.core import ScanMeta

Representer.add_representer(ScanMeta, Representer.represent_name)


class YamlFormat(SingleEvaluator):
    def postprocess(self):
        yaml.dump(self.get_response(), self.stream)


class YamlFormatterAdapter(EvaluatorFormatterAdapter):
    Impl = YamlFormat
Пример #11
0
                        if isinstance(item, dict):
                            d[k][i] = AttrDict(item)
                else:
                    d[k] = v

        for arg in args:
            if hasattr(arg, 'items'):
                combine(self, arg)
        combine(self, kwargs)

Options = AttrDict

# Convince yaml that AttrDict is actually a dict.
try:
    from yaml.representer import Representer
    Representer.add_representer(AttrDict, Representer.represent_dict)
    Representer.add_representer(Options, Representer.represent_dict)
except ImportError:
    pass


class Aliasificator(type):
    """Adds shortcut functions at the module level for easier access to simple
    macros.

    class BrowseTo(Command):
        def __init__(self, arg='nothing'):
            self.arg = arg

        def setup(self):
            print self.arg
Пример #12
0
        """ 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'
            >>> yaml.dump(b, default_flow_style=True)
            '!bunch.OrderedBunch {foo: [bar, !bunch.OrderedBunch {lol: true}], hello: 42}\\n'
Пример #13
0
                            version=version, tags=tags)
        Representer.__init__(self, default_style=default_style,
                             default_flow_style=default_flow_style, sort_keys=sort_keys)
        Resolver.__init__(self)


def represent_unity_class(dumper, instance):
    data = {instance.__class__.__name__: instance.get_serialized_properties_dict()}
    node = dumper.represent_mapping('{}{}'.format(UNITY_TAG_URI, instance.__class_id), data, False)
    # UNITY: set MappingNode anchor and all set all it's descendants anchors to None
    dumper.anchors[node] = instance.anchor
    dumper.extra_anchor_data[instance.anchor] = instance.extra_anchor_data
    for key, value in node.value:
        dumper.anchor_node(key)
        dumper.anchor_node(value)
    return node


def represent_ordered_flow_dict(dumper, instance):
    return dumper.represent_mapping(Resolver.DEFAULT_MAPPING_TAG, instance.items(),
                                    flow_style=instance.get_flow_style())


def represent_none(dumper, instance):
    return dumper.represent_scalar('tag:yaml.org,2002:null', '')


Representer.add_multi_representer(UnityClass, represent_unity_class)
Representer.add_representer(OrderedFlowDict, represent_ordered_flow_dict)
Representer.add_representer(type(None), represent_none)
Пример #14
0
    def __init__(self, *args, **kwargs):
        dict.__init__(self, *args, **kwargs)


class RawList(list):
    def __init__(self, *args, **kwargs):
        list.__init__(self, *args, **kwargs)


class RawString(UserString):
    pass


# Pyyaml hacks to make it work
# Render custom dicts as normal Yaml
Representer.add_representer(RawDict, SafeRepresenter.represent_dict)
Representer.add_representer(RawList, SafeRepresenter.represent_list)
Representer.add_representer(RawString, SafeRepresenter.represent_str)
# Don't render yaml pointers.
yaml.Dumper.ignore_aliases = lambda *args: True


def is_raw(item):
    return isinstance(item, RawDict) or isinstance(
        item, RawList) or isinstance(item, RawString)


def raw(item):
    if is_list(item):
        return RawList(item)
    if is_dict(item):
Пример #15
0
from abc import ABCMeta
from collections import OrderedDict

import yaml
from yaml.representer import Representer

# Support for abstract classes
Representer.add_representer(ABCMeta, Representer.represent_name)


class Config:
    '''
    Configuration object used for initializing an Agent.
    It maintains the order from which the attributes have been set.

    Parameters
    ----------
    configs: Keyword arguments
        Additional parameters that will be stored.

    Returns
    -------
    Config object
        An object containing all configuration details (with possibly nested Config).
    '''
    def __init__(self, *args, **kwargs):
        # We want to maintain the order of the attributes,
        # this is especially necessary when defining NNs architectures
        self.__dict__['_attrs'] = OrderedDict()

        for i, value in enumerate(args):
Пример #16
0
    best_style = True

    if self.alias_key is not None:
        self.represented_objects[self.alias_key] = node

    if hasattr(mapping, 'items'):
        mapping = list(mapping.items())

    for item_key, item_value in mapping:
        node_key = self.represent_data(item_key)
        node_value = self.represent_data(item_value)
        if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
            best_style = False
        if not (isinstance(node_value, yaml.ScalarNode)
                and not node_value.style):
            best_style = False
        value.append((node_key, node_value))
    if flow_style is None:
        if self.default_flow_style is not None:
            node.flow_style = self.default_flow_style
        else:
            node.flow_style = best_style

    return node


BaseRepresenter.represent_mapping = represent_ordered_mapping
SafeRepresenter.add_representer(OrderedDict, SafeRepresenter.represent_dict)
Representer.add_representer(OrderedDict, SafeRepresenter.represent_dict)
yaml.add_representer(OrderedDict, SafeRepresenter.represent_dict)
Пример #17
0
class FloatNode(float, Node):
    pass


class ListNode(list, Node):
    pass


class DictNode(OrderedDict, Node):
    pass


from yaml.representer import Representer
from yaml.constructor import Constructor
yaml_representer = Representer()
yaml_constructor = Constructor()


def yaml_to_node(yaml_obj):
    if isinstance(yaml_obj, yaml.MappingNode):
        v = [(yaml_to_node(k), yaml_to_node(v)) for k, v in yaml_obj.value]
        node = DictNode(v)
    elif isinstance(yaml_obj, yaml.SequenceNode):
        v = [yaml_to_node(v) for v in yaml_obj.value]
        node = ListNode(v)
    elif isinstance(yaml_obj, yaml.ScalarNode):
        # 'tag:yaml.org,2002:bool'
        type = yaml_obj.tag.split(':')[2]
        type = type[0].upper() + type[1:] + 'Node'
        v = yaml_obj.value
Пример #18
0
        """ Converts Bunch to a representation node.

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

    yaml.add_constructor('!bunch', from_yaml)
    yaml.add_constructor('!bunch.Bunch', from_yaml)

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

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

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

            >>> b = Bunch(foo=['bar', Bunch(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'
            >>> yaml.dump(b, default_flow_style=True)
            '!bunch.Bunch {foo: [bar, !bunch.Bunch {lol: true}], hello: 42}\\n'
Пример #19
0
    best_style = True
    
    if self.alias_key is not None:
        self.represented_objects[self.alias_key] = node
    
    if hasattr(mapping, 'items'):
        mapping = list(mapping.items())
    
    for item_key, item_value in mapping:
        node_key = self.represent_data(item_key)
        node_value = self.represent_data(item_value)
        if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
            best_style = False
        if not (isinstance(node_value, yaml.ScalarNode) and not node_value.style):
            best_style = False
        value.append((node_key, node_value))
    if flow_style is None:
        if self.default_flow_style is not None:
            node.flow_style = self.default_flow_style
        else:
            node.flow_style = best_style
    
    return node


BaseRepresenter.represent_mapping = represent_ordered_mapping
SafeRepresenter.add_representer(OrderedDict, SafeRepresenter.represent_dict)
Representer.add_representer(OrderedDict, SafeRepresenter.represent_dict)
yaml.add_representer(OrderedDict, SafeRepresenter.represent_dict)

Пример #20
0
        """ 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'
            >>> yaml.dump(b, default_flow_style=True)
            '!chunk.Chunk {foo: [bar, !chunk.Chunk {lol: true}], hello: 42}\\n'
Пример #21
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()