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.

__init__.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # Copyright (c) 2006-2013, 2015 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2014 Google, Inc.
  3. # Copyright (c) 2015-2016 Claudiu Popa <pcmanticore@gmail.com>
  4. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  5. # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
  6. """Python Abstract Syntax Tree New Generation
  7. The aim of this module is to provide a common base representation of
  8. python source code for projects such as pychecker, pyreverse,
  9. pylint... Well, actually the development of this library is essentially
  10. governed by pylint's needs.
  11. It extends class defined in the python's _ast module with some
  12. additional methods and attributes. Instance attributes are added by a
  13. builder object, which can either generate extended ast (let's call
  14. them astroid ;) by visiting an existent ast tree or by inspecting living
  15. object. Methods are added by monkey patching ast classes.
  16. Main modules are:
  17. * nodes and scoped_nodes for more information about methods and
  18. attributes added to different node classes
  19. * the manager contains a high level object to get astroid trees from
  20. source files and living objects. It maintains a cache of previously
  21. constructed tree for quick access
  22. * builder contains the class responsible to build astroid trees
  23. """
  24. import os
  25. import sys
  26. import re
  27. from operator import attrgetter
  28. import enum
  29. _Context = enum.Enum('Context', 'Load Store Del')
  30. Load = _Context.Load
  31. Store = _Context.Store
  32. Del = _Context.Del
  33. del _Context
  34. from .__pkginfo__ import version as __version__
  35. # WARNING: internal imports order matters !
  36. # pylint: disable=redefined-builtin, wildcard-import
  37. # make all exception classes accessible from astroid package
  38. from astroid.exceptions import *
  39. # make all node classes accessible from astroid package
  40. from astroid.nodes import *
  41. # trigger extra monkey-patching
  42. from astroid import inference
  43. # more stuff available
  44. from astroid import raw_building
  45. from astroid.bases import BaseInstance, Instance, BoundMethod, UnboundMethod
  46. from astroid.node_classes import are_exclusive, unpack_infer
  47. from astroid.scoped_nodes import builtin_lookup
  48. from astroid.builder import parse, extract_node
  49. from astroid.util import Uninferable, YES
  50. # make a manager instance (borg) accessible from astroid package
  51. from astroid.manager import AstroidManager
  52. MANAGER = AstroidManager()
  53. del AstroidManager
  54. # transform utilities (filters and decorator)
  55. class AsStringRegexpPredicate(object):
  56. """ClassDef to be used as predicate that may be given to `register_transform`
  57. First argument is a regular expression that will be searched against the `as_string`
  58. representation of the node onto which it's applied.
  59. If specified, the second argument is an `attrgetter` expression that will be
  60. applied on the node first to get the actual node on which `as_string` should
  61. be called.
  62. WARNING: This can be fairly slow, as it has to convert every AST node back
  63. to Python code; you should consider examining the AST directly instead.
  64. """
  65. def __init__(self, regexp, expression=None):
  66. self.regexp = re.compile(regexp)
  67. self.expression = expression
  68. def __call__(self, node):
  69. if self.expression is not None:
  70. node = attrgetter(self.expression)(node)
  71. # pylint: disable=no-member; github.com/pycqa/astroid/126
  72. return self.regexp.search(node.as_string())
  73. def inference_tip(infer_function, raise_on_overwrite=False):
  74. """Given an instance specific inference function, return a function to be
  75. given to MANAGER.register_transform to set this inference function.
  76. :param bool raise_on_overwrite: Raise an `InferenceOverwriteError`
  77. if the inference tip will overwrite another. Used for debugging
  78. Typical usage
  79. .. sourcecode:: python
  80. MANAGER.register_transform(Call, inference_tip(infer_named_tuple),
  81. predicate)
  82. .. Note::
  83. Using an inference tip will override
  84. any previously set inference tip for the given
  85. node. Use a predicate in the transform to prevent
  86. excess overwrites.
  87. """
  88. def transform(node, infer_function=infer_function):
  89. if (raise_on_overwrite
  90. and node._explicit_inference is not None
  91. and node._explicit_inference is not infer_function):
  92. raise InferenceOverwriteError(
  93. "Inference already set to {existing_inference}. "
  94. "Trying to overwrite with {new_inference} for {node}"
  95. .format(existing_inference=infer_function,
  96. new_inference=node._explicit_inference,
  97. node=node))
  98. node._explicit_inference = infer_function
  99. return node
  100. return transform
  101. def register_module_extender(manager, module_name, get_extension_mod):
  102. def transform(node):
  103. extension_module = get_extension_mod()
  104. for name, objs in extension_module.locals.items():
  105. node.locals[name] = objs
  106. for obj in objs:
  107. if obj.parent is extension_module:
  108. obj.parent = node
  109. manager.register_transform(Module, transform, lambda n: n.name == module_name)
  110. # load brain plugins
  111. BRAIN_MODULES_DIR = os.path.join(os.path.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 os.listdir(BRAIN_MODULES_DIR):
  117. if module.endswith('.py'):
  118. __import__(module[:-3])