diff --git a/src/base/bt/__init__.py b/src/base/bt/__init__.py index 8caaaa69..a6d0c4e8 100644 --- a/src/base/bt/__init__.py +++ b/src/base/bt/__init__.py @@ -1,4 +1,5 @@ # This is the base tools package # It contains various tools needed by the base layer of ocarina2 -__all__ = ["file", "map", "message", "needle", "plugin", "proc", "signal", "xm"] \ No newline at end of file +__all__ = ["alias", "file", "map", "message", "needle", "plugin", "proc", + "signal", "sql", "xm"] \ No newline at end of file diff --git a/src/base/bt/alias.py b/src/base/bt/alias.py new file mode 100644 index 00000000..12a52337 --- /dev/null +++ b/src/base/bt/alias.py @@ -0,0 +1,32 @@ +#! /usr/bin/python + +# To change this template, choose Tools | Templates +# and open the template in the editor. + +__author__="bjschuma" +__date__ ="$Jan 23, 2010 2:33:21 PM$" + + +global aliases +aliases = dict() + +def register(key,value): + aliases[key] = value + + +def unregister(key): + del aliases[key] + + +def get(key): + vals = aliases[key] + if len(vals) > 1: + return vals[0], vals[1:] + return vals[0], None + + +def has(key): + return key in aliases.keys() + + +#register("lsmod",["plugins","list"]) diff --git a/src/base/bt/message.py b/src/base/bt/message.py index 8448bee9..a9cdc50b 100644 --- a/src/base/bt/message.py +++ b/src/base/bt/message.py @@ -8,6 +8,19 @@ import inspect import settings import bt.signal +global enabled +enabled = True + + +def disable(): + global enabled + enabled = False + + +def enable(): + global enabled + enabled = True + def disp(text): print text @@ -15,6 +28,9 @@ def disp(text): # Print general text to the screen def write(text,verbose=False): + global enabled + if enabled == False: + return if (verbose==False) or (settings.get("verbose")==True): bt.signal.emit("write",str(text)) #w = settings.get("write") diff --git a/src/base/bt/plugin.py b/src/base/bt/plugin.py index 279f01df..0f69957e 100644 --- a/src/base/bt/plugin.py +++ b/src/base/bt/plugin.py @@ -16,6 +16,7 @@ class Plugin: def __init__(self): self.name = "plugin" self.help = "No help message available" + self.usage = "" self.type = "core" @@ -28,7 +29,7 @@ class Plugin: def gethelp(self): - return self.help + return self.help, self.usage def start(self,args=None): diff --git a/src/base/bt/sql.py b/src/base/bt/sql.py new file mode 100644 index 00000000..b9e38c18 --- /dev/null +++ b/src/base/bt/sql.py @@ -0,0 +1,113 @@ +#! /usr/bin/python + +# To change this template, choose Tools | Templates +# and open the template in the editor. + +__author__="bjschuma" +__date__ ="$Jan 23, 2010 8:40:03 PM$" + +from bt.file import checkPath +import sqlite3 +import settings +from bt.file import * + + + +def getdb(): + return join(settings.get("appdir"), settings.get("appname")+".db") + + +def connect(): + path = getdb() + con = sqlite3.connect(path) + con.text_factory = str + return con + + +def disconnect(con): + con.commit() + con.close() + + + + +class Statement(): + def __init__(self): + self.statement = "" + self.con = connect() + + def __del__(self): + self.con.close() + + def commit(self): + self.con.commit() + + def execute(self,vals=None): + if self.statement == "": + return + if vals==None: + return self.con.execute(self.statement) + else: + return self.con.execute(self.statement,vals) + + + + +class CTable(Statement): + def __init__(self,name): + Statement.__init__(self) + self.name = name + self.cols = [] + + def addcol(self,name,type,params=""): + self.cols += [(name,type,params)] + + def execute(self): + self.statement = "CREATE TABLE "+self.name + " (" + for index,col in enumerate(self.cols): + if not index == 0: + self.statement += ", " + self.statement += " " + col[0] + " " + col[1] + " " + col[2] + self.statement += " );" + return Statement.execute(self) + + + + +class Select(Statement): + def __init__(self,What,From,Where=None): + Statement.__init__(self) + self.What = What + self.From = From + self.Where = Where + + def execute(self): + self.statement = "SELECT " + self.What + self.statement += " FROM " + self.From + if not self.Where==None: + self.statement += " WHERE " + self.Where + return Statement.execute(self) + + + + +class Insert(Statement): + def __init__(self,table,values=[]): + Statement.__init__(self) + self.table = table + self.values = values + + def addval(self,val): + self.values += [val] + + def execute(self): + self.statement = "INSERT INTO "+ self.table + " VALUES(" + qs = "" + for i in range(len(self.values)): + if not i == 0: + qs+="," + qs+="?" + self.statement+=qs + ")" + #write(self.statement) + Statement.execute(self, self.values) + diff --git a/src/base/manager.py b/src/base/manager.py index e3803f31..22b0d2cc 100644 --- a/src/base/manager.py +++ b/src/base/manager.py @@ -7,6 +7,7 @@ import sys from bt.message import * from bt.file import * from bt.signal import * +from bt import alias import loader import settings @@ -79,10 +80,6 @@ class Manager: # Disable all plugins def shutdown(self): - #funcs = settings.get("loopquit",True) - #if not funcs==None: - # for func in funcs: - # func() emit("quit") write("Shutting down manager, disabling all active plugins", True) session = self.findsession() @@ -106,6 +103,13 @@ class Manager: def run(self,name,args=None): name = name.strip() + # Aliases have top priority + if alias.has(name) == True: + cmd,arg = alias.get(name) + if not args==None: + arg += args + return self.run(cmd,arg) + if (name in self.enabled)==True: try: return self.enabled[name].start(args) diff --git a/src/base/scion.py b/src/base/scion.py index 1a2c4120..4744f49c 100644 --- a/src/base/scion.py +++ b/src/base/scion.py @@ -14,6 +14,7 @@ from bt.message import write # We need this next import to set the process name from bt.proc import setname from bt.signal import emit +from bt.file import join # Import the plugin loader class! import loader @@ -30,8 +31,10 @@ def main(): for path in settings.get("PLUGPATH"): loadPluginPath(path) - write("Welcome to "+settings.get("appname")+"!") - setname(settings.get("appname")) + app = settings.get("appname") + write("Welcome to "+app+"!") + setname(app) + settings.set("appdir", join(settings.get("user"), "."+app) ) manager.manager.restoresession() manager.manager.startup() diff --git a/src/core/alias.py b/src/core/alias.py new file mode 100644 index 00000000..739c0de5 --- /dev/null +++ b/src/core/alias.py @@ -0,0 +1,57 @@ +# Basic plugin class + +__author__="bjschuma" +__date__ ="$Jan 23, 2010 2:46:07 PM$" + + +from bt import plugin +from bt import alias +from bt.message import write +from bt.file import * +import settings + + +class Plugin(plugin.Plugin): + def __init__(self): + plugin.Plugin.__init__(self) + self.help = "Used to create aliases to shorten commands" + self.usage = "alias [list, new old]" + + + def getfile(self,flags='r'): + #path = join( settings.get("user"), "."+settings.get("appname") ) + path = join(settings.get("appdir"), "aliases") + return fopen(path,flags) + + + def open(self): + file = self.getfile() + if file == None: + return + for line in file: + self.run(line.split()) + + + def close(self): + file = self.getfile('w') + join = " " + for key in alias.aliases: + line = key + " " + join.join(alias.aliases[key] ) + file.write(line + "\n") + file.close() + + + def run(self, args=None): + if args==None or len(args)<1: + write("Usage: "+self.usage) + return + + if len(args)==1 and args[0]=="list": + for key in alias.aliases.keys(): + write(key + " = " + str(alias.aliases[key])) + return + + alias.register(args[0],args[1:]) + + + diff --git a/src/core/cline/loop.py b/src/core/cline/loop.py index 16e574e8..1b08b92f 100644 --- a/src/core/cline/loop.py +++ b/src/core/cline/loop.py @@ -83,6 +83,7 @@ def loop(): stdscr = settings.get("stdscr") input = settings.get("prompt") + write("Type \"help\" for a list of valid commands") disp(input) while halt == False: diff --git a/src/core/exit.py b/src/core/exit.py index 17175956..0ec458c3 100644 --- a/src/core/exit.py +++ b/src/core/exit.py @@ -14,6 +14,7 @@ class Plugin(plugin.Plugin): def __init__(self): plugin.Plugin.__init__(self) self.help = "This plugin begins the shutdown routine" + self.usage = "exit" def run(self,args=None): diff --git a/src/core/help.py b/src/core/help.py index adde2510..7e76443b 100644 --- a/src/core/help.py +++ b/src/core/help.py @@ -4,7 +4,7 @@ __author__="bjschuma" __date__ ="$Jan 20, 2010 8:01:55 PM$" -from bt.message import write +from bt.message import write, disable, enable from bt import plugin from manager import manager @@ -13,11 +13,19 @@ class Plugin(plugin.Plugin): def __init__(self): plugin.Plugin.__init__(self) self.help = "Returns a short description of the plugin" + self.usage = "help [plugin name]" def run(self, args=None): if args == None: - plugin = "help" + disable() + list = manager.run("plugins", ["list", "enabled"]) + join = ", " + enable() + write("Usage: " + self.usage) + write("Valid commands are: ") + write(join.join(list)) + return else: plugin = args[0] @@ -31,8 +39,11 @@ class Plugin(plugin.Plugin): try: if not module == None: - message = module.gethelp() + message,usage = module.gethelp() + write(message) + if not usage=="": + write("Usage: "+usage) return message except: write("Plugin "+plugin+" has no help message.") diff --git a/src/core/plugins.py b/src/core/plugins.py index 3bbeb6ea..e27559bc 100644 --- a/src/core/plugins.py +++ b/src/core/plugins.py @@ -13,7 +13,7 @@ class Plugin(plugin.Plugin): def __init__(self): plugin.Plugin.__init__(self) self.help = "Enable, disable, or reload plugins" - self.usage = "Usage: plugins [enable, disable, reload, list]" + self.usage = "plugins [enable, disable, reload, list]" def list(self,args): @@ -52,7 +52,7 @@ class Plugin(plugin.Plugin): def run(self, args=None): if args == None: - write(self.usage) + write("Usage:" + self.usage) return if args[0] == "enable": @@ -64,7 +64,7 @@ class Plugin(plugin.Plugin): elif args[0] == "list": return self.list(args[1:]) else: - write(self.usage) + write("Usage: " + self.usage) return for plugin in args[1:]: diff --git a/src/core/sql.py b/src/core/sql.py new file mode 100644 index 00000000..3b3a214a --- /dev/null +++ b/src/core/sql.py @@ -0,0 +1,51 @@ +# Basic plugin class + +__author__="bjschuma" +__date__ ="$Jan 23, 2010 9:05:45 PM$" + + +from bt import plugin +from bt.message import write +from bt import sql + + +class Plugin(plugin.Plugin): + def __init__(self): + plugin.Plugin.__init__(self) + self.help = "Use to execute sql commands" + self.usage = "sql [table]" + + + def createTable(self,args): + table = sql.CTable("test") + table.addcol("col","text") + table.addcol("id","int") + table.execute() + + + def select(self,args): + sel = sql.Select("*", "test") + result = sel.execute().fetchall() + write(result) + + + def insert(self,args): + ins = sql.Insert("test") + ins.addval("one") + ins.addval(1) + ins.execute() + ins.commit() + + + def run(self, args=None): + if args==None or len(args)<1: + write("Usage: "+self.usage) + return + + if args[0] == "table": + self.createTable(args[1:]) + elif args[0] == "select": + self.select(args[1:]) + elif args[0] == "insert": + self.insert(args[1:]) + diff --git a/src/extra/lsset.py b/src/extra/lsset.py index 1f00eb04..ab03de95 100644 --- a/src/extra/lsset.py +++ b/src/extra/lsset.py @@ -14,6 +14,7 @@ class Plugin(plugin.Plugin): def __init__(self): plugin.Plugin.__init__(self) self.help = "Used to show the current state of settings" + self.usage = "lsset [settings key]" def run(self, args=None): @@ -25,5 +26,5 @@ class Plugin(plugin.Plugin): write(str) else: if settings.has(args[0]) == True: - write( settings.get(args[0], True) ) + write( settings.get(args[0]) ) diff --git a/src/extra/sgtk.py b/src/extra/sgtk.py index 5a29f805..a34247aa 100644 --- a/src/extra/sgtk.py +++ b/src/extra/sgtk.py @@ -22,6 +22,7 @@ class Plugin(plugin.Plugin): def __init__(self): plugin.Plugin.__init__(self) self.help = "Allows the usage of GTK" + self.usage = "sgtk" def open(self):