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)
| 17 | pass |
| 18 | |
| 19 | def 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)' |