Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inspector.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2015-2017 Claudiu Popa <pcmanticore@gmail.com>
  3. # Copyright (c) 2017 Łukasz Rogalski <rogalski.91@gmail.com>
  4. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  5. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  6. """
  7. Visitor doing some postprocessing on the astroid tree.
  8. Try to resolve definitions (namespace) dictionary, relationship...
  9. """
  10. from __future__ import print_function
  11. import collections
  12. import os
  13. import traceback
  14. import astroid
  15. from astroid import bases
  16. from astroid import exceptions
  17. from astroid import manager
  18. from astroid import modutils
  19. from astroid import node_classes
  20. from pylint.pyreverse import utils
  21. def _iface_hdlr(_):
  22. """Handler used by interfaces to handle suspicious interface nodes."""
  23. return True
  24. def _astroid_wrapper(func, modname):
  25. print('parsing %s...' % modname)
  26. try:
  27. return func(modname)
  28. except exceptions.AstroidBuildingException as exc:
  29. print(exc)
  30. except Exception as exc: # pylint: disable=broad-except
  31. traceback.print_exc()
  32. def interfaces(node, herited=True, handler_func=_iface_hdlr):
  33. """Return an iterator on interfaces implemented by the given class node."""
  34. # FIXME: what if __implements__ = (MyIFace, MyParent.__implements__)...
  35. try:
  36. implements = bases.Instance(node).getattr('__implements__')[0]
  37. except exceptions.NotFoundError:
  38. return
  39. if not herited and implements.frame() is not node:
  40. return
  41. found = set()
  42. missing = False
  43. for iface in node_classes.unpack_infer(implements):
  44. if iface is astroid.YES:
  45. missing = True
  46. continue
  47. if iface not in found and handler_func(iface):
  48. found.add(iface)
  49. yield iface
  50. if missing:
  51. raise exceptions.InferenceError()
  52. class IdGeneratorMixIn(object):
  53. """Mixin adding the ability to generate integer uid."""
  54. def __init__(self, start_value=0):
  55. self.id_count = start_value
  56. def init_counter(self, start_value=0):
  57. """init the id counter
  58. """
  59. self.id_count = start_value
  60. def generate_id(self):
  61. """generate a new identifier
  62. """
  63. self.id_count += 1
  64. return self.id_count
  65. class Linker(IdGeneratorMixIn, utils.LocalsVisitor):
  66. """Walk on the project tree and resolve relationships.
  67. According to options the following attributes may be
  68. added to visited nodes:
  69. * uid,
  70. a unique identifier for the node (on astroid.Project, astroid.Module,
  71. astroid.Class and astroid.locals_type). Only if the linker
  72. has been instantiated with tag=True parameter (False by default).
  73. * Function
  74. a mapping from locals names to their bounded value, which may be a
  75. constant like a string or an integer, or an astroid node
  76. (on astroid.Module, astroid.Class and astroid.Function).
  77. * instance_attrs_type
  78. as locals_type but for klass member attributes (only on astroid.Class)
  79. * implements,
  80. list of implemented interface _objects_ (only on astroid.Class nodes)
  81. """
  82. def __init__(self, project, inherited_interfaces=0, tag=False):
  83. IdGeneratorMixIn.__init__(self)
  84. utils.LocalsVisitor.__init__(self)
  85. # take inherited interface in consideration or not
  86. self.inherited_interfaces = inherited_interfaces
  87. # tag nodes or not
  88. self.tag = tag
  89. # visited project
  90. self.project = project
  91. def visit_project(self, node):
  92. """visit an pyreverse.utils.Project node
  93. * optionally tag the node with a unique id
  94. """
  95. if self.tag:
  96. node.uid = self.generate_id()
  97. for module in node.modules:
  98. self.visit(module)
  99. def visit_package(self, node):
  100. """visit an astroid.Package node
  101. * optionally tag the node with a unique id
  102. """
  103. if self.tag:
  104. node.uid = self.generate_id()
  105. for subelmt in node.values():
  106. self.visit(subelmt)
  107. def visit_module(self, node):
  108. """visit an astroid.Module node
  109. * set the locals_type mapping
  110. * set the depends mapping
  111. * optionally tag the node with a unique id
  112. """
  113. if hasattr(node, 'locals_type'):
  114. return
  115. node.locals_type = collections.defaultdict(list)
  116. node.depends = []
  117. if self.tag:
  118. node.uid = self.generate_id()
  119. def visit_classdef(self, node):
  120. """visit an astroid.Class node
  121. * set the locals_type and instance_attrs_type mappings
  122. * set the implements list and build it
  123. * optionally tag the node with a unique id
  124. """
  125. if hasattr(node, 'locals_type'):
  126. return
  127. node.locals_type = collections.defaultdict(list)
  128. if self.tag:
  129. node.uid = self.generate_id()
  130. # resolve ancestors
  131. for baseobj in node.ancestors(recurs=False):
  132. specializations = getattr(baseobj, 'specializations', [])
  133. specializations.append(node)
  134. baseobj.specializations = specializations
  135. # resolve instance attributes
  136. node.instance_attrs_type = collections.defaultdict(list)
  137. for assignattrs in node.instance_attrs.values():
  138. for assignattr in assignattrs:
  139. self.handle_assignattr_type(assignattr, node)
  140. # resolve implemented interface
  141. try:
  142. node.implements = list(interfaces(node, self.inherited_interfaces))
  143. except astroid.InferenceError:
  144. node.implements = ()
  145. def visit_functiondef(self, node):
  146. """visit an astroid.Function node
  147. * set the locals_type mapping
  148. * optionally tag the node with a unique id
  149. """
  150. if hasattr(node, 'locals_type'):
  151. return
  152. node.locals_type = collections.defaultdict(list)
  153. if self.tag:
  154. node.uid = self.generate_id()
  155. link_project = visit_project
  156. link_module = visit_module
  157. link_class = visit_classdef
  158. link_function = visit_functiondef
  159. def visit_assignname(self, node):
  160. """visit an astroid.AssignName node
  161. handle locals_type
  162. """
  163. # avoid double parsing done by different Linkers.visit
  164. # running over the same project:
  165. if hasattr(node, '_handled'):
  166. return
  167. node._handled = True
  168. if node.name in node.frame():
  169. frame = node.frame()
  170. else:
  171. # the name has been defined as 'global' in the frame and belongs
  172. # there.
  173. frame = node.root()
  174. try:
  175. if not hasattr(frame, 'locals_type'):
  176. # If the frame doesn't have a locals_type yet,
  177. # it means it wasn't yet visited. Visit it now
  178. # to add what's missing from it.
  179. if isinstance(frame, astroid.ClassDef):
  180. self.visit_classdef(frame)
  181. elif isinstance(frame, astroid.FunctionDef):
  182. self.visit_functiondef(frame)
  183. else:
  184. self.visit_module(frame)
  185. current = frame.locals_type[node.name]
  186. values = set(node.infer())
  187. frame.locals_type[node.name] = list(set(current) | values)
  188. except astroid.InferenceError:
  189. pass
  190. @staticmethod
  191. def handle_assignattr_type(node, parent):
  192. """handle an astroid.assignattr node
  193. handle instance_attrs_type
  194. """
  195. try:
  196. values = set(node.infer())
  197. current = set(parent.instance_attrs_type[node.attrname])
  198. parent.instance_attrs_type[node.attrname] = list(current | values)
  199. except astroid.InferenceError:
  200. pass
  201. def visit_import(self, node):
  202. """visit an astroid.Import node
  203. resolve module dependencies
  204. """
  205. context_file = node.root().file
  206. for name in node.names:
  207. relative = modutils.is_relative(name[0], context_file)
  208. self._imported_module(node, name[0], relative)
  209. def visit_importfrom(self, node):
  210. """visit an astroid.ImportFrom node
  211. resolve module dependencies
  212. """
  213. basename = node.modname
  214. context_file = node.root().file
  215. if context_file is not None:
  216. relative = modutils.is_relative(basename, context_file)
  217. else:
  218. relative = False
  219. for name in node.names:
  220. if name[0] == '*':
  221. continue
  222. # analyze dependencies
  223. fullname = '%s.%s' % (basename, name[0])
  224. if fullname.find('.') > -1:
  225. try:
  226. # TODO: don't use get_module_part,
  227. # missing package precedence
  228. fullname = modutils.get_module_part(fullname,
  229. context_file)
  230. except ImportError:
  231. continue
  232. if fullname != basename:
  233. self._imported_module(node, fullname, relative)
  234. def compute_module(self, context_name, mod_path):
  235. """return true if the module should be added to dependencies"""
  236. package_dir = os.path.dirname(self.project.path)
  237. if context_name == mod_path:
  238. return 0
  239. elif modutils.is_standard_module(mod_path, (package_dir,)):
  240. return 1
  241. return 0
  242. def _imported_module(self, node, mod_path, relative):
  243. """Notify an imported module, used to analyze dependencies"""
  244. module = node.root()
  245. context_name = module.name
  246. if relative:
  247. mod_path = '%s.%s' % ('.'.join(context_name.split('.')[:-1]),
  248. mod_path)
  249. if self.compute_module(context_name, mod_path):
  250. # handle dependencies
  251. if not hasattr(module, 'depends'):
  252. module.depends = []
  253. mod_paths = module.depends
  254. if mod_path not in mod_paths:
  255. mod_paths.append(mod_path)
  256. class Project(object):
  257. """a project handle a set of modules / packages"""
  258. def __init__(self, name=''):
  259. self.name = name
  260. self.path = None
  261. self.modules = []
  262. self.locals = {}
  263. self.__getitem__ = self.locals.__getitem__
  264. self.__iter__ = self.locals.__iter__
  265. self.values = self.locals.values
  266. self.keys = self.locals.keys
  267. self.items = self.locals.items
  268. def add_module(self, node):
  269. self.locals[node.name] = node
  270. self.modules.append(node)
  271. def get_module(self, name):
  272. return self.locals[name]
  273. def get_children(self):
  274. return self.modules
  275. def __repr__(self):
  276. return '<Project %r at %s (%s modules)>' % (self.name, id(self),
  277. len(self.modules))
  278. def project_from_files(files, func_wrapper=_astroid_wrapper,
  279. project_name="no name",
  280. black_list=('CVS',)):
  281. """return a Project from a list of files or modules"""
  282. # build the project representation
  283. astroid_manager = manager.AstroidManager()
  284. project = Project(project_name)
  285. for something in files:
  286. if not os.path.exists(something):
  287. fpath = modutils.file_from_modpath(something.split('.'))
  288. elif os.path.isdir(something):
  289. fpath = os.path.join(something, '__init__.py')
  290. else:
  291. fpath = something
  292. ast = func_wrapper(astroid_manager.ast_from_file, fpath)
  293. if ast is None:
  294. continue
  295. # XXX why is first file defining the project.path ?
  296. project.path = project.path or ast.file
  297. project.add_module(ast)
  298. base_name = ast.name
  299. # recurse in package except if __init__ was explicitly given
  300. if ast.package and something.find('__init__') == -1:
  301. # recurse on others packages / modules if this is a package
  302. for fpath in modutils.get_module_files(os.path.dirname(ast.file),
  303. black_list):
  304. ast = func_wrapper(astroid_manager.ast_from_file, fpath)
  305. if ast is None or ast.name == base_name:
  306. continue
  307. project.add_module(ast)
  308. return project