getter for Parser outputs
(self, Parser, ext_attr, tree)
| 294 | |
| 295 | # add methods for retrieving parser outputs -------------------------- |
| 296 | def _getx(self, Parser, ext_attr, tree): |
| 297 | """getter for Parser outputs""" |
| 298 | # return cached output if possible |
| 299 | cache_key = Parser.__name__ + str(hash(tree)) |
| 300 | if self._parser_cache.get(cache_key): |
| 301 | p = self._parser_cache[cache_key] |
| 302 | else: |
| 303 | # otherwise, run parser over tree |
| 304 | p = Parser() |
| 305 | # set mappings for parsers that inspect attribute access |
| 306 | if ext_attr != "mappings" and Parser in [ |
| 307 | FunctionParser, |
| 308 | ObjectAccessParser, |
| 309 | ]: |
| 310 | p.mappings = self.context_mappings.copy() |
| 311 | # run parser |
| 312 | p.visit(tree) |
| 313 | # cache |
| 314 | self._parser_cache[cache_key] = p |
| 315 | return getattr(p, ext_attr) |
| 316 | |
| 317 | |
| 318 | # put a function on the dispatcher |