Command line calls shutdown function when exit is entered.

This commit is contained in:
bjschuma 2009-12-19 23:13:50 -05:00
parent b33e02ee48
commit 2086a927e1
9 changed files with 92 additions and 23 deletions

View File

@ -1,4 +1,4 @@
# This is the base tools package # This is the base tools package
# It contains various tools needed by the base layer of ocarina2 # It contains various tools needed by the base layer of ocarina2
__all__ = ["proc", "message"] __all__ = ["file", "proc", "message", "needle"]

View File

@ -11,10 +11,14 @@ from threading import Thread
class Needle(Thread): class Needle(Thread):
def __init__(self, func, args): def __init__(self, func, args=None):
Thread.__init__(self)
self.func = func self.func = func
self.args = args self.args = args
def run(self): def run(self):
func(args) if self.args==None:
self.func()
else:
self.func(self.args)

View File

@ -40,7 +40,9 @@ class PluginLoader:
# Check for things we should not import # Check for things we should not import
if split[0]=="__init__": if split[0]=="__init__":
continue continue
elif len(split)>1 and split[1]=="pyc": elif len(split)==1:
continue
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, True)

View File

@ -4,7 +4,7 @@ __author__="bjschuma"
__date__ ="$Dec 8, 2009 8:40:36 AM$" __date__ ="$Dec 8, 2009 8:40:36 AM$"
import sys import sys
from bt.message import write from bt.message import *
import loader import loader
class Manager: class Manager:
@ -21,8 +21,11 @@ class Manager:
write("Adding plugin: " + str(plugin), True) write("Adding plugin: " + str(plugin), True)
self.disabled[plugin.name] = 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"): try:
self.enablePlugin(plugin.name) if plugin.type=="core":
self.enablePlugin(plugin.name)
except:
error("Error adding plugin: "+plugin.name)
# Move plugin from old[name] to new[name] # Move plugin from old[name] to new[name]
@ -37,7 +40,7 @@ class Manager:
# Returns true if there was a plugin to disable, false otherwise # Returns true if there was a plugin to disable, false otherwise
def disablePlugin(self,name): def disablePlugin(self,name):
if (name in self.enabled) == True: if (name in self.enabled) == True:
write("Disabling plugin: "+name) write("Disabling plugin: "+name, True)
plugin = self.movePlugin(name, self.enabled, self.disabled) plugin = self.movePlugin(name, self.enabled, self.disabled)
plugin.close() plugin.close()
return True return True
@ -47,13 +50,14 @@ class Manager:
# Move a plugin to the enabled list # Move a plugin to the enabled list
# Returns true if a plugin was enabled, false otherwise # Returns true if a plugin was enabled, false otherwise
def enablePlugin(self,name): def enablePlugin(self,name):
#index = self.findPlugin(name, self.disabled)
#if index > -1:
if (name in self.disabled) == True: if (name in self.disabled) == True:
write("Enabling plugin: "+name,True) try:
plugin = self.movePlugin(name, self.disabled, self.enabled) write("Enabling plugin: "+name,True)
plugin.open() plugin = self.movePlugin(name, self.disabled, self.enabled)
return True plugin.open()
return True
except:
error("Error enabling: "+name)
return False return False

View File

@ -35,7 +35,7 @@ def main():
#raw_input("Input something: ") #raw_input("Input something: ")
#manager.manager.reloadPlugin("example") #manager.manager.reloadPlugin("example")
manager.manager.shutdown() #manager.manager.shutdown()

View File

@ -12,32 +12,48 @@ path = ""
opt = [] opt = []
import curses import curses
from bt.message import write from bt.message import *
from bt.needle import Needle #from bt.needle import Needle
import bt.needle
import cline.loop
import settings
global maxxy, input, stdscr global maxxy, stdscr, thread
input = "" threads = []
# Called every time the plugin is enabled # Called every time the plugin is enabled
def open(): def open():
write("Starting CLI", True) write("Starting CLI", True)
global maxxy, input, stdscr global maxxy, stdscr, thread
# Initialize ncurses screen
stdscr = curses.initscr() stdscr = curses.initscr()
maxxy = stdscr.getmaxyx() maxxy = stdscr.getmaxyx()
curses.cbreak() curses.cbreak()
curses.noecho() curses.noecho()
stdscr.keypad(1) stdscr.keypad(1)
# Set these values in settings class for use elsewhere
settings.set("stdscr", stdscr)
settings.set("maxxy", maxxy)
# Start loop in new thread
thread = bt.needle.Needle(cline.loop.loop)
thread.start()
# Called every time the plugin is stopped # Called every time the plugin is stopped
def close(): def close():
global stdscr global stdscr, thread
# Undo ncurses initialization
curses.nocbreak() curses.nocbreak()
curses.echo() curses.echo()
stdscr.keypad(0) stdscr.keypad(0)
curses.endwin() curses.endwin()
# Stop looping
cline.loop.quit()
#thread.join()
write("CLI has been stopped", True) write("CLI has been stopped", True)

View File

@ -1,2 +1,5 @@
__author__="bjschuma" __author__="bjschuma"
__date__ ="$Dec 19, 2009 9:13:05 PM$" __date__ ="$Dec 19, 2009 9:13:05 PM$"
__all__ = ["loop"]

41
src/core/cline/loop.py Normal file
View File

@ -0,0 +1,41 @@
#! /usr/bin/python
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="bjschuma"
__date__ ="$Dec 19, 2009 9:39:37 PM$"
import curses
from bt.message import write
import manager
import settings
global halt
halt = False
def loop():
write("Beginning command line loop",True)
global halt
stdscr = settings.get("stdscr")
input = ""
while halt == False:
c = stdscr.getch()
if(c >= 32 and c<= 126):
input += curses.keyname(c)
#input=input[:x-4]+curses.keyname(c)+input[x-4:]
#self.changeLine(y)
#self.stdscr.move(y,x+1)
if input == "exit":
manager.manager.shutdown()
halt = True
def quit():
global halt
halt = True

View File

@ -23,7 +23,6 @@ def open():
# Called every time the plugin is stopped # Called every time the plugin is stopped
def close(): def close():
write("Example plugin has been stopped",True) write("Example plugin has been stopped",True)
pass
# Called when the plugin needs to perform some action # Called when the plugin needs to perform some action