Continued migrating to using a session to store settings. Can load

saved settings.
This commit is contained in:
bjschuma 2010-02-20 00:52:06 -05:00
parent e28bf46c60
commit be07a50c20
20 changed files with 105 additions and 188 deletions

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1"> <project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<coverage xmlns="http://www.netbeans.org/ns/code-coverage/1" enabled="false"/>
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/> <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
</project-private> </project-private>

View File

@ -1,5 +1,5 @@
java.lib.path= java.lib.path=
main.file=ocarina.py main.file=core/scion.py
platform.active=Python_2.6.3 platform.active=default
python.lib.path= python.lib.path=/usr/bin/python|
src.dir=src src.dir=src

View File

@ -2,5 +2,7 @@
rm -r ./*/*.pyc rm -r ./*/*.pyc
rm -r ./*/*/*.pyc rm -r ./*/*/*.pyc
#rm -r ./*/*.class
#rm -r ./*/*/*.class
#rm -r ./*/*.pyo #rm -r ./*/*.pyo
#rm -r ./*/*/*.pyo #rm -r ./*/*/*.pyo

View File

@ -17,13 +17,13 @@ def checkDir(path):
# Check if path exists # Check if path exists
def checkPath(path): def checkPath(path):
path = os.path.expanduser(path) path = os.path.expanduser(path)
write("Checking if "+path+" exists", True) write("Checking if "+path+" exists", 2)
return os.path.exists(path) return os.path.exists(path)
def mkdir(path): def mkdir(path):
if checkDir(path)==False: if checkDir(path)==False:
write("Creating directory: "+path,True) write("Creating directory: "+path,2)
os.mkdir(path) os.mkdir(path)
@ -38,7 +38,7 @@ def fopen(path,flags='r'):
if ('r' in flags) == True: if ('r' in flags) == True:
exists = checkPath(path) exists = checkPath(path)
if exists == False: if exists == False:
write(path+" does not exist", True) write(path+" does not exist", 2)
return None return None
return open(path, flags) return open(path, flags)

View File

@ -10,6 +10,8 @@ from sys import argv,platform
import os import os
from bt.message import write from bt.message import write
from bt.file import join
from bt import xm
class Settings(dict): class Settings(dict):
@ -57,4 +59,17 @@ class Settings(dict):
def load(self,path): 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 pass

View File

@ -6,7 +6,6 @@
__author__="bjschuma" __author__="bjschuma"
__date__ ="$Jan 20, 2010 12:13:21 AM$" __date__ ="$Jan 20, 2010 12:13:21 AM$"
from map import Map from map import Map
global signals, status global signals, status
signals = Map() signals = Map()

View File

@ -23,7 +23,7 @@ class PluginLoader:
# Load plugins from a directory # Load plugins from a directory
def loaddir(self, dir): def loaddir(self, dir):
write("Loading plugins from " + dir, True) write("Loading plugins from " + dir, 1)
# Add the directory to our import path # Add the directory to our import path
sys.path.append(dir) sys.path.append(dir)
@ -45,7 +45,7 @@ class PluginLoader:
elif split[1]=="pyc": elif split[1]=="pyc":
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, 2)
#try: #try:
self.loadmod(split[0], os.path.join(dir,mod)) self.loadmod(split[0], os.path.join(dir,mod))
#except: #except:

View File

@ -9,13 +9,14 @@ from bt.signal import *
from bt import alias from bt import alias
#import loader #import loader
import session #import session
import inspect import inspect
class Manager: class Manager:
def __init__(self): def __init__(self):
write("Creating plugin manager", True) write("Creating plugin manager", True)
register("scion-end",self.shutdown,1)
# Map the plugin name to a dictionary # Map the plugin name to a dictionary
self.enabled = dict() self.enabled = dict()
self.disabled = dict() self.disabled = dict()
@ -25,7 +26,7 @@ class Manager:
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: "+plugin.name, True) write("Adding plugin: "+plugin.name, 2)
self.disabled[plugin.name] = plugin self.disabled[plugin.name] = plugin
@ -53,7 +54,7 @@ class Manager:
def enablePlugin(self,name): def enablePlugin(self,name):
if (name in self.disabled) == True: if (name in self.disabled) == True:
try: try:
write("Enabling plugin: "+name,True) write("Enabling plugin: "+name,2)
plugin = self.movePlugin(name, self.disabled, self.enabled) plugin = self.movePlugin(name, self.disabled, self.enabled)
plugin.open() plugin.open()
return True return True
@ -82,27 +83,12 @@ class Manager:
# Disable all plugins # Disable all plugins
def shutdown(self): def shutdown(self):
emit("scion-end") write("Shutting down manager, disabling all active plugins", 1)
write("Shutting down manager, disabling all active plugins", True)
path = session.getSession()
#session = self.findsession()
self.savesession(path)
keys = self.enabled.keys() keys = self.enabled.keys()
for plugin in keys: for plugin in keys:
self.disablePlugin(plugin) 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): def run(self,name,args=None):
name = name.strip() name = name.strip()
@ -124,15 +110,11 @@ class Manager:
return None return None
#def findsession(self): def call(self,name,args):
# path = session.session.settings.get("user") disable()
# if path == expandPath("~"): result = self.run(name,args)
# #path = join(path,".scion") enable()
# path = join(path,"."+settings.get("appname")) return result
# mkdir(path)
# path = join(path,"session")
# mkdir(path)
# return path
def savesession(self,session): def savesession(self,session):
@ -140,34 +122,4 @@ class Manager:
file = fopen(path,'w') file = fopen(path,'w')
for key in self.enabled.keys(): for key in self.enabled.keys():
file.write(key+"\n") file.write(key+"\n")
file.close() 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

