2 __all__ = ['BaseResolver', 'Resolver']
9 class ResolverError(YAMLError):
12 class BaseResolver(object):
14 DEFAULT_SCALAR_TAG = u'tag:yaml.org,2002:str'
15 DEFAULT_SEQUENCE_TAG = u'tag:yaml.org,2002:seq'
16 DEFAULT_MAPPING_TAG = u'tag:yaml.org,2002:map'
18 yaml_implicit_resolvers = {}
19 yaml_path_resolvers = {}
22 self.resolver_exact_paths = []
23 self.resolver_prefix_paths = []
25 def add_implicit_resolver(cls, tag, regexp, first):
26 if not 'yaml_implicit_resolvers' in cls.__dict__:
27 cls.yaml_implicit_resolvers = cls.yaml_implicit_resolvers.copy()
31 cls.yaml_implicit_resolvers.setdefault(ch, []).append((tag, regexp))
32 add_implicit_resolver = classmethod(add_implicit_resolver)
34 def add_path_resolver(cls, tag, path, kind=None):
35 # Note: `add_path_resolver` is experimental. The API could be changed.
36 # `new_path` is a pattern that is matched against the path from the
37 # root to the node that is being considered. `node_path` elements are
38 # tuples `(node_check, index_check)`. `node_check` is a node class:
39 # `ScalarNode`, `SequenceNode`, `MappingNode` or `None`. `None`
40 # matches any kind of a node. `index_check` could be `None`, a boolean
41 # value, a string value, or a number. `None` and `False` match against
42 # any _value_ of sequence and mapping nodes. `True` matches against
43 # any _key_ of a mapping node. A string `index_check` matches against
44 # a mapping value that corresponds to a scalar key which content is
45 # equal to the `index_check` value. An integer `index_check` matches
46 # against a sequence value with the index equal to `index_check`.
47 if not 'yaml_path_resolvers' in cls.__dict__:
48 cls.yaml_path_resolvers = cls.yaml_path_resolvers.copy()
51 if isinstance(element, (list, tuple)):
53 node_check, index_check = element
54 elif len(element) == 1:
55 node_check = element[0]
58 raise ResolverError("Invalid path element: %s" % element)
63 node_check = ScalarNode
64 elif node_check is list:
65 node_check = SequenceNode
66 elif node_check is dict:
67 node_check = MappingNode
68 elif node_check not in [ScalarNode, SequenceNode, MappingNode] \
69 and not isinstance(node_check, basestring) \
70 and node_check is not None:
71 raise ResolverError("Invalid node checker: %s" % node_check)
72 if not isinstance(index_check, (basestring, int)) \
73 and index_check is not None:
74 raise ResolverError("Invalid index checker: %s" % index_check)
75 new_path.append((node_check, index_check))
82 elif kind not in [ScalarNode, SequenceNode, MappingNode] \
84 raise ResolverError("Invalid node kind: %s" % kind)
85 cls.yaml_path_resolvers[tuple(new_path), kind] = tag
86 add_path_resolver = classmethod(add_path_resolver)
88 def descend_resolver(self, current_node, current_index):
89 if not self.yaml_path_resolvers:
94 depth = len(self.resolver_prefix_paths)
95 for path, kind in self.resolver_prefix_paths[-1]:
96 if self.check_resolver_prefix(depth, path, kind,
97 current_node, current_index):
99 prefix_paths.append((path, kind))
101 exact_paths[kind] = self.yaml_path_resolvers[path, kind]
103 for path, kind in self.yaml_path_resolvers:
105 exact_paths[kind] = self.yaml_path_resolvers[path, kind]
107 prefix_paths.append((path, kind))
108 self.resolver_exact_paths.append(exact_paths)
109 self.resolver_prefix_paths.append(prefix_paths)
111 def ascend_resolver(self):
112 if not self.yaml_path_resolvers:
114 self.resolver_exact_paths.pop()
115 self.resolver_prefix_paths.pop()
117 def check_resolver_prefix(self, depth, path, kind,
118 current_node, current_index):
119 node_check, index_check = path[depth-1]
120 if isinstance(node_check, basestring):
121 if current_node.tag != node_check:
123 elif node_check is not None:
124 if not isinstance(current_node, node_check):
126 if index_check is True and current_index is not None:
128 if (index_check is False or index_check is None) \
129 and current_index is None:
131 if isinstance(index_check, basestring):
132 if not (isinstance(current_index, ScalarNode)
133 and index_check == current_index.value):
135 elif isinstance(index_check, int) and not isinstance(index_check, bool):
136 if index_check != current_index:
140 def resolve(self, kind, value, implicit):
141 if kind is ScalarNode and implicit[0]:
143 resolvers = self.yaml_implicit_resolvers.get(u'', [])
145 resolvers = self.yaml_implicit_resolvers.get(value[0], [])
146 resolvers += self.yaml_implicit_resolvers.get(None, [])
147 for tag, regexp in resolvers:
148 if regexp.match(value):
150 implicit = implicit[1]
151 if self.yaml_path_resolvers:
152 exact_paths = self.resolver_exact_paths[-1]
153 if kind in exact_paths:
154 return exact_paths[kind]
155 if None in exact_paths:
156 return exact_paths[None]
157 if kind is ScalarNode:
158 return self.DEFAULT_SCALAR_TAG
159 elif kind is SequenceNode:
160 return self.DEFAULT_SEQUENCE_TAG
161 elif kind is MappingNode:
162 return self.DEFAULT_MAPPING_TAG
164 class Resolver(BaseResolver):
167 Resolver.add_implicit_resolver(
168 u'tag:yaml.org,2002:bool',
169 re.compile(ur'''^(?:yes|Yes|YES|no|No|NO
170 |true|True|TRUE|false|False|FALSE
171 |on|On|ON|off|Off|OFF)$''', re.X),
174 Resolver.add_implicit_resolver(
175 u'tag:yaml.org,2002:float',
176 re.compile(ur'''^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+][0-9]+)?
177 |\.[0-9_]+(?:[eE][-+][0-9]+)?
178 |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*
179 |[-+]?\.(?:inf|Inf|INF)
180 |\.(?:nan|NaN|NAN))$''', re.X),
181 list(u'-+0123456789.'))
183 Resolver.add_implicit_resolver(
184 u'tag:yaml.org,2002:int',
185 re.compile(ur'''^(?:[-+]?0b[0-1_]+
187 |[-+]?(?:0|[1-9][0-9_]*)
188 |[-+]?0x[0-9a-fA-F_]+
189 |[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re.X),
190 list(u'-+0123456789'))
192 Resolver.add_implicit_resolver(
193 u'tag:yaml.org,2002:merge',
194 re.compile(ur'^(?:<<)$'),
197 Resolver.add_implicit_resolver(
198 u'tag:yaml.org,2002:null',
199 re.compile(ur'''^(?: ~
202 [u'~', u'n', u'N', u''])
204 Resolver.add_implicit_resolver(
205 u'tag:yaml.org,2002:timestamp',
206 re.compile(ur'''^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
207 |[0-9][0-9][0-9][0-9] -[0-9][0-9]? -[0-9][0-9]?
208 (?:[Tt]|[ \t]+)[0-9][0-9]?
209 :[0-9][0-9] :[0-9][0-9] (?:\.[0-9]*)?
210 (?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$''', re.X),
213 Resolver.add_implicit_resolver(
214 u'tag:yaml.org,2002:value',
215 re.compile(ur'^(?:=)$'),
218 # The following resolver is only for documentation purposes. It cannot work
219 # because plain scalars cannot start with '!', '&', or '*'.
220 Resolver.add_implicit_resolver(
221 u'tag:yaml.org,2002:yaml',
222 re.compile(ur'^(?:!|&|\*)$'),