Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7a840e8

Browse files
committed
Add support for dotted module names to readmodule().
1 parent 1c5fb1c commit 7a840e8

1 file changed

Lines changed: 26 additions & 18 deletions

File tree

Lib/pyclbr.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,22 @@ def __init__(self, module, name, super, file, lineno):
6565
def _addmethod(self, name, lineno):
6666
self.methods[name] = lineno
6767

68-
def readmodule(module, path = []):
68+
def readmodule(module, path=[], inpackage=0):
6969
'''Read a module file and return a dictionary of classes.
7070
7171
Search for MODULE in PATH and sys.path, read and parse the
7272
module and return a dictionary with one entry for each class
7373
found in the module.'''
7474

75+
i = string.rfind(module, '.')
76+
if i >= 0:
77+
# Dotted module name
78+
package = module[:i]
79+
submodule = module[i+1:]
80+
parent = readmodule(package, path, inpackage)
81+
child = readmodule(submodule, parent['__path__'], 1)
82+
return child
83+
7584
if _modules.has_key(module):
7685
# we've seen this module before...
7786
return _modules[module]
@@ -83,21 +92,20 @@ def readmodule(module, path = []):
8392

8493
# search the path for the module
8594
f = None
86-
suffixes = imp.get_suffixes()
87-
for dir in path + sys.path:
88-
for suff, mode, type in suffixes:
89-
file = os.path.join(dir, module + suff)
90-
try:
91-
f = open(file, mode)
92-
except IOError:
93-
pass
94-
else:
95-
# found the module
96-
break
97-
if f:
98-
break
99-
if not f:
100-
raise IOError, 'module ' + module + ' not found'
95+
if inpackage:
96+
try:
97+
f, file, (suff, mode, type) = \
98+
imp.find_module(module, path)
99+
except ImportError:
100+
f = None
101+
if f is None:
102+
fullpath = path + sys.path
103+
f, file, (suff, mode, type) = imp.find_module(module, fullpath)
104+
if type == imp.PKG_DIRECTORY:
105+
dict = {'__path__': [file]}
106+
_modules[module] = dict
107+
# XXX Should we recursively look for submodules?
108+
return dict
101109
if type != imp.PY_SOURCE:
102110
# not Python source, can't do anything with this module
103111
f.close()
@@ -130,7 +138,7 @@ def readmodule(module, path = []):
130138
try:
131139
# recursively read the
132140
# imported module
133-
d = readmodule(n, path)
141+
d = readmodule(n, path, inpackage)
134142
except:
135143
print 'module',n,'not found'
136144
pass
@@ -142,7 +150,7 @@ def readmodule(module, path = []):
142150
names = string.splitfields(res.group('imp'), ',')
143151
try:
144152
# recursively read the imported module
145-
d = readmodule(mod, path)
153+
d = readmodule(mod, path, inpackage)
146154
except:
147155
print 'module',mod,'not found'
148156
continue

0 commit comments

Comments
 (0)