View File

@ -17,12 +17,11 @@ from bt.file import join
# Import the plugin loader class! # Import the plugin loader class!
import loader import loader
import manager
def loadPluginPath(path): def loadPluginPath(path):
loader.load.loaddir(path) loader.load.loaddir(path)
manager.manager.addPlugins(loader.load.getPlugins()) session.manager.addPlugins(loader.load.getPlugins())
# Begin our main loop # Begin our main loop
@ -30,15 +29,12 @@ def main():
for path in session.settings.get("PLUGPATH"): for path in session.settings.get("PLUGPATH"):
loadPluginPath(path) loadPluginPath(path)
emit("scion-plugins-loaded")
app = session.settings["appname"] app = session.settings["appname"]
write("Welcome to "+app+"!") write("Welcome to "+app+"!")
setname(app) setname(app)
session.settings["appdir"] = join(session.settings["user"], "."+app) session.settings["appdir"] = join(session.settings["user"], "."+app)
manager.manager.restoresession() emit("scion-plugins-loaded")
manager.manager.startup()
emit("scion-begin") emit("scion-begin")

View File

@ -9,19 +9,42 @@ __date__ ="$Feb 17, 2010 9:18:50 PM$"
from bt.settings import Settings from bt.settings import Settings
from bt.file import * from bt.file import *
from bt.message import write
from bt import signal
from manager import Manager
global settings global settings
global manager
def getSession(): def getSession():
path = settings["user"] path = settings["appdir"]
if path == expandPath("~"): mkdir(path)
path = join(path,"."+settings["appname"])
mkdir(path)
path = join(path,"session") path = join(path,"session")
mkdir(path) mkdir(path)
return 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 = Settings()
settings.setdefaults() 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)

View File

@ -13,47 +13,6 @@ global settings
settings = dict() 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 # Parse the user input
@ -106,36 +65,3 @@ def save(path):
xm.append(t,e) xm.append(t,e)
xm.append(e,elm) xm.append(e,elm)
xm.write(path) 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()

View File

@ -2,44 +2,46 @@ Documentation for signals, and where they are emitted
cli-loop-begin: cli-loop-begin:
Called at the beginning of every loop in the cli 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: cli-loop-end:
Called at the end of every loop in the cli Called at the end of every loop in the cli
Emitted in core.cline.loop.loop() Emitted in plugins.cline.loop.loop()
message-write: message-write:
Displays text to the screen Displays text to the screen
Emitted in bt.message.write() Emitted in core.bt.message.write()
Registered in bt.message Registered in core.bt.message
Registered in core.cline.loop.init() Registered in plugins.cline.loop.init()
Removed in core.cline.loop.quit() Removed in plugins.cline.loop.quit()
Stopped in core.cline.message.insert() Stopped in plugins.cline.message.insert()
scion-begin: scion-begin:
Begins a main loop for the program. Begins a main loop for the program.
Emitted in base.scion.main() Emitted in core.scion.main()
Registered in core.cli.Plugin.open() Registered in plugins.cli.Plugin.open()
Registered in extra.sgtk.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() Removed in extra.sgtk.Plugin.close()
scion-end: scion-end:
Ends the main loop and closes all plugins Ends the main loop and closes all plugins
Emitted in base.manager.shutdown() Emitted in plugins.exit.run()
Registered in core.cli.Plugin.open() Registered in plugins.manager.Manager.__init__()
Registered in plugins.cli.Plugin.open()
Registered in extra.sgtk.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() Removed in extra.sgtk.Plugin.close()
scion-plugins-loaded: scion-plugins-loaded:
Called right after loading all the plugins (before restoring settings or Called right after loading all the plugins (before restoring settings or
enabling any plugins) enabling any plugins)
Emitted in scion.main() Emitted in core.scion.main()
Registered in core.session

View File

