OLD | NEW |
(Empty) | |
| 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 |
| 22 The builder is not thread safe and can't be used to parse different sources |
| 23 at the same time. |
| 24 """ |
| 25 |
| 26 __docformat__ = "restructuredtext en" |
| 27 |
| 28 import sys, re |
| 29 from os.path import splitext, basename, dirname, exists, abspath |
| 30 |
| 31 from logilab.common.modutils import modpath_from_file |
| 32 |
| 33 from logilab.astng.exceptions import ASTNGBuildingException, InferenceError |
| 34 from logilab.astng.raw_building import InspectBuilder |
| 35 from logilab.astng.rebuilder import TreeRebuilder |
| 36 from logilab.astng.manager import ASTNGManager |
| 37 from logilab.astng.bases import YES, Instance |
| 38 |
| 39 from _ast import PyCF_ONLY_AST |
| 40 def parse(string): |
| 41 return compile(string, "<string>", 'exec', PyCF_ONLY_AST) |
| 42 |
| 43 if sys.version_info >= (3, 0): |
| 44 from tokenize import detect_encoding |
| 45 |
| 46 def open_source_file(filename): |
| 47 byte_stream = open(filename, 'bU') |
| 48 encoding = detect_encoding(byte_stream.readline)[0] |
| 49 stream = open(filename, 'U', encoding=encoding) |
| 50 try: |
| 51 data = stream.read() |
| 52 except UnicodeError, uex: # wrong encodingg |
| 53 # detect_encoding returns utf-8 if no encoding specified |
| 54 msg = 'Wrong (%s) or no encoding specified' % encoding |
| 55 raise ASTNGBuildingException(msg) |
| 56 return stream, encoding, data |
| 57 |
| 58 else: |
| 59 import re |
| 60 |
| 61 _ENCODING_RGX = re.compile("\s*#+.*coding[:=]\s*([-\w.]+)") |
| 62 |
| 63 def _guess_encoding(string): |
| 64 """get encoding from a python file as string or return None if not found |
| 65 """ |
| 66 # check for UTF-8 byte-order mark |
| 67 if string.startswith('\xef\xbb\xbf'): |
| 68 return 'UTF-8' |
| 69 for line in string.split('\n', 2)[:2]: |
| 70 # check for encoding declaration |
| 71 match = _ENCODING_RGX.match(line) |
| 72 if match is not None: |
| 73 return match.group(1) |
| 74 |
| 75 def open_source_file(filename): |
| 76 """get data for parsing a file""" |
| 77 stream = open(filename, 'U') |
| 78 data = stream.read() |
| 79 encoding = _guess_encoding(data) |
| 80 return stream, encoding, data |
| 81 |
| 82 # ast NG builder ############################################################## |
| 83 |
| 84 MANAGER = ASTNGManager() |
| 85 |
| 86 class ASTNGBuilder(InspectBuilder): |
| 87 """provide astng building methods""" |
| 88 rebuilder = TreeRebuilder() |
| 89 |
| 90 def __init__(self, manager=None): |
| 91 self._manager = manager or MANAGER |
| 92 |
| 93 def module_build(self, module, modname=None): |
| 94 """build an astng from a living module instance |
| 95 """ |
| 96 node = None |
| 97 path = getattr(module, '__file__', None) |
| 98 if path is not None: |
| 99 path_, ext = splitext(module.__file__) |
| 100 if ext in ('.py', '.pyc', '.pyo') and exists(path_ + '.py'): |
| 101 node = self.file_build(path_ + '.py', modname) |
| 102 if node is None: |
| 103 # this is a built-in module |
| 104 # get a partial representation by introspection |
| 105 node = self.inspect_build(module, modname=modname, path=path) |
| 106 return node |
| 107 |
| 108 def file_build(self, path, modname=None): |
| 109 """build astng from a source code file (i.e. from an ast) |
| 110 |
| 111 path is expected to be a python source file |
| 112 """ |
| 113 try: |
| 114 stream, encoding, data = open_source_file(path) |
| 115 except IOError, exc: |
| 116 msg = 'Unable to load file %r (%s)' % (path, exc) |
| 117 raise ASTNGBuildingException(msg) |
| 118 except SyntaxError, exc: # py3k encoding specification error |
| 119 raise ASTNGBuildingException(exc) |
| 120 except LookupError, exc: # unknown encoding |
| 121 raise ASTNGBuildingException(exc) |
| 122 # get module name if necessary |
| 123 if modname is None: |
| 124 try: |
| 125 modname = '.'.join(modpath_from_file(path)) |
| 126 except ImportError: |
| 127 modname = splitext(basename(path))[0] |
| 128 # build astng representation |
| 129 node = self.string_build(data, modname, path) |
| 130 node.file_encoding = encoding |
| 131 return node |
| 132 |
| 133 def string_build(self, data, modname='', path=None): |
| 134 """build astng from source code string and return rebuilded astng""" |
| 135 module = self._data_build(data, modname, path) |
| 136 self._manager.astng_cache[module.name] = module |
| 137 # post tree building steps after we stored the module in the cache: |
| 138 for from_node in module._from_nodes: |
| 139 self.add_from_names_to_locals(from_node) |
| 140 # handle delayed assattr nodes |
| 141 for delayed in module._delayed_assattr: |
| 142 self.delayed_assattr(delayed) |
| 143 if modname: |
| 144 for transformer in self._manager.transformers: |
| 145 transformer(module) |
| 146 return module |
| 147 |
| 148 def _data_build(self, data, modname, path): |
| 149 """build tree node from data and add some informations""" |
| 150 # this method could be wrapped with a pickle/cache function |
| 151 node = parse(data + '\n') |
| 152 if path is not None: |
| 153 node_file = abspath(path) |
| 154 else: |
| 155 node_file = '<?>' |
| 156 if modname.endswith('.__init__'): |
| 157 modname = modname[:-9] |
| 158 package = True |
| 159 else: |
| 160 package = path and path.find('__init__.py') > -1 or False |
| 161 self.rebuilder.init() |
| 162 module = self.rebuilder.visit_module(node, modname, package) |
| 163 module.file = module.path = node_file |
| 164 module._from_nodes = self.rebuilder._from_nodes |
| 165 module._delayed_assattr = self.rebuilder._delayed_assattr |
| 166 return module |
| 167 |
| 168 def add_from_names_to_locals(self, node): |
| 169 """store imported names to the locals; |
| 170 resort the locals if coming from a delayed node |
| 171 """ |
| 172 |
| 173 _key_func = lambda node: node.fromlineno |
| 174 def sort_locals(my_list): |
| 175 my_list.sort(key=_key_func) |
| 176 for (name, asname) in node.names: |
| 177 if name == '*': |
| 178 try: |
| 179 imported = node.root().import_module(node.modname) |
| 180 except ASTNGBuildingException: |
| 181 continue |
| 182 for name in imported.wildcard_import_names(): |
| 183 node.parent.set_local(name, node) |
| 184 sort_locals(node.parent.scope().locals[name]) |
| 185 else: |
| 186 node.parent.set_local(asname or name, node) |
| 187 sort_locals(node.parent.scope().locals[asname or name]) |
| 188 |
| 189 def delayed_assattr(self, node): |
| 190 """visit a AssAttr node -> add name to locals, handle members |
| 191 definition |
| 192 """ |
| 193 try: |
| 194 frame = node.frame() |
| 195 for infered in node.expr.infer(): |
| 196 if infered is YES: |
| 197 continue |
| 198 try: |
| 199 if infered.__class__ is Instance: |
| 200 infered = infered._proxied |
| 201 iattrs = infered.instance_attrs |
| 202 elif isinstance(infered, Instance): |
| 203 # Const, Tuple, ... we may be wrong, may be not, but |
| 204 # anyway we don't want to pollute builtin's namespace |
| 205 continue |
| 206 elif infered.is_function: |
| 207 iattrs = infered.instance_attrs |
| 208 else: |
| 209 iattrs = infered.locals |
| 210 except AttributeError: |
| 211 # XXX log error |
| 212 #import traceback |
| 213 #traceback.print_exc() |
| 214 continue |
| 215 values = iattrs.setdefault(node.attrname, []) |
| 216 if node in values: |
| 217 continue |
| 218 # get assign in __init__ first XXX useful ? |
| 219 if frame.name == '__init__' and values and not \ |
| 220 values[0].frame().name == '__init__': |
| 221 values.insert(0, node) |
| 222 else: |
| 223 values.append(node) |
| 224 except InferenceError: |
| 225 pass |
| 226 |
OLD | NEW |