MCPcopy Create free account
hub / github.com/Distrotech/reportlab / getObjectsDefinedIn

Function getObjectsDefinedIn

tools/docco/codegrab.py:19–139  ·  view source on GitHub ↗

Returns two tuple of (functions, classes) defined in the given module. 'directory' must be the directory containing the script; modulename should not include the .py suffix

(modulename, directory=None)

Source from the content-addressed store, hash-verified

17 pass
18
19def getObjectsDefinedIn(modulename, directory=None):
20 """Returns two tuple of (functions, classes) defined
21 in the given module. 'directory' must be the directory
22 containing the script; modulename should not include
23 the .py suffix"""
24
25 if directory:
26 searchpath = [directory]
27 else:
28 searchpath = sys.path # searches usual Python path
29
30 #might be a package. If so, check the top level
31 #package is there, then recalculate the path needed
32 words = string.split(modulename, '.')
33 if len(words) > 1:
34 packagename = words[0]
35 packagefound = imp.find_module(packagename, searchpath)
36 assert packagefound, "Package %s not found" % packagename
37 (file, packagepath, description) = packagefound
38 #now the full path should be known, if it is in the
39 #package
40
41 directory = apply(os.path.join, tuple([packagepath] + words[1:-1]))
42 modulename = words[-1]
43 searchpath = [directory]
44
45
46
47 #find and import the module.
48 found = imp.find_module(modulename, searchpath)
49 assert found, "Module %s not found" % modulename
50 (file, pathname, description) = found
51 mod = imp.load_module(modulename, file, pathname, description)
52
53 #grab the code too, minus trailing newlines
54 lines = open(pathname, 'r').readlines()
55 lines = map(string.rstrip, lines)
56
57 result = Struct()
58 result.functions = []
59 result.classes = []
60 result.doc = mod.__doc__
61 for name in dir(mod):
62 value = getattr(mod, name)
63 if type(value) is types.FunctionType:
64 path, file = os.path.split(value.func_code.co_filename)
65 root, ext = os.path.splitext(file)
66 #we're possibly interested in it
67 if root == modulename:
68 #it was defined here
69 funcObj = value
70 fn = Struct()
71 fn.name = name
72 fn.proto = getFunctionPrototype(funcObj, lines)
73 if funcObj.__doc__:
74 fn.doc = dedent(funcObj.__doc__)
75 else:
76 fn.doc = '(no documentation string)'

Callers 1

dumpDocFunction · 0.85

Calls 6

StructClass · 0.85
getFunctionPrototypeFunction · 0.85
dedentFunction · 0.85
appendMethod · 0.80
splitMethod · 0.45
sortMethod · 0.45

Tested by

no test coverage detected