summaryrefslogtreecommitdiff
path: root/builder.py
diff options
context:
space:
mode:
authorEmile Anclin <emile.anclin@logilab.fr>2010-12-14 15:42:06 +0100
committerEmile Anclin <emile.anclin@logilab.fr>2010-12-14 15:42:06 +0100
commit7fc42a779d45282d83d04e1cfe238bdfab461982 (patch)
tree26c6b3cfd176bbd6cf9e808f4caf49ad1e210f05 /builder.py
parente35605352798bc22b922894fe560f38c9c3ef065 (diff)
downloadastroid-7fc42a779d45282d83d04e1cfe238bdfab461982.tar.gz
cleanup: move raw building stuff from builder to raw_building
Diffstat (limited to 'builder.py')
-rw-r--r--builder.py111
1 files changed, 2 insertions, 109 deletions
diff --git a/builder.py b/builder.py
index f744d60..f838ba8 100644
--- a/builder.py
+++ b/builder.py
@@ -29,16 +29,11 @@ __docformat__ = "restructuredtext en"
import sys
from os.path import splitext, basename, dirname, exists, abspath
-from inspect import isfunction, ismethod, ismethoddescriptor, isclass, \
- isbuiltin
-from inspect import isdatadescriptor
from logilab.common.modutils import modpath_from_file
from logilab.astng.exceptions import ASTNGBuildingException, InferenceError
-from logilab.astng.raw_building import build_module, object_build_class, \
- object_build_function, object_build_datadescriptor, attach_dummy_node, \
- object_build_methoddescriptor, attach_const_node, attach_import_node
+from logilab.astng.raw_building import InspectBuilder
from logilab.astng.rebuilder import TreeRebuilder
from logilab.astng.manager import ASTNGManager
from logilab.astng.bases import YES, Instance
@@ -51,22 +46,18 @@ def parse(string):
MANAGER = ASTNGManager()
-class ASTNGBuilder:
+class ASTNGBuilder(InspectBuilder):
"""provide astng building methods
"""
rebuilder = TreeRebuilder()
def __init__(self, manager=None):
self._manager = manager or MANAGER
- self._module = None
- self._done = None
- self._dyn_modname_map = {'gtk': 'gtk._gtk'}
def module_build(self, module, modname=None):
"""build an astng from a living module instance
"""
node = None
- self._module = module
path = getattr(module, '__file__', None)
if path is not None:
path_, ext = splitext(module.__file__)
@@ -78,23 +69,6 @@ class ASTNGBuilder:
node = self.inspect_build(module, modname=modname, path=path)
return node
- def inspect_build(self, module, modname=None, path=None):
- """build astng from a living module (i.e. using inspect)
- this is used when there is no python source code available (either
- because it's a built-in module or because the .py is not available)
- """
- self._module = module
- if modname is None:
- modname = module.__name__
- node = build_module(modname, module.__doc__)
- node.file = node.path = path and abspath(path) or path
- if self._manager is not None:
- self._manager._cache[modname] = node
- node.package = hasattr(module, '__path__')
- self._done = {}
- self.object_build(node, module)
- return node
-
def file_build(self, path, modname=None):
"""build astng from a source code file (i.e. from an ast)
@@ -211,84 +185,3 @@ class ASTNGBuilder:
except InferenceError:
pass
-
- # astng from living objects ###############################################
- #
- # this is actually a really minimal representation, including only Module,
- # Function and Class nodes and some others as guessed
-
- def object_build(self, node, obj):
- """recursive method which create a partial ast from real objects
- (only function, class, and method are handled)
- """
- if obj in self._done:
- return self._done[obj]
- self._done[obj] = node
- for name in dir(obj):
- try:
- member = getattr(obj, name)
- except AttributeError:
- # damned ExtensionClass.Base, I know you're there !
- attach_dummy_node(node, name)
- continue
- if ismethod(member):
- member = member.im_func
- if isfunction(member):
- # verify this is not an imported function
- if member.func_code.co_filename != getattr(self._module, '__file__', None):
- attach_dummy_node(node, name, member)
- continue
- object_build_function(node, member, name)
- elif isbuiltin(member):
- # verify this is not an imported member
- if self._member_module(member) != self._module.__name__:
- imported_member(node, member, name)
- continue
- object_build_methoddescriptor(node, member, name)
- elif isclass(member):
- # verify this is not an imported class
- if self._member_module(member) != self._module.__name__:
- imported_member(node, member, name)
- continue
- if member in self._done:
- class_node = self._done[member]
- if not class_node in node.locals.get(name, ()):
- node.add_local_node(class_node, name)
- else:
- class_node = object_build_class(node, member, name)
- # recursion
- self.object_build(class_node, member)
- if name == '__class__' and class_node.parent is None:
- class_node.parent = self._done[self._module]
- elif ismethoddescriptor(member):
- assert isinstance(member, object)
- object_build_methoddescriptor(node, member, name)
- elif isdatadescriptor(member):
- assert isinstance(member, object)
- object_build_datadescriptor(node, member, name)
- elif isinstance(member, (int, long, float, str, unicode)) or member is None:
- attach_const_node(node, name, member)
- else:
- # create an empty node so that the name is actually defined
- attach_dummy_node(node, name, member)
-
- def _member_module(self, member):
- modname = getattr(member, '__module__', None)
- return self._dyn_modname_map.get(modname, modname)
-
-
-def imported_member(node, member, name):
- """consider a class/builtin member where __module__ != current module name
-
- check if it's sound valid and then add an import node, else use a dummy node
- """
- # /!\ some classes like ExtensionClass doesn't have a
- # __module__ attribute !
- member_module = getattr(member, '__module__', '__builtin__')
- try:
- getattr(sys.modules[member_module], name)
- except (KeyError, AttributeError):
- attach_dummy_node(node, name, member)
- else:
- attach_import_node(node, member_module, name)
-