ocarina/src/base/loader.py

65 lines
1.4 KiB
Python

# This class is used to load plugins
import sys
from bt.message import write
from bt.file import *
class PluginLoader:
def __init__(self):
# Plugins are added to this array upon loading
self.plugins = []
def clearPlugins(self):
self.plugins = []
def getPlugins(self):
plugs = self.plugins
self.clearPlugins()
return plugs
# Load plugins from a directory
def loaddir(self, dir):
write("Loading plugins from " + dir, True)
# Add the directory to our import path
sys.path.append(dir)
modlist = ls(dir)
# ls will return false if the directory doesn't exst.
# We check this here to avoid checking if the dir exists twice
if modlist == False:
write("Directory does not exist: "+dir)
return
for mod in modlist:
split = mod.rsplit('.',1)
# Check for things we should not import
if split[0]=="__init__":
continue
elif len(split)>1 and split[1]=="pyc":
continue
# Load the module into our module array
write("Attempting to load "+mod, True)
self.loadmod(split[0], os.path.join(dir,mod))
# Call with a module name to import and path to plugin
# Adds the imported module to a list
def loadmod(self,mod,path):
__import__(mod)
plugin = sys.modules[mod]
plugin.name = mod
plugin.path = path
self.plugins += [plugin]
# Load specific plugin
def loadpath(self, path):
write("Loading " + path)
load = PluginLoader()