Took newer version of ocarina2 and renamed to scion

This commit is contained in:
bjschuma 2009-12-17 22:30:27 -05:00
commit bd2c285a1d
19 changed files with 383 additions and 0 deletions

2
src/base/__init__.py Normal file
View File

@ -0,0 +1,2 @@
#__all__ = ["bt"]

4
src/base/bt/__init__.py Normal file
View File

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

BIN
src/base/bt/__init__.pyc Normal file

Binary file not shown.

44
src/base/bt/file.py Normal file
View File

@ -0,0 +1,44 @@
import os.path
__author__="bjschuma"
__date__ ="$Dec 6, 2009 11:35:56 PM$"
import os
from message import write
# Check if path is a directory
def checkDir(path):
path = os.path.expanduser(path)
if checkPath(path) == True:
return os.path.isdir(path)
return False
# Check if path exists
def checkPath(path):
path = os.path.expanduser(path)
write("Checking if "+path+" exists", True)
return os.path.exists(path)
def expandPath(path):
return os.path.expanduser(path)
# Open a file
def fopen(path,flags='r'):
path = os.path.expanduser(path)
# If we are reading a file, check that it exists
if ('r' in flags) == True:
exists = checkPath(path)
if exists == False:
write(path+" does not exist", True)
return None
return open(path, flags)
# Return a listing of directory contents
def ls(path):
if checkDir(path) == False:
return []
return os.listdir(path)

BIN
src/base/bt/file.pyc Normal file

Binary file not shown.

22
src/base/bt/message.py Normal file
View File

@ -0,0 +1,22 @@
__author__="bjschuma"
__date__ ="$Dec 5, 2009 6:46:13 PM$"
import os
import inspect
import settings
# Print an error message
def error(text):
lineno = str(inspect.currentframe().f_back.f_lineno)
filename = inspect.currentframe().f_back.f_code.co_filename
filename = filename.rsplit(os.sep,1)[1]
print filename,"("+lineno+"):",text
# Print general text to the screen
def write(text,verbose=False):
if (verbose==False) or (settings.get("verbose")==True):
print text

BIN
src/base/bt/message.pyc Normal file

Binary file not shown.

30
src/base/bt/proc.py Normal file
View File

@ -0,0 +1,30 @@
# Manages process-related tasks
__author__="bjschuma"
__date__ ="$Dec 5, 2009 6:33:50 PM$"
import settings
from message import write
# Set our process name to name
def setname(name):
# Set the process name (thank you exaile.py)
# This only works on linux2 machines
message = "Attempting to set process name to ocarina2..."
if settings.settings["ARCH"] == 'linux2':
try:
import ctypes
libc = ctypes.CDLL('libc.so.6')
libc.prctl(15,name, 0, 0, 0)
message += "success!"
except:
try:
import dl
libc = dl.open('/lib/libc.so.6')
name+="\0"
libc.call('prctl',15, name, 0, 0, 0) # 15 is PR_SET_NAME
message += "success!"
except:
message += "failed."
write(message,True)

BIN
src/base/bt/proc.pyc Normal file

Binary file not shown.

54
src/base/loader.py Normal file
View File

@ -0,0 +1,54 @@
# This class is used to load plugins
import sys
from bt.message import write
from bt.file import *
class PluginLoader:
def __init__(self):
# Plugins are added to this array upon loading
self.plugins = []
def clearPlugins(self):
self.plugins = []
# Load plugins from a directory
def loaddir(self, dir):
exists = checkDir(dir)
# Exit now if the directory doesn't exist
if exists == False:
return
write("Loading plugins from " + dir, True)
# Add the directory to our import path
sys.path.append(dir)
modlist = ls(dir)
for mod in modlist:
split = mod.rsplit('.',1)
# Check for things we should not import
if split[0]=="__init__":
continue
elif split[1]=="pyc":
continue
# Load the module into our module array
write("Attempting to load "+mod, True)
self.loadmod(split[0])
# Call with a module name to import
# Adds the imported module to a list
def loadmod(self,mod):
__import__(mod)
plugin = sys.modules[mod]
plugin.__name__ = mod
self.plugins += [plugin]
# Load specific plugin
def loadpath(self, path):
write("Loading " + path)

BIN
src/base/loader.pyc Normal file

Binary file not shown.

90
src/base/manager.py Normal file
View File

