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