|
Description:
For something I am working on, I needed the ability to scan a supplied directory, adding the directory to the sys.path within Python, and then blanket import the modules within that directory. Following that, I had to filter any builtin or special methods within those modules and return a list of the methods for the module I had imported.
The script is very simplistic in what it does.
Source: Text Source
"""
loader.py - From a directory name:
1: append the directory to the sys.path
2: find all modules within that directory
3: import all modules within that directory
4: filter out built in methods from those modules
5: return a list of useable methods from those modules
Allows the user to import a series of python modules without "knowing" anything
about those modules.
Copyright 2005 Jesse Noller <jnoller@gmail.com>
"""
import os, sys
def import_libs(dir):
""" Imports the libs, returns a list of the libraries.
Pass in dir to scan """
library_list = []
for f in os.listdir(os.path.abspath(dir)):
module_name, ext = os.path.splitext(f)
if ext == 'py':
print 'imported module: %s' % (module_name)
module = __import__(module_name)
library_list.append(module)
return library_list
def filter_builtins(module):
""" Filter out the builtin functions, methods from module """
built_in_list = ['__builtins__', '__doc__', '__file__', '__name__']
built_in_list.append('special_remove')
module_methods = dir(module)
for b in built_in_list:
if b in module_methods:
module_methods.remove(b)
print module_methods
return module_methods
def main(dir):
if os.path.isdir(dir):
sys.path.append(dir)
else:
print '%s is not a directory!' % (dir)
lib_list = import_libs(dir)
for l in lib_list:
filter_builtins(l)
if __name__ == "__main__":
if len(sys.argv) < 2:
print "error: missing directory name"
sys.exit(1)
else:
main(sys.argv[1])
Discussion:
This may not be terribly useful to many people - in most cases, not only will you know the module name, but you will also know the methods accessible to you within the module.
Usage example:
jesse$python loader.py mydir/
imported module: basic
imported module: mod1
imported module: mod2
['a_a', 'a_b', 'a_c', 'a_d', 'a_e']
[]
[]
You can use this script, as well as getattr() to actually call the functions returned in the list.
Suggestions welcome!
|