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 if not 'yaml_path_resolvers' in cls
.__dict
__:
36 cls
.yaml_path_resolvers
= cls
.yaml_path_resolvers
.copy()
39 if isinstance(element
, (list, tuple)):
41 node_check
, index_check
= element
42 elif len(element
) == 1:
43 node_check
= element
[0]
46 raise ResolverError("Invalid path element: %s" % element
)
51 node_check
= ScalarNode
52 elif node_check
is list:
53 node_check
= SequenceNode
54 elif node_check
is dict:
55 node_check
= MappingNode
56 elif node_check
not in [ScalarNode
, SequenceNode
, MappingNode
] \
57 and not isinstance(node_check
, basestring
) \
58 and node_check
is not None:
59 raise ResolverError("Invalid node checker: %s" % node_check
)
60 if not isinstance(index_check
, (basestring
, int)) \
61 and index_check
is not None:
62 raise ResolverError("Invalid index checker: %s" % index_check
)
63 new_path
.append((node_check
, index_check
))
70 elif kind
not in [ScalarNode
, SequenceNode
, MappingNode
] \
72 raise ResolverError("Invalid node kind: %s" % kind
)
73 cls
.yaml_path_resolvers
[tuple(new_path
), kind
] = tag
74 add_path_resolver
= classmethod(add_path_resolver
)
76 def descend_resolver(self
, current_node
, current_index
):
80 depth
= len(self
.resolver_prefix_paths
)
81 for path
, kind
in self
.resolver_prefix_paths
[-1]:
82 if self
.check_resolver_prefix(depth
, path
, kind
,
83 current_node
, current_index
):
85 prefix_paths
.append((path
, kind
))
87 exact_paths
[kind
] = self
.yaml_path_resolvers
[path
, kind
]
89 for path
, kind
in self
.yaml_path_resolvers
:
91 exact_paths
[kind
] = self
.yaml_path_resolvers
[path
, kind
]
93 prefix_paths
.append((path
, kind
))
94 self
.resolver_exact_paths
.append(exact_paths
)
95 self
.resolver_prefix_paths
.append(prefix_paths
)
97 def ascend_resolver(self
):
98 self
.resolver_exact_paths
.pop()
99 self
.resolver_prefix_paths
.pop()
101 def check_resolver_prefix(self
, depth
, path
, kind
,
102 current_node
, current_index
):
103 node_check
, index_check
= path
[depth
-1]
104 if isinstance(node_check
, basestring
):
105 if current_node
.tag
!= node_check
:
107 elif node_check
is not None:
108 if not isinstance(current_node
, node_check
):
110 if index_check
is True and current_index
is not None:
112 if index_check
in [False, None] and current_index
is None:
114 if isinstance(index_check
, basestring
):
115 if not (isinstance(current_index
, ScalarNode
)
116 and index_check
== current_index
.value
):
118 elif isinstance(index_check
, int):
119 if index_check
!= current_index
:
123 def resolve(self
, kind
, value
, implicit
):
124 if kind
is ScalarNode
and implicit
[0]:
126 resolvers
= self
.yaml_implicit_resolvers
.get(u
'', [])
128 resolvers
= self
.yaml_implicit_resolvers
.get(value
[0], [])
129 resolvers
+= self
.yaml_implicit_resolvers
.get(None, [])
130 for tag
, regexp
in resolvers
:
131 if regexp
.match(value
):
133 implicit
= implicit
[1]
134 exact_paths
= self
.resolver_exact_paths
[-1]
135 if kind
in exact_paths
:
136 return exact_paths
[kind
]
137 if None in exact_paths
:
138 return exact_paths
[None]
139 if kind
is ScalarNode
:
140 return self
.DEFAULT_SCALAR_TAG
141 elif kind
is SequenceNode
:
142 return self
.DEFAULT_SEQUENCE_TAG
143 elif kind
is MappingNode
:
144 return self
.DEFAULT_MAPPING_TAG
146 class Resolver(BaseResolver
):
149 Resolver
.add_implicit_resolver(
150 u
'tag:yaml.org,2002:bool',
151 re
.compile(ur
'''^(?:yes|Yes|YES|n|N|no|No|NO
152 |true|True|TRUE|false|False|FALSE
153 |on|On|ON|off|Off|OFF)$''', re
.X
),
156 Resolver
.add_implicit_resolver(
157 u
'tag:yaml.org,2002:float',
158 re
.compile(ur
'''^(?:[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*(?:[eE][-+][0-9]+)?
159 |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*
160 |[-+]?\.(?:inf|Inf|INF)
161 |\.(?:nan|NaN|NAN))$''', re
.X
),
162 list(u
'-+0123456789.'))
164 Resolver
.add_implicit_resolver(
165 u
'tag:yaml.org,2002:int',
166 re
.compile(ur
'''^(?:[-+]?0b[0-1_]+
168 |[-+]?(?:0|[1-9][0-9_]*)
169 |[-+]?0x[0-9a-fA-F_]+
170 |[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re
.X
),
171 list(u
'-+0123456789'))
173 Resolver
.add_implicit_resolver(
174 u
'tag:yaml.org,2002:merge',
175 re
.compile(ur
'^(?:<<)$'),
178 Resolver
.add_implicit_resolver(
179 u
'tag:yaml.org,2002:null',
180 re
.compile(ur
'''^(?: ~
183 [u
'~', u
'n', u
'N', u
''])
185 Resolver
.add_implicit_resolver(
186 u
'tag:yaml.org,2002:timestamp',
187 re
.compile(ur
'''^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
188 |[0-9][0-9][0-9][0-9] -[0-9][0-9]? -[0-9][0-9]?
189 (?:[Tt]|[ \t]+)[0-9][0-9]?
190 :[0-9][0-9] :[0-9][0-9] (?:\.[0-9]*)?
191 (?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$''', re
.X
),
194 Resolver
.add_implicit_resolver(
195 u
'tag:yaml.org,2002:value',
196 re
.compile(ur
'^(?:=)$'),
199 # The following resolver is only for documentation purposes. It cannot work
200 # because plain scalars cannot start with '!', '&', or '*'.
201 Resolver
.add_implicit_resolver(
202 u
'tag:yaml.org,2002:yaml',
203 re
.compile(ur
'^(?:!|&|\*)$'),