| OLD | NEW |
| (Empty) |
| 1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | |
| 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | |
| 3 # | |
| 4 # This file is part of astroid. | |
| 5 # | |
| 6 # astroid is free software: you can redistribute it and/or modify it | |
| 7 # under the terms of the GNU Lesser General Public License as published by the | |
| 8 # Free Software Foundation, either version 2.1 of the License, or (at your | |
| 9 # option) any later version. | |
| 10 # | |
| 11 # astroid is distributed in the hope that it will be useful, but | |
| 12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
| 14 # for more details. | |
| 15 # | |
| 16 # You should have received a copy of the GNU Lesser General Public License along | |
| 17 # with astroid. If not, see <http://www.gnu.org/licenses/>. | |
| 18 """Python Abstract Syntax Tree New Generation | |
| 19 | |
| 20 The aim of this module is to provide a common base representation of | |
| 21 python source code for projects such as pychecker, pyreverse, | |
| 22 pylint... Well, actually the development of this library is essentially | |
| 23 governed by pylint's needs. | |
| 24 | |
| 25 It extends class defined in the python's _ast module with some | |
| 26 additional methods and attributes. Instance attributes are added by a | |
| 27 builder object, which can either generate extended ast (let's call | |
| 28 them astroid ;) by visiting an existent ast tree or by inspecting living | |
| 29 object. Methods are added by monkey patching ast classes. | |
| 30 | |
| 31 Main modules are: | |
| 32 | |
| 33 * nodes and scoped_nodes for more information about methods and | |
| 34 attributes added to different node classes | |
| 35 | |
| 36 * the manager contains a high level object to get astroid trees from | |
| 37 source files and living objects. It maintains a cache of previously | |
| 38 constructed tree for quick access | |
| 39 | |
| 40 * builder contains the class responsible to build astroid trees | |
| 41 """ | |
| 42 __doctype__ = "restructuredtext en" | |
| 43 | |
| 44 import sys | |
| 45 import re | |
| 46 from operator import attrgetter | |
| 47 | |
| 48 # WARNING: internal imports order matters ! | |
| 49 | |
| 50 # make all exception classes accessible from astroid package | |
| 51 from astroid.exceptions import * | |
| 52 | |
| 53 # make all node classes accessible from astroid package | |
| 54 from astroid.nodes import * | |
| 55 | |
| 56 # trigger extra monkey-patching | |
| 57 from astroid import inference | |
| 58 | |
| 59 # more stuff available | |
| 60 from astroid import raw_building | |
| 61 from astroid.bases import YES, Instance, BoundMethod, UnboundMethod | |
| 62 from astroid.node_classes import are_exclusive, unpack_infer | |
| 63 from astroid.scoped_nodes import builtin_lookup | |
| 64 | |
| 65 # make a manager instance (borg) as well as Project and Package classes | |
| 66 # accessible from astroid package | |
| 67 from astroid.manager import AstroidManager, Project | |
| 68 MANAGER = AstroidManager() | |
| 69 del AstroidManager | |
| 70 | |
| 71 # transform utilities (filters and decorator) | |
| 72 | |
| 73 class AsStringRegexpPredicate(object): | |
| 74 """Class to be used as predicate that may be given to `register_transform` | |
| 75 | |
| 76 First argument is a regular expression that will be searched against the `as
_string` | |
| 77 representation of the node onto which it's applied. | |
| 78 | |
| 79 If specified, the second argument is an `attrgetter` expression that will be | |
| 80 applied on the node first to get the actual node on which `as_string` should | |
| 81 be called. | |
| 82 """ | |
| 83 def __init__(self, regexp, expression=None): | |
| 84 self.regexp = re.compile(regexp) | |
| 85 self.expression = expression | |
| 86 | |
| 87 def __call__(self, node): | |
| 88 if self.expression is not None: | |
| 89 node = attrgetter(self.expression)(node) | |
| 90 return self.regexp.search(node.as_string()) | |
| 91 | |
| 92 def inference_tip(infer_function): | |
| 93 """Given an instance specific inference function, return a function to be | |
| 94 given to MANAGER.register_transform to set this inference function. | |
| 95 | |
| 96 Typical usage | |
| 97 | |
| 98 .. sourcecode:: python | |
| 99 | |
| 100 MANAGER.register_transform(CallFunc, inference_tip(infer_named_tuple), | |
| 101 AsStringRegexpPredicate('namedtuple', 'func')) | |
| 102 """ | |
| 103 def transform(node, infer_function=infer_function): | |
| 104 node._explicit_inference = infer_function | |
| 105 return node | |
| 106 return transform | |
| 107 | |
| 108 # load brain plugins | |
| 109 from os import listdir | |
| 110 from os.path import join, dirname | |
| 111 BRAIN_MODULES_DIR = join(dirname(__file__), 'brain') | |
| 112 if BRAIN_MODULES_DIR not in sys.path: | |
| 113 # add it to the end of the list so user path take precedence | |
| 114 sys.path.append(BRAIN_MODULES_DIR) | |
| 115 # load modules in this directory | |
| 116 for module in listdir(BRAIN_MODULES_DIR): | |
| 117 if module.endswith('.py'): | |
| 118 __import__(module[:-3]) | |
| OLD | NEW |