diff options
author | Ka-Ping Yee <ping@zesty.ca> | 2001-04-10 11:43:00 (GMT) |
---|---|---|
committer | Ka-Ping Yee <ping@zesty.ca> | 2001-04-10 11:43:00 (GMT) |
commit | 4d6fc7fae18d15160350d210ae58043312564459 (patch) | |
tree | 21a53299c5366e82849dfe00822bfc9a8d39c675 | |
parent | 697c9c9c718475767e334793c0118f6015ec7cd6 (diff) | |
download | cpython-4d6fc7fae18d15160350d210ae58043312564459.zip cpython-4d6fc7fae18d15160350d210ae58043312564459.tar.gz cpython-4d6fc7fae18d15160350d210ae58043312564459.tar.bz2 |
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
-rw-r--r-- | Lib/inspect.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index eeb3034..140dbd1 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -192,6 +192,21 @@ def getfile(object): raise TypeError, 'arg is not a module, class, method, ' \ 'function, traceback, frame, or code object' +def getmoduleinfo(path): + """Get the module name, suffix, mode, and module type for a given file.""" + filename = os.path.basename(path) + suffixes = map(lambda (suffix, mode, mtype): + (-len(suffix), suffix, mode, mtype), imp.get_suffixes()) + suffixes.sort() # try longest suffixes first, in case they overlap + for neglen, suffix, mode, mtype in suffixes: + if filename[neglen:] == suffix: + return filename[:neglen], suffix, mode, mtype + +def getmodulename(path): + """Return the module name for a given file, or None.""" + info = getmoduleinfo(path) + if info: return info[0] + def getsourcefile(object): """Return the Python source file an object was defined in, if it exists.""" filename = getfile(object) |