From f63bc51d2e58c8b21e2ac7d3317b93722458793b Mon Sep 17 00:00:00 2001 From: bjschuma Date: Fri, 18 Dec 2009 00:07:45 -0500 Subject: [PATCH] Can override the default write function, and possibly other settings values too --- src/base/bt/file.py | 11 +++++- src/base/bt/message.py | 10 +++++- src/base/bt/proc.py | 2 +- src/base/loader.py | 27 ++++++++++----- src/base/manager.py | 76 +++++++++++++++++------------------------- src/base/ocarina.py | 44 ------------------------ src/base/settings.py | 19 +++++++---- src/core/example.py | 12 +++++-- src/ocarina | 4 --- 9 files changed, 91 insertions(+), 114 deletions(-) delete mode 100644 src/base/ocarina.py delete mode 100644 src/ocarina diff --git a/src/base/bt/file.py b/src/base/bt/file.py index 8ea7c3fe..2d5639b4 100644 --- a/src/base/bt/file.py +++ b/src/base/bt/file.py @@ -40,5 +40,14 @@ def fopen(path,flags='r'): # Return a listing of directory contents def ls(path): if checkDir(path) == False: - return [] + return False return os.listdir(path) + + + +# Remove a file +def rm(path): + if checkPath(path)==False: + write("File does not exist: "+path) + return; + os.remove(path) \ No newline at end of file diff --git a/src/base/bt/message.py b/src/base/bt/message.py index 5f5577be..4cee3e56 100644 --- a/src/base/bt/message.py +++ b/src/base/bt/message.py @@ -15,8 +15,16 @@ def error(text): filename = filename.rsplit(os.sep,1)[1] print filename,"("+lineno+"):",text + +def disp(text): + print text + + # Print general text to the screen def write(text,verbose=False): if (verbose==False) or (settings.get("verbose")==True): - print text + w = settings.get("write") + w(text) + +settings.set("write", disp) \ No newline at end of file diff --git a/src/base/bt/proc.py b/src/base/bt/proc.py index b423f024..e1e7518a 100644 --- a/src/base/bt/proc.py +++ b/src/base/bt/proc.py @@ -10,7 +10,7 @@ from message import write def setname(name): # Set the process name (thank you exaile.py) # This only works on linux2 machines - message = "Attempting to set process name to ocarina2..." + message = "Attempting to set process name to " + name + "..." if settings.settings["ARCH"] == 'linux2': try: import ctypes diff --git a/src/base/loader.py b/src/base/loader.py index 3e7a7604..b0c3c66a 100644 --- a/src/base/loader.py +++ b/src/base/loader.py @@ -15,17 +15,26 @@ class PluginLoader: self.plugins = [] + def getPlugins(self): + plugs = self.plugins + self.clearPlugins() + return plugs + + # Load plugins from a directory def loaddir(self, dir): - exists = checkDir(dir) - # Exit now if the directory doesn't exist - if exists == False: - return 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 @@ -35,15 +44,16 @@ class PluginLoader: continue # Load the module into our module array write("Attempting to load "+mod, True) - self.loadmod(split[0]) + self.loadmod(split[0], os.path.join(dir,mod)) - # Call with a module name to import + # Call with a module name to import and path to plugin # Adds the imported module to a list - def loadmod(self,mod): + def loadmod(self,mod,path): __import__(mod) plugin = sys.modules[mod] - plugin.__name__ = mod + plugin.name = mod + plugin.path = path self.plugins += [plugin] @@ -52,3 +62,4 @@ class PluginLoader: write("Loading " + path) +load = PluginLoader() \ No newline at end of file diff --git a/src/base/manager.py b/src/base/manager.py index 110145c8..129f458c 100644 --- a/src/base/manager.py +++ b/src/base/manager.py @@ -5,61 +5,56 @@ __date__ ="$Dec 8, 2009 8:40:36 AM$" import sys from bt.message import write +import loader class Manager: def __init__(self): write("Creating plugin manager", True) - self.enabled = [] - # Map plugin name to dictionary - self.disabled = [] + # Map the plugin name to a dictionary + self.enabled = dict() + self.disabled = dict() def addPlugins(self, plugins): # Add each plugin to the disabled list for plugin in plugins: write("Adding plugin: " + str(plugin), True) - self.disabled += [plugin] + self.disabled[plugin.name] = plugin # If we are adding a core plugin, activate it right away - if(plugin.__type__=="core"): - self.enablePlugin(plugin.__name__) + if(plugin.type=="core"): + self.enablePlugin(plugin.name) - # Move plugin from old[index] to new + # Move plugin from old[name] to new[name] # Return the plugin - def movePlugin(self, index, old, new): - plugin = old.pop(index) - index = len(new) - new += [plugin] - return plugin,index + def movePlugin(self, name, old, new): + plugin = old.pop(name) + new[name] = plugin + return plugin - # Disable a plugin, return index of disabled plugin + # Disable a plugin + # Returns true if there was a plugin to disable, false otherwise def disablePlugin(self,name): - index = self.findPlugin(name, self.enabled) - if index > -1: + if (name in self.enabled) == True: write("Disabling plugin: "+name) - plugin,index = self.movePlugin(index, self.enabled, self.disabled) + plugin = self.movePlugin(name, self.enabled, self.disabled) plugin.close() - return index + return True + return False - # Move a plugin to the enabled list, return number of plugins enabled + # Move a plugin to the enabled list + # Returns true if a plugin was enabled, false otherwise def enablePlugin(self,name): - index = self.findPlugin(name, self.disabled) - if index > -1: + #index = self.findPlugin(name, self.disabled) + #if index > -1: + if (name in self.disabled) == True: write("Enabling plugin: "+name,True) - plugin,index = self.movePlugin(index, self.disabled, self.enabled) + plugin = self.movePlugin(name, self.disabled, self.enabled) plugin.open() - return True - - - # Return first location where plugin.__name__ == name - def findPlugin(self,name,array): - write("Finding plugin: "+name,True) - for i in range(len(array)): - if array[i].__name__ == name: - return i - return -1 + return True + return False # Disable the plugin, reload it, reenable it @@ -68,22 +63,11 @@ class Manager: if (name in sys.modules) == False: write("Plugin not loaded: "+name, True) return + # Disable the plugin and pop from sys.modules reenable = self.disablePlugin(name) - - #import sys - #write(sys.modules["example"]) - #return - #reenable = self.disablePlugin(name) - #index = self.findPlugin(name, self.disabled) - #write("Reloading plugin: "+name) - #plugin = self.disabled[index] - #print plugin - #reload(plugin) - # Only reenable if a plugin was disabled - #if reenable == True: - # self.enablePlugin(name) - - + reload(self.disabled[name]) + if reenable == True: + self.enablePlugin(name) global manager diff --git a/src/base/ocarina.py b/src/base/ocarina.py deleted file mode 100644 index 97ecdc5a..00000000 --- a/src/base/ocarina.py +++ /dev/null @@ -1,44 +0,0 @@ -# To change this template, choose Tools | Templates -# and open the template in the editor. - -__author__="bjschuma" -__date__ ="$Dec 4, 2009 3:37:21 PM$" - -global name -name = "ocarina2" - -# We need to import settings before we can use disp() -import settings - -# The first thing we do is import write() so we can occasionally print messages -from bt.message import write -write("Welcome to Ocarina2") - -# Next, we set the process name to ocarina2 -from bt.proc import setname -setname(name) - -# Import the plugin loader class! -import loader -import manager - - -def loadPluginPath(path): - # Load a plugin directory, add give to the plugin manager - load = loader.PluginLoader() - load.loaddir("../core") - manager.manager.addPlugins(load.plugins) - - -# Begin our main loop -def main(): - load = loader.PluginLoader() - for path in settings.get("PLUGPATH"): - loadPluginPath(path) - - raw_input("Input something:") - manager.manager.reloadPlugin("example") - - - -if __name__ == "__main__":main() diff --git a/src/base/settings.py b/src/base/settings.py index 716697cc..f9ccaf9e 100644 --- a/src/base/settings.py +++ b/src/base/settings.py @@ -2,9 +2,6 @@ import os.path __author__="bjschuma" __date__ ="$Dec 4, 2009 4:04:24 PM$" -from bt.message import write -from bt.message import error -from bt.file import fopen #import bt.message.disp import os @@ -16,6 +13,19 @@ import getopt global settings settings = dict() + +# Set key to value +# This must be defined before other imports so bt.message can use it +def set(key,value): + settings[key.upper()] = value + + +from bt.message import write +from bt.message import error +from bt.file import fopen + + + # Return the value at key def get(key): return settings[key.upper()] @@ -43,9 +53,6 @@ def parseInput(): return - - - # Set default values # Set verbose first so we can use write() settings["VERBOSE"] = ('-v' in sys.argv) or ("--verbose" in sys.argv) diff --git a/src/core/example.py b/src/core/example.py index cef74541..80ca8e74 100644 --- a/src/core/example.py +++ b/src/core/example.py @@ -3,9 +3,12 @@ __author__="bjschuma" __date__ ="$Dec 7, 2009 9:12:00 AM$" -__name__="test" -__type__="core" -__opt__=[] + +global name, type, path, opt +name = "test" +type = "core" +path = "" +opt = [] from bt.message import write @@ -13,12 +16,15 @@ from bt.message import write # Called every time the plugin is enabled def open(): write("Example plugin has been started",True) + #write("Example plugin has been changed",True) +# Called every time the plugin is stopped def close(): write("Example plugin has been stopped") pass +# Called when the plugin needs to perform some action def run(): pass diff --git a/src/ocarina b/src/ocarina deleted file mode 100644 index 538c2c48..00000000 --- a/src/ocarina +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -# This is a simple shell script for properly starting ocarina -cd base && `which python` ocarina.py $* \ No newline at end of file