def ordered_dumper(dumper, data): """ Ensure that maps are always dumped in the items order. """ return dumper.represent_mapping(u'tag:yaml.org,2002:map', data.items()) SaneDumper.add_representer(OrderedDict, ordered_dumper) def null_dumper(dumper, value): """ Always dump nulls as empty string. """ return dumper.represent_scalar(u'tag:yaml.org,2002:null', u'') SafeDumper.add_representer(type(None), null_dumper) def string_dumper(dumper, value, _tag=u'tag:yaml.org,2002:str'): """ Ensure that all scalars are dumped as UTF-8 unicode, folded and quoted in the sanest and most readable way. """ if not isinstance(value, basestring): value = repr(value) if isinstance(value, str): value = value.decode('utf-8') style = None multilines = '\n' in value
from hepdata_lib.root_utils import RootFileReader MAPPING_TAG = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG def dict_representer(dumper, data): """represent dict.""" return dumper.represent_dict(data.items()) def dict_constructor(loader, node): """construct dict.""" return defaultdict(loader.construct_pairs(node)) yaml.add_representer(defaultdict, SafeRepresenter.represent_dict) Dumper.add_representer(defaultdict, dict_representer) Loader.add_constructor(MAPPING_TAG, dict_constructor) Dumper.add_representer(str, SafeRepresenter.represent_str) yaml.add_representer(np.str_, SafeRepresenter.represent_str) # Display deprecation warnings warnings.filterwarnings("always", category=DeprecationWarning, module="hepdata_lib") __version__ = "0.7.0" class AdditionalResourceMixin(object): """Functionality related to additional materials."""
try: from yaml import CSafeDumper as SafeDumper except ImportError: from yaml import SafeDumper class SafeDumper(SafeDumper): def represent_decimal(self, data): return self.represent_scalar('tag:yaml.org,2002:str', str(data)) def represent_ordered_dict(self, data): return self.represent_mapping('tag:yaml.org,2002:map', data.items()) SafeDumper.add_representer(Decimal, SafeDumper.represent_decimal) SafeDumper.add_representer(OrderedDict, SafeDumper.represent_ordered_dict) class PrettyYamlSerializer(Serializer): def end_serialization(self): objects = [ OrderedDict([ ( 'model', i['model'], ), ( 'pk', i['pk'], ),
Ensure that maps are always dumped in the items order. """ return dumper.represent_mapping(u'tag:yaml.org,2002:map', data.items()) SaneDumper.add_representer(OrderedDict, ordered_dumper) def null_dumper(dumper, value): """ Always dump nulls as empty string. """ return dumper.represent_scalar(u'tag:yaml.org,2002:null', u'') SafeDumper.add_representer(type(None), null_dumper) def string_dumper(dumper, value, _tag=u'tag:yaml.org,2002:str'): """ Ensure that all scalars are dumped as UTF-8 unicode, folded and quoted in the sanest and most readable way. """ if not isinstance(value, basestring): value = repr(value) if isinstance(value, str): value = value.decode('utf-8') style = None multilines = '\n' in value
def add_representer(klass: Any, representer: Any) -> None: """Add to representer to the dumper.""" FastestAvailableSafeDumper.add_representer(klass, representer)
def _represent_ordered_dict(self, data): """ Serialize OrderedDict to a yaml map :param data: [description] :type data: collections.OrderedDict """ return self.represent_mapping("tag:yaml.org,2002:map", data.items()) # Serializing Python `Path` objects to `str` # NOTE: Path("./aaa") serializes to "aaa" _Dumper.add_multi_representer(Path, _represent_path) _Dumper.add_representer(OrderedDict, _represent_ordered_dict) load = partial(_yaml.load, Loader=_Loader) def loads(s: str, *args, **kwargs) -> Dict: """ Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance containing a YAML document) to a Python dictionary. [extended_summary] :param s: [description] :type s: str :return: [description] :rtype: Dict """
# -*- coding:utf-8 -*- from __future__ import absolute_import, unicode_literals from types import GeneratorType try: from yaml import CSafeLoader as YamlLoader, CSafeDumper as YamlDumper except ImportError: from yaml import SafeLoader as YamlLoader, SafeDumper as YamlDumper from django.utils.six.moves import zip from yepes.contrib.datamigrations.serializers import Serializer from yepes.types import Undefined YamlDumper.add_representer(GeneratorType, YamlDumper.represent_list) class YamlSerializer(Serializer): def __init__(self, **serializer_parameters): defaults = { 'allow_unicode': True, 'encoding': None, 'explicit_end': False, 'explicit_start': False, 'width': 1000, } defaults.update(serializer_parameters) super(YamlSerializer, self).__init__(**defaults) def dump(self, headers, data, file):
return TemplateString(self.construct_scalar(node)) def _construct_yaml_tuple(self, node): # Used to convert sequences from lists to tuples. Only applies to lists # without any nested structures. seq = self.construct_sequence(node) if any(isinstance(e, (list, tuple, dict)) for e in seq): return seq return tuple(seq) def _represent_pipeline_config(self, data): return self.represent_dict(dict(data)) def dump(data, stream=None, Dumper=SafeDumper, **kwargs): """See :py:func:`yaml.dump`.""" return yaml.dump(data, stream=stream, Dumper=Dumper, **kwargs) def load(stream, Loader=SafeLoader): """See :py:func:`yaml.load`.""" return yaml.load(stream, Loader) SafeLoader.add_constructor('tag:yaml.org,2002:seq', _construct_yaml_tuple) SafeDumper.add_representer(phyre_engine.pipeline.PipelineConfig, _represent_pipeline_config)