| 1 | |
|---|
| 2 | __all__ = ['BaseResolver', 'Resolver'] |
|---|
| 3 | |
|---|
| 4 | from error import * |
|---|
| 5 | from nodes import * |
|---|
| 6 | |
|---|
| 7 | import re |
|---|
| 8 | |
|---|
| 9 | class ResolverError(YAMLError): |
|---|
| 10 | pass |
|---|
| 11 | |
|---|
| 12 | class BaseResolver: |
|---|
| 13 | |
|---|
| 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' |
|---|
| 17 | |
|---|
| 18 | yaml_implicit_resolvers = {} |
|---|
| 19 | yaml_path_resolvers = {} |
|---|
| 20 | |
|---|
| 21 | def __init__(self): |
|---|
| 22 | self.resolver_exact_paths = [] |
|---|
| 23 | self.resolver_prefix_paths = [] |
|---|
| 24 | |
|---|
| 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() |
|---|
| 28 | for ch in first: |
|---|
| 29 | cls.yaml_implicit_resolvers.setdefault(ch, []).append((tag, regexp)) |
|---|
| 30 | add_implicit_resolver = classmethod(add_implicit_resolver) |
|---|
| 31 | |
|---|
| 32 | def add_path_resolver(cls, tag, path, kind=None): |
|---|
| 33 | if not 'yaml_path_resolvers' in cls.__dict__: |
|---|
| 34 | cls.yaml_path_resolvers = cls.yaml_path_resolvers.copy() |
|---|
| 35 | new_path = [] |
|---|
| 36 | for element in path: |
|---|
| 37 | if isinstance(element, (list, tuple)): |
|---|
| 38 | if len(element) == 2: |
|---|
| 39 | node_check, index_check = element |
|---|
| 40 | elif len(element) == 1: |
|---|
| 41 | node_check = element[0] |
|---|
| 42 | index_check = True |
|---|
| 43 | else: |
|---|
| 44 | raise ResolverError("Invalid path element: %s" % element) |
|---|
| 45 | else: |
|---|
| 46 | node_check = None |
|---|
| 47 | index_check = element |
|---|
| 48 | if node_check is str: |
|---|
| 49 | node_check = ScalarNode |
|---|
| 50 | elif node_check is list: |
|---|
| 51 | node_check = SequenceNode |
|---|
| 52 | elif node_check is map: |
|---|
| 53 | node_check = MappingNode |
|---|
| 54 | elif node_check not in [ScalarNode, SequenceNode, MappingNode] \ |
|---|
| 55 | and not isinstance(node_check, basestring) \ |
|---|
| 56 | and node_check is not None: |
|---|
| 57 | raise ResolverError("Invalid node checker: %s" % node_check) |
|---|
| 58 | if not isinstance(index_check, (basestring, int)) \ |
|---|
| 59 | and index_check is not None: |
|---|
| 60 | raise ResolverError("Invalid index checker: %s" % index_check) |
|---|
| 61 | new_path.append((node_check, index_check)) |
|---|
| 62 | if kind is str: |
|---|
| 63 | kind = ScalarNode |
|---|
| 64 | elif kind is list: |
|---|
| 65 | kind = SequenceNode |
|---|
| 66 | elif kind is map: |
|---|
| 67 | kind = MappingNode |
|---|
| 68 | elif kind not in [ScalarNode, SequenceNode, MappingNode] \ |
|---|
| 69 | and kind is not None: |
|---|
| 70 | raise ResolverError("Invalid node kind: %s" % kind) |
|---|
| 71 | cls.yaml_path_resolvers[tuple(new_path), kind] = tag |
|---|
| 72 | add_path_resolver = classmethod(add_path_resolver) |
|---|
| 73 | |
|---|
| 74 | def descend_resolver(self, current_node, current_index): |
|---|
| 75 | exact_paths = {} |
|---|
| 76 | prefix_paths = [] |
|---|
| 77 | if current_node: |
|---|
| 78 | depth = len(self.resolver_prefix_paths) |
|---|
| 79 | for path, kind in self.resolver_prefix_paths[-1]: |
|---|
| 80 | if self.check_resolver_prefix(depth, path, kind, |
|---|
| 81 | current_node, current_index): |
|---|
| 82 | if len(path) > depth: |
|---|
| 83 | prefix_paths.append((path, kind)) |
|---|
| 84 | else: |
|---|
| 85 | exact_paths[kind] = self.yaml_path_resolvers[path, kind] |
|---|
| 86 | else: |
|---|
| 87 | for path, kind in self.yaml_path_resolvers: |
|---|
| 88 | if not path: |
|---|
| 89 | exact_paths[kind] = self.yaml_path_resolvers[path, kind] |
|---|
| 90 | else: |
|---|
| 91 | prefix_paths.append((path, kind)) |
|---|
| 92 | self.resolver_exact_paths.append(exact_paths) |
|---|
| 93 | self.resolver_prefix_paths.append(prefix_paths) |
|---|
| 94 | |
|---|
| 95 | def ascend_resolver(self): |
|---|
| 96 | self.resolver_exact_paths.pop() |
|---|
| 97 | self.resolver_prefix_paths.pop() |
|---|
| 98 | |
|---|
| 99 | def check_resolver_prefix(self, depth, path, kind, |
|---|
| 100 | current_node, current_index): |
|---|
| 101 | node_check, index_check = path[depth-1] |
|---|
| 102 | if isinstance(node_check, basestring): |
|---|
| 103 | if current_node.tag != node_check: |
|---|
| 104 | return |
|---|
| 105 | elif node_check is not None: |
|---|
| 106 | if not isinstance(current_node, node_check): |
|---|
| 107 | return |
|---|
| 108 | if index_check is True and current_index is not None: |
|---|
| 109 | return |
|---|
| 110 | if index_check in [False, None] and current_index is None: |
|---|
| 111 | return |
|---|
| 112 | if isinstance(index_check, basestring): |
|---|
| 113 | if not (isinstance(current_index, ScalarNode) |
|---|
| 114 | and index_check == current_index.value): |
|---|
| 115 | return |
|---|
| 116 | elif isinstance(index_check, int): |
|---|
| 117 | if index_check != current_index: |
|---|
| 118 | return |
|---|
| 119 | return True |
|---|
| 120 | |
|---|
| 121 | def resolve(self, kind, value, implicit): |
|---|
| 122 | if kind is ScalarNode and implicit[0]: |
|---|
| 123 | if value == u'': |
|---|
| 124 | resolvers = self.yaml_implicit_resolvers.get(u'', []) |
|---|
| 125 | else: |
|---|
| 126 | resolvers = self.yaml_implicit_resolvers.get(value[0], []) |
|---|
| 127 | resolvers += self.yaml_implicit_resolvers.get(None, []) |
|---|
| 128 | for tag, regexp in resolvers: |
|---|
| 129 | if regexp.match(value): |
|---|
| 130 | return tag |
|---|
| 131 | implicit = implicit[1] |
|---|
| 132 | exact_paths = self.resolver_exact_paths[-1] |
|---|
| 133 | if kind in exact_paths: |
|---|
| 134 | return exact_paths[kind] |
|---|
| 135 | if None in exact_paths: |
|---|
| 136 | return exact_paths[None] |
|---|
| 137 | if kind is ScalarNode: |
|---|
| 138 | return self.DEFAULT_SCALAR_TAG |
|---|
| 139 | elif kind is SequenceNode: |
|---|
| 140 | return self.DEFAULT_SEQUENCE_TAG |
|---|
| 141 | elif kind is MappingNode: |
|---|
| 142 | return self.DEFAULT_MAPPING_TAG |
|---|
| 143 | |
|---|
| 144 | class Resolver(BaseResolver): |
|---|
| 145 | pass |
|---|
| 146 | |
|---|
| 147 | Resolver.add_implicit_resolver( |
|---|
| 148 | u'tag:yaml.org,2002:bool', |
|---|
| 149 | re.compile(ur'''^(?:yes|Yes|YES|n|N|no|No|NO |
|---|
| 150 | |true|True|TRUE|false|False|FALSE |
|---|
| 151 | |on|On|ON|off|Off|OFF)$''', re.X), |
|---|
| 152 | list(u'yYnNtTfFoO')) |
|---|
| 153 | |
|---|
| 154 | Resolver.add_implicit_resolver( |
|---|
| 155 | u'tag:yaml.org,2002:float', |
|---|
| 156 | re.compile(ur'''^(?:[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*(?:[eE][-+][0-9]+)? |
|---|
| 157 | |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]* |
|---|
| 158 | |[-+]?\.(?:inf|Inf|INF) |
|---|
| 159 | |\.(?:nan|NaN|NAN))$''', re.X), |
|---|
| 160 | list(u'-+0123456789.')) |
|---|
| 161 | |
|---|
| 162 | Resolver.add_implicit_resolver( |
|---|
| 163 | u'tag:yaml.org,2002:int', |
|---|
| 164 | re.compile(ur'''^(?:[-+]?0b[0-1_]+ |
|---|
| 165 | |[-+]?0[0-7_]+ |
|---|
| 166 | |[-+]?(?:0|[1-9][0-9_]*) |
|---|
| 167 | |[-+]?0x[0-9a-fA-F_]+ |
|---|
| 168 | |[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re.X), |
|---|
| 169 | list(u'-+0123456789')) |
|---|
| 170 | |
|---|
| 171 | Resolver.add_implicit_resolver( |
|---|
| 172 | u'tag:yaml.org,2002:merge', |
|---|
| 173 | re.compile(ur'^(?:<<)$'), |
|---|
| 174 | ['<']) |
|---|
| 175 | |
|---|
| 176 | Resolver.add_implicit_resolver( |
|---|
| 177 | u'tag:yaml.org,2002:null', |
|---|
| 178 | re.compile(ur'''^(?: ~ |
|---|
| 179 | |null|Null|NULL |
|---|
| 180 | | )$''', re.X), |
|---|
| 181 | [u'~', u'n', u'N', u'']) |
|---|
| 182 | |
|---|
| 183 | Resolver.add_implicit_resolver( |
|---|
| 184 | u'tag:yaml.org,2002:timestamp', |
|---|
| 185 | re.compile(ur'''^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] |
|---|
| 186 | |[0-9][0-9][0-9][0-9] -[0-9][0-9]? -[0-9][0-9]? |
|---|
| 187 | (?:[Tt]|[ \t]+)[0-9][0-9]? |
|---|
| 188 | :[0-9][0-9] :[0-9][0-9] (?:\.[0-9]*)? |
|---|
| 189 | (?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$''', re.X), |
|---|
| 190 | list(u'0123456789')) |
|---|
| 191 | |
|---|
| 192 | Resolver.add_implicit_resolver( |
|---|
| 193 | u'tag:yaml.org,2002:value', |
|---|
| 194 | re.compile(ur'^(?:=)$'), |
|---|
| 195 | ['=']) |
|---|
| 196 | |
|---|
| 197 | # The following resolver is only for documentation purposes. It cannot work |
|---|
| 198 | # because plain scalars cannot start with '!', '&', or '*'. |
|---|
| 199 | Resolver.add_implicit_resolver( |
|---|
| 200 | u'tag:yaml.org,2002:yaml', |
|---|
| 201 | re.compile(ur'^(?:!|&|\*)$'), |
|---|
| 202 | list(u'!&*')) |
|---|
| 203 | |
|---|