From 2ea4de43fe2e0160e27b605f2c65f33c5a2083d7 Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Wed, 12 Jul 2017 12:13:34 +0200 Subject: update to flake8==3.3.0 with --ignore F405 --- .appveyor.yaml | 5 ++- __init__.py | 41 +++++++++++------------ _example/transform.py | 1 + _test/lib/canonical.py | 25 +++++++++----- _test/lib/test_all.py | 1 + _test/lib/test_appliance.py | 12 +++---- _test/lib/test_canonical.py | 3 ++ _test/lib/test_constructor.py | 11 ++++--- _test/lib/test_emitter.py | 9 +++-- _test/lib/test_errors.py | 5 +++ _test/lib/test_input_output.py | 32 ++++++++++-------- _test/lib/test_mark.py | 3 +- _test/lib/test_reader.py | 3 +- _test/lib/test_recursive.py | 1 + _test/lib/test_representer.py | 1 + _test/lib/test_resolver.py | 3 ++ _test/lib/test_structure.py | 5 +++ _test/lib/test_tokens.py | 2 ++ _test/lib/test_yaml_ext.py | 19 ++++++++++- _test/roundtrip.py | 2 +- _test/test_comments.py | 1 + _test/test_literal.py | 2 +- _test/test_tag.py | 1 + comments.py | 12 +++---- compat.py | 9 ++--- constructor.py | 58 ++++++++++++++++---------------- emitter.py | 75 +++++++++++++++++++++--------------------- error.py | 19 ++++++----- main.py | 3 +- nodes.py | 8 +++-- parser.py | 6 ++-- reader.py | 16 ++++----- representer.py | 19 ++++++----- resolver.py | 3 +- scanner.py | 68 ++++++++++++++++++-------------------- util.py | 2 +- 36 files changed, 278 insertions(+), 208 deletions(-) diff --git a/.appveyor.yaml b/.appveyor.yaml index d64dd1d..8968ef6 100644 --- a/.appveyor.yaml +++ b/.appveyor.yaml @@ -14,7 +14,10 @@ environment: install: - | %PYTHON%\python.exe -m pip install --upgrade pip - %PYTHON%\python.exe -m pip install wheel==0.24 + %PYTHON%\python.exe -m pip install wheel + %PYTHON%\python.exe -m pip list + +# %PYTHON%\python.exe -m pip install wheel==0.24 build: off diff --git a/__init__.py b/__init__.py index a40d008..30fe00a 100644 --- a/__init__.py +++ b/__init__.py @@ -7,39 +7,38 @@ if False: # MYPY _package_data = dict( full_package_name='ruamel.yaml', - version_info=(0, 15, 18), - __version__='0.15.18', + version_info=(0, 15, 19, 'dev'), + __version__='0.15.19.dev', author='Anthon van der Neut', author_email='a.van.der.neut@ruamel.eu', description='ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order', # NOQA entry_points=None, since=2014, - extras_require={':platform_python_implementation=="CPython" and python_version<="2.7"': [ - 'ruamel.ordereddict', - ], 'jinja2': ['ruamel.yaml.jinja2>=0.2']}, + extras_require={ + ':platform_python_implementation=="CPython" and python_version<="2.7"': + ['ruamel.ordereddict'], + 'jinja2': ['ruamel.yaml.jinja2>=0.2'], + }, ext_modules=[dict( - name='_ruamel_yaml', + name='_ruamel_yaml', src=['ext/_ruamel_yaml.c', 'ext/api.c', 'ext/writer.c', 'ext/dumper.c', - 'ext/loader.c', - 'ext/reader.c', - 'ext/scanner.c', - 'ext/parser.c', - 'ext/emitter.c', + 'ext/loader.c', 'ext/reader.c', 'ext/scanner.c', 'ext/parser.c', + 'ext/emitter.c', ], lib=[], test='#include "ext/yaml.h"\n\nint main(int argc, char* argv[])\n{\nyaml_parser_t parser;\nparser = parser; /* prevent warning */\nreturn 0;\n}\n', # NOQA )], classifiers=[ - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3.3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: Implementation :: CPython', - 'Programming Language :: Python :: Implementation :: PyPy', - 'Programming Language :: Python :: Implementation :: Jython', - 'Topic :: Software Development :: Libraries :: Python Modules', - 'Topic :: Text Processing :: Markup', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: Implementation :: CPython', + 'Programming Language :: Python :: Implementation :: PyPy', + 'Programming Language :: Python :: Implementation :: Jython', + 'Topic :: Software Development :: Libraries :: Python Modules', + 'Topic :: Text Processing :: Markup', ], keywords='yaml 1.2 parser round-trip preserve quotes order config', windows_wheels=True, diff --git a/_example/transform.py b/_example/transform.py index c88b0c9..9ec243e 100644 --- a/_example/transform.py +++ b/_example/transform.py @@ -34,6 +34,7 @@ def sequence_indent_four(s): ret_val += ' ' * len(levels) + line return ret_val + yaml = YAML() yaml.explicit_start = True yaml.dump(data, sys.stdout, transform=sequence_indent_four) diff --git a/_test/lib/canonical.py b/_test/lib/canonical.py index 39dc87a..64f3153 100644 --- a/_test/lib/canonical.py +++ b/_test/lib/canonical.py @@ -18,7 +18,7 @@ class CanonicalScanner: if isinstance(data, bytes): data = data.decode('utf-8') else: - data = unicode(data, 'utf-8') + data = unicode(data, 'utf-8') # NOQA except UnicodeDecodeError: raise CanonicalError("utf-8 stream is expected") self.data = data + u'\0' @@ -48,7 +48,7 @@ class CanonicalScanner: self.scan() token = self.tokens.pop(0) if choice and not isinstance(token, choice): - raise CanonicalError("unexpected token "+repr(token)) + raise CanonicalError("unexpected token " + repr(token)) return token def get_token_value(self): @@ -65,7 +65,7 @@ class CanonicalScanner: break elif ch == u'%': self.tokens.append(self.scan_directive()) - elif ch == u'-' and self.data[self.index:self.index+3] == u'---': + elif ch == u'-' and self.data[self.index:self.index + 3] == u'---': self.index += 3 self.tokens.append(ruamel.yaml.DocumentStartToken(None, None)) elif ch == u'[': @@ -102,8 +102,8 @@ class CanonicalScanner: DIRECTIVE = u'%YAML 1.1' def scan_directive(self): - if self.data[self.index:self.index+len(self.DIRECTIVE)] == self.DIRECTIVE and \ - self.data[self.index+len(self.DIRECTIVE)] in u' \n\0': + if self.data[self.index:self.index + len(self.DIRECTIVE)] == self.DIRECTIVE and \ + self.data[self.index + len(self.DIRECTIVE)] in u' \n\0': self.index += len(self.DIRECTIVE) return ruamel.yaml.DirectiveToken('YAML', (1, 1), None, None) else: @@ -130,11 +130,11 @@ class CanonicalScanner: if not value: value = u'!' elif value[0] == u'!': - value = 'tag:yaml.org,2002:'+value[1:] + value = 'tag:yaml.org,2002:' + value[1:] elif value[0] == u'<' and value[-1] == u'>': value = value[1:-1] else: - value = u'!'+value + value = u'!' + value return ruamel.yaml.TagToken(value, None, None) QUOTE_CODES = { @@ -179,7 +179,7 @@ class CanonicalScanner: ignore_spaces = True elif ch in self.QUOTE_CODES: length = self.QUOTE_CODES[ch] - code = int(self.data[self.index:self.index+length], 16) + code = int(self.data[self.index:self.index + length], 16) chunks.append(unichr(code)) self.index += length else: @@ -233,7 +233,7 @@ class CanonicalParser: if self.check_token(ruamel.yaml.DirectiveToken, ruamel.yaml.DocumentStartToken): self.parse_document() else: - raise CanonicalError("document is expected, got "+repr(self.tokens[0])) + raise CanonicalError("document is expected, got " + repr(self.tokens[0])) self.get_token(ruamel.yaml.StreamEndToken) self.events.append(ruamel.yaml.StreamEndEvent(None, None)) @@ -340,40 +340,47 @@ class CanonicalLoader(CanonicalScanner, CanonicalParser, Constructor.__init__(self) Resolver.__init__(self) + ruamel.yaml.CanonicalLoader = CanonicalLoader def canonical_scan(stream): return ruamel.yaml.scan(stream, Loader=CanonicalLoader) + ruamel.yaml.canonical_scan = canonical_scan def canonical_parse(stream): return ruamel.yaml.parse(stream, Loader=CanonicalLoader) + ruamel.yaml.canonical_parse = canonical_parse def canonical_compose(stream): return ruamel.yaml.compose(stream, Loader=CanonicalLoader) + ruamel.yaml.canonical_compose = canonical_compose def canonical_compose_all(stream): return ruamel.yaml.compose_all(stream, Loader=CanonicalLoader) + ruamel.yaml.canonical_compose_all = canonical_compose_all def canonical_load(stream): return ruamel.yaml.load(stream, Loader=CanonicalLoader) + ruamel.yaml.canonical_load = canonical_load def canonical_load_all(stream): return ruamel.yaml.load_all(stream, Loader=CanonicalLoader) + ruamel.yaml.canonical_load_all = canonical_load_all diff --git a/_test/lib/test_all.py b/_test/lib/test_all.py index 57485fc..9e951eb 100644 --- a/_test/lib/test_all.py +++ b/_test/lib/test_all.py @@ -13,5 +13,6 @@ def main(args=None): collections.append(test_yaml_ext) test_appliance.run(collections, args) + if __name__ == '__main__': main() diff --git a/_test/lib/test_appliance.py b/_test/lib/test_appliance.py index fd957ad..a96052d 100644 --- a/_test/lib/test_appliance.py +++ b/_test/lib/test_appliance.py @@ -110,7 +110,7 @@ def execute(function, filenames, verbose): else: name = function.func_name if verbose: - sys.stdout.write('='*75+'\n') + sys.stdout.write('=' * 75 + '\n') sys.stdout.write('%s(%s)...\n' % (name, ', '.join(filenames))) try: function(verbose=verbose, *filenames) @@ -147,7 +147,7 @@ def display(results, verbose): failures += 1 if kind == 'ERROR': errors += 1 - sys.stdout.write('='*75+'\n') + sys.stdout.write('=' * 75 + '\n') sys.stdout.write('%s(%s): %s\n' % (name, ', '.join(filenames), kind)) if kind == 'ERROR': traceback.print_exception(file=sys.stdout, *info) @@ -155,11 +155,11 @@ def display(results, verbose): sys.stdout.write('Traceback (most recent call last):\n') traceback.print_tb(info[2], file=sys.stdout) sys.stdout.write('%s: see below\n' % info[0].__name__) - sys.stdout.write('~'*75+'\n') + sys.stdout.write('~' * 75 + '\n') for arg in info[1].args: pprint.pprint(arg, stream=sys.stdout) for filename in filenames: - sys.stdout.write('-'*75+'\n') + sys.stdout.write('-' * 75 + '\n') sys.stdout.write('%s:\n' % filename) if PY3: with open(filename, 'r', errors='replace') as fp: @@ -170,7 +170,7 @@ def display(results, verbose): sys.stdout.write(data) if data and data[-1] != '\n': sys.stdout.write('\n') - sys.stdout.write('='*75+'\n') + sys.stdout.write('=' * 75 + '\n') sys.stdout.write('TESTS: %s\n' % total) ret_val = 0 if failures: @@ -203,7 +203,7 @@ def run(collections, args=None): for ext in function.unittest: if ext not in exts: break - filenames.append(os.path.join(DATA, base+ext)) + filenames.append(os.path.join(DATA, base + ext)) else: skip_exts = getattr(function, 'skip', []) for skip_ext in skip_exts: diff --git a/_test/lib/test_canonical.py b/_test/lib/test_canonical.py index 33387cd..682f6ab 100644 --- a/_test/lib/test_canonical.py +++ b/_test/lib/test_canonical.py @@ -14,6 +14,7 @@ def test_canonical_scanner(canonical_filename, verbose=False): for token in tokens: print(token) + test_canonical_scanner.unittest = ['.canonical'] @@ -26,6 +27,7 @@ def test_canonical_parser(canonical_filename, verbose=False): for event in events: print(event) + test_canonical_parser.unittest = ['.canonical'] @@ -40,6 +42,7 @@ def test_canonical_error(data_filename, canonical_filename, verbose=False): else: raise AssertionError("expected an exception") + test_canonical_error.unittest = ['.data', '.canonical'] test_canonical_error.skip = ['.empty'] diff --git a/_test/lib/test_constructor.py b/_test/lib/test_constructor.py index fa8d8df..87e7a6d 100644 --- a/_test/lib/test_constructor.py +++ b/_test/lib/test_constructor.py @@ -131,7 +131,7 @@ def _make_objects(): return self def __cmp__(self, other): - return cmp((type(self), self.foo, self.bar, self.baz), + return cmp((type(self), self.foo, self.bar, self.baz), # NOQA (type(other), other.foo, other.bar, other.baz)) def __eq__(self, other): @@ -145,7 +145,7 @@ def _make_objects(): self.baz = baz def __cmp__(self, other): - return cmp((type(self), self.foo, self.bar, self.baz), + return cmp((type(self), self.foo, self.bar, self.baz), # NOQA (type(other), other.foo, other.bar, other.baz)) def __eq__(self, other): @@ -225,7 +225,7 @@ def _make_objects(): class MyList(list): def __init__(self, n=1): - self.extend([None]*n) + self.extend([None] * n) def __eq__(self, other): return type(self) is type(other) and list(self) == list(other) @@ -254,6 +254,7 @@ def _make_objects(): today = datetime.date.today() + try: from ruamel.ordereddict import ordereddict except: @@ -281,7 +282,7 @@ def _serialize_value(data): return '{%s}' % ', '.join(items) elif isinstance(data, datetime.datetime): return repr(data.utctimetuple()) - elif PY2 and isinstance(data, unicode): + elif PY2 and isinstance(data, unicode): # NOQA return data.encode('utf-8') elif isinstance(data, float) and data != data: return '?' @@ -319,6 +320,7 @@ def test_constructor_types(data_filename, code_filename, verbose=False): print("NATIVE2:") pprint.pprint(native2) + test_constructor_types.unittest = ['.data', '.code'] @@ -344,6 +346,7 @@ def test_roundtrip_data(code_filename, roundtrip_filename, verbose=False): print("NATIVE2:") pprint.pprint(native2) + test_roundtrip_data.unittest = ['.data', '.roundtrip'] diff --git a/_test/lib/test_emitter.py b/_test/lib/test_emitter.py index 7fff498..4b6fb76 100644 --- a/_test/lib/test_emitter.py +++ b/_test/lib/test_emitter.py @@ -13,7 +13,7 @@ def _compare_events(events1, events2): if isinstance(event1, yaml.CollectionStartEvent): assert event1.tag == event2.tag, (event1, event2) if isinstance(event1, yaml.ScalarEvent): - if True not in event1.implicit+event2.implicit: + if True not in event1.implicit + event2.implicit: assert event1.tag == event2.tag, (event1, event2) assert event1.value == event2.value, (event1, event2) @@ -28,6 +28,7 @@ def test_emitter_on_data(data_filename, canonical_filename, verbose=False): new_events = list(yaml.parse(output)) _compare_events(events, new_events) + test_emitter_on_data.unittest = ['.data', '.canonical'] @@ -42,6 +43,7 @@ def test_emitter_on_canonical(canonical_filename, verbose=False): new_events = list(yaml.parse(output)) _compare_events(events, new_events) + test_emitter_on_canonical.unittest = ['.canonical'] @@ -71,6 +73,7 @@ def test_emitter_styles(data_filename, canonical_filename, verbose=False): new_events = list(yaml.parse(output)) _compare_events(events, new_events) + test_emitter_styles.unittest = ['.data', '.canonical'] @@ -81,7 +84,7 @@ class EventsLoader(yaml.Loader): mapping = {} else: mapping = self.construct_mapping(node) - class_name = str(node.tag[1:])+'Event' + class_name = str(node.tag[1:]) + 'Event' if class_name in ['AliasEvent', 'ScalarEvent', 'SequenceStartEvent', 'MappingStartEvent']: mapping.setdefault('anchor', None) @@ -98,6 +101,7 @@ class EventsLoader(yaml.Loader): # if Loader is not a composite, add this function # EventsLoader.add_constructor = yaml.constructor.Constructor.add_constructor + EventsLoader.add_constructor(None, EventsLoader.construct_event) @@ -111,6 +115,7 @@ def test_emitter_events(events_filename, verbose=False): new_events = list(yaml.parse(output)) _compare_events(events, new_events) + if __name__ == '__main__': import test_appliance test_appliance.run(globals()) diff --git a/_test/lib/test_errors.py b/_test/lib/test_errors.py index 9576c90..cfaa000 100644 --- a/_test/lib/test_errors.py +++ b/_test/lib/test_errors.py @@ -17,6 +17,7 @@ def test_loader_error(error_filename, verbose=False): else: raise AssertionError("expected an exception") + test_loader_error.unittest = ['.loader-error'] @@ -30,6 +31,7 @@ def test_loader_error_string(error_filename, verbose=False): else: raise AssertionError("expected an exception") + test_loader_error_string.unittest = ['.loader-error'] @@ -43,6 +45,7 @@ def test_loader_error_single(error_filename, verbose=False): else: raise AssertionError("expected an exception") + test_loader_error_single.unittest = ['.single-loader-error'] @@ -58,6 +61,7 @@ def test_emitter_error(error_filename, verbose=False): else: raise AssertionError("expected an exception") + test_emitter_error.unittest = ['.emitter-error'] @@ -73,6 +77,7 @@ def test_dumper_error(error_filename, verbose=False): else: raise AssertionError("expected an exception") + test_dumper_error.unittest = ['.dumper-error'] if __name__ == '__main__': diff --git a/_test/lib/test_input_output.py b/_test/lib/test_input_output.py index 399a263..d83cb5a 100644 --- a/_test/lib/test_input_output.py +++ b/_test/lib/test_input_output.py @@ -31,9 +31,9 @@ if PY3: output = yaml.load(StringIO(data)) assert output == value, (output, value) for input in [data.encode('utf-8'), - codecs.BOM_UTF8+data.encode('utf-8'), - codecs.BOM_UTF16_BE+data.encode('utf-16-be'), - codecs.BOM_UTF16_LE+data.encode('utf-16-le')]: + codecs.BOM_UTF8 + data.encode('utf-8'), + codecs.BOM_UTF16_BE + data.encode('utf-16-be'), + codecs.BOM_UTF16_LE + data.encode('utf-16-le')]: if verbose: print("INPUT:", repr(input[:10]), "...") output = yaml.load(input) @@ -48,9 +48,9 @@ else: output = yaml.load(_unicode_open(StringIO(data.encode('utf-8')), 'utf-8')) assert output == value, (output, value) for input in [data, data.encode('utf-8'), - codecs.BOM_UTF8+data.encode('utf-8'), - codecs.BOM_UTF16_BE+data.encode('utf-16-be'), - codecs.BOM_UTF16_LE+data.encode('utf-16-le')]: + codecs.BOM_UTF8 + data.encode('utf-8'), + codecs.BOM_UTF16_BE + data.encode('utf-16-be'), + codecs.BOM_UTF16_LE + data.encode('utf-16-le')]: if verbose: print("INPUT:", repr(input[:10]), "...") output = yaml.load(input) @@ -66,9 +66,9 @@ def test_unicode_input_errors(unicode_filename, verbose=False): data = fp.read().decode('utf-8') for input in [data.encode('latin1', 'ignore'), data.encode('utf-16-be'), data.encode('utf-16-le'), - codecs.BOM_UTF8+data.encode('utf-16-be'), - codecs.BOM_UTF16_BE+data.encode('utf-16-le'), - codecs.BOM_UTF16_LE+data.encode('utf-8')+b'!']: + codecs.BOM_UTF8 + data.encode('utf-16-be'), + codecs.BOM_UTF16_BE + data.encode('utf-16-le'), + codecs.BOM_UTF16_LE + data.encode('utf-8') + b'!']: try: yaml.load(input) except yaml.YAMLError as exc: @@ -84,6 +84,7 @@ def test_unicode_input_errors(unicode_filename, verbose=False): else: raise AssertionError("expected an exception") + test_unicode_input_errors.unittest = ['.unicode'] if PY3: @@ -166,14 +167,15 @@ else: assert isinstance(data2, str), (type(data2), encoding) data2.decode('utf-8') if encoding is None: - assert isinstance(data3, unicode), (type(data3), encoding) - assert isinstance(data4, unicode), (type(data4), encoding) + assert isinstance(data3, unicode), (type(data3), encoding) # NOQA + assert isinstance(data4, unicode), (type(data4), encoding) # NOQA else: assert isinstance(data3, str), (type(data3), encoding) data3.decode(encoding) assert isinstance(data4, str), (type(data4), encoding) data4.decode(encoding) + test_unicode_output.unittest = ['.unicode'] @@ -220,6 +222,7 @@ def test_file_output(unicode_filename, verbose=False): if os.path.exists(filename): os.unlink(filename) + test_file_output.unittest = ['.unicode'] @@ -230,7 +233,7 @@ def test_unicode_transfer(unicode_filename, verbose=False): input = data if PY3: if encoding is not None: - input = ('\ufeff'+input).encode(encoding) + input = ('\ufeff' + input).encode(encoding) output1 = yaml.emit(yaml.parse(input), allow_unicode=True) if encoding is None: stream = StringIO() @@ -246,19 +249,20 @@ def test_unicode_transfer(unicode_filename, verbose=False): output2.decode(encoding) else: if encoding is not None: - input = (u'\ufeff'+input).encode(encoding) + input = (u'\ufeff' + input).encode(encoding) output1 = yaml.emit(yaml.parse(input), allow_unicode=True) stream = StringIO() yaml.emit(yaml.parse(input), _unicode_open(stream, 'utf-8'), allow_unicode=True) output2 = stream.getvalue() if encoding is None: - assert isinstance(output1, unicode), (type(output1), encoding) + assert isinstance(output1, unicode), (type(output1), encoding) # NOQA else: assert isinstance(output1, str), (type(output1), encoding) output1.decode(encoding) assert isinstance(output2, str), (type(output2), encoding) output2.decode('utf-8') + test_unicode_transfer.unittest = ['.unicode'] if __name__ == '__main__': diff --git a/_test/lib/test_mark.py b/_test/lib/test_mark.py index 56cb52f..4e0728f 100644 --- a/_test/lib/test_mark.py +++ b/_test/lib/test_mark.py @@ -28,7 +28,8 @@ def test_marks(marks_filename, verbose=False): assert snippet.count('\n') == 1, snippet.count('\n') data, pointer = snippet.split('\n') assert len(data) < 82, len(data) - assert data[len(pointer)-1] == '*', data[len(pointer)-1] + assert data[len(pointer) - 1] == '*', data[len(pointer) - 1] + test_marks.unittest = ['.marks'] diff --git a/_test/lib/test_reader.py b/_test/lib/test_reader.py index ce5de81..7ce0dc9 100644 --- a/_test/lib/test_reader.py +++ b/_test/lib/test_reader.py @@ -29,7 +29,7 @@ def test_stream_error(error_filename, verbose=False): try: if PY2: with open(error_filename, 'rb') as fp0: - data = unicode(fp0.read(), encoding) + data = unicode(fp0.read(), encoding) # NOQA else: with open(error_filename, 'rb') as fp0: data = fp0.read().decode(encoding) @@ -42,6 +42,7 @@ def test_stream_error(error_filename, verbose=False): with io.open(error_filename, encoding=encoding) as fp: _run_reader(fp, verbose) + test_stream_error.unittest = ['.stream-error'] if __name__ == '__main__': diff --git a/_test/lib/test_recursive.py b/_test/lib/test_recursive.py index 824ceaa..e7084e7 100644 --- a/_test/lib/test_recursive.py +++ b/_test/lib/test_recursive.py @@ -48,6 +48,7 @@ def test_recursive(recursive_filename, verbose=False): print("OUTPUT2:") print(output2) + test_recursive.unittest = ['.recursive'] if __name__ == '__main__': diff --git a/_test/lib/test_representer.py b/_test/lib/test_representer.py index 6b449a8..2c7e2c8 100644 --- a/_test/lib/test_representer.py +++ b/_test/lib/test_representer.py @@ -39,6 +39,7 @@ def test_representer_types(code_filename, verbose=False): print("OUTPUT:") print(output) + test_representer_types.unittest = ['.code'] if __name__ == '__main__': diff --git a/_test/lib/test_resolver.py b/_test/lib/test_resolver.py index 64d401d..4d1e6a4 100644 --- a/_test/lib/test_resolver.py +++ b/_test/lib/test_resolver.py @@ -25,6 +25,7 @@ def test_implicit_resolver(data_filename, detect_filename, verbose=False): print("CHILDREN:") pprint.pprint(node.value) + test_implicit_resolver.unittest = ['.data', '.detect'] @@ -81,6 +82,7 @@ def test_path_resolver_loader(data_filename, path_filename, verbose=False): if verbose: print(yaml.serialize_all(nodes1)) + test_path_resolver_loader.unittest = ['.data', '.path'] @@ -99,6 +101,7 @@ def test_path_resolver_dumper(data_filename, path_filename, verbose=False): data2 = _convert_node(node2) assert data1 == data2, (data1, data2) + test_path_resolver_dumper.unittest = ['.data', '.path'] if __name__ == '__main__': diff --git a/_test/lib/test_structure.py b/_test/lib/test_structure.py index a23dac8..4d04b11 100644 --- a/_test/lib/test_structure.py +++ b/_test/lib/test_structure.py @@ -61,6 +61,7 @@ def test_structure(data_filename, structure_filename, verbose=False): print("NODES2:") pprint.pprint(nodes2) + test_structure.unittest = ['.data', '.structure'] @@ -93,6 +94,7 @@ def test_parser(data_filename, canonical_filename, verbose=False): print("EVENTS2:") pprint.pprint(events2) + test_parser.unittest = ['.data', '.canonical'] @@ -112,6 +114,7 @@ def test_parser_on_canonical(canonical_filename, verbose=False): print("EVENTS2:") pprint.pprint(events2) + test_parser_on_canonical.unittest = ['.canonical'] @@ -148,6 +151,7 @@ def test_composer(data_filename, canonical_filename, verbose=False): print("NODES2:") pprint.pprint(nodes2) + test_composer.unittest = ['.data', '.canonical'] @@ -208,6 +212,7 @@ def test_constructor(data_filename, canonical_filename, verbose=False): print("NATIVE2:") pprint.pprint(native2) + test_constructor.unittest = ['.data', '.canonical'] if __name__ == '__main__': diff --git a/_test/lib/test_tokens.py b/_test/lib/test_tokens.py index be067f0..e240efe 100644 --- a/_test/lib/test_tokens.py +++ b/_test/lib/test_tokens.py @@ -64,6 +64,7 @@ def test_tokens(data_filename, tokens_filename, verbose=False): for token1, token2 in zip(tokens1, tokens2): assert token1 == token2, (token1, token2) + test_tokens.unittest = ['.data', '.tokens'] @@ -78,6 +79,7 @@ def test_scanner(data_filename, canonical_filename, verbose=False): if verbose: pprint.pprint(tokens) + test_scanner.unittest = ['.data', '.canonical'] if __name__ == '__main__': diff --git a/_test/lib/test_yaml_ext.py b/_test/lib/test_yaml_ext.py index ebb9f58..c4a1493 100644 --- a/_test/lib/test_yaml_ext.py +++ b/_test/lib/test_yaml_ext.py @@ -22,84 +22,98 @@ old_scan = ruamel.yaml.scan def new_scan(stream, Loader=ruamel.yaml.CLoader): return old_scan(stream, Loader) + old_parse = ruamel.yaml.parse def new_parse(stream, Loader=ruamel.yaml.CLoader): return old_parse(stream, Loader) + old_compose = ruamel.yaml.compose def new_compose(stream, Loader=ruamel.yaml.CLoader): return old_compose(stream, Loader) + old_compose_all = ruamel.yaml.compose_all def new_compose_all(stream, Loader=ruamel.yaml.CLoader): return old_compose_all(stream, Loader) + old_load = ruamel.yaml.load def new_load(stream, Loader=ruamel.yaml.CLoader): return old_load(stream, Loader) + old_load_all = ruamel.yaml.load_all def new_load_all(stream, Loader=ruamel.yaml.CLoader): return old_load_all(stream, Loader) + old_safe_load = ruamel.yaml.safe_load def new_safe_load(stream): return old_load(stream, ruamel.yaml.CSafeLoader) + old_safe_load_all = ruamel.yaml.safe_load_all def new_safe_load_all(stream): return old_load_all(stream, ruamel.yaml.CSafeLoader) + old_emit = ruamel.yaml.emit def new_emit(events, stream=None, Dumper=ruamel.yaml.CDumper, **kwds): return old_emit(events, stream, Dumper, **kwds) + old_serialize = ruamel.yaml.serialize def new_serialize(node, stream, Dumper=ruamel.yaml.CDumper, **kwds): return old_serialize(node, stream, Dumper, **kwds) + old_serialize_all = ruamel.yaml.serialize_all def new_serialize_all(nodes, stream=None, Dumper=ruamel.yaml.CDumper, **kwds): return old_serialize_all(nodes, stream, Dumper, **kwds) + old_dump = ruamel.yaml.dump def new_dump(data, stream=None, Dumper=ruamel.yaml.CDumper, **kwds): return old_dump(data, stream, Dumper, **kwds) + old_dump_all = ruamel.yaml.dump_all def new_dump_all(documents, stream=None, Dumper=ruamel.yaml.CDumper, **kwds): return old_dump_all(documents, stream, Dumper, **kwds) + old_safe_dump = ruamel.yaml.safe_dump def new_safe_dump(data, stream=None, **kwds): return old_dump(data, stream, ruamel.yaml.CSafeDumper, **kwds) + old_safe_dump_all = ruamel.yaml.safe_dump_all @@ -207,6 +221,7 @@ def test_c_scanner(data_filename, canonical_filename, verbose=False): with open(canonical_filename, 'rb') as fp1: _compare_scanners(fp0.read(), fp1.read(), verbose) + test_c_scanner.unittest = ['.data', '.canonical'] test_c_scanner.skip = ['.skip-ext'] @@ -246,6 +261,7 @@ def test_c_parser(data_filename, canonical_filename, verbose=False): with open(canonical_filename, 'rb') as fp1: _compare_parsers(fp0.read(), fp1.read(), verbose) + test_c_parser.unittest = ['.data', '.canonical'] test_c_parser.skip = ['.skip-ext'] @@ -289,6 +305,7 @@ def test_c_emitter(data_filename, canonical_filename, verbose=False): with open(canonical_filename, 'rb') as fp0: _compare_emitters(fp0.read(), verbose) + test_c_emitter.unittest = ['.data', '.canonical'] test_c_emitter.skip = ['.skip-ext'] @@ -309,7 +326,7 @@ def wrap_ext_function(function): pass wrapper.unittest_name = '%s_ext' % function.__name__ wrapper.unittest = function.unittest - wrapper.skip = getattr(function, 'skip', [])+['.skip-ext'] + wrapper.skip = getattr(function, 'skip', []) + ['.skip-ext'] return wrapper diff --git a/_test/roundtrip.py b/_test/roundtrip.py index ac41e7b..f226e38 100644 --- a/_test/roundtrip.py +++ b/_test/roundtrip.py @@ -19,7 +19,7 @@ def dedent(data): except ValueError: pass else: - data = data[position_of_first_newline+1:] + data = data[position_of_first_newline + 1:] return textwrap.dedent(data) diff --git a/_test/test_comments.py b/_test/test_comments.py index d1ef7fb..2557b94 100644 --- a/_test/test_comments.py +++ b/_test/test_comments.py @@ -736,6 +736,7 @@ class TestEmptyValueBeforeComments: b: # comment b """) + test_block_scalar_commented_line_template = """\ y: p # Some comment diff --git a/_test/test_literal.py b/_test/test_literal.py index 6068b3a..1bb8367 100644 --- a/_test/test_literal.py +++ b/_test/test_literal.py @@ -112,7 +112,7 @@ class TestNoIndent: {s} """.format(s=s)) print(d) - assert d == (s + '\n')*2 + assert d == (s + '\n') * 2 def test_top_literal_scalar_no_indent_special(self): yaml = YAML() diff --git a/_test/test_tag.py b/_test/test_tag.py index 58249ae..f1a72c3 100644 --- a/_test/test_tag.py +++ b/_test/test_tag.py @@ -17,6 +17,7 @@ class XXX(yaml.comments.CommentedMap): yield data constructor.construct_mapping(node, data) + yaml.add_constructor(u'!xxx', XXX.yaml_load, constructor=yaml.RoundTripConstructor) yaml.add_representer(XXX, XXX.yaml_dump, representer=yaml.RoundTripRepresenter) diff --git a/comments.py b/comments.py index d5a9d9d..9535ff5 100644 --- a/comments.py +++ b/comments.py @@ -389,7 +389,7 @@ class CommentedSeq(list, CommentedBase): for list_index in sorted(self.ca.items, reverse=True): if list_index < idx: break - self.ca.items[list_index+1] = self.ca.items.pop(list_index) + self.ca.items[list_index + 1] = self.ca.items.pop(list_index) def pop(self, idx=None): # type: (Any) -> Any @@ -398,14 +398,14 @@ class CommentedSeq(list, CommentedBase): for list_index in sorted(self.ca.items): if list_index < idx: continue - self.ca.items[list_index-1] = self.ca.items.pop(list_index) + self.ca.items[list_index - 1] = self.ca.items.pop(list_index) return res def _yaml_get_column(self, key): # type: (Any) -> Any column = None sel_idx = None - pre, post = key-1, key+1 + pre, post = key - 1, key + 1 if pre in self.ca.items: sel_idx = pre elif post in self.ca.items: @@ -472,7 +472,7 @@ class CommentedKeySeq(tuple, CommentedBase): # type: (Any) -> Any column = None sel_idx = None - pre, post = key-1, key+1 + pre, post = key - 1, key + 1 if pre in self.ca.items: sel_idx = pre elif post in self.ca.items: @@ -677,8 +677,8 @@ class CommentedMap(ordereddict, CommentedBase): if level >= len(key_list): if level > len(key_list): raise IndexError - return d[key_list[level-1]] - return get_one_level(key_list, level+1, d[key_list[level-1]]) + return d[key_list[level - 1]] + return get_one_level(key_list, level + 1, d[key_list[level - 1]]) try: return get_one_level(key, 1, self) diff --git a/compat.py b/compat.py index 41c52e7..7fffedf 100644 --- a/compat.py +++ b/compat.py @@ -67,7 +67,7 @@ else: def to_unicode(s): # type: (str) -> unicode - return unicode(s) + return unicode(s) # NOQA if PY3: string_types = str @@ -83,10 +83,10 @@ if PY3: BytesIO = io.BytesIO else: - string_types = basestring - integer_types = (int, long) + string_types = basestring # NOQA + integer_types = (int, long) # NOQA class_types = (type, types.ClassType) - text_type = unicode + text_type = unicode # NOQA binary_type = str # to allow importing @@ -114,6 +114,7 @@ def with_metaclass(meta, *bases): """Create a base class with a metaclass.""" return meta("NewBase", bases, {}) + DBG_TOKEN = 1 DBG_EVENT = 2 DBG_NODE = 4 diff --git a/constructor.py b/constructor.py index 61e7305..fd1926e 100644 --- a/constructor.py +++ b/constructor.py @@ -360,12 +360,12 @@ class SafeConstructor(BaseConstructor): # YAML 1.2 spec doesn't mention yes/no etc any more, 1.1 does bool_values = { - u'yes': True, - u'no': False, - u'true': True, - u'false': False, - u'on': True, - u'off': False, + u'yes': True, + u'no': False, + u'true': True, + u'false': False, + u'on': True, + u'off': False, } def construct_yaml_bool(self, node): @@ -385,29 +385,29 @@ class SafeConstructor(BaseConstructor): if value_s == '0': return 0 elif value_s.startswith('0b'): - return sign*int(value_s[2:], 2) + return sign * int(value_s[2:], 2) elif value_s.startswith('0x'): - return sign*int(value_s[2:], 16) + return sign * int(value_s[2:], 16) elif value_s.startswith('0o'): - return sign*int(value_s[2:], 8) + return sign * int(value_s[2:], 8) elif self.resolver.processing_version != (1, 2) and value_s[0] == '0': - return sign*int(value_s, 8) + return sign * int(value_s, 8) elif self.resolver.processing_version != (1, 2) and ':' in value_s: digits = [int(part) for part in value_s.split(':')] digits.reverse() base = 1 value = 0 for digit in digits: - value += digit*base + value += digit * base base *= 60 - return sign*value + return sign * value else: - return sign*int(value_s) + return sign * int(value_s) inf_value = 1e300 - while inf_value != inf_value*inf_value: + while inf_value != inf_value * inf_value: inf_value *= inf_value - nan_value = -inf_value/inf_value # Trying to make a quiet NaN (like C99). + nan_value = -inf_value / inf_value # Trying to make a quiet NaN (like C99). def construct_yaml_float(self, node): # type: (Any) -> float @@ -419,7 +419,7 @@ class SafeConstructor(BaseConstructor): if value_s[0] in '+-': value_s = value_s[1:] if value_s == '.inf': - return sign*self.inf_value + return sign * self.inf_value elif value_s == '.nan': return self.nan_value elif ':' in value_s: @@ -428,11 +428,11 @@ class SafeConstructor(BaseConstructor): base = 1 value = 0.0 for digit in digits: - value += digit*base + value += digit * base base *= 60 - return sign*value + return sign * value else: - return sign*float(value_s) + return sign * float(value_s) if PY3: def construct_yaml_binary(self, node): @@ -615,6 +615,7 @@ class SafeConstructor(BaseConstructor): utf8(node.tag), node.start_mark) + SafeConstructor.add_constructor( u'tag:yaml.org,2002:null', SafeConstructor.construct_yaml_null) @@ -864,6 +865,7 @@ class Constructor(SafeConstructor): # type: (Any, Any) -> Any return self.construct_python_object_apply(suffix, node, newobj=True) + Constructor.add_constructor( u'tag:yaml.org,2002:python/none', Constructor.construct_yaml_null) @@ -981,7 +983,7 @@ class RoundTripConstructor(SafeConstructor): if underscore is not None: underscore[1] = value_su[2] == '_' underscore[2] = len(value_su[2:]) > 1 and value_su[-1] == '_' - return BinaryInt(sign*int(value_s[2:], 2), width=width, # type: ignore + return BinaryInt(sign * int(value_s[2:], 2), width=width, # type: ignore underscore=underscore) elif value_s.startswith('0x'): # default to lower-case if no a-fA-F in string @@ -997,40 +999,40 @@ class RoundTripConstructor(SafeConstructor): if underscore is not None: underscore[1] = value_su[2] == '_' underscore[2] = len(value_su[2:]) > 1 and value_su[-1] == '_' - return hex_fun(sign*int(value_s[2:], 16), width=width, underscore=underscore) + return hex_fun(sign * int(value_s[2:], 16), width=width, underscore=underscore) elif value_s.startswith('0o'): if self.resolver.processing_version > (1, 1) and value_s[2] == '0': width = len(value_s[2:]) if underscore is not None: underscore[1] = value_su[2] == '_' underscore[2] = len(value_su[2:]) > 1 and value_su[-1] == '_' - return OctalInt(sign*int(value_s[2:], 8), width=width, # type: ignore + return OctalInt(sign * int(value_s[2:], 8), width=width, # type: ignore underscore=underscore) elif self.resolver.processing_version != (1, 2) and value_s[0] == '0': - return sign*int(value_s, 8) + return sign * int(value_s, 8) elif self.resolver.processing_version != (1, 2) and ':' in value_s: digits = [int(part) for part in value_s.split(':')] digits.reverse() base = 1 value = 0 for digit in digits: - value += digit*base + value += digit * base base *= 60 - return sign*value + return sign * value elif self.resolver.processing_version > (1, 1) and value_s[0] == '0': # not an octal, an integer with leading zero(s) if underscore is not None: # cannot have a leading underscore underscore[2] = len(value_su) > 1 and value_su[-1] == '_' - return ScalarInt(sign*int(value_s), width=len(value_s), # type: ignore + return ScalarInt(sign * int(value_s), width=len(value_s), # type: ignore underscore=underscore) elif underscore: # cannot have a leading underscore underscore[2] = len(value_su) > 1 and value_su[-1] == '_' - return ScalarInt(sign*int(value_s), width=None, # type: ignore + return ScalarInt(sign * int(value_s), width=None, # type: ignore underscore=underscore) else: - return sign*int(value_s) + return sign * int(value_s) def construct_yaml_str(self, node): # type: (Any) -> Any diff --git a/emitter.py b/emitter.py index e9388bc..617d439 100644 --- a/emitter.py +++ b/emitter.py @@ -118,7 +118,7 @@ class Emitter(object): # if self.best_indent < self.block_seq_indent + 1: # self.best_indent = self.block_seq_indent + 1 self.best_width = 80 - if width and width > self.best_indent*2: + if width and width > self.best_indent * 2: self.best_width = width self.best_line_break = u'\n' # type: Any if line_break in [u'\r', u'\n', u'\r\n']: @@ -196,7 +196,7 @@ class Emitter(object): level = -1 if level < 0: return False - return (len(self.events) < count+1) + return (len(self.events) < count + 1) def increase_indent(self, flow=False, sequence=None, indentless=False): # type: (bool, bool, bool) -> None @@ -606,7 +606,7 @@ class Emitter(object): if self.prepared_anchor is None: self.prepared_anchor = self.prepare_anchor(self.event.anchor) if self.prepared_anchor: - self.write_indicator(indicator+self.prepared_anchor, True) + self.write_indicator(indicator + self.prepared_anchor, True) self.prepared_anchor = None def process_tag(self): @@ -732,7 +732,7 @@ class Emitter(object): else: if start < end: chunks.append(prefix[start:end]) - start = end = end+1 + start = end = end + 1 data = utf8(ch) for ch in data: chunks.append(u'%%%02X' % ord(ch)) @@ -765,7 +765,7 @@ class Emitter(object): else: if start < end: chunks.append(suffix[start:end]) - start = end = end+1 + start = end = end + 1 data = utf8(ch) for ch in data: chunks.append(u'%%%02X' % ord(ch)) @@ -876,7 +876,7 @@ class Emitter(object): if ch == u' ': if index == 0: leading_space = True - if index == len(scalar)-1: + if index == len(scalar) - 1: trailing_space = True if previous_break: break_space = True @@ -885,7 +885,7 @@ class Emitter(object): elif ch in u'\n\x85\u2028\u2029': if index == 0: leading_break = True - if index == len(scalar)-1: + if index == len(scalar) - 1: trailing_break = True if previous_space: space_break = True @@ -899,8 +899,8 @@ class Emitter(object): index += 1 preceeded_by_whitespace = (ch in u'\0 \t\r\n\x85\u2028\u2029') followed_by_whitespace = ( - index+1 >= len(scalar) or - scalar[index+1] in u'\0 \t\r\n\x85\u2028\u2029') + index + 1 >= len(scalar) or + scalar[index + 1] in u'\0 \t\r\n\x85\u2028\u2029') # Let's decide what styles are allowed. allow_flow_plain = True @@ -975,7 +975,7 @@ class Emitter(object): if self.whitespace or not need_whitespace: data = indicator else: - data = u' '+indicator + data = u' ' + indicator self.whitespace = whitespace self.indention = self.indention and indention self.column += len(data) @@ -995,7 +995,7 @@ class Emitter(object): self.write_line_break() if self.column < indent: self.whitespace = True - data = u' '*(indent-self.column) + data = u' ' * (indent - self.column) self.column = indent if self.encoding: data = data.encode(self.encoding) @@ -1048,7 +1048,7 @@ class Emitter(object): ch = text[end] if spaces: if ch is None or ch != u' ': - if start+1 == end and self.column > self.best_width and split \ + if start + 1 == end and self.column > self.best_width and split \ and start != 0 and end != len(text): self.write_indent() else: @@ -1092,21 +1092,21 @@ class Emitter(object): self.write_indicator(u'\'', False) ESCAPE_REPLACEMENTS = { - u'\0': u'0', - u'\x07': u'a', - u'\x08': u'b', - u'\x09': u't', - u'\x0A': u'n', - u'\x0B': u'v', - u'\x0C': u'f', - u'\x0D': u'r', - u'\x1B': u'e', - u'\"': u'\"', - u'\\': u'\\', - u'\x85': u'N', - u'\xA0': u'_', - u'\u2028': u'L', - u'\u2029': u'P', + u'\0': u'0', + u'\x07': u'a', + u'\x08': u'b', + u'\x09': u't', + u'\x0A': u'n', + u'\x0B': u'v', + u'\x0C': u'f', + u'\x0D': u'r', + u'\x1B': u'e', + u'\"': u'\"', + u'\\': u'\\', + u'\x85': u'N', + u'\xA0': u'_', + u'\u2028': u'L', + u'\u2029': u'P', } def write_double_quoted(self, text, split=True): @@ -1135,7 +1135,7 @@ class Emitter(object): start = end if ch is not None: if ch in self.ESCAPE_REPLACEMENTS: - data = u'\\'+self.ESCAPE_REPLACEMENTS[ch] + data = u'\\' + self.ESCAPE_REPLACEMENTS[ch] elif ch <= u'\xFF': data = u'\\x%02X' % ord(ch) elif ch <= u'\uFFFF': @@ -1146,10 +1146,10 @@ class Emitter(object): if bool(self.encoding): data = data.encode(self.encoding) self.stream.write(data) - start = end+1 - if 0 < end < len(text)-1 and (ch == u' ' or start >= end) \ - and self.column+(end-start) > self.best_width and split: - data = text[start:end]+u'\\' + start = end + 1 + if 0 < end < len(text) - 1 and (ch == u' ' or start >= end) \ + and self.column + (end - start) > self.best_width and split: + data = text[start:end] + u'\\' if start < end: start = end self.column += len(data) @@ -1183,7 +1183,7 @@ class Emitter(object): def write_folded(self, text): # type: (Any) -> None hints = self.determine_block_hints(text) - self.write_indicator(u'>'+hints, True) + self.write_indicator(u'>' + hints, True) if hints[-1:] == u'+': self.open_ended = True self.write_line_break() @@ -1211,7 +1211,7 @@ class Emitter(object): start = end elif spaces: if ch != u' ': - if start+1 == end and self.column > self.best_width: + if start + 1 == end and self.column > self.best_width: self.write_indent() else: data = text[start:end] @@ -1238,7 +1238,7 @@ class Emitter(object): def write_literal(self, text): # type: (Any) -> None hints = self.determine_block_hints(text) - self.write_indicator(u'|'+hints, True) + self.write_indicator(u'|' + hints, True) if hints[-1:] == u'+': self.open_ended = True self.write_line_break() @@ -1299,8 +1299,7 @@ class Emitter(object): ch = text[end] if spaces: if ch != u' ': - if start+1 == end and self.column > self.best_width \ - and split: + if start + 1 == end and self.column > self.best_width and split: self.write_indent() self.whitespace = False self.indention = False @@ -1384,7 +1383,7 @@ class Emitter(object): if isinstance(event, MappingStartEvent): comment.pre_done = True except TypeError: - print ('eventtt', type(event), event) + print('eventtt', type(event), event) raise def write_post_comment(self, event): diff --git a/error.py b/error.py index 9ecd7ed..4faab94 100644 --- a/error.py +++ b/error.py @@ -31,7 +31,7 @@ class StreamMark(object): def __str__(self): # type: () -> Any where = " in \"%s\", line %d, column %d" \ - % (self.name, self.line+1, self.column+1) + % (self.name, self.line + 1, self.column + 1) return where @@ -55,9 +55,9 @@ class StringMark(StreamMark): head = '' start = self.pointer while (start > 0 and - self.buffer[start-1] not in u'\0\r\n\x85\u2028\u2029'): + self.buffer[start - 1] not in u'\0\r\n\x85\u2028\u2029'): start -= 1 - if self.pointer-start > max_length/2-1: + if self.pointer - start > max_length / 2 - 1: head = ' ... ' start += 5 break @@ -66,23 +66,23 @@ class StringMark(StreamMark): while (end < len(self.buffer) and self.buffer[end] not in u'\0\r\n\x85\u2028\u2029'): end += 1 - if end-self.pointer > max_length/2-1: + if end - self.pointer > max_length / 2 - 1: tail = ' ... ' end -= 5 break snippet = utf8(self.buffer[start:end]) caret = '^' - caret = '^ (line: {})'.format(self.line+1) - return ' '*indent + head + snippet + tail + '\n' \ - + ' '*(indent+self.pointer-start+len(head)) + caret + caret = '^ (line: {})'.format(self.line + 1) + return ' ' * indent + head + snippet + tail + '\n' \ + + ' ' * (indent + self.pointer - start + len(head)) + caret def __str__(self): # type: () -> Any snippet = self.get_snippet() where = " in \"%s\", line %d, column %d" \ - % (self.name, self.line+1, self.column+1) + % (self.name, self.line + 1, self.column + 1) if snippet is not None: - where += ":\n"+snippet + where += ":\n" + snippet return where @@ -189,6 +189,7 @@ Alternatively include the following in your code: In most other cases you should consider using 'safe_load(stream)'""" pass + warnings.simplefilter('once', UnsafeLoaderWarning) diff --git a/main.py b/main.py index b0f847b..fec0295 100644 --- a/main.py +++ b/main.py @@ -41,7 +41,7 @@ enforce = object() # YAML is an acronym, i.e. spoken: rhymes with "camel". And thus a -# subset of abbreviations, which should all caps according to PEP8 +# subset of abbreviations, which should be all caps according to PEP8 class YAML(object): def __init__(self, _kw=enforce, typ=None, pure=False, plug_ins=None): @@ -607,6 +607,7 @@ def emit(events, stream=None, Dumper=Dumper, if getvalue is not None: return getvalue() + enc = None if PY3 else 'utf-8' diff --git a/nodes.py b/nodes.py index 8babbd7..781df94 100644 --- a/nodes.py +++ b/nodes.py @@ -2,6 +2,8 @@ from __future__ import print_function +from .compat import string_types + if False: # MYPY from typing import Dict, Any, Text # NOQA @@ -39,7 +41,7 @@ class Node(object): def dump(self, indent=0): # type: (int) -> None - if isinstance(self.value, basestring): + if isinstance(self.value, string_types): print('{}{}(tag={!r}, value={!r})'.format( ' ' * indent, self.__class__.__name__, self.tag, self.value)) if self.comment: @@ -54,9 +56,9 @@ class Node(object): for v in self.value: if isinstance(v, tuple): for v1 in v: - v1.dump(indent+1) + v1.dump(indent + 1) elif isinstance(v, Node): - v.dump(indent+1) + v.dump(indent + 1) else: print('Node value type?', type(v)) diff --git a/parser.py b/parser.py index c7a68fd..779d50f 100644 --- a/parser.py +++ b/parser.py @@ -97,8 +97,8 @@ class Parser(object): # do not give many comments here. DEFAULT_TAGS = { - u'!': u'!', - u'!!': u'tag:yaml.org,2002:', + u'!': u'!', + u'!!': u'tag:yaml.org,2002:', } def __init__(self, loader): @@ -732,4 +732,4 @@ class RoundTripParser(Parser): u'timestamp', u'omap', u'pairs', u'set', u'str', u'seq', u'map'): return Parser.transform_tag(self, handle, suffix) - return handle+suffix + return handle + suffix diff --git a/reader.py b/reader.py index d200031..9ad2b5f 100644 --- a/reader.py +++ b/reader.py @@ -127,21 +127,21 @@ class Reader(object): def peek(self, index=0): # type: (int) -> Text try: - return self.buffer[self.pointer+index] + return self.buffer[self.pointer + index] except IndexError: - self.update(index+1) - return self.buffer[self.pointer+index] + self.update(index + 1) + return self.buffer[self.pointer + index] def prefix(self, length=1): # type: (int) -> Any - if self.pointer+length >= len(self.buffer): + if self.pointer + length >= len(self.buffer): self.update(length) - return self.buffer[self.pointer:self.pointer+length] + return self.buffer[self.pointer:self.pointer + length] def forward(self, length=1): # type: (int) -> None - if self.pointer+length+1 >= len(self.buffer): - self.update(length+1) + if self.pointer + length + 1 >= len(self.buffer): + self.update(length + 1) while length != 0: ch = self.buffer[self.pointer] self.pointer += 1 @@ -203,7 +203,7 @@ class Reader(object): match = self.NON_PRINTABLE.search(data) if bool(match): character = match.group() - position = self.index+(len(self.buffer)-self.pointer)+match.start() + position = self.index + (len(self.buffer) - self.pointer) + match.start() raise ReaderError(self.name, position, ord(character), 'unicode', "special characters are not allowed") diff --git a/representer.py b/representer.py index 38fc823..3b0ff63 100644 --- a/representer.py +++ b/representer.py @@ -31,6 +31,7 @@ __all__ = ['BaseRepresenter', 'SafeRepresenter', 'Representer', class RepresenterError(YAMLError): pass + if PY2: def get_classobj_bases(cls): # type: (Any) -> Any @@ -291,7 +292,7 @@ class SafeRepresenter(BaseRepresenter): text_type(data)) inf_value = 1e300 - while repr(inf_value) != repr(inf_value*inf_value): + while repr(inf_value) != repr(inf_value * inf_value): inf_value *= inf_value def represent_float(self, data): @@ -368,6 +369,7 @@ class SafeRepresenter(BaseRepresenter): # type: (Any) -> None raise RepresenterError("cannot represent an object: %s" % data) + SafeRepresenter.add_representer(type(None), SafeRepresenter.represent_none) @@ -485,7 +487,7 @@ class Representer(SafeRepresenter): def represent_module(self, data): # type: (Any) -> Any return self.represent_scalar( - u'tag:yaml.org,2002:python/module:'+data.__name__, u'') + u'tag:yaml.org,2002:python/module:' + data.__name__, u'') if PY2: def represent_instance(self, data): @@ -519,7 +521,7 @@ 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:' + @@ -529,7 +531,7 @@ class Representer(SafeRepresenter): 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 @@ -558,7 +560,7 @@ class Representer(SafeRepresenter): reduce = data.__reduce__() else: raise RepresenterError("cannot represent object: %r" % data) - reduce = (list(reduce)+[None]*5)[:5] + reduce = (list(reduce) + [None] * 5)[:5] function, args, state, listitems, dictitems = reduce args = list(args) if state is None: @@ -579,10 +581,10 @@ class Representer(SafeRepresenter): 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) + 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) + return self.represent_sequence(tag + function_name, args) value = {} if args: value['args'] = args @@ -592,7 +594,8 @@ class Representer(SafeRepresenter): value['listitems'] = listitems if dictitems: value['dictitems'] = dictitems - return self.represent_mapping(tag+function_name, value) + return self.represent_mapping(tag + function_name, value) + if PY2: Representer.add_representer(str, diff --git a/resolver.py b/resolver.py index c684d60..cbbda62 100644 --- a/resolver.py +++ b/resolver.py @@ -240,7 +240,7 @@ class BaseResolver(object): def check_resolver_prefix(self, depth, path, kind, current_node, current_index): # type: (int, Text, Any, Any, Any) -> bool - node_check, index_check = path[depth-1] + node_check, index_check = path[depth - 1] if isinstance(node_check, string_types): if current_node.tag != node_check: return False @@ -296,6 +296,7 @@ class BaseResolver(object): class Resolver(BaseResolver): pass + Resolver.add_implicit_resolver_base( u'tag:yaml.org,2002:bool', re.compile(u'''^(?:yes|Yes|YES|no|No|NO diff --git a/scanner.py b/scanner.py index 3f30157..883ef96 100644 --- a/scanner.py +++ b/scanner.py @@ -352,7 +352,7 @@ class Scanner(object): # position. if self.allow_simple_key: self.remove_possible_simple_key() - token_number = self.tokens_taken+len(self.tokens) + token_number = self.tokens_taken + len(self.tokens) key = SimpleKey( token_number, required, self.reader.index, self.reader.line, self.reader.column, @@ -590,7 +590,7 @@ class Scanner(object): # Add KEY. key = self.possible_simple_keys[self.flow_level] del self.possible_simple_keys[self.flow_level] - self.tokens.insert(key.token_number-self.tokens_taken, + self.tokens.insert(key.token_number - self.tokens_taken, KeyToken(key.mark, key.mark)) # If this key starts a new block mapping, we need to add @@ -598,7 +598,7 @@ class Scanner(object): if not self.flow_level: if self.add_indent(key.column): self.tokens.insert( - key.token_number-self.tokens_taken, + key.token_number - self.tokens_taken, BlockMappingStartToken(key.mark, key.mark)) # There cannot be two simple keys one after another. @@ -1052,7 +1052,7 @@ class Scanner(object): self.scan_block_scalar_ignored_line(start_mark) # Determine the indentation level and go to the first non-empty line. - min_indent = self.indent+1 + min_indent = self.indent + 1 if increment is None: # no increment and top level, min_indent could be 0 if min_indent < 1 and \ @@ -1066,7 +1066,7 @@ class Scanner(object): else: if min_indent < 1: min_indent = 1 - indent = min_indent+increment-1 + indent = min_indent + increment - 1 breaks, end_mark = self.scan_block_scalar_breaks(indent) line_break = u'' @@ -1253,30 +1253,30 @@ class Scanner(object): style) ESCAPE_REPLACEMENTS = { - u'0': u'\0', - u'a': u'\x07', - u'b': u'\x08', - u't': u'\x09', - u'\t': u'\x09', - u'n': u'\x0A', - u'v': u'\x0B', - u'f': u'\x0C', - u'r': u'\x0D', - u'e': u'\x1B', - u' ': u'\x20', - u'\"': u'\"', - u'/': u'/', # as per http://www.json.org/ - u'\\': u'\\', - u'N': u'\x85', - u'_': u'\xA0', - u'L': u'\u2028', - u'P': u'\u2029', + u'0': u'\0', + u'a': u'\x07', + u'b': u'\x08', + u't': u'\x09', + u'\t': u'\x09', + u'n': u'\x0A', + u'v': u'\x0B', + u'f': u'\x0C', + u'r': u'\x0D', + u'e': u'\x1B', + u' ': u'\x20', + u'\"': u'\"', + u'/': u'/', # as per http://www.json.org/ + u'\\': u'\\', + u'N': u'\x85', + u'_': u'\xA0', + u'L': u'\u2028', + u'P': u'\u2029', } ESCAPE_CODES = { - u'x': 2, - u'u': 4, - u'U': 8, + u'x': 2, + u'u': 4, + u'U': 8, } def scan_flow_scalar_non_spaces(self, double, start_mark): @@ -1386,7 +1386,7 @@ class Scanner(object): chunks = [] # type: List[Any] start_mark = self.reader.get_mark() end_mark = start_mark - indent = self.indent+1 + indent = self.indent + 1 # We allow zero indentation for scalars, but then we need to check for # document separators at the beginning of the line. # if indent == 0: @@ -1399,17 +1399,17 @@ class Scanner(object): while True: ch = self.reader.peek(length) if (ch == u':' and - self.reader.peek(length+1) not in u'\0 \t\r\n\x85\u2028\u2029'): + self.reader.peek(length + 1) not in u'\0 \t\r\n\x85\u2028\u2029'): pass elif (ch in u'\0 \t\r\n\x85\u2028\u2029' or (not self.flow_level and ch == u':' and - self.reader.peek(length+1) in u'\0 \t\r\n\x85\u2028\u2029') or + self.reader.peek(length + 1) in u'\0 \t\r\n\x85\u2028\u2029') or (self.flow_level and ch in u',:?[]{}')): break length += 1 # It's not clear what we should do with ':' in the flow context. if (self.flow_level and ch == u':' and - self.reader.peek(length+1) not in u'\0 \t\r\n\x85\u2028\u2029,[]{}'): + self.reader.peek(length + 1) not in u'\0 \t\r\n\x85\u2028\u2029,[]{}'): self.reader.forward(length) raise ScannerError( "while scanning a plain scalar", start_mark, @@ -1647,12 +1647,8 @@ class RoundTripScanner(Scanner): # hidden streamtokens could get them (leave them and they will be # pre comments for the next map/seq if len(self.tokens) > 1 and \ - isinstance(self.tokens[0], ( - ScalarToken, - ValueToken, - FlowSequenceEndToken, - FlowMappingEndToken, - )) and \ + isinstance(self.tokens[0], (ScalarToken, ValueToken, + FlowSequenceEndToken, FlowMappingEndToken, )) and \ isinstance(self.tokens[1], CommentToken) and \ self.tokens[0].end_mark.line == self.tokens[1].start_mark.line: self.tokens_taken += 1 diff --git a/util.py b/util.py index b7be3e1..9f939cf 100644 --- a/util.py +++ b/util.py @@ -126,7 +126,7 @@ def _walk_section(s, level=0): if c: line += u' ' + c yield line - for val in _walk_section(s[name], level=level+1): + for val in _walk_section(s[name], level=level + 1): yield val # def config_obj_2_rt_yaml(cfg): -- cgit v1.2.1