@ -0,0 +1,90 @@
__author__="bjschuma"
__date__ ="$Dec 8, 2009 8:40:36 AM$"
import sys
from bt.message import write
class Manager:
def __init__(self):
write("Creating plugin manager", True)
self.enabled = []
# Map plugin name to dictionary
self.disabled = []
def addPlugins(self, plugins):
# Add each plugin to the disabled list
for plugin in plugins:
write("Adding plugin: " + str(plugin), True)
self.disabled += [plugin]
# If we are adding a core plugin, activate it right away
if(plugin.__type__=="core"):
self.enablePlugin(plugin.__name__)
# Move plugin from old[index] to new
# Return the plugin
def movePlugin(self, index, old, new):
plugin = old.pop(index)
index = len(new)
new += [plugin]
return plugin,index
# Disable a plugin, return index of disabled plugin
def disablePlugin(self,name):
index = self.findPlugin(name, self.enabled)
if index > -1:
write("Disabling plugin: "+name)
plugin,index = self.movePlugin(index, self.enabled, self.disabled)
plugin.close()
return index
# Move a plugin to the enabled list, return number of plugins enabled
def enablePlugin(self,name):
index = self.findPlugin(name, self.disabled)
if index > -1:
write("Enabling plugin: "+name,True)
plugin,index = self.movePlugin(index, self.disabled, self.enabled)
plugin.open()
return True
# Return first location where plugin.__name__ == name
def findPlugin(self,name,array):
write("Finding plugin: "+name,True)
for i in range(len(array)):
if array[i].__name__ == name:
return i
return -1
# Disable the plugin, reload it, reenable it
def reloadPlugin(self,name):
# Check if plugin has been loaded
if (name in sys.modules) == False:
write("Plugin not loaded: "+name, True)
return
reenable = self.disablePlugin(name)
#import sys
#write(sys.modules["example"])
#return
#reenable = self.disablePlugin(name)
#index = self.findPlugin(name, self.disabled)
#write("Reloading plugin: "+name)
#plugin = self.disabled[index]
#print plugin
#reload(plugin)
# Only reenable if a plugin was disabled
#if reenable == True:
# self.enablePlugin(name)
global manager
manager = Manager()

BIN
src/base/manager.pyc Normal file

Binary file not shown.

44
src/base/ocarina.py Normal file
View File

@ -0,0 +1,44 @@
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="bjschuma"
__date__ ="$Dec 4, 2009 3:37:21 PM$"
global name
name = "ocarina2"
# We need to import settings before we can use disp()
import settings
# The first thing we do is import write() so we can occasionally print messages
from bt.message import write
write("Welcome to Ocarina2")
# Next, we set the process name to ocarina2
from bt.proc import setname
setname(name)
# Import the plugin loader class!
import loader
import manager
def loadPluginPath(path):
# Load a plugin directory, add give to the plugin manager
load = loader.PluginLoader()
load.loaddir("../core")
manager.manager.addPlugins(load.plugins)
# Begin our main loop
def main():
load = loader.PluginLoader()
for path in settings.get("PLUGPATH"):
loadPluginPath(path)
raw_input("Input something:")
manager.manager.reloadPlugin("example")
if __name__ == "__main__":main()

65
src/base/settings.py Normal file
View File

@ -0,0 +1,65 @@
import os.path
__author__="bjschuma"
__date__ ="$Dec 4, 2009 4:04:24 PM$"
from bt.message import write
from bt.message import error
from bt.file import fopen
#import bt.message.disp
import os
import sys
import getopt
# Create a dictionary to hold the settings
global settings
settings = dict()
# Return the value at key
def get(key):
return settings[key.upper()]
# Read settings from file
def readfile(file):
write("Reading file: "+file,True)
file = fopen(file)
# Parse the user input
def parseInput():
write("Parsing user input",True)
input = sys.argv[1:]
write(input,True)
optc = "v"
long = ["verbose"]
# Attempt to parse user input
try:
opts, args = getopt.getopt(input, optc, long)
except getopt.GetoptError, e:
error(e.msg)
return
# Set default values
# Set verbose first so we can use write()
settings["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("~")
user = os.path.join(user,".ocarina2")
settings["USER"] = user
settings["PLUGPATH"] = ["../core"]
# Find out what platform we are running on
settings["ARCH"] = sys.platform
if settings["ARCH"] == 'linux2':
readfile("/etc/ocarina.conf")
parseInput()

BIN
src/base/settings.pyc Normal file

Binary file not shown.

24
src/core/example.py Normal file
View File

@ -0,0 +1,24 @@
# This is a simple test plugin, to make sure everything is working
__author__="bjschuma"
__date__ ="$Dec 7, 2009 9:12:00 AM$"
__name__="test"
__type__="core"
__opt__=[]
from bt.message import write
# Called every time the plugin is enabled
def open():
write("Example plugin has been started",True)
def close():
write("Example plugin has been stopped")
pass
def run():
pass

BIN
src/core/example.pyc Normal file

Binary file not shown.

4
src/ocarina Normal file
View File

@ -0,0 +1,4 @@
#!/bin/bash
# This is a simple shell script for properly starting ocarina
cd base && `which python` ocarina.py $*