| OLD | NEW |
| 1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 1 # This program is free software; you can redistribute it and/or modify it under |
| 2 # the terms of the GNU Lesser General Public License as published by the Free So
ftware |
| 3 # Foundation; either version 2 of the License, or (at your option) any later |
| 4 # version. |
| 5 # |
| 6 # This program is distributed in the hope that it will be useful, but WITHOUT |
| 7 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 8 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more d
etails. |
| 9 # |
| 10 # You should have received a copy of the GNU Lesser General Public License along
with |
| 11 # this program; if not, write to the Free Software Foundation, Inc., |
| 12 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 13 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
| 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 14 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 15 # copyright 2003-2010 Sylvain Thenault, all rights reserved. |
| 16 # contact mailto:thenault@gmail.com |
| 3 # | 17 # |
| 4 # This file is part of astroid. | 18 # This file is part of logilab-astng. |
| 5 # | 19 # |
| 6 # astroid is free software: you can redistribute it and/or modify it | 20 # logilab-astng 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 | 21 # 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 | 22 # Free Software Foundation, either version 2.1 of the License, or (at your |
| 9 # option) any later version. | 23 # option) any later version. |
| 10 # | 24 # |
| 11 # astroid is distributed in the hope that it will be useful, but | 25 # logilab-astng is distributed in the hope that it will be useful, but |
| 12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 26 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | 27 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 14 # for more details. | 28 # for more details. |
| 15 # | 29 # |
| 16 # You should have received a copy of the GNU Lesser General Public License along | 30 # 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/>. | 31 # with logilab-astng. If not, see <http://www.gnu.org/licenses/>. |
| 18 """visitor doing some postprocessing on the astroid tree. | 32 """visitor doing some postprocessing on the astng tree. |
| 19 Try to resolve definitions (namespace) dictionary, relationship... | 33 Try to resolve definitions (namespace) dictionary, relationship... |
| 20 | 34 |
| 21 This module has been imported from pyreverse | 35 This module has been imported from pyreverse |
| 22 """ | 36 """ |
| 23 | 37 |
| 24 __docformat__ = "restructuredtext en" | 38 __docformat__ = "restructuredtext en" |
| 25 | 39 |
| 26 from os.path import dirname | 40 from os.path import dirname |
| 27 | 41 |
| 28 import astroid | 42 from logilab.common.modutils import get_module_part, is_relative, \ |
| 29 from astroid.exceptions import InferenceError | 43 is_standard_module |
| 30 from astroid.utils import LocalsVisitor | |
| 31 from astroid.modutils import get_module_part, is_relative, is_standard_module | |
| 32 | 44 |
| 33 class IdGeneratorMixIn(object): | 45 from logilab import astng |
| 46 from logilab.astng.exceptions import InferenceError |
| 47 from logilab.astng.utils import LocalsVisitor |
| 48 |
| 49 class IdGeneratorMixIn: |
| 34 """ | 50 """ |
| 35 Mixin adding the ability to generate integer uid | 51 Mixin adding the ability to generate integer uid |
| 36 """ | 52 """ |
| 37 def __init__(self, start_value=0): | 53 def __init__(self, start_value=0): |
| 38 self.id_count = start_value | 54 self.id_count = start_value |
| 39 | 55 |
| 40 def init_counter(self, start_value=0): | 56 def init_counter(self, start_value=0): |
| 41 """init the id counter | 57 """init the id counter |
| 42 """ | 58 """ |
| 43 self.id_count = start_value | 59 self.id_count = start_value |
| 44 | 60 |
| 45 def generate_id(self): | 61 def generate_id(self): |
| 46 """generate a new identifier | 62 """generate a new identifier |
| 47 """ | 63 """ |
| 48 self.id_count += 1 | 64 self.id_count += 1 |
| 49 return self.id_count | 65 return self.id_count |
| 50 | 66 |
| 51 | 67 |
| 52 class Linker(IdGeneratorMixIn, LocalsVisitor): | 68 class Linker(IdGeneratorMixIn, LocalsVisitor): |
| 53 """ | 69 """ |
| 54 walk on the project tree and resolve relationships. | 70 walk on the project tree and resolve relationships. |
| 55 | 71 |
| 56 According to options the following attributes may be added to visited nodes: | 72 According to options the following attributes may be added to visited nodes: |
| 57 | 73 |
| 58 * uid, | 74 * uid, |
| 59 a unique identifier for the node (on astroid.Project, astroid.Module, | 75 a unique identifier for the node (on astng.Project, astng.Module, |
| 60 astroid.Class and astroid.locals_type). Only if the linker has been instan
tiated | 76 astng.Class and astng.locals_type). Only if the linker has been instantiat
ed |
| 61 with tag=True parameter (False by default). | 77 with tag=True parameter (False by default). |
| 62 | 78 |
| 63 * Function | 79 * Function |
| 64 a mapping from locals names to their bounded value, which may be a | 80 a mapping from locals names to their bounded value, which may be a |
| 65 constant like a string or an integer, or an astroid node (on astroid.Modul
e, | 81 constant like a string or an integer, or an astng node (on astng.Module, |
| 66 astroid.Class and astroid.Function). | 82 astng.Class and astng.Function). |
| 67 | 83 |
| 68 * instance_attrs_type | 84 * instance_attrs_type |
| 69 as locals_type but for klass member attributes (only on astroid.Class) | 85 as locals_type but for klass member attributes (only on astng.Class) |
| 70 | 86 |
| 71 * implements, | 87 * implements, |
| 72 list of implemented interface _objects_ (only on astroid.Class nodes) | 88 list of implemented interface _objects_ (only on astng.Class nodes) |
| 73 """ | 89 """ |
| 74 | 90 |
| 75 def __init__(self, project, inherited_interfaces=0, tag=False): | 91 def __init__(self, project, inherited_interfaces=0, tag=False): |
| 76 IdGeneratorMixIn.__init__(self) | 92 IdGeneratorMixIn.__init__(self) |
| 77 LocalsVisitor.__init__(self) | 93 LocalsVisitor.__init__(self) |
| 78 # take inherited interface in consideration or not | 94 # take inherited interface in consideration or not |
| 79 self.inherited_interfaces = inherited_interfaces | 95 self.inherited_interfaces = inherited_interfaces |
| 80 # tag nodes or not | 96 # tag nodes or not |
| 81 self.tag = tag | 97 self.tag = tag |
| 82 # visited project | 98 # visited project |
| 83 self.project = project | 99 self.project = project |
| 84 | 100 |
| 85 | 101 |
| 86 def visit_project(self, node): | 102 def visit_project(self, node): |
| 87 """visit an astroid.Project node | 103 """visit an astng.Project node |
| 88 | 104 |
| 89 * optionally tag the node with a unique id | 105 * optionally tag the node with a unique id |
| 90 """ | 106 """ |
| 91 if self.tag: | 107 if self.tag: |
| 92 node.uid = self.generate_id() | 108 node.uid = self.generate_id() |
| 93 for module in node.modules: | 109 for module in node.modules: |
| 94 self.visit(module) | 110 self.visit(module) |
| 95 | 111 |
| 96 def visit_package(self, node): | 112 def visit_package(self, node): |
| 97 """visit an astroid.Package node | 113 """visit an astng.Package node |
| 98 | 114 |
| 99 * optionally tag the node with a unique id | 115 * optionally tag the node with a unique id |
| 100 """ | 116 """ |
| 101 if self.tag: | 117 if self.tag: |
| 102 node.uid = self.generate_id() | 118 node.uid = self.generate_id() |
| 103 for subelmt in node.values(): | 119 for subelmt in node.values(): |
| 104 self.visit(subelmt) | 120 self.visit(subelmt) |
| 105 | 121 |
| 106 def visit_module(self, node): | 122 def visit_module(self, node): |
| 107 """visit an astroid.Module node | 123 """visit an astng.Module node |
| 108 | 124 |
| 109 * set the locals_type mapping | 125 * set the locals_type mapping |
| 110 * set the depends mapping | 126 * set the depends mapping |
| 111 * optionally tag the node with a unique id | 127 * optionally tag the node with a unique id |
| 112 """ | 128 """ |
| 113 if hasattr(node, 'locals_type'): | 129 if hasattr(node, 'locals_type'): |
| 114 return | 130 return |
| 115 node.locals_type = {} | 131 node.locals_type = {} |
| 116 node.depends = [] | 132 node.depends = [] |
| 117 if self.tag: | 133 if self.tag: |
| 118 node.uid = self.generate_id() | 134 node.uid = self.generate_id() |
| 119 | 135 |
| 120 def visit_class(self, node): | 136 def visit_class(self, node): |
| 121 """visit an astroid.Class node | 137 """visit an astng.Class node |
| 122 | 138 |
| 123 * set the locals_type and instance_attrs_type mappings | 139 * set the locals_type and instance_attrs_type mappings |
| 124 * set the implements list and build it | 140 * set the implements list and build it |
| 125 * optionally tag the node with a unique id | 141 * optionally tag the node with a unique id |
| 126 """ | 142 """ |
| 127 if hasattr(node, 'locals_type'): | 143 if hasattr(node, 'locals_type'): |
| 128 return | 144 return |
| 129 node.locals_type = {} | 145 node.locals_type = {} |
| 130 if self.tag: | 146 if self.tag: |
| 131 node.uid = self.generate_id() | 147 node.uid = self.generate_id() |
| 132 # resolve ancestors | 148 # resolve ancestors |
| 133 for baseobj in node.ancestors(recurs=False): | 149 for baseobj in node.ancestors(recurs=False): |
| 134 specializations = getattr(baseobj, 'specializations', []) | 150 specializations = getattr(baseobj, 'specializations', []) |
| 135 specializations.append(node) | 151 specializations.append(node) |
| 136 baseobj.specializations = specializations | 152 baseobj.specializations = specializations |
| 137 # resolve instance attributes | 153 # resolve instance attributes |
| 138 node.instance_attrs_type = {} | 154 node.instance_attrs_type = {} |
| 139 for assattrs in node.instance_attrs.values(): | 155 for assattrs in node.instance_attrs.values(): |
| 140 for assattr in assattrs: | 156 for assattr in assattrs: |
| 141 self.handle_assattr_type(assattr, node) | 157 self.handle_assattr_type(assattr, node) |
| 142 # resolve implemented interface | 158 # resolve implemented interface |
| 143 try: | 159 try: |
| 144 node.implements = list(node.interfaces(self.inherited_interfaces)) | 160 node.implements = list(node.interfaces(self.inherited_interfaces)) |
| 145 except InferenceError: | 161 except InferenceError: |
| 146 node.implements = () | 162 node.implements = () |
| 147 | 163 |
| 148 def visit_function(self, node): | 164 def visit_function(self, node): |
| 149 """visit an astroid.Function node | 165 """visit an astng.Function node |
| 150 | 166 |
| 151 * set the locals_type mapping | 167 * set the locals_type mapping |
| 152 * optionally tag the node with a unique id | 168 * optionally tag the node with a unique id |
| 153 """ | 169 """ |
| 154 if hasattr(node, 'locals_type'): | 170 if hasattr(node, 'locals_type'): |
| 155 return | 171 return |
| 156 node.locals_type = {} | 172 node.locals_type = {} |
| 157 if self.tag: | 173 if self.tag: |
| 158 node.uid = self.generate_id() | 174 node.uid = self.generate_id() |
| 159 | 175 |
| 160 link_project = visit_project | 176 link_project = visit_project |
| 161 link_module = visit_module | 177 link_module = visit_module |
| 162 link_class = visit_class | 178 link_class = visit_class |
| 163 link_function = visit_function | 179 link_function = visit_function |
| 164 | 180 |
| 165 def visit_assname(self, node): | 181 def visit_assname(self, node): |
| 166 """visit an astroid.AssName node | 182 """visit an astng.AssName node |
| 167 | 183 |
| 168 handle locals_type | 184 handle locals_type |
| 169 """ | 185 """ |
| 170 # avoid double parsing done by different Linkers.visit | 186 # avoid double parsing done by different Linkers.visit |
| 171 # running over the same project: | 187 # running over the same project: |
| 172 if hasattr(node, '_handled'): | 188 if hasattr(node, '_handled'): |
| 173 return | 189 return |
| 174 node._handled = True | 190 node._handled = True |
| 175 if node.name in node.frame(): | 191 if node.name in node.frame(): |
| 176 frame = node.frame() | 192 frame = node.frame() |
| 177 else: | 193 else: |
| 178 # the name has been defined as 'global' in the frame and belongs | 194 # the name has been defined as 'global' in the frame and belongs |
| 179 # there. Btw the frame is not yet visited as the name is in the | 195 # there. Btw the frame is not yet visited as the name is in the |
| 180 # root locals; the frame hence has no locals_type attribute | 196 # root locals; the frame hence has no locals_type attribute |
| 181 frame = node.root() | 197 frame = node.root() |
| 182 try: | 198 try: |
| 183 values = node.infered() | 199 values = node.infered() |
| 184 try: | 200 try: |
| 185 already_infered = frame.locals_type[node.name] | 201 already_infered = frame.locals_type[node.name] |
| 186 for valnode in values: | 202 for valnode in values: |
| 187 if not valnode in already_infered: | 203 if not valnode in already_infered: |
| 188 already_infered.append(valnode) | 204 already_infered.append(valnode) |
| 189 except KeyError: | 205 except KeyError: |
| 190 frame.locals_type[node.name] = values | 206 frame.locals_type[node.name] = values |
| 191 except astroid.InferenceError: | 207 except astng.InferenceError: |
| 192 pass | 208 pass |
| 193 | 209 |
| 194 def handle_assattr_type(self, node, parent): | 210 def handle_assattr_type(self, node, parent): |
| 195 """handle an astroid.AssAttr node | 211 """handle an astng.AssAttr node |
| 196 | 212 |
| 197 handle instance_attrs_type | 213 handle instance_attrs_type |
| 198 """ | 214 """ |
| 199 try: | 215 try: |
| 200 values = list(node.infer()) | 216 values = list(node.infer()) |
| 201 try: | 217 try: |
| 202 already_infered = parent.instance_attrs_type[node.attrname] | 218 already_infered = parent.instance_attrs_type[node.attrname] |
| 203 for valnode in values: | 219 for valnode in values: |
| 204 if not valnode in already_infered: | 220 if not valnode in already_infered: |
| 205 already_infered.append(valnode) | 221 already_infered.append(valnode) |
| 206 except KeyError: | 222 except KeyError: |
| 207 parent.instance_attrs_type[node.attrname] = values | 223 parent.instance_attrs_type[node.attrname] = values |
| 208 except astroid.InferenceError: | 224 except astng.InferenceError: |
| 209 pass | 225 pass |
| 210 | 226 |
| 211 def visit_import(self, node): | 227 def visit_import(self, node): |
| 212 """visit an astroid.Import node | 228 """visit an astng.Import node |
| 213 | 229 |
| 214 resolve module dependencies | 230 resolve module dependencies |
| 215 """ | 231 """ |
| 216 context_file = node.root().file | 232 context_file = node.root().file |
| 217 for name in node.names: | 233 for name in node.names: |
| 218 relative = is_relative(name[0], context_file) | 234 relative = is_relative(name[0], context_file) |
| 219 self._imported_module(node, name[0], relative) | 235 self._imported_module(node, name[0], relative) |
| 220 | 236 |
| 221 | 237 |
| 222 def visit_from(self, node): | 238 def visit_from(self, node): |
| 223 """visit an astroid.From node | 239 """visit an astng.From node |
| 224 | 240 |
| 225 resolve module dependencies | 241 resolve module dependencies |
| 226 """ | 242 """ |
| 227 basename = node.modname | 243 basename = node.modname |
| 228 context_file = node.root().file | 244 context_file = node.root().file |
| 229 if context_file is not None: | 245 if context_file is not None: |
| 230 relative = is_relative(basename, context_file) | 246 relative = is_relative(basename, context_file) |
| 231 else: | 247 else: |
| 232 relative = False | 248 relative = False |
| 233 for name in node.names: | 249 for name in node.names: |
| 234 if name[0] == '*': | 250 if name[0] == '*': |
| 235 continue | 251 continue |
| 236 # analyze dependencies | 252 # analyze dependencies |
| 237 fullname = '%s.%s' % (basename, name[0]) | 253 fullname = '%s.%s' % (basename, name[0]) |
| 238 if fullname.find('.') > -1: | 254 if fullname.find('.') > -1: |
| 239 try: | 255 try: |
| 240 # XXX: don't use get_module_part, missing package precedence | 256 # XXX: don't use get_module_part, missing package precedence |
| 241 fullname = get_module_part(fullname, context_file) | 257 fullname = get_module_part(fullname) |
| 242 except ImportError: | 258 except ImportError: |
| 243 continue | 259 continue |
| 244 if fullname != basename: | 260 if fullname != basename: |
| 245 self._imported_module(node, fullname, relative) | 261 self._imported_module(node, fullname, relative) |
| 246 | 262 |
| 247 | 263 |
| 248 def compute_module(self, context_name, mod_path): | 264 def compute_module(self, context_name, mod_path): |
| 249 """return true if the module should be added to dependencies""" | 265 """return true if the module should be added to dependencies""" |
| 250 package_dir = dirname(self.project.path) | 266 package_dir = dirname(self.project.path) |
| 251 if context_name == mod_path: | 267 if context_name == mod_path: |
| 252 return 0 | 268 return 0 |
| 253 elif is_standard_module(mod_path, (package_dir,)): | 269 elif is_standard_module(mod_path, (package_dir,)): |
| 254 return 1 | 270 return 1 |
| 255 return 0 | 271 return 0 |
| 256 | 272 |
| 257 # protected methods ######################################################## | 273 # protected methods ######################################################## |
| 258 | 274 |
| 259 def _imported_module(self, node, mod_path, relative): | 275 def _imported_module(self, node, mod_path, relative): |
| 260 """notify an imported module, used to analyze dependencies | 276 """notify an imported module, used to analyze dependencies |
| 261 """ | 277 """ |
| 262 module = node.root() | 278 module = node.root() |
| 263 context_name = module.name | 279 context_name = module.name |
| 264 if relative: | 280 if relative: |
| 265 mod_path = '%s.%s' % ('.'.join(context_name.split('.')[:-1]), | 281 mod_path = '%s.%s' % ('.'.join(context_name.split('.')[:-1]), |
| 266 mod_path) | 282 mod_path) |
| 267 if self.compute_module(context_name, mod_path): | 283 if self.compute_module(context_name, mod_path): |
| 268 # handle dependencies | 284 # handle dependencies |
| 269 if not hasattr(module, 'depends'): | 285 if not hasattr(module, 'depends'): |
| 270 module.depends = [] | 286 module.depends = [] |
| 271 mod_paths = module.depends | 287 mod_paths = module.depends |
| 272 if not mod_path in mod_paths: | 288 if not mod_path in mod_paths: |
| 273 mod_paths.append(mod_path) | 289 mod_paths.append(mod_path) |
| OLD | NEW |