Example #1
0
 def __init__(self,
              stream,
              default_style=None,
              default_flow_style=None,
              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=False,
              **kwargs):
     IndentingEmitter.__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)
     SafeRepresenter.__init__(self,
                              default_style=default_style,
                              default_flow_style=default_flow_style)
     Resolver.__init__(self)
    def __write_state_file(self, address, data_center, environment):
        host = parse_hostname(address)
        state_path = os.path.join(STATE_PATH, self.team, self.project, host)
        if not os.path.exists(state_path):
            os.makedirs(state_path)

        state_file = os.path.join(state_path, "{0}.yml".format(os.getpid()))
        if os.path.isfile(state_file):
            raise RuntimeError("Unable to run freight forwarder pid already in use.")
        else:
            # TODO: move to a yaml util
            def config_unicode_presenter(dumper, data):
                return dumper.represent_scalar('tag:yaml.org,2002:str', data)

            SafeRepresenter.add_representer(ConfigUnicode, config_unicode_presenter)

            with open(state_file, 'w') as f:
                f.write(
                    yaml.safe_dump(
                        {
                            "team": self.team,
                            "project": self.project,
                            "environment": environment,
                            "data_center": data_center,
                            "host": host,
                            "pid": os.getpid()
                        }
                    )
                )
    def __write_state_file(self, address, data_center, environment):
        host = parse_hostname(address)
        state_path = os.path.join(STATE_PATH, self.team, self.project, host)
        if not os.path.exists(state_path):
            os.makedirs(state_path)

        state_file = os.path.join(state_path, "{0}.yml".format(os.getpid()))
        if os.path.isfile(state_file):
            raise RuntimeError(
                "Unable to run freight forwarder pid already in use.")
        else:
            # TODO: move to a yaml util
            def config_unicode_presenter(dumper, data):
                return dumper.represent_scalar('tag:yaml.org,2002:str', data)

            SafeRepresenter.add_representer(ConfigUnicode,
                                            config_unicode_presenter)

            with open(state_file, 'w') as f:
                f.write(
                    yaml.safe_dump({
                        "team": self.team,
                        "project": self.project,
                        "environment": environment,
                        "data_center": data_center,
                        "host": host,
                        "pid": os.getpid()
                    }))
Example #4
0
 def _represent_str(cls, dumper, orig_data):
     try:
         data = unicode(orig_data, 'ascii')
         if data.count('\n') == 0:
             return SafeRepresenter.represent_str(dumper, orig_data)
     except UnicodeDecodeError:
         try:
             data = unicode(orig_data, 'utf-8')
             if data.count('\n') == 0:
                 return SafeRepresenter.represent_str(dumper, orig_data)
         except UnicodeDecodeError:
             return SafeRepresenter.represent_str(dumper, orig_data)
     return dumper.represent_scalar(u'tag:yaml.org,2002:str',
                                    data, style='|')
Example #5
0
 def represent_data(self, data):
     """
     This is very hackish. There is for sure a better way either by using
     the add_multi_representer or add_representer, the issue though lies in
     the fact that Scapy packets are metaclasses that leads to
     yaml.representer.get_classobj_bases to not be able to properly get the
     base of class of a Scapy packet.
     XXX fully debug this problem
     """
     if isinstance(data, Packet):
         data = createPacketReport(data)
     return SafeRepresenter.represent_data(self, data)
Example #6
0
 def represent_data(self, data):
     """
     This is very hackish. There is for sure a better way either by using
     the add_multi_representer or add_representer, the issue though lies in
     the fact that Scapy packets are metaclasses that leads to
     yaml.representer.get_classobj_bases to not be able to properly get the
     base of class of a Scapy packet.
     XXX fully debug this problem
     """
     if isinstance(data, Packet):
         data = createPacketReport(data)
     return SafeRepresenter.represent_data(self, data)
Example #7
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'
Example #8
0
    # 'explicit_start' : True,
}

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)
Example #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)
Example #10
0
# Setup to use freezegun with pyyaml
from freezegun.api import FakeDatetime
from yaml.representer import SafeRepresenter
SafeRepresenter.add_representer(FakeDatetime,
                                SafeRepresenter.represent_datetime)
Example #11
0
        self.x_attr_dict = kwargs.pop('x_attr_dict', {})
        super().__init__(*args, **kwargs)

    def ignore_aliases(self, data):
        if isinstance(data, int): # py2 vs future int
            return True

