Scripting support is now in! See cmd_look (the end of it), scripthandler.py, and scripts/basicobject.py for very brief examples. I'm not sure how well this is going to scale, I had to kludge the import a bit due to some oddities with __import__. There has to be a better way to do this, hopefully I'll be able to figure it out. In any case, expect basicobject to start fleshing out. You'll be able to use it directly or sub-class it with your own stuff.
This commit is contained in:
parent
27c0e7a873
commit
94ceec3719
5 changed files with 91 additions and 10 deletions
|
|
@ -1,3 +1,7 @@
|
|||
import os
|
||||
from traceback import format_exc
|
||||
|
||||
import functions_general
|
||||
"""
|
||||
This module is responsible for managing scripts and their connection to the
|
||||
Object class model. It is important to keep this as independent from the
|
||||
|
|
@ -9,27 +13,54 @@ interaction with actual script methods should happen via calls to Objects.
|
|||
# contain references to the associated module for each key.
|
||||
cached_scripts = {}
|
||||
|
||||
def scriptlink(scriptname):
|
||||
def scriptlink(source_obj, scriptname):
|
||||
"""
|
||||
Each Object will refer to this function when trying to execute a function
|
||||
contained within a scripted module. For the sake of ease of management,
|
||||
modules are cached and compiled as they are requested and stored in
|
||||
the cached_scripts dictionary.
|
||||
|
||||
Returns a reference to an instance of the script's class as per it's
|
||||
class_factory() method.
|
||||
|
||||
source_obj: (Object) A reference to the object being scripted.
|
||||
scriptname: (str) Name of the module to load (minus 'scripts').
|
||||
"""
|
||||
# The module is already cached, just return it rather than re-load.
|
||||
retval = cached_scripts.get('scripts.%s' % (scriptname), False)
|
||||
retval = cached_scripts.get(scriptname, False)
|
||||
if retval:
|
||||
return retval
|
||||
|
||||
modname = 'scripts.%s' % (scriptname)
|
||||
print 'Caching script module %s.' % (modname)
|
||||
return retval.class_factory(source_obj)
|
||||
|
||||
# Store the original working directory so we can revert.
|
||||
orig_path = os.getcwd()
|
||||
|
||||
# Split the script name up by periods to give us the directory we need
|
||||
# to change to. I really wish we didn't have to do this, but there's some
|
||||
# strange issue with __import__ and more than two directories worth of
|
||||
# nesting.
|
||||
path_split = scriptname.split('.')
|
||||
newpath_str = '/'.join(path_split[:-1])
|
||||
# Lop the module name off the end.
|
||||
modname = path_split[-1]
|
||||
|
||||
try:
|
||||
# Change the working directory to the location of the script and import.
|
||||
os.chdir('scripts/%s/' % (newpath_str))
|
||||
functions_general.log_infomsg("SCRIPT: Caching and importing %s." % (modname))
|
||||
modreference = __import__(modname)
|
||||
cached_scripts[modname] = modreference
|
||||
# Store the module reference for later fast retrieval.
|
||||
cached_scripts[scriptname] = modreference
|
||||
except ImportError:
|
||||
print 'Error importing %s.' % (modname)
|
||||
functions_general.log_infomsg('Error importing %s: %s' % (modname, format_exc()))
|
||||
os.chdir(orig_path)
|
||||
return
|
||||
except OSError:
|
||||
functions_general.log_infomsg('Invalid module path: %s' % (format_exc()))
|
||||
os.chdir(orig_path)
|
||||
return
|
||||
finally:
|
||||
# Change back to the original working directory.
|
||||
os.chdir(orig_path)
|
||||
|
||||
# The new script module has been cached, return the reference.
|
||||
return modreference
|
||||
return modreference.class_factory(source_obj)
|
||||
Loading…
Add table
Add a link
Reference in a new issue