From be07a50c20f406ce8a3bf6c7c19133d46990e5a1 Mon Sep 17 00:00:00 2001 From: bjschuma Date: Sat, 20 Feb 2010 00:52:06 -0500 Subject: [PATCH] Continued migrating to using a session to store settings. Can load saved settings. --- nbproject/private/private.xml | 1 + nbproject/project.properties | 6 +-- src/clean.sh | 2 + src/core/bt/file.py | 6 +-- src/core/bt/settings.py | 15 +++++++ src/core/bt/signal.py | 1 - src/core/loader.py | 4 +- src/core/manager.py | 70 ++++++--------------------------- src/core/scion.py | 8 +--- src/core/session.py | 33 +++++++++++++--- src/core/settings.py | 74 ----------------------------------- src/doc/signals.txt | 30 +++++++------- src/extra/lsset.py | 6 +-- src/extra/sgtk.py | 20 +++++----- src/plugins/cli.py | 2 +- src/plugins/cline/loop.py | 3 +- src/plugins/cline/run.py | 2 +- src/plugins/exit.py | 4 +- src/plugins/help.py | 2 +- src/plugins/plugins.py | 4 +- 20 files changed, 105 insertions(+), 188 deletions(-) diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index c1f155a7..79ffd31e 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -1,4 +1,5 @@ + diff --git a/nbproject/project.properties b/nbproject/project.properties index 20bcbdbc..022f8ab8 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,5 +1,5 @@ java.lib.path= -main.file=ocarina.py -platform.active=Python_2.6.3 -python.lib.path= +main.file=core/scion.py +platform.active=default +python.lib.path=/usr/bin/python| src.dir=src diff --git a/src/clean.sh b/src/clean.sh index 091e0ed0..ac3cb902 100644 --- a/src/clean.sh +++ b/src/clean.sh @@ -2,5 +2,7 @@ rm -r ./*/*.pyc rm -r ./*/*/*.pyc +#rm -r ./*/*.class +#rm -r ./*/*/*.class #rm -r ./*/*.pyo #rm -r ./*/*/*.pyo diff --git a/src/core/bt/file.py b/src/core/bt/file.py index c39ccb3f..025c9006 100644 --- a/src/core/bt/file.py +++ b/src/core/bt/file.py @@ -17,13 +17,13 @@ def checkDir(path): # Check if path exists def checkPath(path): path = os.path.expanduser(path) - write("Checking if "+path+" exists", True) + write("Checking if "+path+" exists", 2) return os.path.exists(path) def mkdir(path): if checkDir(path)==False: - write("Creating directory: "+path,True) + write("Creating directory: "+path,2) os.mkdir(path) @@ -38,7 +38,7 @@ def fopen(path,flags='r'): if ('r' in flags) == True: exists = checkPath(path) if exists == False: - write(path+" does not exist", True) + write(path+" does not exist", 2) return None return open(path, flags) diff --git a/src/core/bt/settings.py b/src/core/bt/settings.py index 11b9ebf3..e5f57b4e 100644 --- a/src/core/bt/settings.py +++ b/src/core/bt/settings.py @@ -10,6 +10,8 @@ from sys import argv,platform import os from bt.message import write +from bt.file import join +from bt import xm class Settings(dict): @@ -57,4 +59,17 @@ class Settings(dict): def load(self,path): + write("Loading saved settings",1) + path = join(path,"settings") + xm.load(path) + elm = xm.child() + if elm == None: + return + for node in xm.children(elm): + if xm.isElm(node) == False: + continue + key = xm.name(node) + val = xm.value( xm.child(node) ) + write(" ( "+key + ", " + str(val) + " )",2) + self[key] = val pass diff --git a/src/core/bt/signal.py b/src/core/bt/signal.py index 6dc47585..702a8b71 100644 --- a/src/core/bt/signal.py +++ b/src/core/bt/signal.py @@ -6,7 +6,6 @@ __author__="bjschuma" __date__ ="$Jan 20, 2010 12:13:21 AM$" - from map import Map global signals, status signals = Map() diff --git a/src/core/loader.py b/src/core/loader.py index 0a91c571..f8a2e238 100644 --- a/src/core/loader.py +++ b/src/core/loader.py @@ -23,7 +23,7 @@ class PluginLoader: # Load plugins from a directory def loaddir(self, dir): - write("Loading plugins from " + dir, True) + write("Loading plugins from " + dir, 1) # Add the directory to our import path sys.path.append(dir) @@ -45,7 +45,7 @@ class PluginLoader: elif split[1]=="pyc": continue # Load the module into our module array - write("Attempting to load "+mod, True) + write("Attempting to load "+mod, 2) #try: self.loadmod(split[0], os.path.join(dir,mod)) #except: diff --git a/src/core/manager.py b/src/core/manager.py index 3e13f3c7..da08d043 100644 --- a/src/core/manager.py +++ b/src/core/manager.py @@ -9,13 +9,14 @@ from bt.signal import * from bt import alias #import loader -import session +#import session import inspect class Manager: def __init__(self): write("Creating plugin manager", True) + register("scion-end",self.shutdown,1) # Map the plugin name to a dictionary self.enabled = dict() self.disabled = dict() @@ -25,7 +26,7 @@ class Manager: def addPlugins(self, plugins): # Add each plugin to the disabled list for plugin in plugins: - write("Adding plugin: "+plugin.name, True) + write("Adding plugin: "+plugin.name, 2) self.disabled[plugin.name] = plugin @@ -53,7 +54,7 @@ class Manager: def enablePlugin(self,name): if (name in self.disabled) == True: try: - write("Enabling plugin: "+name,True) + write("Enabling plugin: "+name,2) plugin = self.movePlugin(name, self.disabled, self.enabled) plugin.open() return True @@ -82,27 +83,12 @@ class Manager: # Disable all plugins def shutdown(self): - emit("scion-end") - write("Shutting down manager, disabling all active plugins", True) - path = session.getSession() - #session = self.findsession() - self.savesession(path) + write("Shutting down manager, disabling all active plugins", 1) keys = self.enabled.keys() for plugin in keys: self.disablePlugin(plugin) - #session.settings.save(path) - def startup(self): - if len(self.restored) == 0: - for key in self.disabled.keys(): - if self.disabled[key].type == "core": - self.enablePlugin(key) - return - - for name in self.restored: - self.enablePlugin(name) - def run(self,name,args=None): name = name.strip() @@ -124,15 +110,11 @@ class Manager: return None - #def findsession(self): - # path = session.session.settings.get("user") - # if path == expandPath("~"): - # #path = join(path,".scion") - # path = join(path,"."+settings.get("appname")) - # mkdir(path) - # path = join(path,"session") - # mkdir(path) - # return path + def call(self,name,args): + disable() + result = self.run(name,args) + enable() + return result def savesession(self,session): @@ -140,34 +122,4 @@ class Manager: file = fopen(path,'w') for key in self.enabled.keys(): file.write(key+"\n") - file.close() - - - def restoresession(self): - path = session.getSession() - session.settings.load(path) - path = join(path,"enabled") - file = fopen(path) - self.restored = [] - if file==None: - return - for line in file: - self.restored+=[line.strip()] - - - -global manager -manager = Manager() - - -def run(name,args=None): - global manager - return manager.run(name,args) - - -def call(name,args=None): - global manager - disable() - result = manager.run(name,args) - enable() - return result \ No newline at end of file + file.close() \ No newline at end of file diff --git a/src/core/scion.py b/src/core/scion.py index 86ed5f07..bb2ddb24 100644 --- a/src/core/scion.py +++ b/src/core/scion.py @@ -17,12 +17,11 @@ from bt.file import join # Import the plugin loader class! import loader -import manager def loadPluginPath(path): loader.load.loaddir(path) - manager.manager.addPlugins(loader.load.getPlugins()) + session.manager.addPlugins(loader.load.getPlugins()) # Begin our main loop @@ -30,15 +29,12 @@ def main(): for path in session.settings.get("PLUGPATH"): loadPluginPath(path) - emit("scion-plugins-loaded") - app = session.settings["appname"] write("Welcome to "+app+"!") setname(app) session.settings["appdir"] = join(session.settings["user"], "."+app) - manager.manager.restoresession() - manager.manager.startup() + emit("scion-plugins-loaded") emit("scion-begin") diff --git a/src/core/session.py b/src/core/session.py index 2ed264e2..6f9e22f5 100644 --- a/src/core/session.py +++ b/src/core/session.py @@ -9,19 +9,42 @@ __date__ ="$Feb 17, 2010 9:18:50 PM$" from bt.settings import Settings from bt.file import * +from bt.message import write +from bt import signal +from manager import Manager + global settings +global manager def getSession(): - path = settings["user"] - if path == expandPath("~"): - path = join(path,"."+settings["appname"]) - mkdir(path) + path = settings["appdir"] + mkdir(path) path = join(path,"session") mkdir(path) return path +def restore(): + write("Restoring session",1) + path = getSession() + write(path,2) + settings.load(path) + enabled = fopen( join(path,"enabled") ) + if not enabled == None: + for line in enabled: + manager.enablePlugin(line.strip()) + + +def save(): + write("Saving session") + + settings = Settings() -settings.setdefaults() \ No newline at end of file +manager = Manager() +# We want to restore settings as early as possible +settings.setdefaults() + +signal.register("scion-plugins-loaded",restore,0) +signal.register("scion-end",save,0) \ No newline at end of file diff --git a/src/core/settings.py b/src/core/settings.py index 46a1ae27..0e4deaea 100644 --- a/src/core/settings.py +++ b/src/core/settings.py @@ -13,47 +13,6 @@ global settings settings = dict() -# Return true if settings contains key -def has(key): - return (key.upper() in settings.keys()) - - -# Set key to value -# This must be defined before other imports so bt.message can use it -def set(key,value): - key = key.upper() - settings[key] = value - - -# Return the value at key -def get(key): - key = key.upper() - if has(key) == False: - return None - return settings[key] - - -from bt.message import write -from bt.message import error -from bt.file import fopen -from bt.file import join -from bt import xm -from bt import scripting - - - - - -def delete(key): - key = key.upper() - if has(key) == True: - del settings[key] - - -# Read settings from file -def readfile(file): - write("Reading file: "+file,True) - file = fopen(file) # Parse the user input @@ -106,36 +65,3 @@ def save(path): xm.append(t,e) xm.append(e,elm) xm.write(path) - - -def load(path): - write("Loading saved settings",True) - global settings - path = join(path,"settings") - xm.load(path) - elm = xm.child() - if elm == None: - return - for node in xm.children(elm): - if xm.isElm(node) == False: - continue - key = xm.name(node) - val = xm.child(node) - set(key,xm.value(val)) - - -# Set default values -# Set verbose first so we can use write() -set("VERBOSE", ('-v' in sys.argv) or ("--verbose" in sys.argv) ) -write("Setting default values...", True) - -# Find who is running the program -user = os.path.expanduser("~") -set("USER", user) -set("PLUGPATH", ["plugins", "extra"]) - -# Find out what platform we are running on -set("ARCH", sys.platform) - - -parseInput() \ No newline at end of file diff --git a/src/doc/signals.txt b/src/doc/signals.txt index 8ac2f614..166f282c 100644 --- a/src/doc/signals.txt +++ b/src/doc/signals.txt @@ -2,44 +2,46 @@ Documentation for signals, and where they are emitted cli-loop-begin: Called at the beginning of every loop in the cli - Emitted in core.cline.loop.loop() + Emitted in plugins.cline.loop.loop() cli-loop-end: Called at the end of every loop in the cli - Emitted in core.cline.loop.loop() + Emitted in plugins.cline.loop.loop() message-write: Displays text to the screen - Emitted in bt.message.write() - Registered in bt.message - Registered in core.cline.loop.init() - Removed in core.cline.loop.quit() - Stopped in core.cline.message.insert() + Emitted in core.bt.message.write() + Registered in core.bt.message + Registered in plugins.cline.loop.init() + Removed in plugins.cline.loop.quit() + Stopped in plugins.cline.message.insert() scion-begin: Begins a main loop for the program. - Emitted in base.scion.main() - Registered in core.cli.Plugin.open() + Emitted in core.scion.main() + Registered in plugins.cli.Plugin.open() Registered in extra.sgtk.Plugin.open() - Removed in core.cli.Plugin.close() + Removed in plugins.cli.Plugin.close() Removed in extra.sgtk.Plugin.close() scion-end: Ends the main loop and closes all plugins - Emitted in base.manager.shutdown() - Registered in core.cli.Plugin.open() + Emitted in plugins.exit.run() + Registered in plugins.manager.Manager.__init__() + Registered in plugins.cli.Plugin.open() Registered in extra.sgtk.Plugin.open() - Removed in core.cli.Plugin.close() + Removed in plugins.cli.Plugin.close() Removed in extra.sgtk.Plugin.close() scion-plugins-loaded: Called right after loading all the plugins (before restoring settings or enabling any plugins) - Emitted in scion.main() + Emitted in core.scion.main() + Registered in core.session diff --git a/src/extra/lsset.py b/src/extra/lsset.py index 983aea3d..d1b7b859 100644 --- a/src/extra/lsset.py +++ b/src/extra/lsset.py @@ -6,7 +6,7 @@ __date__ ="$Jan 9, 2010 7:22:31 PM$" from bt.message import write -import settings +from session import settings from bt import plugin @@ -18,7 +18,7 @@ class Plugin(plugin.Plugin): def run(self, args=None): - keys = settings.settings.keys() + keys = settings.keys() join = ", " if args == None: @@ -26,5 +26,5 @@ class Plugin(plugin.Plugin): write(str) else: if settings.has(args[0]) == True: - write( settings.get(args[0]) ) + write( settings[args[0]] ) diff --git a/src/extra/sgtk.py b/src/extra/sgtk.py index 130111d5..59eac52a 100644 --- a/src/extra/sgtk.py +++ b/src/extra/sgtk.py @@ -8,8 +8,8 @@ __date__ ="$Jan 13, 2010 12:02:58 AM$" from bt import plugin from bt.message import write from bt import signal -from manager import manager -import settings +#from manager import manager +from session import settings,manager from guiGTK import * import gtk @@ -27,17 +27,17 @@ class Plugin(plugin.Plugin): def open(self): #settings.set("guirunning",False) - if settings.get("guirunning") == True: + if settings["guirunning"] == True: signal.register("scion-begin",self.loop,90) signal.register("scion-end",self.close,90) else: - settings.set("guirunning",False) + settings["guirunning"] = False def close(self): - if settings.get("guirunning") == True: + if settings["guirunning"] == True: gtk.main_quit() - settings.delete("gtkfuncs") + #del settings["gtkfuncs"] signal.remove("scion-begin",self.loop) signal.remove("scion-end",self.close) @@ -71,9 +71,9 @@ class Plugin(plugin.Plugin): bar = menu.makeBar() vbox.pack(bar) - if settings.has("gtkfuncs") == True: - for func in settings.get("gtkfuncs", True): - func([win,vbox,bar]) + #if settings.has("gtkfuncs") == True: + # for func in settings.get("gtkfuncs", True): + # func([win,vbox,bar]) - settings.set("guirunning",True) + settings["guirunning"] = True gtk.main() diff --git a/src/plugins/cli.py b/src/plugins/cli.py index 77b7ece8..c095e844 100644 --- a/src/plugins/cli.py +++ b/src/plugins/cli.py @@ -57,7 +57,7 @@ class Plugin(plugin.Plugin): return if args[0]=="prompt": - settings.set("prompt",args[1]+" ") + settings["prompt"] = args[1] + " " elif args[0]=="maxhist": if len(args) == 1: write(settings.get("maxhist")) diff --git a/src/plugins/cline/loop.py b/src/plugins/cline/loop.py index 554ef97b..cc7ceb34 100644 --- a/src/plugins/cline/loop.py +++ b/src/plugins/cline/loop.py @@ -36,7 +36,8 @@ def init(): settings["maxyx"] = maxyx #if settings.has("prompt") == False: # settings.set("prompt", ">>> ") - settings.init("prompt",">>> ") + settings.init("prompt",">>>") + settings["prompt"] += " " #else: # settings.set("prompt",settings.get("prompt")+" ") diff --git a/src/plugins/cline/run.py b/src/plugins/cline/run.py index d595a6bb..cd2915b0 100644 --- a/src/plugins/cline/run.py +++ b/src/plugins/cline/run.py @@ -6,7 +6,7 @@ __author__="bjschuma" __date__ ="$Dec 19, 2009 11:38:46 PM$" -from manager import manager +from session import manager from session import settings #from bt.message import write import history diff --git a/src/plugins/exit.py b/src/plugins/exit.py index 0ec458c3..57b89012 100644 --- a/src/plugins/exit.py +++ b/src/plugins/exit.py @@ -7,7 +7,7 @@ __author__="bjschuma" __date__ ="$Jan 20, 2010 6:42:45 PM$" from bt import plugin -from manager import manager +from bt.signal import emit class Plugin(plugin.Plugin): @@ -18,6 +18,6 @@ class Plugin(plugin.Plugin): def run(self,args=None): - manager.shutdown() + emit("scion-end") diff --git a/src/plugins/help.py b/src/plugins/help.py index 7e76443b..de145ecd 100644 --- a/src/plugins/help.py +++ b/src/plugins/help.py @@ -6,7 +6,7 @@ __date__ ="$Jan 20, 2010 8:01:55 PM$" from bt.message import write, disable, enable from bt import plugin -from manager import manager +from session import manager class Plugin(plugin.Plugin): diff --git a/src/plugins/plugins.py b/src/plugins/plugins.py index 0e82ccf1..d659b579 100644 --- a/src/plugins/plugins.py +++ b/src/plugins/plugins.py @@ -6,7 +6,7 @@ __date__ ="$Jan 22, 2010 12:14:01 AM$" from bt.message import write from bt import plugin -from manager import manager +from session import manager class Plugin(plugin.Plugin): @@ -52,7 +52,7 @@ class Plugin(plugin.Plugin): def run(self, args=None): if args == None: - write("Usage:" + self.usage) + write("Usage: " + self.usage) return if args[0] == "enable":