Beginnings of @parent, the in-game tie-in for the scripting system. Can now list the currently cached scripts via @parent/showcache, and clear the cache via @parent/clearcache.

This commit is contained in:
Greg Taylor 2008-12-16 03:36:49 +00:00
parent 18e2eca2c5
commit bd3d195d5b
3 changed files with 52 additions and 4 deletions

View file

@ -11,6 +11,7 @@ permissions tuple.
""" """
import commands.general import commands.general
import commands.paging import commands.paging
import commands.parents
import commands.privileged import commands.privileged
import commands.comsys import commands.comsys
import commands.unloggedin import commands.unloggedin
@ -96,6 +97,8 @@ GLOBAL_CMD_TABLE.add_command("@newpassword", commands.privileged.cmd_newpassword
GLOBAL_CMD_TABLE.add_command("@open", commands.objmanip.cmd_open, GLOBAL_CMD_TABLE.add_command("@open", commands.objmanip.cmd_open,
priv_tuple=("genperms.builder")), priv_tuple=("genperms.builder")),
GLOBAL_CMD_TABLE.add_command("@password", commands.general.cmd_password), GLOBAL_CMD_TABLE.add_command("@password", commands.general.cmd_password),
GLOBAL_CMD_TABLE.add_command("@parent", commands.parents.cmd_parent,
priv_tuple=("genperms.builder")),
GLOBAL_CMD_TABLE.add_command("@ps", commands.info.cmd_ps, GLOBAL_CMD_TABLE.add_command("@ps", commands.info.cmd_ps,
priv_tuple=("genperms.process_control")), priv_tuple=("genperms.process_control")),
GLOBAL_CMD_TABLE.add_command("@reload", commands.privileged.cmd_reload, GLOBAL_CMD_TABLE.add_command("@reload", commands.privileged.cmd_reload,

45
src/commands/parents.py Normal file
View file

@ -0,0 +1,45 @@
"""
Contains commands for managing script parents.
"""
from src import scripthandler
def clear_cached_scripts(command):
"""
Show the currently cached scripts.
"""
session = command.session
cache_dict = scripthandler.CACHED_SCRIPTS
cache_size = len(cache_dict)
cache_dict.clear()
session.msg("Script parent cached cleared (%d previously in cache)." %
cache_size)
def show_cached_scripts(command):
"""
Clears the cached scripts by deleting their keys from the script cache
dictionary. The next time an object needs the previously loaded scripts,
they are loaded again.
"""
session = command.session
cache_dict = scripthandler.CACHED_SCRIPTS
retval = "Currently Cached Script Parents\n"
retval += "-" * 78
for script in cache_dict.keys():
retval += "\n " + script
retval += "\n" + "-" * 78 + "\n"
retval += "%d cached parents." % len(cache_dict)
session.msg(retval)
def cmd_parent(command):
"""
Figure out what form of the command the user is using and branch off
accordingly.
"""
if "showcache" in command.command_switches:
show_cached_scripts(command)
return
if "clearcache" in command.command_switches:
clear_cached_scripts(command)
return

View file

@ -11,14 +11,14 @@ from src import logger
# A dictionary with keys equivalent to the script's name and values that # A dictionary with keys equivalent to the script's name and values that
# contain references to the associated module for each key. # contain references to the associated module for each key.
cached_scripts = {} CACHED_SCRIPTS = {}
def scriptlink(source_obj, scriptname): def scriptlink(source_obj, scriptname):
""" """
Each Object will refer to this function when trying to execute a function 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, contained within a scripted module. For the sake of ease of management,
modules are cached and compiled as they are requested and stored in modules are cached and compiled as they are requested and stored in
the cached_scripts dictionary. the CACHED_SCRIPTS dictionary.
Returns a reference to an instance of the script's class as per it's Returns a reference to an instance of the script's class as per it's
class_factory() method. class_factory() method.
@ -27,7 +27,7 @@ def scriptlink(source_obj, scriptname):
scriptname: (str) Name of the module to load (minus 'scripts'). scriptname: (str) Name of the module to load (minus 'scripts').
""" """
# The module is already cached, just return it rather than re-load. # The module is already cached, just return it rather than re-load.
retval = cached_scripts.get(scriptname, False) retval = CACHED_SCRIPTS.get(scriptname, False)
if retval: if retval:
return retval.class_factory(source_obj) return retval.class_factory(source_obj)
@ -47,7 +47,7 @@ def scriptlink(source_obj, scriptname):
logger.log_infomsg("SCRIPT: Caching and importing %s." % (scriptname)) logger.log_infomsg("SCRIPT: Caching and importing %s." % (scriptname))
modreference = __import__(full_script, fromlist=[script_name]) modreference = __import__(full_script, fromlist=[script_name])
# Store the module reference for later fast retrieval. # Store the module reference for later fast retrieval.
cached_scripts[scriptname] = modreference CACHED_SCRIPTS[scriptname] = modreference
except ImportError: except ImportError:
logger.log_infomsg('Error importing %s: %s' % (modname, format_exc())) logger.log_infomsg('Error importing %s: %s' % (modname, format_exc()))
os.chdir(settings.BASE_PATH) os.chdir(settings.BASE_PATH)