diff options
author | Anthon van der Neut <anthon@mnt.org> | 2018-08-03 22:14:57 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2018-08-03 22:14:57 +0200 |
commit | dce10fcff1de54121fb8b440b883ef5d3fe2f96a (patch) | |
tree | 072b4bd247e6f1cd95c08c7b67fea0fc96f0578e /representer.py | |
parent | 2966a4f215861fa05e0dc7e0cd53350766e794c6 (diff) | |
download | ruamel.yaml-dce10fcff1de54121fb8b440b883ef5d3fe2f96a.tar.gz |
Apply oitnb and mypy 0.620, then make everything work again0.15.48
Diffstat (limited to 'representer.py')
-rw-r--r-- | representer.py | 315 |
1 files changed, 147 insertions, 168 deletions
diff --git a/representer.py b/representer.py index 43f2c06..7b821fe 100644 --- a/representer.py +++ b/representer.py @@ -3,11 +3,14 @@ from __future__ import print_function, absolute_import, division -from ruamel.yaml.error import * # NOQA -from ruamel.yaml.nodes import * # NOQA +from ruamel.yaml.error import * # NOQA +from ruamel.yaml.nodes import * # NOQA from ruamel.yaml.compat import text_type, binary_type, to_unicode, PY2, PY3, ordereddict -from ruamel.yaml.scalarstring import (PreservedScalarString, SingleQuotedScalarString, - DoubleQuotedScalarString) +from ruamel.yaml.scalarstring import ( + PreservedScalarString, + SingleQuotedScalarString, + DoubleQuotedScalarString, +) from ruamel.yaml.scalarint import ScalarInt, BinaryInt, OctalInt, HexInt, HexCapsInt from ruamel.yaml.scalarfloat import ScalarFloat from ruamel.yaml.timestamp import TimeStamp @@ -15,6 +18,7 @@ from ruamel.yaml.timestamp import TimeStamp import datetime import sys import types + if PY3: import copyreg import base64 @@ -22,11 +26,12 @@ else: import copy_reg as copyreg # type: ignore if False: # MYPY - from typing import Dict, List, Any, Union, Text # NOQA - + from typing import Dict, List, Any, Union, Text # NOQA +# fmt: off __all__ = ['BaseRepresenter', 'SafeRepresenter', 'Representer', 'RepresenterError', 'RoundTripRepresenter'] +# fmt: on class RepresenterError(YAMLError): @@ -34,6 +39,7 @@ class RepresenterError(YAMLError): if PY2: + def get_classobj_bases(cls): # type: (Any) -> Any bases = [cls] @@ -44,7 +50,7 @@ if PY2: class BaseRepresenter(object): - yaml_representers = {} # type: Dict[Any, Any] + yaml_representers = {} # type: Dict[Any, Any] yaml_multi_representers = {} # type: Dict[Any, Any] def __init__(self, default_style=None, default_flow_style=None, dumper=None): @@ -63,8 +69,8 @@ class BaseRepresenter(object): # type: () -> Any try: if hasattr(self.dumper, 'typ'): - return self.dumper.serializer # type: ignore - return self.dumper._serializer # type: ignore + return self.dumper.serializer + return self.dumper._serializer except AttributeError: return self # cyaml @@ -95,8 +101,7 @@ class BaseRepresenter(object): if PY2: # if type(data) is types.InstanceType: if isinstance(data, types.InstanceType): - data_types = get_classobj_bases(data.__class__) + \ - list(data_types) + data_types = get_classobj_bases(data.__class__) + list(data_types) if data_types[0] in self.yaml_representers: node = self.yaml_representers[data_types[0]](self, data) else: @@ -206,8 +211,7 @@ class BaseRepresenter(object): node_value = self.represent_data(item_value) if not (isinstance(node_key, ScalarNode) and not node_key.style): best_style = False - if not (isinstance(node_value, ScalarNode) and not - node_value.style): + if not (isinstance(node_value, ScalarNode) and not node_value.style): best_style = False value.append((node_key, node_value)) if flow_style is None: @@ -223,7 +227,6 @@ class BaseRepresenter(object): class SafeRepresenter(BaseRepresenter): - def ignore_aliases(self, data): # type: (Any) -> bool # https://docs.python.org/3/reference/expressions.html#parenthesized-forms : @@ -240,6 +243,7 @@ class SafeRepresenter(BaseRepresenter): return self.represent_scalar(u'tag:yaml.org,2002:null', u'null') if PY3: + def represent_str(self, data): # type: (Any) -> Any return self.represent_scalar(u'tag:yaml.org,2002:str', data) @@ -250,9 +254,10 @@ class SafeRepresenter(BaseRepresenter): data = base64.encodebytes(data).decode('ascii') else: data = base64.encodestring(data).decode('ascii') - return self.represent_scalar(u'tag:yaml.org,2002:binary', data, - style='|') + return self.represent_scalar(u'tag:yaml.org,2002:binary', data, style='|') + else: + def represent_str(self, data): # type: (Any) -> Any tag = None @@ -277,7 +282,7 @@ class SafeRepresenter(BaseRepresenter): def represent_bool(self, data): # type: (Any) -> Any try: - value = self.dumper.boolean_representation[bool(data)] # type: ignore + value = self.dumper.boolean_representation[bool(data)] except AttributeError: if data: value = u'true' @@ -290,10 +295,10 @@ class SafeRepresenter(BaseRepresenter): return self.represent_scalar(u'tag:yaml.org,2002:int', text_type(data)) if PY2: + def represent_long(self, data): # type: (Any) -> Any - return self.represent_scalar(u'tag:yaml.org,2002:int', - text_type(data)) + return self.represent_scalar(u'tag:yaml.org,2002:int', text_type(data)) inf_value = 1e300 while repr(inf_value) != repr(inf_value * inf_value): @@ -330,12 +335,13 @@ class SafeRepresenter(BaseRepresenter): # pairs = False # break # if not pairs: - return self.represent_sequence(u'tag:yaml.org,2002:seq', data) - # value = [] - # for item_key, item_value in data: - # value.append(self.represent_mapping(u'tag:yaml.org,2002:map', - # [(item_key, item_value)])) - # return SequenceNode(u'tag:yaml.org,2002:pairs', value) + return self.represent_sequence(u'tag:yaml.org,2002:seq', data) + + # value = [] + # for item_key, item_value in data: + # value.append(self.represent_mapping(u'tag:yaml.org,2002:map', + # [(item_key, item_value)])) + # return SequenceNode(u'tag:yaml.org,2002:pairs', value) def represent_dict(self, data): # type: (Any) -> Any @@ -372,67 +378,54 @@ class SafeRepresenter(BaseRepresenter): def represent_undefined(self, data): # type: (Any) -> None - raise RepresenterError("cannot represent an object: %s" % data) + raise RepresenterError('cannot represent an object: %s' % data) -SafeRepresenter.add_representer(type(None), - SafeRepresenter.represent_none) +SafeRepresenter.add_representer(type(None), SafeRepresenter.represent_none) -SafeRepresenter.add_representer(str, - SafeRepresenter.represent_str) +SafeRepresenter.add_representer(str, SafeRepresenter.represent_str) if PY2: - SafeRepresenter.add_representer(unicode, - SafeRepresenter.represent_unicode) + SafeRepresenter.add_representer(unicode, SafeRepresenter.represent_unicode) else: - SafeRepresenter.add_representer(bytes, - SafeRepresenter.represent_binary) + SafeRepresenter.add_representer(bytes, SafeRepresenter.represent_binary) -SafeRepresenter.add_representer(bool, - SafeRepresenter.represent_bool) +SafeRepresenter.add_representer(bool, SafeRepresenter.represent_bool) -SafeRepresenter.add_representer(int, - SafeRepresenter.represent_int) +SafeRepresenter.add_representer(int, SafeRepresenter.represent_int) if PY2: - SafeRepresenter.add_representer(long, - SafeRepresenter.represent_long) + SafeRepresenter.add_representer(long, SafeRepresenter.represent_long) -SafeRepresenter.add_representer(float, - SafeRepresenter.represent_float) +SafeRepresenter.add_representer(float, SafeRepresenter.represent_float) -SafeRepresenter.add_representer(list, - SafeRepresenter.represent_list) +SafeRepresenter.add_representer(list, SafeRepresenter.represent_list) -SafeRepresenter.add_representer(tuple, - SafeRepresenter.represent_list) +SafeRepresenter.add_representer(tuple, SafeRepresenter.represent_list) -SafeRepresenter.add_representer(dict, - SafeRepresenter.represent_dict) +SafeRepresenter.add_representer(dict, SafeRepresenter.represent_dict) -SafeRepresenter.add_representer(set, - SafeRepresenter.represent_set) +SafeRepresenter.add_representer(set, SafeRepresenter.represent_set) -SafeRepresenter.add_representer(ordereddict, - SafeRepresenter.represent_ordereddict) +SafeRepresenter.add_representer(ordereddict, SafeRepresenter.represent_ordereddict) if sys.version_info >= (2, 7): import collections - SafeRepresenter.add_representer(collections.OrderedDict, - SafeRepresenter.represent_ordereddict) -SafeRepresenter.add_representer(datetime.date, - SafeRepresenter.represent_date) + SafeRepresenter.add_representer( + collections.OrderedDict, SafeRepresenter.represent_ordereddict + ) + +SafeRepresenter.add_representer(datetime.date, SafeRepresenter.represent_date) -SafeRepresenter.add_representer(datetime.datetime, - SafeRepresenter.represent_datetime) +SafeRepresenter.add_representer(datetime.datetime, SafeRepresenter.represent_datetime) -SafeRepresenter.add_representer(None, - SafeRepresenter.represent_undefined) +SafeRepresenter.add_representer(None, SafeRepresenter.represent_undefined) class Representer(SafeRepresenter): if PY2: + def represent_str(self, data): # type: (Any) -> Any tag = None @@ -486,15 +479,14 @@ class Representer(SafeRepresenter): def represent_name(self, data): # type: (Any) -> Any name = u'%s.%s' % (data.__module__, data.__name__) - return self.represent_scalar(u'tag:yaml.org,2002:python/name:' + - name, u'') + return self.represent_scalar(u'tag:yaml.org,2002:python/name:' + name, "") def represent_module(self, data): # type: (Any) -> Any - return self.represent_scalar( - u'tag:yaml.org,2002:python/module:' + data.__name__, u'') + return self.represent_scalar(u'tag:yaml.org,2002:python/module:' + data.__name__, "") if PY2: + def represent_instance(self, data): # type: (Any) -> Any # For instances of classic classes, we use __getinitargs__ and @@ -526,17 +518,19 @@ class Representer(SafeRepresenter): state = data.__dict__ if args is None and isinstance(state, dict): return self.represent_mapping( - u'tag:yaml.org,2002:python/object:' + class_name, state) + u'tag:yaml.org,2002:python/object:' + class_name, state + ) if isinstance(state, dict) and not state: return self.represent_sequence( - u'tag:yaml.org,2002:python/object/new:' + - class_name, args) + u'tag:yaml.org,2002:python/object/new:' + class_name, args + ) value = {} if bool(args): value['args'] = args value['state'] = state # type: ignore return self.represent_mapping( - u'tag:yaml.org,2002:python/object/new:' + class_name, value) + u'tag:yaml.org,2002:python/object/new:' + class_name, value + ) def represent_object(self, data): # type: (Any) -> Any @@ -564,7 +558,7 @@ class Representer(SafeRepresenter): elif hasattr(data, '__reduce__'): reduce = data.__reduce__() else: - raise RepresenterError("cannot represent object: %r" % data) + raise RepresenterError('cannot represent object: %r' % data) reduce = (list(reduce) + [None] * 5)[:5] function, args, state, listitems, dictitems = reduce args = list(args) @@ -583,12 +577,11 @@ class Representer(SafeRepresenter): tag = u'tag:yaml.org,2002:python/object/apply:' newobj = False function_name = u'%s.%s' % (function.__module__, function.__name__) - if not args and not listitems and not dictitems \ - and isinstance(state, dict) and newobj: + if not args and not listitems and not dictitems and isinstance(state, dict) and newobj: return self.represent_mapping( - u'tag:yaml.org,2002:python/object:' + function_name, state) - if not listitems and not dictitems \ - and isinstance(state, dict) and not state: + u'tag:yaml.org,2002:python/object:' + function_name, state + ) + if not listitems and not dictitems and isinstance(state, dict) and not state: return self.represent_sequence(tag + function_name, args) value = {} if args: @@ -603,47 +596,43 @@ class Representer(SafeRepresenter): if PY2: - Representer.add_representer(str, - Representer.represent_str) + Representer.add_representer(str, Representer.represent_str) - Representer.add_representer(unicode, - Representer.represent_unicode) + Representer.add_representer(unicode, Representer.represent_unicode) - Representer.add_representer(long, - Representer.represent_long) + Representer.add_representer(long, Representer.represent_long) -Representer.add_representer(complex, - Representer.represent_complex) +Representer.add_representer(complex, Representer.represent_complex) -Representer.add_representer(tuple, - Representer.represent_tuple) +Representer.add_representer(tuple, Representer.represent_tuple) -Representer.add_representer(type, - Representer.represent_name) +Representer.add_representer(type, Representer.represent_name) if PY2: - Representer.add_representer(types.ClassType, - Representer.represent_name) + Representer.add_representer(types.ClassType, Representer.represent_name) -Representer.add_representer(types.FunctionType, - Representer.represent_name) +Representer.add_representer(types.FunctionType, Representer.represent_name) -Representer.add_representer(types.BuiltinFunctionType, - Representer.represent_name) +Representer.add_representer(types.BuiltinFunctionType, Representer.represent_name) -Representer.add_representer(types.ModuleType, - Representer.represent_module) +Representer.add_representer(types.ModuleType, Representer.represent_module) if PY2: - Representer.add_multi_representer(types.InstanceType, - Representer.represent_instance) + Representer.add_multi_representer(types.InstanceType, Representer.represent_instance) -Representer.add_multi_representer(object, - Representer.represent_object) +Representer.add_multi_representer(object, Representer.represent_object) -from ruamel.yaml.comments import (CommentedMap, CommentedOrderedMap, CommentedSeq, - CommentedKeySeq, CommentedSet, comment_attrib, merge_attrib, TaggedScalar) # NOQA +from ruamel.yaml.comments import ( + CommentedMap, + CommentedOrderedMap, + CommentedSeq, + CommentedKeySeq, + CommentedSet, + comment_attrib, + merge_attrib, + TaggedScalar, +) # NOQA class RoundTripRepresenter(SafeRepresenter): @@ -654,16 +643,19 @@ class RoundTripRepresenter(SafeRepresenter): # type: (Any, Any, Any) -> None if not hasattr(dumper, 'typ') and default_flow_style is None: default_flow_style = False - SafeRepresenter.__init__(self, default_style=default_style, - default_flow_style=default_flow_style, - dumper=dumper) + SafeRepresenter.__init__( + self, + default_style=default_style, + default_flow_style=default_flow_style, + dumper=dumper, + ) def represent_none(self, data): # type: (Any) -> Any if len(self.represented_objects) == 0 and not self.serializer.use_explicit_start: # this will be open ended (although it is not yet) return self.represent_scalar(u'tag:yaml.org,2002:null', u'null') - return self.represent_scalar(u'tag:yaml.org,2002:null', u'') + return self.represent_scalar(u'tag:yaml.org,2002:null', "") def represent_preserved_scalarstring(self, data): # type: (Any) -> Any @@ -702,7 +694,7 @@ class RoundTripRepresenter(SafeRepresenter): while pos > 0: sl.insert(pos, '_') pos -= underscore[0] - s = ''.join(sl) + s = "".join(sl) if underscore[1]: s = '_' + s if underscore[2]: @@ -715,7 +707,7 @@ class RoundTripRepresenter(SafeRepresenter): s = '{:0{}d}'.format(data, data._width) else: s = format(data, 'd') - return self.insert_underscore('', s, data._underscore) + return self.insert_underscore("", s, data._underscore) def represent_binary_int(self, data): # type: (Any) -> Any @@ -767,17 +759,18 @@ class RoundTripRepresenter(SafeRepresenter): return self.represent_scalar(u'tag:yaml.org,2002:float', value) if data._exp is None and data._prec > 0 and data._prec == data._width - 1: # no exponent, but trailing dot - value = u'{}{:d}.'.format(data._m_sign if data._m_sign else u'', abs(int(data))) + value = u'{}{:d}.'.format(data._m_sign if data._m_sign else "", abs(int(data))) elif data._exp is None: # no exponent, "normal" dot prec = data._prec if prec < 1: prec = 1 # print('dw2', data._width, prec) - ms = data._m_sign if data._m_sign else u'' + ms = data._m_sign if data._m_sign else "" # -1 for the dot - value = u'{}{:0{}.{}f}'.format(ms, abs(data), data._width - len(ms), - data._width - prec - 1) + value = u'{}{:0{}.{}f}'.format( + ms, abs(data), data._width - len(ms), data._width - prec - 1 + ) while len(value) < data._width: value += u'0' else: @@ -795,32 +788,34 @@ class RoundTripRepresenter(SafeRepresenter): m2 += u'0' if data._m_sign and data > 0: m1 = '+' + m1 - esgn = u'+' if data._e_sign else u'' - if data._prec < 0: # mantissa without dot + esgn = u'+' if data._e_sign else "" + if data._prec < 0: # mantissa without dot if m2 != u'0': e -= len(m2) else: - m2 = u'' + m2 = "" while (len(m1) + len(m2) - (1 if data._m_sign else 0)) < data._width: m2 += u'0' e -= 1 value = m1 + m2 + data._exp + u'{:{}0{}d}'.format(e, esgn, data._e_width) - elif data._prec == 0: # mantissa with trailing dot + elif data._prec == 0: # mantissa with trailing dot e -= len(m2) - value = m1 + m2 + u'.' + data._exp + u'{:{}0{}d}'.format( - e, esgn, data._e_width) + value = ( + m1 + m2 + u'.' + data._exp + u'{:{}0{}d}'.format(e, esgn, data._e_width) + ) else: if data._m_lead0 > 0: m2 = u'0' * (data._m_lead0 - 1) + m1 + m2 m1 = u'0' - m2 = m2[:-data._m_lead0] # these should be zeros + m2 = m2[: -data._m_lead0] # these should be zeros e += data._m_lead0 while len(m1) < data._prec: m1 += m2[0] m2 = m2[1:] e -= 1 - value = m1 + u'.' + m2 + data._exp + u'{:{}0{}d}'.format( - e, esgn, data._e_width) + value = ( + m1 + u'.' + m2 + data._exp + u'{:{}0{}d}'.format(e, esgn, data._e_width) + ) if value is None: value = to_unicode(repr(data)).lower() @@ -881,8 +876,7 @@ class RoundTripRepresenter(SafeRepresenter): # type: (Any) -> Any if isinstance(data, CommentedKeySeq): self.alias_key = None - return self.represent_sequence(u'tag:yaml.org,2002:seq', data, - flow_style=True) + return self.represent_sequence(u'tag:yaml.org,2002:seq', data, flow_style=True) return SafeRepresenter.represent_key(self, data) def represent_mapping(self, tag, mapping, flow_style=None): @@ -938,8 +932,7 @@ class RoundTripRepresenter(SafeRepresenter): node_value.comment = item_comment[2:] if not (isinstance(node_key, ScalarNode) and not node_key.style): best_style = False - if not (isinstance(node_value, ScalarNode) and not - node_value.style): + if not (isinstance(node_value, ScalarNode) and not node_value.style): best_style = False value.append((node_key, node_value)) if flow_style is None: @@ -955,8 +948,7 @@ class RoundTripRepresenter(SafeRepresenter): else: arg = self.represent_data(merge_list) arg.flow_style = True - value.insert(0, - (ScalarNode(u'tag:yaml.org,2002:merge', '<<'), arg)) + value.insert(0, (ScalarNode(u'tag:yaml.org,2002:merge', '<<'), arg)) return node def represent_omap(self, tag, omap, flow_style=None): @@ -1059,11 +1051,10 @@ class RoundTripRepresenter(SafeRepresenter): if item_comment: assert getattr(node_key, 'comment', None) is None node_key.comment = item_comment[:2] - node_key.style = node_value.style = "?" + node_key.style = node_value.style = '?' if not (isinstance(node_key, ScalarNode) and not node_key.style): best_style = False - if not (isinstance(node_value, ScalarNode) and not - node_value.style): + if not (isinstance(node_value, ScalarNode) and not node_value.style): best_style = False value.append((node_key, node_value)) best_style = best_style @@ -1099,6 +1090,7 @@ class RoundTripRepresenter(SafeRepresenter): return self.represent_scalar(u'tag:yaml.org,2002:timestamp', to_unicode(value)) def represent_tagged_scalar(self, data): + # type: (Any) -> Any try: tag = data.tag.value except AttributeError: @@ -1106,64 +1098,51 @@ class RoundTripRepresenter(SafeRepresenter): return self.represent_scalar(tag, data.value, style=data.style) -RoundTripRepresenter.add_representer(type(None), - RoundTripRepresenter.represent_none) +RoundTripRepresenter.add_representer(type(None), RoundTripRepresenter.represent_none) RoundTripRepresenter.add_representer( - PreservedScalarString, - RoundTripRepresenter.represent_preserved_scalarstring) + PreservedScalarString, RoundTripRepresenter.represent_preserved_scalarstring +) RoundTripRepresenter.add_representer( - SingleQuotedScalarString, - RoundTripRepresenter.represent_single_quoted_scalarstring) + SingleQuotedScalarString, RoundTripRepresenter.represent_single_quoted_scalarstring +) RoundTripRepresenter.add_representer( - DoubleQuotedScalarString, - RoundTripRepresenter.represent_double_quoted_scalarstring) + DoubleQuotedScalarString, RoundTripRepresenter.represent_double_quoted_scalarstring +) -RoundTripRepresenter.add_representer( - ScalarInt, - RoundTripRepresenter.represent_scalar_int) +RoundTripRepresenter.add_representer(ScalarInt, RoundTripRepresenter.represent_scalar_int) -RoundTripRepresenter.add_representer( - BinaryInt, - RoundTripRepresenter.represent_binary_int) +RoundTripRepresenter.add_representer(BinaryInt, RoundTripRepresenter.represent_binary_int) -RoundTripRepresenter.add_representer( - OctalInt, - RoundTripRepresenter.represent_octal_int) +RoundTripRepresenter.add_representer(OctalInt, RoundTripRepresenter.represent_octal_int) -RoundTripRepresenter.add_representer( - HexInt, - RoundTripRepresenter.represent_hex_int) +RoundTripRepresenter.add_representer(HexInt, RoundTripRepresenter.represent_hex_int) -RoundTripRepresenter.add_representer( - HexCapsInt, - RoundTripRepresenter.represent_hex_caps_int) +RoundTripRepresenter.add_representer(HexCapsInt, RoundTripRepresenter.represent_hex_caps_int) -RoundTripRepresenter.add_representer( - ScalarFloat, - RoundTripRepresenter.represent_scalar_float) +RoundTripRepresenter.add_representer(ScalarFloat, RoundTripRepresenter.represent_scalar_float) -RoundTripRepresenter.add_representer(CommentedSeq, - RoundTripRepresenter.represent_list) +RoundTripRepresenter.add_representer(CommentedSeq, RoundTripRepresenter.represent_list) -RoundTripRepresenter.add_representer(CommentedMap, - RoundTripRepresenter.represent_dict) +RoundTripRepresenter.add_representer(CommentedMap, RoundTripRepresenter.represent_dict) -RoundTripRepresenter.add_representer(CommentedOrderedMap, - RoundTripRepresenter.represent_ordereddict) +RoundTripRepresenter.add_representer( + CommentedOrderedMap, RoundTripRepresenter.represent_ordereddict +) if sys.version_info >= (2, 7): import collections - RoundTripRepresenter.add_representer(collections.OrderedDict, - RoundTripRepresenter.represent_ordereddict) -RoundTripRepresenter.add_representer(CommentedSet, - RoundTripRepresenter.represent_set) + RoundTripRepresenter.add_representer( + collections.OrderedDict, RoundTripRepresenter.represent_ordereddict + ) + +RoundTripRepresenter.add_representer(CommentedSet, RoundTripRepresenter.represent_set) -RoundTripRepresenter.add_representer(TaggedScalar, - RoundTripRepresenter.represent_tagged_scalar) +RoundTripRepresenter.add_representer( + TaggedScalar, RoundTripRepresenter.represent_tagged_scalar +) -RoundTripRepresenter.add_representer(TimeStamp, - RoundTripRepresenter.represent_datetime) +RoundTripRepresenter.add_representer(TimeStamp, RoundTripRepresenter.represent_datetime) |