Tuesday, June 23, 2009

Plugin for Python

To develop software that extensible and maintainable, we have to separate the concern in our programs. Plugin system is one way to do it. So, I made a Plugin class, for my own programs, that will load python script dynamically.

Here is the code:

class Plugin:
def __init__(self,base,dir):
"""docstring for __"""
self.plugins={}
self.plugin_dir=base+'/'+dir
sys.path.append(self.plugin_dir)
def load(self,fname):
"""docstring for load"""
p=''
if fname.find('.pyc')>0:
p=fname.replace('.pyc','').strip()
#print 'importing:',p
try:
self.plugins[p] = __import__(p)
except:
pass
def load_all(self):
"""docstring for load"""
try:
dir_list=os.listdir(self.plugin_dir)
for fname in dir_list:
self.load(fname)
except Exception, e:
raise e

No comments:

Post a Comment