builder.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  2. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3. # copyright 2003-2010 Sylvain Thenault, all rights reserved.
  4. # contact mailto:thenault@gmail.com
  5. #
  6. # This file is part of logilab-astng.
  7. #
  8. # logilab-astng is free software: you can redistribute it and/or modify it
  9. # under the terms of the GNU Lesser General Public License as published by the
  10. # Free Software Foundation, either version 2.1 of the License, or (at your
  11. # option) any later version.
  12. #
  13. # logilab-astng is distributed in the hope that it will be useful, but
  14. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  16. # for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with logilab-astng. If not, see <http://www.gnu.org/licenses/>.
  20. """The ASTNGBuilder makes astng from living object and / or from _ast
  21. The builder is not thread safe and can't be used to parse different sources
  22. at the same time.
  23. """
  24. __docformat__ = "restructuredtext en"
  25. import sys, re
  26. from os.path import splitext, basename, dirname, exists, abspath
  27. from logilab.common.modutils import modpath_from_file
  28. from logilab.astng.exceptions import ASTNGBuildingException, InferenceError
  29. from logilab.astng.raw_building import InspectBuilder
  30. from logilab.astng.rebuilder import TreeRebuilder
  31. from logilab.astng.manager import ASTNGManager
  32. from logilab.astng.bases import YES, Instance
  33. from _ast import PyCF_ONLY_AST
  34. def parse(string):
  35. return compile(string, "<string>", 'exec', PyCF_ONLY_AST)
  36. if sys.version_info >= (3, 0):
  37. from tokenize import detect_encoding
  38. def open_source_file(filename):
  39. byte_stream = open(filename, 'bU')
  40. encoding = detect_encoding(byte_stream.readline)[0]
  41. stream = open(filename, 'U', encoding=encoding)
  42. try:
  43. data = stream.read()
  44. except UnicodeError, uex: # wrong encodingg
  45. # detect_encoding returns utf-8 if no encoding specified
  46. msg = 'Wrong (%s) or no encoding specified' % encoding
  47. raise ASTNGBuildingException(msg)
  48. return stream, encoding, data
  49. else:
  50. import re
  51. _ENCODING_RGX = re.compile("\s*#+.*coding[:=]\s*([-\w.]+)")
  52. def _guess_encoding(string):
  53. """get encoding from a python file as string or return None if not found
  54. """
  55. # check for UTF-8 byte-order mark
  56. if string.startswith('\xef\xbb\xbf'):
  57. return 'UTF-8'
  58. for line in string.split('\n', 2)[:2]:
  59. # check for encoding declaration
  60. match = _ENCODING_RGX.match(line)
  61. if match is not None:
  62. return match.group(1)
  63. def open_source_file(filename):
  64. """get data for parsing a file"""
  65. stream = open(filename, 'U')
  66. data = stream.read()
  67. encoding = _guess_encoding(data)
  68. return stream, encoding, data
  69. # ast NG builder ##############################################################
  70. MANAGER = ASTNGManager()
  71. class ASTNGBuilder(InspectBuilder):
  72. """provide astng building methods"""
  73. rebuilder = TreeRebuilder()
  74. def __init__(self, manager=None):
  75. self._manager = manager or MANAGER
  76. def module_build(self, module, modname=None):
  77. """build an astng from a living module instance
  78. """
  79. node = None
  80. path = getattr(module, '__file__', None)
  81. if path is not None:
  82. path_, ext = splitext(module.__file__)
  83. if ext in ('.py', '.pyc', '.pyo') and exists(path_ + '.py'):
  84. node = self.file_build(path_ + '.py', modname)
  85. if node is None:
  86. # this is a built-in module
  87. # get a partial representation by introspection
  88. node = self.inspect_build(module, modname=modname, path=path)
  89. return node
  90. def file_build(self, path, modname=None):
  91. """build astng from a source code file (i.e. from an ast)
  92. path is expected to be a python source file
  93. """
  94. try:
  95. stream, encoding, data = open_source_file(path)
  96. except IOError, exc:
  97. msg = 'Unable to load file %r (%s)' % (path, exc)
  98. raise ASTNGBuildingException(msg)
  99. except SyntaxError, exc: # py3k encoding specification error
  100. raise ASTNGBuildingException(exc)
  101. except LookupError, exc: # unknown encoding
  102. raise ASTNGBuildingException(exc)
  103. # get module name if necessary
  104. if modname is None:
  105. try:
  106. modname = '.'.join(modpath_from_file(path))
  107. except ImportError:
  108. modname = splitext(basename(path))[0]
  109. # build astng representation
  110. node = self.string_build(data, modname, path)
  111. node.file_encoding = encoding
  112. return node
  113. def string_build(self, data, modname='', path=None):
  114. """build astng from source code string and return rebuilded astng"""
  115. module = self._data_build(data, modname, path)
  116. self._manager.astng_cache[module.name] = module
  117. # post tree building steps after we stored the module in the cache:
  118. for from_node in module._from_nodes:
  119. self.add_from_names_to_locals(from_node)
  120. # handle delayed assattr nodes
  121. for delayed in module._delayed_assattr:
  122. self.delayed_assattr(delayed)
  123. if modname:
  124. for transformer in self._manager.transformers:
  125. transformer(module)
  126. return module
  127. def _data_build(self, data, modname, path):
  128. """build tree node from data and add some informations"""
  129. # this method could be wrapped with a pickle/cache function
  130. node = parse(data + '\n')
  131. if path is not None:
  132. node_file = abspath(path)
  133. else:
  134. node_file = '<?>'
  135. if modname.endswith('.__init__'):
  136. modname = modname[:-9]
  137. package = True
  138. else:
  139. package = path and path.find('__init__.py') > -1 or False
  140. self.rebuilder.init()
  141. module = self.rebuilder.visit_module(node, modname, package)
  142. module.file = module.path = node_file
  143. module._from_nodes = self.rebuilder._from_nodes
  144. module._delayed_assattr = self.rebuilder._delayed_assattr
  145. return module
  146. def add_from_names_to_locals(self, node):
  147. """store imported names to the locals;
  148. resort the locals if coming from a delayed node
  149. """
  150. _key_func = lambda node: node.fromlineno
  151. def sort_locals(my_list):
  152. my_list.sort(key=_key_func)
  153. for (name, asname) in node.names:
  154. if name == '*':
  155. try:
  156. imported = node.root().import_module(node.modname)
  157. except ASTNGBuildingException:
  158. continue
  159. for name in imported.wildcard_import_names():
  160. node.parent.set_local(name, node)
  161. sort_locals(node.parent.scope().locals[name])
  162. else:
  163. node.parent.set_local(asname or name, node)
  164. sort_locals(node.parent.scope().locals[asname or name])
  165. def delayed_assattr(self, node):
  166. """visit a AssAttr node -> add name to locals, handle members
  167. definition
  168. """
  169. try:
  170. frame = node.frame()
  171. for infered in node.expr.infer():
  172. if infered is YES:
  173. continue
  174. try:
  175. if infered.__class__ is Instance:
  176. infered = infered._proxied
  177. iattrs = infered.instance_attrs
  178. elif isinstance(infered, Instance):
  179. # Const, Tuple, ... we may be wrong, may be not, but
  180. # anyway we don't want to pollute builtin's namespace
  181. continue
  182. elif infered.is_function:
  183. iattrs = infered.instance_attrs
  184. else:
  185. iattrs = infered.locals
  186. except AttributeError:
  187. # XXX log error
  188. #import traceback
  189. #traceback.print_exc()
  190. continue
  191. values = iattrs.setdefault(node.attrname, [])
  192. if node in values:
  193. continue
  194. # get assign in __init__ first XXX useful ?
  195. if frame.name == '__init__' and values and not \
  196. values[0].frame().name == '__init__':
  197. values.insert(0, node)
  198. else:
  199. values.append(node)
  200. except InferenceError:
  201. pass