class XLoader(yaml.Loader):
    def __init__(self, *args, **kwargs):
        self.x_attr_dict = kwargs.pop('x_attr_dict', {})
        super().__init__(*args, **kwargs)

if PY2:
    XDumper.add_representer(
        int,
        lambda dumper, data: SafeRepresenter.represent_int(
            dumper, native(data)))
    XDumper.add_representer(
        dict,
        lambda dumper, data: SafeRepresenter.represent_dict(
            dumper, native(data)))

for kl in [DolfinMeshTag, DolfinFunctionTag,
           DolfinMeshFunctionSizetTag,
           DolfinMeshFunctionIntTag,
           DolfinMeshFunctionDoubleTag,
           DolfinMeshFunctionBoolTag,
           FrozenSetTag, PintQTag]:
    kl.register_in_dumper(XDumper)
    kl.register_in_loader(XLoader)

def _load(stream, Loader=None, x_attr_dict=None,
Example #12
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 #13
0
#!/usr/bin/env python3

import sys
import yaml
import datetime

import fvm
import fvol

from collections import defaultdict
from yaml.representer import SafeRepresenter

# to serialize defaultdicts normally
SafeRepresenter.add_representer(defaultdict, SafeRepresenter.represent_dict)

arguments = yaml.safe_load(sys.stdin)

vm_ids = list(arguments.get('instances', {}).keys())

def multidict():
    return defaultdict(multidict)

vm_infos = {}
for vmid in vm_ids:
    vm = fvm.load(vmid)
    if vm:
        status = {
            'flags': {
                'active': True,
                'converging': False,
                'failed': bool(vm.model.get('failure'))
Example #14
0
def timestamp_representer(dumper, data):
    return SafeRepresenter.represent_datetime(dumper, data.to_pydatetime())
Example #15
0
            >>> 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)

    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)
Example #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)

Example #17
0
File: tools.py Project: rnc/cekit
LOGGER = logging.getLogger('cekit')


class Map(dict):
    """
    Class to enable access via properties to dictionaries.
    """

    __getattr__ = dict.get
    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__


# Make sure YAML can understand how to represent the Map object
SafeRepresenter.add_representer(Map, SafeRepresenter.represent_dict)


def load_descriptor(descriptor):
    """ parses descriptor and validate it against requested schema type

    Args:
      descriptor - yaml descriptor or path to a descriptor to be loaded.
      If a path is provided it must be an absolute path. In other case it's
      assumed that it is a yaml descriptor.

    Returns descriptor as a dictionary
    """

    try:
        data = yaml.safe_load(descriptor)
Example #18
0
def bytes_representer(dumper: SafeRepresenter, data: bytes) -> ScalarNode:
    return dumper.represent_str(data.decode("UTF-8"))
Example #19
0
"""
A docker composition.

"""
from collections import OrderedDict
from yaml import dump, load
from yaml.representer import SafeRepresenter
try:
    from yaml import CSafeDumper as SafeDumper, CSafeLoader as SafeLoader
except ImportError:
    from yaml import SafeDumper, SafeLoader
finally:
    # Register OrderedDict to dump like a regular dict
    SafeRepresenter.add_representer(OrderedDict,
                                    SafeRepresenter.represent_dict)

from docker_etude.models.base import Model
from docker_etude.models.network import Network
from docker_etude.models.service import Service
from docker_etude.models.volume import Volume


class Composition(Model):
    """
    The top-level model for a docker composition.

    Maps to/from a docker-compose.yml file.

    """
    def __init__(self,
                 networks=None,
Example #20
0
 def _represent_unicode(cls, dumper, data):
     if data.count('\n') == 0:
         return SafeRepresenter.represent_unicode(dumper, data)
     return dumper.represent_scalar(u'tag:yaml.org,2002:str',
                                    data, style='|')
Example #21
0
        return dumper.represent_dict(data)

    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)
Example #22
0
import testtools
import fixtures

from acceptable import __main__ as main
from acceptable import get_metadata
from acceptable.tests._fixtures import (
    CleanUpModuleImport,
    TemporaryModuleFixture,
)

if PY2:
    # teach yaml about future's newlist and newdict py3 backports
    from future.types.newlist import newlist
    from future.types.newdict import newdict
    SafeRepresenter.add_representer(newlist, SafeRepresenter.represent_list)
    SafeRepresenter.add_representer(newdict, SafeRepresenter.represent_dict)


# sys.exit on error, but rather throws an exception, so we can catch that in
# our tests:
class SaneArgumentParser(argparse.ArgumentParser):
    def error(self, message):
        raise RuntimeError(message)


class ParseArgsTests(testtools.TestCase):
    def test_error_with_no_args(self):
        self.assertRaisesRegex(
            RuntimeError,
            'arguments are required' if not PY2 else 'too few arguments',
            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 #24
0
"""Parse header of text notebooks
"""

import re
import yaml
from yaml.representer import SafeRepresenter
import nbformat
from nbformat.v4.nbbase import new_raw_cell
from .version import __version__
from .languages import _SCRIPT_EXTENSIONS, comment_lines
from .metadata_filter import filter_metadata, _DEFAULT_NOTEBOOK_METADATA
from .pep8 import pep8_lines_between_cells

SafeRepresenter.add_representer(nbformat.NotebookNode,
                                SafeRepresenter.represent_dict)

_HEADER_RE = re.compile(r"^---\s*$")
_BLANK_RE = re.compile(r"^\s*$")
_JUPYTER_RE = re.compile(r"^jupyter\s*:\s*$")
_LEFTSPACE_RE = re.compile(r"^\s")
_UTF8_HEADER = " -*- coding: utf-8 -*-"

# Change this to False in tests
INSERT_AND_CHECK_VERSION_NUMBER = True


def insert_or_test_version_number():
    """Should the format name and version number be inserted in text
    representations (not in tests!)"""
    return INSERT_AND_CHECK_VERSION_NUMBER
Example #25
0
 def represent_literal_str(self, data):
     node = SafeRepresenter.represent_str(self, data)
     if '\n' in data:
         node.style = '|'
     return node
Example #26
0
            res[k] = combine_dict(*v, cls=cls)
    return res


class attrdict(dict):
    """A dictionary which can be accessed via attributes, for convenience"""
    def __getattr__(self, a):
        if a.startswith("_"):
            return object.__getattr__(self, a)
        try:
            return self[a]
        except KeyError:
            raise AttributeError(a) from None

    def __setattr__(self, a, b):
        if a.startswith("_"):
            super(attrdict, self).__setattr__(a, b)
        else:
            self[a] = b

    def __delattr__(self, a):
        try:
            del self[a]
        except KeyError:
            raise AttributeError(a) from None


from yaml.representer import SafeRepresenter

SafeRepresenter.add_representer(attrdict, SafeRepresenter.represent_dict)
Example #27
0
def _represent_hasstr(obj, value: HasStr):
    from yaml.representer import SafeRepresenter

    return SafeRepresenter.represent_str(obj, str(value))
Example #28
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()
Example #29
0
def _represent_enum(obj, value: Enum):
    from yaml.representer import SafeRepresenter

    return SafeRepresenter.represent_str(obj, value.value)
Example #30
0
        return dumper.represent_dict(data)

    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)
Example #31
0
def _represent_uuid(obj, value: UUID):
    from yaml.representer import SafeRepresenter

    return SafeRepresenter.represent_str(obj, str(value))
Example #32
0
        return dumper.represent_dict(data)

    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 #33
0
def watermark_representer(dumper: SafeRepresenter,
                          data: Watermark) -> SequenceNode:
    return dumper.represent_list(list(data))
Example #34
0
__author__ = "Yuan Chang"
__copyright__ = "Copyright (C) 2016-2021"
__license__ = "AGPL"
__email__ = "*****@*****.**"

from re import sub
from numpy import float64
from yaml import safe_load, safe_dump
from yaml.error import YAMLError
from yaml.representer import SafeRepresenter
from qtpy.QtWidgets import QMessageBox
from .format_editor import FormatEditor, ProjectFormat

# Add a patch for numpy numbers
SafeRepresenter.add_representer(float64, SafeRepresenter.represent_float)


class YamlEditor(FormatEditor):
    """YAML reader and writer."""

    def __init__(self, *args):
        super(YamlEditor, self).__init__(*args)

    @staticmethod
    def test(file_name: str) -> bool:
        """Test the file is valid."""
        with open(file_name, 'r', encoding='utf-8') as f:
            try:
                yaml_script = f.read()
                safe_load(yaml_script)
Example #35
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()