Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
resolver.py
Go to the documentation of this file.
1 # (c) Copyright Rosetta Commons Member Institutions.
2 # (c) This file is part of the Rosetta software suite and is made available under license.
3 # (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
4 # (c) For more information, see http://www.rosettacommons.org. Questions about this can be
5 # (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
6 
7 __all__ = ['BaseResolver', 'Resolver']
8 
9 from error import *
10 from nodes import *
11 
12 import re
13 
15  pass
16 
17 class BaseResolver(object):
18 
19  DEFAULT_SCALAR_TAG = u'tag:yaml.org,2002:str'
20  DEFAULT_SEQUENCE_TAG = u'tag:yaml.org,2002:seq'
21  DEFAULT_MAPPING_TAG = u'tag:yaml.org,2002:map'
22 
23  yaml_implicit_resolvers = {}
24  yaml_path_resolvers = {}
25 
26  def __init__(self):
29 
30  def add_implicit_resolver(cls, tag, regexp, first):
31  if not 'yaml_implicit_resolvers' in cls.__dict__:
32  cls.yaml_implicit_resolvers = cls.yaml_implicit_resolvers.copy()
33  if first is None:
34  first = [None]
35  for ch in first:
36  cls.yaml_implicit_resolvers.setdefault(ch, []).append((tag, regexp))
37  add_implicit_resolver = classmethod(add_implicit_resolver)
38 
39  def add_path_resolver(cls, tag, path, kind=None):
40  # Note: `add_path_resolver` is experimental. The API could be changed.
41  # `new_path` is a pattern that is matched against the path from the
42  # root to the node that is being considered. `node_path` elements are
43  # tuples `(node_check, index_check)`. `node_check` is a node class:
44  # `ScalarNode`, `SequenceNode`, `MappingNode` or `None`. `None`
45  # matches any kind of a node. `index_check` could be `None`, a boolean
46  # value, a string value, or a number. `None` and `False` match against
47  # any _value_ of sequence and mapping nodes. `True` matches against
48  # any _key_ of a mapping node. A string `index_check` matches against
49  # a mapping value that corresponds to a scalar key which content is
50  # equal to the `index_check` value. An integer `index_check` matches
51  # against a sequence value with the index equal to `index_check`.
52  if not 'yaml_path_resolvers' in cls.__dict__:
53  cls.yaml_path_resolvers = cls.yaml_path_resolvers.copy()
54  new_path = []
55  for element in path:
56  if isinstance(element, (list, tuple)):
57  if len(element) == 2:
58  node_check, index_check = element
59  elif len(element) == 1:
60  node_check = element[0]
61  index_check = True
62  else:
63  raise ResolverError("Invalid path element: %s" % element)
64  else:
65  node_check = None
66  index_check = element
67  if node_check is str:
68  node_check = ScalarNode
69  elif node_check is list:
70  node_check = SequenceNode
71  elif node_check is dict:
72  node_check = MappingNode
73  elif node_check not in [ScalarNode, SequenceNode, MappingNode] \
74  and not isinstance(node_check, basestring) \
75  and node_check is not None:
76  raise ResolverError("Invalid node checker: %s" % node_check)
77  if not isinstance(index_check, (basestring, int)) \
78  and index_check is not None:
79  raise ResolverError("Invalid index checker: %s" % index_check)
80  new_path.append((node_check, index_check))
81  if kind is str:
82  kind = ScalarNode
83  elif kind is list:
84  kind = SequenceNode
85  elif kind is dict:
86  kind = MappingNode
87  elif kind not in [ScalarNode, SequenceNode, MappingNode] \
88  and kind is not None:
89  raise ResolverError("Invalid node kind: %s" % kind)
90  cls.yaml_path_resolvers[tuple(new_path), kind] = tag
91  add_path_resolver = classmethod(add_path_resolver)
92 
93  def descend_resolver(self, current_node, current_index):
94  if not self.yaml_path_resolvers:
95  return
96  exact_paths = {}
97  prefix_paths = []
98  if current_node:
99  depth = len(self.resolver_prefix_paths)
100  for path, kind in self.resolver_prefix_paths[-1]:
101  if self.check_resolver_prefix(depth, path, kind,
102  current_node, current_index):
103  if len(path) > depth:
104  prefix_paths.append((path, kind))
105  else:
106  exact_paths[kind] = self.yaml_path_resolvers[path, kind]
107  else:
108  for path, kind in self.yaml_path_resolvers:
109  if not path:
110  exact_paths[kind] = self.yaml_path_resolvers[path, kind]
111  else:
112  prefix_paths.append((path, kind))
113  self.resolver_exact_paths.append(exact_paths)
114  self.resolver_prefix_paths.append(prefix_paths)
115 
116  def ascend_resolver(self):
117  if not self.yaml_path_resolvers:
118  return
119  self.resolver_exact_paths.pop()
120  self.resolver_prefix_paths.pop()
121 
122  def check_resolver_prefix(self, depth, path, kind,
123  current_node, current_index):
124  node_check, index_check = path[depth-1]
125  if isinstance(node_check, basestring):
126  if current_node.tag != node_check:
127  return
128  elif node_check is not None:
129  if not isinstance(current_node, node_check):
130  return
131  if index_check is True and current_index is not None:
132  return
133  if (index_check is False or index_check is None) \
134  and current_index is None:
135  return
136  if isinstance(index_check, basestring):
137  if not (isinstance(current_index, ScalarNode)
138  and index_check == current_index.value):
139  return
140  elif isinstance(index_check, int) and not isinstance(index_check, bool):
141  if index_check != current_index:
142  return
143  return True
144 
145  def resolve(self, kind, value, implicit):
146  if kind is ScalarNode and implicit[0]:
147  if value == u'':
148  resolvers = self.yaml_implicit_resolvers.get(u'', [])
149  else:
150  resolvers = self.yaml_implicit_resolvers.get(value[0], [])
151  resolvers += self.yaml_implicit_resolvers.get(None, [])
152  for tag, regexp in resolvers:
153  if regexp.match(value):
154  return tag
155  implicit = implicit[1]
156  if self.yaml_path_resolvers:
157  exact_paths = self.resolver_exact_paths[-1]
158  if kind in exact_paths:
159  return exact_paths[kind]
160  if None in exact_paths:
161  return exact_paths[None]
162  if kind is ScalarNode:
163  return self.DEFAULT_SCALAR_TAG
164  elif kind is SequenceNode:
165  return self.DEFAULT_SEQUENCE_TAG
166  elif kind is MappingNode:
167  return self.DEFAULT_MAPPING_TAG
168 
170  pass
171 
172 Resolver.add_implicit_resolver(
173  u'tag:yaml.org,2002:bool',
174  re.compile(ur'''^(?:yes|Yes|YES|no|No|NO
175  |true|True|TRUE|false|False|FALSE
176  |on|On|ON|off|Off|OFF)$''', re.X),
177  list(u'yYnNtTfFoO'))
178 
179 Resolver.add_implicit_resolver(
180  u'tag:yaml.org,2002:float',
181  re.compile(ur'''^(?:[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*(?:[eE][-+][0-9]+)?
182  |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*
183  |[-+]?\.(?:inf|Inf|INF)
184  |\.(?:nan|NaN|NAN))$''', re.X),
185  list(u'-+0123456789.'))
186 
187 Resolver.add_implicit_resolver(
188  u'tag:yaml.org,2002:int',
189  re.compile(ur'''^(?:[-+]?0b[0-1_]+
190  |[-+]?0[0-7_]+
191  |[-+]?(?:0|[1-9][0-9_]*)
192  |[-+]?0x[0-9a-fA-F_]+
193  |[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re.X),
194  list(u'-+0123456789'))
195 
196 Resolver.add_implicit_resolver(
197  u'tag:yaml.org,2002:merge',
198  re.compile(ur'^(?:<<)$'),
199  ['<'])
200 
201 Resolver.add_implicit_resolver(
202  u'tag:yaml.org,2002:null',
203  re.compile(ur'''^(?: ~
204  |null|Null|NULL
205  | )$''', re.X),
206  [u'~', u'n', u'N', u''])
207 
208 Resolver.add_implicit_resolver(
209  u'tag:yaml.org,2002:timestamp',
210  re.compile(ur'''^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
211  |[0-9][0-9][0-9][0-9] -[0-9][0-9]? -[0-9][0-9]?
212  (?:[Tt]|[ \t]+)[0-9][0-9]?
213  :[0-9][0-9] :[0-9][0-9] (?:\.[0-9]*)?
214  (?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$''', re.X),
215  list(u'0123456789'))
216 
217 Resolver.add_implicit_resolver(
218  u'tag:yaml.org,2002:value',
219  re.compile(ur'^(?:=)$'),
220  ['='])
221 
222 # The following resolver is only for documentation purposes. It cannot work
223 # because plain scalars cannot start with '!', '&', or '*'.
224 Resolver.add_implicit_resolver(
225  u'tag:yaml.org,2002:yaml',
226  re.compile(ur'^(?:!|&|\*)$'),
227  list(u'!&*'))
228 
Fstring::size_type len(Fstring const &s)
Length.
Definition: Fstring.hh:2207
dictionary yaml_path_resolvers
Definition: resolver.py:24