lazyweb code review

I’m writing a tool in python that will automate the posting of the weekly newsletters for the film club. Currently, I manually post to LiveJournal, Craigslist, the SNFC webpage, and the mailing list. I had a tool a long time ago but it broke for various reasons.

Anyway, I want to have a directory of .py containing classes, and call a method [execute()] on each of them. This way I can just create a new subclass, pop it in that directory, and it will be picked up automatically. This is what I came up with, somehow I feel it’s crude and there is a better way to do it (ignore bad var names pls):

plugins = glob.glob("plugins/*.py")

for x in plugins:
pathName = x.replace(".py","")
className = x.replace(".py","").replace("plugins/","")

foo = __import__(pathName,globals(), locals(), [''])
bar = getattr(foo,className)

obj = bar()

obj.execute(nl)

This code looks for all of the .py files in the dir, imports them, gets the class from the imported module, and instantiates them. Finally, it calls the execute() method. Any suggestions?

Leave a Reply