From bd3d195d5b520089b532e045dcb8d5f471cf2442 Mon Sep 17 00:00:00 2001 From: Greg Taylor Date: Tue, 16 Dec 2008 03:36:49 +0000 Subject: [PATCH] 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. --- src/cmdtable.py | 3 +++ src/commands/parents.py | 45 +++++++++++++++++++++++++++++++++++++++++ src/scripthandler.py | 8 ++++---- 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 src/commands/parents.py diff --git a/src/cmdtable.py b/src/cmdtable.py index 9e02f186ce..0fae0f6dc6 100644 --- a/src/cmdtable.py +++ b/src/cmdtable.py @@ -11,6 +11,7 @@ permissions tuple. """ import commands.general import commands.paging +import commands.parents import commands.privileged import commands.comsys 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, priv_tuple=("genperms.builder")), 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, priv_tuple=("genperms.process_control")), GLOBAL_CMD_TABLE.add_command("@reload", commands.privileged.cmd_reload, diff --git a/src/commands/parents.py b/src/commands/parents.py new file mode 100644 index 0000000000..74ea2a354d --- /dev/null +++ b/src/commands/parents.py @@ -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 diff --git a/src/scripthandler.py b/src/scripthandler.py index 4f582ad442..30f5a29227 100644 --- a/src/scripthandler.py +++ b/src/scripthandler.py @@ -11,14 +11,14 @@ from src import logger # A dictionary with keys equivalent to the script's name and values that # contain references to the associated module for each key. -cached_scripts = {} +CACHED_SCRIPTS = {} 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. + the CACHED_SCRIPTS dictionary. Returns a reference to an instance of the script's class as per it's class_factory() method. @@ -27,7 +27,7 @@ def scriptlink(source_obj, scriptname): 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(scriptname, False) + retval = CACHED_SCRIPTS.get(scriptname, False) if retval: return retval.class_factory(source_obj) @@ -47,7 +47,7 @@ def scriptlink(source_obj, scriptname): logger.log_infomsg("SCRIPT: Caching and importing %s." % (scriptname)) modreference = __import__(full_script, fromlist=[script_name]) # Store the module reference for later fast retrieval. - cached_scripts[scriptname] = modreference + CACHED_SCRIPTS[scriptname] = modreference except ImportError: logger.log_infomsg('Error importing %s: %s' % (modname, format_exc())) os.chdir(settings.BASE_PATH)