Began shifting towards events and away from signals
This commit is contained in:
parent
f1172d85d0
commit
3f8a023813
|
@ -8,20 +8,17 @@ __date__ ="$Jan 23, 2010 2:33:21 PM$"
|
|||
|
||||
from bt.file import *
|
||||
from bt import signal
|
||||
#import session
|
||||
from bt import dict
|
||||
import session
|
||||
|
||||
|
||||
class Alias(dict):
|
||||
class Alias(dict.Dict):
|
||||
def __init__(self):
|
||||
dict.__init__(self)
|
||||
signal.register("scion-plugins-loaded",self.load,3)
|
||||
dict.Dict.__init__(self)
|
||||
session.events.invite("scion-plugins-loaded",self.load,3)
|
||||
signal.register("scion-end",self.save,90)
|
||||
|
||||
|
||||
def has(self,key):
|
||||
return ( key in self.keys() )
|
||||
|
||||
|
||||
def save(self):
|
||||
import session
|
||||
write("Saving aliases",1)
|
||||
|
@ -35,4 +32,4 @@ class Alias(dict):
|
|||
import session
|
||||
path = join(session.settings["session"],"aliases")
|
||||
write("Alias file: "+path,1)
|
||||
signal.attachScript("scion-plugins-loaded", path )
|
||||
session.events.invite("scion-plugins-loaded", path )
|
|
@ -1,5 +1,5 @@
|
|||
# This is the base tools package
|
||||
# It contains various tools needed by the base layer of ocarina2
|
||||
|
||||
__all__ = ["alias", "cmd", "file", "map", "message", "needle", "plugin", "proc",
|
||||
"scripting", "settings", "signal", "sql", "xm"]
|
||||
__all__ = ["cmd", "dict", "event", "file", "map", "message", "needle", "plugin",
|
||||
"proc", "scripting", "signal", "slist", "sql", "xm"]
|
|
@ -0,0 +1,17 @@
|
|||
#! /usr/bin/python
|
||||
|
||||
# To change this template, choose Tools | Templates
|
||||
# and open the template in the editor.
|
||||
|
||||
__author__="bjschuma"
|
||||
__date__ ="$Feb 24, 2010 8:59:47 PM$"
|
||||
|
||||
|
||||
|
||||
class Dict(dict):
|
||||
def __init__(self):
|
||||
dict.__init__(self)
|
||||
|
||||
|
||||
def has(self,key):
|
||||
return (key in self.keys())
|
|
@ -33,7 +33,7 @@ def write(text,verbose=0):
|
|||
if enabled == False:
|
||||
return
|
||||
if (verbose <= session.settings["verbose"]):
|
||||
bt.signal.emit("message-write",str(text))
|
||||
session.events.start("message-write",str(text))
|
||||
|
||||
|
||||
# Print an error message
|
||||
|
@ -46,4 +46,4 @@ def error(text, trace):
|
|||
|
||||
|
||||
#settings.set("write", disp)
|
||||
bt.signal.register("message-write",disp)
|
||||
session.events.invite("message-write",disp)
|
|
@ -4,12 +4,19 @@ __date__ ="$Feb 22, 2010 10:52:13 PM$"
|
|||
|
||||
|
||||
# This is a sorted list (as long as items are inserted with add() )
|
||||
class slist(list):
|
||||
class Slist(list):
|
||||
def __init__(self):
|
||||
list.__init__(self)
|
||||
|
||||
|
||||
def add(self,item):
|
||||
self += [item]
|
||||
def add(self, item, priority):
|
||||
self += [(priority,item)]
|
||||
self.sort()
|
||||
|
||||
|
||||
def remove(self, item):
|
||||
for tuple in self:
|
||||
if tuple[1] == item:
|
||||
list.remove(self,tuple)
|
||||
break
|
||||
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
#! /usr/bin/python
|
||||
|
||||
# To change this template, choose Tools | Templates
|
||||
# and open the template in the editor.
|
||||
|
||||
__author__="bjschuma"
|
||||
__date__ ="$Feb 24, 2010 9:13:41 PM$"
|
||||
|
||||
|
||||
from bt.dict import Dict
|
||||
from bt.slist import Slist
|
||||
|
||||
|
||||
# Maintain the list of guests
|
||||
class GuestList(Slist):
|
||||
def __init__(self,name):
|
||||
Slist.__init__(self)
|
||||
self.name = name
|
||||
self.active = False
|
||||
|
||||
|
||||
|
||||
|
||||
class Event(Dict):
|
||||
def __init__(self):
|
||||
Dict.__init__(self)
|
||||
|
||||
|
||||
def type(self,guest):
|
||||
return type(guest).__name__
|
||||
|
||||
|
||||
# Add a "guest" (function or script) to this event
|
||||
def invite(self, name, guest, priority=None):
|
||||
if priority==None:
|
||||
if self.type(guest)=='str' or self.type(guest)=='unicode':
|
||||
priority = 200
|
||||
elif self.type(guest)=='function' or self.type(guest)=='instancemethod':
|
||||
priority = 100
|
||||
if self.has(name) == False:
|
||||
self[name] = GuestList(name)
|
||||
self[name].add(guest, priority)
|
||||
|
||||
|
||||
# Remove a guest from the guest list
|
||||
def uninvite(self, name, guest):
|
||||
self[name].remove(guest)
|
||||
|
||||
|
||||
def stop(self,name):
|
||||
self[name].active = False
|
||||
|
||||
|
||||
# Start the party!
|
||||
def start(self,name,args=None):
|
||||
if self.has(name) == False:
|
||||
return
|
||||
|
||||
self[name].active = True
|
||||
|
||||
for priority,guest in self[name]:
|
||||
if self[name].active == True:
|
||||
if self.type(guest)=='unicode' or self.type(guest)=='str':
|
||||
from bt import scripting
|
||||
scripting.runScript(guest)
|
||||
elif args==None:
|
||||
guest()
|
||||
else:
|
||||
guest(args)
|
||||
|
||||
self.stop(name)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -13,7 +13,7 @@ import inspect
|
|||
class Manager:
|
||||
def __init__(self):
|
||||
write("Creating plugin manager", 1)
|
||||
register("scion-plugins-loaded",self.startup)
|
||||
session.events.invite("scion-plugins-loaded",self.startup)
|
||||
register("scion-end",self.shutdown,0)
|
||||
# Map the plugin name to a dictionary
|
||||
self.enabled = dict()
|
||||
|
|
|
@ -19,7 +19,7 @@ def main():
|
|||
for path in session.settings["PLUGPATH"]:
|
||||
loadPluginPath(path)
|
||||
|
||||
emit("scion-plugins-loaded")
|
||||
session.events.start("scion-plugins-loaded")
|
||||
emit("scion-begin")
|
||||
|
||||
|
||||
|
|
|
@ -8,8 +8,12 @@ __date__ ="$Feb 17, 2010 9:18:50 PM$"
|
|||
import sys
|
||||
import getopt
|
||||
|
||||
from bt.settings import Settings
|
||||
from bt.alias import Alias
|
||||
from event import Event
|
||||
global events
|
||||
events = Event()
|
||||
|
||||
from settings import Settings
|
||||
from aliases import Alias
|
||||
from bt.file import *
|
||||
from bt.message import write
|
||||
from bt import signal
|
||||
|
@ -19,6 +23,7 @@ from bt import proc
|
|||
global alias
|
||||
global settings
|
||||
global manager
|
||||
#global events
|
||||
|
||||
|
||||
def setSession():
|
||||
|
@ -57,7 +62,6 @@ def parseInput():
|
|||
write("User input has been processed",1)
|
||||
|
||||
|
||||
|
||||
settings = Settings()
|
||||
parseInput()
|
||||
# We want to configure settings as early as possible
|
||||
|
@ -67,5 +71,5 @@ manager = Manager()
|
|||
alias = Alias()
|
||||
|
||||
# Register functions to call when all plugins have been loaded
|
||||
signal.register("scion-plugins-loaded",proc.setup,0)
|
||||
signal.register("scion-plugins-loaded",setSession,1)
|
||||
events.invite("scion-plugins-loaded", proc.setup, 0)
|
||||
events.invite("scion-plugins-loaded", setSession, 1)
|
||||
|
|
|
@ -13,13 +13,15 @@ from bt.message import write
|
|||
from bt import signal
|
||||
from bt.file import join
|
||||
from bt import xm
|
||||
from bt import dict
|
||||
import session
|
||||
|
||||
|
||||
class Settings(dict):
|
||||
class Settings(dict.Dict):
|
||||
def __init__(self):
|
||||
dict.__init__(self)
|
||||
signal.register("scion-plugins-loaded",self.load,2)
|
||||
signal.register("scion-end",self.save)
|
||||
dict.Dict.__init__(self)
|
||||
session.events.invite("scion-plugins-loaded",self.load,2)
|
||||
session.events.invite("scion-end",self.save)
|
||||
|
||||
|
||||
def setdefaults(self):
|
||||
|
@ -40,10 +42,6 @@ class Settings(dict):
|
|||
del self['verbose']
|
||||
|
||||
|
||||
def has(self,key):
|
||||
return (key.lower() in self)
|
||||
|
||||
|
||||
def init(self,key,value):
|
||||
if self.has(key)==False:
|
||||
self[key] = value
|
||||
|
@ -51,18 +49,18 @@ class Settings(dict):
|
|||
|
||||
def __setitem__(self,key,value):
|
||||
key = key.lower()
|
||||
dict.__setitem__(self,key,value)
|
||||
dict.Dict.__setitem__(self,key,value)
|
||||
|
||||
|
||||
def __getitem__(self,key):
|
||||
key = key.lower()
|
||||
if self.has(key)==True:
|
||||
return dict.__getitem__(self,key)
|
||||
return dict.Dict.__getitem__(self,key)
|
||||
return None
|
||||
|
||||
|
||||
def __delitem__(self,key):
|
||||
dict.__delitem__(self,key.lower())
|
||||
dict.Dict.__delitem__(self,key.lower())
|
||||
|
||||
|
||||
def load(self):
|
|
@ -0,0 +1,56 @@
|
|||
Documentation for events.
|
||||
|
||||
|
||||
cli-loop-begin:
|
||||
Started in plugins.cline.loop.loop(), at the beginning of every loop
|
||||
|
||||
|
||||
cli-loop-end:
|
||||
Started in plugins.cline.loop.loop(), at the end of every loop
|
||||
|
||||
|
||||
message-write:
|
||||
Started in bt.message.py to print a message
|
||||
Stopped in plugins.cline.message.insert()
|
||||
|
||||
plugins.cline.message.insert()
|
||||
Invited in plugins.cline.loop.init(), Priority 90
|
||||
Uses ncurses to display the message
|
||||
|
||||
core.bt.message.disp()
|
||||
Invited in bt.message.py, Default priority
|
||||
Uses the python print function to display text
|
||||
|
||||
|
||||
|
||||
scion-end:
|
||||
Started to begin the shutdown procedure
|
||||
|
||||
core.settings.Settings.save()
|
||||
Invited in core.settings.Settings.__init__(), Default priority
|
||||
Saves all settings key/value pairs to a file
|
||||
|
||||
|
||||
|
||||
scion-plugins-loaded:
|
||||
Started in core/scion.py after loading plugins
|
||||
|
||||
core.bt.proc.setup()
|
||||
Invited in session.py, Priority 0
|
||||
Sets the application name and prints a welcome message
|
||||
|
||||
core.session.Session.setSession()
|
||||
Invited in session.py, Priority 1
|
||||
Creates the session directory if it doesn't already exist
|
||||
|
||||
core.settings.load()
|
||||
Invited in core.settings.Settings.__init__(), Priority 2
|
||||
Loads user settings from a saved file
|
||||
|
||||
core.aliases.Alias.load()
|
||||
Invited in core.aliases.Alias.__init__(), Priority 3
|
||||
Loads user aliases from a file
|
||||
|
||||
core.manager.startup()
|
||||
Invited in core.manager.Manager.__init__(), Default priority
|
||||
Enables the plugins that the user had enabled before the last shutdown
|
|
@ -5,7 +5,6 @@ __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 *
|
||||
from session import settings
|
||||
|
|
|
@ -8,9 +8,9 @@ __date__ ="$Dec 19, 2009 9:39:37 PM$"
|
|||
|
||||
|
||||
from bt.message import write
|
||||
from bt.signal import *
|
||||
import manager
|
||||
from session import settings
|
||||
from session import events
|
||||
|
||||
# Command line related imports
|
||||
from addch import addch
|
||||
|
@ -34,20 +34,14 @@ def init():
|
|||
# Set these values in settings class for use elsewhere
|
||||
settings["stdscr"] = stdscr
|
||||
settings["maxyx"] = maxyx
|
||||
#if settings.has("prompt") == False:
|
||||
# settings.set("prompt", ">>> ")
|
||||
settings.init("prompt",">>>")
|
||||
settings["prompt"] += " "
|
||||
#else:
|
||||
# settings.set("prompt",settings.get("prompt")+" ")
|
||||
|
||||
#if settings.has("maxhist") == False:
|
||||
# settings.set("maxhist",50)
|
||||
settings.init("maxhist",50)
|
||||
|
||||
settings["clinex"] = 0
|
||||
settings["cliney"] = 0
|
||||
register("message-write", insert,90)
|
||||
events.invite("message-write", insert,90)
|
||||
|
||||
|
||||
|
||||
|
@ -57,7 +51,6 @@ def quit():
|
|||
|
||||
if halt == True:
|
||||
return
|
||||
|
||||
halt = True
|
||||
|
||||
# Undo ncurses initialization
|
||||
|
@ -71,16 +64,9 @@ def quit():
|
|||
del settings["maxyx"]
|
||||
del settings["cliney"]
|
||||
del settings["clinex"]
|
||||
#settings.delete("stdscr")
|
||||
#settings.delete("maxyx")
|
||||
#settings.delete("prompt")
|
||||
#settings.delete("clinex")
|
||||
#settings.delete("cliney")
|
||||
#settings.delete("everyloop")
|
||||
|
||||
# Return to previous write function
|
||||
remove("message-write",insert)
|
||||
#settings.pop("write")
|
||||
events.uninvite("message-write",insert)
|
||||
write("Command line loop has ended",True)
|
||||
|
||||
|
||||
|
@ -99,12 +85,12 @@ def loop():
|
|||
disp(input)
|
||||
|
||||
while halt == False:
|
||||
emit("cli-loop-begin")
|
||||
events.start("cli-loop-begin")
|
||||
try:
|
||||
input = addch(input)
|
||||
disp(input)
|
||||
except:
|
||||
pass
|
||||
emit("cli-loop-end")
|
||||
events.start("cli-loop-end")
|
||||
|
||||
|
||||
|
|
|
@ -9,8 +9,8 @@ __date__ ="$Dec 19, 2009 11:57:19 PM$"
|
|||
|
||||
import curses
|
||||
from session import settings
|
||||
from session import events
|
||||
import bt.message
|
||||
import bt.signal
|
||||
|
||||
|
||||
def advance(y, maxy, stdscr):
|
||||
|
@ -42,7 +42,7 @@ def insert(string):
|
|||
stdscr.addstr(y, 0, string)
|
||||
advance(y, maxyx[0], stdscr)
|
||||
|
||||
bt.signal.stop("message-write")
|
||||
events.stop("message-write")
|
||||
|
||||
|
||||
|
||||
|
@ -62,7 +62,6 @@ def disp(string, y=-1):
|
|||
stdscr.leaveok(True)
|
||||
move = stdscr.getyx()
|
||||
space = " " * settings["maxyx"][1]
|
||||
#stdscr.addstr(y, 0, space)
|
||||
|
||||
|
||||
try:
|
||||
|
|
Loading…
Reference in New Issue