Can override the default write function, and possibly other settings
values too
This commit is contained in:
parent
9db6365e4a
commit
f63bc51d2e
|
@ -40,5 +40,14 @@ def fopen(path,flags='r'):
|
||||||
# Return a listing of directory contents
|
# Return a listing of directory contents
|
||||||
def ls(path):
|
def ls(path):
|
||||||
if checkDir(path) == False:
|
if checkDir(path) == False:
|
||||||
return []
|
return False
|
||||||
return os.listdir(path)
|
return os.listdir(path)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Remove a file
|
||||||
|
def rm(path):
|
||||||
|
if checkPath(path)==False:
|
||||||
|
write("File does not exist: "+path)
|
||||||
|
return;
|
||||||
|
os.remove(path)
|
|
@ -15,8 +15,16 @@ def error(text):
|
||||||
filename = filename.rsplit(os.sep,1)[1]
|
filename = filename.rsplit(os.sep,1)[1]
|
||||||
print filename,"("+lineno+"):",text
|
print filename,"("+lineno+"):",text
|
||||||
|
|
||||||
|
|
||||||
|
def disp(text):
|
||||||
|
print text
|
||||||
|
|
||||||
|
|
||||||
# Print general text to the screen
|
# Print general text to the screen
|
||||||
def write(text,verbose=False):
|
def write(text,verbose=False):
|
||||||
if (verbose==False) or (settings.get("verbose")==True):
|
if (verbose==False) or (settings.get("verbose")==True):
|
||||||
print text
|
w = settings.get("write")
|
||||||
|
w(text)
|
||||||
|
|
||||||
|
|
||||||
|
settings.set("write", disp)
|
|
@ -10,7 +10,7 @@ from message import write
|
||||||
def setname(name):
|
def setname(name):
|
||||||
# Set the process name (thank you exaile.py)
|
# Set the process name (thank you exaile.py)
|
||||||
# This only works on linux2 machines
|
# 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':
|
if settings.settings["ARCH"] == 'linux2':
|
||||||
try:
|
try:
|
||||||
import ctypes
|
import ctypes
|
||||||
|
|
|
@ -15,17 +15,26 @@ class PluginLoader:
|
||||||
self.plugins = []
|
self.plugins = []
|
||||||
|
|
||||||
|
|
||||||
|
def getPlugins(self):
|
||||||
|
plugs = self.plugins
|
||||||
|
self.clearPlugins()
|
||||||
|
return plugs
|
||||||
|
|
||||||
|
|
||||||
# Load plugins from a directory
|
# Load plugins from a directory
|
||||||
def loaddir(self, dir):
|
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)
|
write("Loading plugins from " + dir, True)
|
||||||
|
|
||||||
# Add the directory to our import path
|
# Add the directory to our import path
|
||||||
sys.path.append(dir)
|
sys.path.append(dir)
|
||||||
modlist = ls(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:
|
for mod in modlist:
|
||||||
split = mod.rsplit('.',1)
|
split = mod.rsplit('.',1)
|
||||||
# Check for things we should not import
|
# Check for things we should not import
|
||||||
|
@ -35,15 +44,16 @@ class PluginLoader:
|
||||||
continue
|
continue
|
||||||
# Load the module into our module array
|
# Load the module into our module array
|
||||||
write("Attempting to load "+mod, True)
|
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
|
# Adds the imported module to a list
|
||||||
def loadmod(self,mod):
|
def loadmod(self,mod,path):
|
||||||
__import__(mod)
|
__import__(mod)
|
||||||
plugin = sys.modules[mod]
|
plugin = sys.modules[mod]
|
||||||
plugin.__name__ = mod
|
plugin.name = mod
|
||||||
|
plugin.path = path
|
||||||
self.plugins += [plugin]
|
self.plugins += [plugin]
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,3 +62,4 @@ class PluginLoader:
|
||||||
write("Loading " + path)
|
write("Loading " + path)
|
||||||
|
|
||||||
|
|
||||||
|
load = PluginLoader()
|
|
@ -5,61 +5,56 @@ __date__ ="$Dec 8, 2009 8:40:36 AM$"
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from bt.message import write
|
from bt.message import write
|
||||||
|
import loader
|
||||||
|
|
||||||
class Manager:
|
class Manager:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
write("Creating plugin manager", True)
|
write("Creating plugin manager", True)
|
||||||
self.enabled = []
|
# Map the plugin name to a dictionary
|
||||||
# Map plugin name to dictionary
|
self.enabled = dict()
|
||||||
self.disabled = []
|
self.disabled = dict()
|
||||||
|
|
||||||
|
|
||||||
def addPlugins(self, plugins):
|
def addPlugins(self, plugins):
|
||||||
# Add each plugin to the disabled list
|
# Add each plugin to the disabled list
|
||||||
for plugin in plugins:
|
for plugin in plugins:
|
||||||
write("Adding plugin: " + str(plugin), True)
|
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 we are adding a core plugin, activate it right away
|
||||||
if(plugin.__type__=="core"):
|
if(plugin.type=="core"):
|
||||||
self.enablePlugin(plugin.__name__)
|
self.enablePlugin(plugin.name)
|
||||||
|
|
||||||
|
|
||||||
# Move plugin from old[index] to new
|
# Move plugin from old[name] to new[name]
|
||||||
# Return the plugin
|
# Return the plugin
|
||||||
def movePlugin(self, index, old, new):
|
def movePlugin(self, name, old, new):
|
||||||
plugin = old.pop(index)
|
plugin = old.pop(name)
|
||||||
index = len(new)
|
new[name] = plugin
|
||||||
new += [plugin]
|
return plugin
|
||||||
return plugin,index
|
|
||||||
|
|
||||||
|
|
||||||
# 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):
|
def disablePlugin(self,name):
|
||||||
index = self.findPlugin(name, self.enabled)
|
if (name in self.enabled) == True:
|
||||||
if index > -1:
|
|
||||||
write("Disabling plugin: "+name)
|
write("Disabling plugin: "+name)
|
||||||
plugin,index = self.movePlugin(index, self.enabled, self.disabled)
|
plugin = self.movePlugin(name, self.enabled, self.disabled)
|
||||||
plugin.close()
|
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):
|
def enablePlugin(self,name):
|
||||||
index = self.findPlugin(name, self.disabled)
|
#index = self.findPlugin(name, self.disabled)
|
||||||
if index > -1:
|
#if index > -1:
|
||||||
|
if (name in self.disabled) == True:
|
||||||
write("Enabling plugin: "+name,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()
|
plugin.open()
|
||||||
return True
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
|
|
||||||
# Disable the plugin, reload it, reenable it
|
# Disable the plugin, reload it, reenable it
|
||||||
|
@ -68,22 +63,11 @@ class Manager:
|
||||||
if (name in sys.modules) == False:
|
if (name in sys.modules) == False:
|
||||||
write("Plugin not loaded: "+name, True)
|
write("Plugin not loaded: "+name, True)
|
||||||
return
|
return
|
||||||
|
# Disable the plugin and pop from sys.modules
|
||||||
reenable = self.disablePlugin(name)
|
reenable = self.disablePlugin(name)
|
||||||
|
reload(self.disabled[name])
|
||||||
#import sys
|
if reenable == True:
|
||||||
#write(sys.modules["example"])
|
self.enablePlugin(name)
|
||||||
#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)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
global manager
|
global manager
|
||||||
|
|
|
@ -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()
|
|
|
@ -2,9 +2,6 @@ import os.path
|
||||||
__author__="bjschuma"
|
__author__="bjschuma"
|
||||||
__date__ ="$Dec 4, 2009 4:04:24 PM$"
|
__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 bt.message.disp
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
@ -16,6 +13,19 @@ import getopt
|
||||||
global settings
|
global settings
|
||||||
settings = dict()
|
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
|
# Return the value at key
|
||||||
def get(key):
|
def get(key):
|
||||||
return settings[key.upper()]
|
return settings[key.upper()]
|
||||||
|
@ -43,9 +53,6 @@ def parseInput():
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Set default values
|
# Set default values
|
||||||
# Set verbose first so we can use write()
|
# Set verbose first so we can use write()
|
||||||
settings["VERBOSE"] = ('-v' in sys.argv) or ("--verbose" in sys.argv)
|
settings["VERBOSE"] = ('-v' in sys.argv) or ("--verbose" in sys.argv)
|
||||||
|
|
|
@ -3,9 +3,12 @@
|
||||||
__author__="bjschuma"
|
__author__="bjschuma"
|
||||||
__date__ ="$Dec 7, 2009 9:12:00 AM$"
|
__date__ ="$Dec 7, 2009 9:12:00 AM$"
|
||||||
|
|
||||||
__name__="test"
|
|
||||||
__type__="core"
|
global name, type, path, opt
|
||||||
__opt__=[]
|
name = "test"
|
||||||
|
type = "core"
|
||||||
|
path = ""
|
||||||
|
opt = []
|
||||||
|
|
||||||
from bt.message import write
|
from bt.message import write
|
||||||
|
|
||||||
|
@ -13,12 +16,15 @@ from bt.message import write
|
||||||
# Called every time the plugin is enabled
|
# Called every time the plugin is enabled
|
||||||
def open():
|
def open():
|
||||||
write("Example plugin has been started",True)
|
write("Example plugin has been started",True)
|
||||||
|
#write("Example plugin has been changed",True)
|
||||||
|
|
||||||
|
|
||||||
|
# Called every time the plugin is stopped
|
||||||
def close():
|
def close():
|
||||||
write("Example plugin has been stopped")
|
write("Example plugin has been stopped")
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# Called when the plugin needs to perform some action
|
||||||
def run():
|
def run():
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# This is a simple shell script for properly starting ocarina
|
|
||||||
cd base && `which python` ocarina.py $*
|
|
Loading…
Reference in New Issue