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 / mixins.py @ 745
History | View | Annotate | Download (5.38 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 |
"""This module contains some mixins for the different nodes.
|
19 |
"""
|
20 |
|
21 |
import warnings |
22 |
|
23 |
from astroid import decorators |
24 |
from astroid import exceptions |
25 |
|
26 |
|
27 |
class BlockRangeMixIn(object): |
28 |
"""override block range """
|
29 |
|
30 |
@decorators.cachedproperty
|
31 |
def blockstart_tolineno(self): |
32 |
return self.lineno |
33 |
|
34 |
def _elsed_block_range(self, lineno, orelse, last=None): |
35 |
"""handle block line numbers range for try/finally, for, if and while
|
36 |
statements
|
37 |
"""
|
38 |
if lineno == self.fromlineno: |
39 |
return lineno, lineno
|
40 |
if orelse:
|
41 |
if lineno >= orelse[0].fromlineno: |
42 |
return lineno, orelse[-1].tolineno |
43 |
return lineno, orelse[0].fromlineno - 1 |
44 |
return lineno, last or self.tolineno |
45 |
|
46 |
|
47 |
class FilterStmtsMixin(object): |
48 |
"""Mixin for statement filtering and assignment type"""
|
49 |
|
50 |
def _get_filtered_stmts(self, _, node, _stmts, mystmt): |
51 |
"""method used in _filter_stmts to get statemtents and trigger break"""
|
52 |
if self.statement() is mystmt: |
53 |
# original node's statement is the assignment, only keep
|
54 |
# current node (gen exp, list comp)
|
55 |
return [node], True |
56 |
return _stmts, False |
57 |
|
58 |
def assign_type(self): |
59 |
return self |
60 |
|
61 |
def ass_type(self): |
62 |
warnings.warn('%s.ass_type() is deprecated and slated for removal '
|
63 |
'in astroid 2.0, use %s.assign_type() instead.'
|
64 |
% (type(self).__name__, type(self).__name__), |
65 |
PendingDeprecationWarning, stacklevel=2) |
66 |
return self.assign_type() |
67 |
|
68 |
|
69 |
class AssignTypeMixin(object): |
70 |
|
71 |
def assign_type(self): |
72 |
return self |
73 |
|
74 |
def ass_type(self): |
75 |
warnings.warn('%s.ass_type() is deprecated and slated for removal '
|
76 |
'in astroid 2.0, use %s.assign_type() instead.'
|
77 |
% (type(self).__name__, type(self).__name__), |
78 |
PendingDeprecationWarning, stacklevel=2) |
79 |
return self.assign_type() |
80 |
|
81 |
def _get_filtered_stmts(self, lookup_node, node, _stmts, mystmt): |
82 |
"""method used in filter_stmts"""
|
83 |
if self is mystmt: |
84 |
return _stmts, True |
85 |
if self.statement() is mystmt: |
86 |
# original node's statement is the assignment, only keep
|
87 |
# current node (gen exp, list comp)
|
88 |
return [node], True |
89 |
return _stmts, False |
90 |
|
91 |
|
92 |
class ParentAssignTypeMixin(AssignTypeMixin): |
93 |
|
94 |
def assign_type(self): |
95 |
return self.parent.assign_type() |
96 |
|
97 |
def ass_type(self): |
98 |
warnings.warn('%s.ass_type() is deprecated and slated for removal '
|
99 |
'in astroid 2.0, use %s.assign_type() instead.'
|
100 |
% (type(self).__name__, type(self).__name__), |
101 |
PendingDeprecationWarning, stacklevel=2) |
102 |
return self.assign_type() |
103 |
|
104 |
|
105 |
class ImportFromMixin(FilterStmtsMixin): |
106 |
"""MixIn for From and Import Nodes"""
|
107 |
|
108 |
def _infer_name(self, frame, name): |
109 |
return name
|
110 |
|
111 |
def do_import_module(self, modname=None): |
112 |
"""return the ast for a module whose name is <modname> imported by <self>
|
113 |
"""
|
114 |
# handle special case where we are on a package node importing a module
|
115 |
# using the same name as the package, which may end in an infinite loop
|
116 |
# on relative imports
|
117 |
# XXX: no more needed ?
|
118 |
mymodule = self.root()
|
119 |
level = getattr(self, 'level', None) # Import as no level |
120 |
if modname is None: |
121 |
modname = self.modname
|
122 |
# XXX we should investigate deeper if we really want to check
|
123 |
# importing itself: modname and mymodule.name be relative or absolute
|
124 |
if mymodule.relative_to_absolute_name(modname, level) == mymodule.name:
|
125 |
# FIXME: we used to raise InferenceError here, but why ?
|
126 |
return mymodule
|
127 |
try:
|
128 |
return mymodule.import_module(modname, level=level,
|
129 |
relative_only=level and level >= 1) |
130 |
except exceptions.AstroidBuildingException as ex: |
131 |
if isinstance(ex.args[0], SyntaxError): |
132 |
raise exceptions.InferenceError(str(ex)) |
133 |
raise exceptions.InferenceError(modname)
|
134 |
except SyntaxError as ex: |
135 |
raise exceptions.InferenceError(str(ex)) |
136 |
|
137 |
def real_name(self, asname): |
138 |
"""get name from 'as' name"""
|
139 |
for name, _asname in self.names: |
140 |
if name == '*': |
141 |
return asname
|
142 |
if not _asname: |
143 |
name = name.split('.', 1)[0] |
144 |
_asname = name |
145 |
if asname == _asname:
|
146 |
return name
|
147 |
raise exceptions.NotFoundError(asname)
|