summaryrefslogtreecommitdiff
path: root/resolver.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2018-08-03 22:14:57 +0200
committerAnthon van der Neut <anthon@mnt.org>2018-08-03 22:14:57 +0200
commitdce10fcff1de54121fb8b440b883ef5d3fe2f96a (patch)
tree072b4bd247e6f1cd95c08c7b67fea0fc96f0578e /resolver.py
parent2966a4f215861fa05e0dc7e0cd53350766e794c6 (diff)
downloadruamel.yaml-dce10fcff1de54121fb8b440b883ef5d3fe2f96a.tar.gz
Apply oitnb and mypy 0.620, then make everything work again0.15.48
Diffstat (limited to 'resolver.py')
-rw-r--r--resolver.py86
1 files changed, 43 insertions, 43 deletions
diff --git a/resolver.py b/resolver.py
index 905bebe..6b8c56e 100644
--- a/resolver.py
+++ b/resolver.py
@@ -5,17 +5,18 @@ from __future__ import absolute_import
import re
if False: # MYPY
- from typing import Any, Dict, List, Union # NOQA
+ from typing import Any, Dict, List, Union, Text # NOQA
from ruamel.yaml.compat import VersionType # NOQA
from ruamel.yaml.compat import string_types, _DEFAULT_YAML_VERSION # NOQA
-from ruamel.yaml.error import * # NOQA
-from ruamel.yaml.nodes import * # NOQA
+from ruamel.yaml.error import * # NOQA
+from ruamel.yaml.nodes import MappingNode, ScalarNode, SequenceNode # NOQA
from ruamel.yaml.util import RegExp # NOQA
__all__ = ['BaseResolver', 'Resolver', 'VersionedResolver']
+# fmt: off
# resolvers consist of
# - a list of applicable version
# - a tag
@@ -95,6 +96,7 @@ implicit_resolvers = [
RegExp(u'^(?:!|&|\\*)$'),
list(u'!&*')),
]
+# fmt: on
class ResolverError(YAMLError):
@@ -108,7 +110,7 @@ class BaseResolver(object):
DEFAULT_MAPPING_TAG = u'tag:yaml.org,2002:map'
yaml_implicit_resolvers = {} # type: Dict[Any, Any]
- yaml_path_resolvers = {} # type: Dict[Any, Any]
+ yaml_path_resolvers = {} # type: Dict[Any, Any]
def __init__(self, loadumper=None):
# type: (Any, Any) -> None
@@ -133,26 +135,26 @@ class BaseResolver(object):
# type: (Any, Any, Any) -> None
if 'yaml_implicit_resolvers' not in cls.__dict__:
# deepcopy doesn't work here
- cls.yaml_implicit_resolvers = dict((k, cls.yaml_implicit_resolvers[k][:])
- for k in cls.yaml_implicit_resolvers)
+ cls.yaml_implicit_resolvers = dict(
+ (k, cls.yaml_implicit_resolvers[k][:]) for k in cls.yaml_implicit_resolvers
+ )
if first is None:
first = [None]
for ch in first:
- cls.yaml_implicit_resolvers.setdefault(ch, []).append(
- (tag, regexp))
+ cls.yaml_implicit_resolvers.setdefault(ch, []).append((tag, regexp))
@classmethod
def add_implicit_resolver(cls, tag, regexp, first):
# type: (Any, Any, Any) -> None
if 'yaml_implicit_resolvers' not in cls.__dict__:
# deepcopy doesn't work here
- cls.yaml_implicit_resolvers = dict((k, cls.yaml_implicit_resolvers[k][:])
- for k in cls.yaml_implicit_resolvers)
+ cls.yaml_implicit_resolvers = dict(
+ (k, cls.yaml_implicit_resolvers[k][:]) for k in cls.yaml_implicit_resolvers
+ )
if first is None:
first = [None]
for ch in first:
- cls.yaml_implicit_resolvers.setdefault(ch, []).append(
- (tag, regexp))
+ cls.yaml_implicit_resolvers.setdefault(ch, []).append((tag, regexp))
implicit_resolvers.append(([(1, 2), (1, 1)], tag, regexp, first))
# @classmethod
@@ -179,12 +181,12 @@ class BaseResolver(object):
for element in path:
if isinstance(element, (list, tuple)):
if len(element) == 2:
- node_check, index_check = element # type: ignore
+ node_check, index_check = element
elif len(element) == 1:
node_check = element[0]
index_check = True
else:
- raise ResolverError("Invalid path element: %s" % element)
+ raise ResolverError('Invalid path element: %s' % element)
else:
node_check = None
index_check = element
@@ -194,13 +196,14 @@ class BaseResolver(object):
node_check = SequenceNode
elif node_check is dict:
node_check = MappingNode
- elif node_check not in [ScalarNode, SequenceNode, MappingNode] \
- and not isinstance(node_check, string_types) \
- and node_check is not None:
- raise ResolverError("Invalid node checker: %s" % node_check)
- if not isinstance(index_check, (string_types, int)) \
- and index_check is not None:
- raise ResolverError("Invalid index checker: %s" % index_check)
+ elif (
+ node_check not in [ScalarNode, SequenceNode, MappingNode]
+ and not isinstance(node_check, string_types)
+ and node_check is not None
+ ):
+ raise ResolverError('Invalid node checker: %s' % node_check)
+ if not isinstance(index_check, (string_types, int)) and index_check is not None:
+ raise ResolverError('Invalid index checker: %s' % index_check)
new_path.append((node_check, index_check))
if kind is str:
kind = ScalarNode
@@ -208,9 +211,8 @@ class BaseResolver(object):
kind = SequenceNode
elif kind is dict:
kind = MappingNode
- elif kind not in [ScalarNode, SequenceNode, MappingNode] \
- and kind is not None:
- raise ResolverError("Invalid node kind: %s" % kind)
+ elif kind not in [ScalarNode, SequenceNode, MappingNode] and kind is not None:
+ raise ResolverError('Invalid node kind: %s' % kind)
cls.yaml_path_resolvers[tuple(new_path), kind] = tag
def descend_resolver(self, current_node, current_index):
@@ -224,13 +226,11 @@ class BaseResolver(object):
if current_node:
depth = len(self.resolver_prefix_paths)
for path, kind in self.resolver_prefix_paths[-1]:
- if self.check_resolver_prefix(depth, path, kind,
- current_node, current_index):
+ if self.check_resolver_prefix(depth, path, kind, current_node, current_index):
if len(path) > depth:
prefix_paths.append((path, kind))
else:
- exact_paths[kind] = self.yaml_path_resolvers[path,
- kind]
+ exact_paths[kind] = self.yaml_path_resolvers[path, kind]
else:
for path, kind in self.yaml_path_resolvers:
if not path:
@@ -247,8 +247,7 @@ class BaseResolver(object):
self.resolver_exact_paths.pop()
self.resolver_prefix_paths.pop()
- def check_resolver_prefix(self, depth, path, kind,
- current_node, current_index):
+ 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]
if isinstance(node_check, string_types):
@@ -259,15 +258,14 @@ class BaseResolver(object):
return False
if index_check is True and current_index is not None:
return False
- if (index_check is False or index_check is None) \
- and current_index is None:
+ if (index_check is False or index_check is None) and current_index is None:
return False
if isinstance(index_check, string_types):
- if not (isinstance(current_index, ScalarNode) and
- index_check == current_index.value):
+ if not (
+ isinstance(current_index, ScalarNode) and index_check == current_index.value
+ ):
return False
- elif isinstance(index_check, int) and not isinstance(index_check,
- bool):
+ elif isinstance(index_check, int) and not isinstance(index_check, bool):
if index_check != current_index:
return False
return True
@@ -275,8 +273,8 @@ class BaseResolver(object):
def resolve(self, kind, value, implicit):
# type: (Any, Any, Any) -> Any
if kind is ScalarNode and implicit[0]:
- if value == u'':
- resolvers = self.yaml_implicit_resolvers.get(u'', [])
+ if value == "":
+ resolvers = self.yaml_implicit_resolvers.get("", [])
else:
resolvers = self.yaml_implicit_resolvers.get(value[0], [])
resolvers += self.yaml_implicit_resolvers.get(None, [])
@@ -307,6 +305,7 @@ class Resolver(BaseResolver):
pass
+# fmt: off
Resolver.add_implicit_resolver_base(
u'tag:yaml.org,2002:bool',
RegExp(u'''^(?:yes|Yes|YES|no|No|NO
@@ -366,6 +365,7 @@ Resolver.add_implicit_resolver_base(
u'tag:yaml.org,2002:yaml',
RegExp(u'^(?:!|&|\\*)$'),
list(u'!&*'))
+# fmt: on
class VersionedResolver(BaseResolver):
@@ -377,7 +377,7 @@ class VersionedResolver(BaseResolver):
"""
def __init__(self, version=None, loader=None):
- # type: (VersionType, Any) -> None
+ # type: (Union[None, VersionType], Any) -> None
BaseResolver.__init__(self, loader)
self._loader_version = self.get_loader_version(version)
self._version_implicit_resolver = {} # type: Dict[Any, Any]
@@ -415,8 +415,8 @@ class VersionedResolver(BaseResolver):
def resolve(self, kind, value, implicit):
# type: (Any, Any, Any) -> Any
if kind is ScalarNode and implicit[0]:
- if value == u'':
- resolvers = self.versioned_resolver.get(u'', [])
+ if value == "":
+ resolvers = self.versioned_resolver.get("", [])
else:
resolvers = self.versioned_resolver.get(value[0], [])
resolvers += self.versioned_resolver.get(None, [])
@@ -444,9 +444,9 @@ class VersionedResolver(BaseResolver):
version = self.parser.yaml_version
except AttributeError:
if hasattr(self.loadumper, 'typ'):
- version = self.loadumper.version # type: ignore
+ version = self.loadumper.version
else:
- version = self.loadumper._serializer.use_version # type: ignore # dumping
+ version = self.loadumper._serializer.use_version # dumping
if version is None:
version = self._loader_version
if version is None: