gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / astroid / __init__.py @ 1227
History | View | Annotate | Download (5.24 KB)
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 Instance, BoundMethod, UnboundMethod |
62 |
from astroid.node_classes import are_exclusive, unpack_infer |
63 |
from astroid.scoped_nodes import builtin_lookup |
64 |
from astroid.builder import parse |
65 |
from astroid.util import YES |
66 |
|
67 |
# make a manager instance (borg) as well as Project and Package classes
|
68 |
# accessible from astroid package
|
69 |
from astroid.manager import AstroidManager |
70 |
MANAGER = AstroidManager() |
71 |
del AstroidManager
|
72 |
|
73 |
# transform utilities (filters and decorator)
|
74 |
|
75 |
class AsStringRegexpPredicate(object): |
76 |
"""Class to be used as predicate that may be given to `register_transform`
|
77 |
|
78 |
First argument is a regular expression that will be searched against the `as_string`
|
79 |
representation of the node onto which it's applied.
|
80 |
|
81 |
If specified, the second argument is an `attrgetter` expression that will be
|
82 |
applied on the node first to get the actual node on which `as_string` should
|
83 |
be called.
|
84 |
|
85 |
WARNING: This can be fairly slow, as it has to convert every AST node back
|
86 |
to Python code; you should consider examining the AST directly instead.
|
87 |
"""
|
88 |
def __init__(self, regexp, expression=None): |
89 |
self.regexp = re.compile(regexp)
|
90 |
self.expression = expression
|
91 |
|
92 |
def __call__(self, node): |
93 |
if self.expression is not None: |
94 |
node = attrgetter(self.expression)(node)
|
95 |
return self.regexp.search(node.as_string()) |
96 |
|
97 |
def inference_tip(infer_function): |
98 |
"""Given an instance specific inference function, return a function to be
|
99 |
given to MANAGER.register_transform to set this inference function.
|
100 |
|
101 |
Typical usage
|
102 |
|
103 |
.. sourcecode:: python
|
104 |
|
105 |
MANAGER.register_transform(Call, inference_tip(infer_named_tuple),
|
106 |
predicate)
|
107 |
"""
|
108 |
def transform(node, infer_function=infer_function): |
109 |
node._explicit_inference = infer_function |
110 |
return node
|
111 |
return transform
|
112 |
|
113 |
|
114 |
def register_module_extender(manager, module_name, get_extension_mod): |
115 |
def transform(node): |
116 |
extension_module = get_extension_mod() |
117 |
for name, objs in extension_module._locals.items(): |
118 |
node._locals[name] = objs |
119 |
for obj in objs: |
120 |
if obj.parent is extension_module: |
121 |
obj.parent = node |
122 |
|
123 |
manager.register_transform(Module, transform, lambda n: n.name == module_name)
|
124 |
|
125 |
|
126 |
def sys_path(): |
127 |
if sys.getClassLoader()==None: |
128 |
return sys.path
|
129 |
path = getattr(sys.getClassLoader(),"path",None) |
130 |
if path == None: |
131 |
return sys.path
|
132 |
return path(sys.path)
|
133 |
|
134 |
# load brain plugins
|
135 |
from os import listdir |
136 |
from os.path import join, dirname |
137 |
_file_ = __file__ |
138 |
if sys.getClassLoader()!=None: |
139 |
getResourcePath = getattr(sys.getClassLoader(),"getResourcePath",None) |
140 |
if getResourcePath!=None: |
141 |
_file_ = getResourcePath(_file_) |
142 |
BRAIN_MODULES_DIR = join(dirname(_file_), 'brain')
|
143 |
if BRAIN_MODULES_DIR not in sys_path(): |
144 |
# add it to the end of the list so user path take precedence
|
145 |
sys.path.append(BRAIN_MODULES_DIR) |
146 |
# load modules in this directory
|
147 |
for module in listdir(BRAIN_MODULES_DIR): |
148 |
if module.endswith('.py'): |
149 |
__import__(module[:-3]) |