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() }))
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)
"""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
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)
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)
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)
""" 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,
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)
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)
# Setup to use freezegun with pyyaml from freezegun.api import FakeDatetime from yaml.representer import SafeRepresenter SafeRepresenter.add_representer(FakeDatetime, SafeRepresenter.represent_datetime)
#!/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'))
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()
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)
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'
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)
return dumper.represent_dict(data) def to_yaml(dumper, data): """ 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)
# '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)
__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)