@ -6,7 +6,7 @@ __date__ ="$Jan 9, 2010 7:22:31 PM$"
from bt.message import write from bt.message import write
import settings from session import settings
from bt import plugin from bt import plugin
@ -18,7 +18,7 @@ class Plugin(plugin.Plugin):
def run(self, args=None): def run(self, args=None):
keys = settings.settings.keys() keys = settings.keys()
join = ", " join = ", "
if args == None: if args == None:
@ -26,5 +26,5 @@ class Plugin(plugin.Plugin):
write(str) write(str)
else: else:
if settings.has(args[0]) == True: if settings.has(args[0]) == True:
write( settings.get(args[0]) ) write( settings[args[0]] )

View File

@ -8,8 +8,8 @@ __date__ ="$Jan 13, 2010 12:02:58 AM$"
from bt import plugin from bt import plugin
from bt.message import write from bt.message import write
from bt import signal from bt import signal
from manager import manager #from manager import manager
import settings from session import settings,manager
from guiGTK import * from guiGTK import *
import gtk import gtk
@ -27,17 +27,17 @@ class Plugin(plugin.Plugin):
def open(self): def open(self):
#settings.set("guirunning",False) #settings.set("guirunning",False)
if settings.get("guirunning") == True: if settings["guirunning"] == True:
signal.register("scion-begin",self.loop,90) signal.register("scion-begin",self.loop,90)
signal.register("scion-end",self.close,90) signal.register("scion-end",self.close,90)
else: else:
settings.set("guirunning",False) settings["guirunning"] = False
def close(self): def close(self):
if settings.get("guirunning") == True: if settings["guirunning"] == True:
gtk.main_quit() gtk.main_quit()
settings.delete("gtkfuncs") #del settings["gtkfuncs"]
signal.remove("scion-begin",self.loop) signal.remove("scion-begin",self.loop)
signal.remove("scion-end",self.close) signal.remove("scion-end",self.close)
@ -71,9 +71,9 @@ class Plugin(plugin.Plugin):
bar = menu.makeBar() bar = menu.makeBar()
vbox.pack(bar) vbox.pack(bar)
if settings.has("gtkfuncs") == True: #if settings.has("gtkfuncs") == True:
for func in settings.get("gtkfuncs", True): # for func in settings.get("gtkfuncs", True):
func([win,vbox,bar]) # func([win,vbox,bar])
settings.set("guirunning",True) settings["guirunning"] = True
gtk.main() gtk.main()

View File

@ -57,7 +57,7 @@ class Plugin(plugin.Plugin):
return return
if args[0]=="prompt": if args[0]=="prompt":
settings.set("prompt",args[1]+" ") settings["prompt"] = args[1] + " "
elif args[0]=="maxhist": elif args[0]=="maxhist":
if len(args) == 1: if len(args) == 1:
write(settings.get("maxhist")) write(settings.get("maxhist"))

View File

@ -36,7 +36,8 @@ def init():
settings["maxyx"] = maxyx settings["maxyx"] = maxyx
#if settings.has("prompt") == False: #if settings.has("prompt") == False:
# settings.set("prompt", ">>> ") # settings.set("prompt", ">>> ")
settings.init("prompt",">>> ") settings.init("prompt",">>>")
settings["prompt"] += " "
#else: #else:
# settings.set("prompt",settings.get("prompt")+" ") # settings.set("prompt",settings.get("prompt")+" ")

View File

@ -6,7 +6,7 @@
__author__="bjschuma" __author__="bjschuma"
__date__ ="$Dec 19, 2009 11:38:46 PM$" __date__ ="$Dec 19, 2009 11:38:46 PM$"
from manager import manager from session import manager
from session import settings from session import settings
#from bt.message import write #from bt.message import write
import history import history

View File

@ -7,7 +7,7 @@ __author__="bjschuma"
__date__ ="$Jan 20, 2010 6:42:45 PM$" __date__ ="$Jan 20, 2010 6:42:45 PM$"
from bt import plugin from bt import plugin
from manager import manager from bt.signal import emit
class Plugin(plugin.Plugin): class Plugin(plugin.Plugin):
@ -18,6 +18,6 @@ class Plugin(plugin.Plugin):
def run(self,args=None): def run(self,args=None):
manager.shutdown() emit("scion-end")

View File

@ -6,7 +6,7 @@ __date__ ="$Jan 20, 2010 8:01:55 PM$"
from bt.message import write, disable, enable from bt.message import write, disable, enable
from bt import plugin from bt import plugin
from manager import manager from session import manager
class Plugin(plugin.Plugin): class Plugin(plugin.Plugin):

View File

@ -6,7 +6,7 @@ __date__ ="$Jan 22, 2010 12:14:01 AM$"
from bt.message import write from bt.message import write
from bt import plugin from bt import plugin
from manager import manager from session import manager
class Plugin(plugin.Plugin): class Plugin(plugin.Plugin):
@ -52,7 +52,7 @@ class Plugin(plugin.Plugin):
def run(self, args=None): def run(self, args=None):
if args == None: if args == None:
write("Usage:" + self.usage) write("Usage: " + self.usage)
return return
if args[0] == "enable": if args[0] == "enable":