From 3e57f493952768896248f7095bbc80bd615e4fb9 Mon Sep 17 00:00:00 2001 From: loki77 Date: Wed, 18 Jun 2008 04:36:23 +0000 Subject: [PATCH] New way of working with scriptlinks. Scripts should be defined as '.' separated directory structures, starting in the base evennia directory. --- apps/objects/models.py | 2 +- src/scripthandler.py | 17 ++++------------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/apps/objects/models.py b/apps/objects/models.py index 1fc332b30..416120c2d 100755 --- a/apps/objects/models.py +++ b/apps/objects/models.py @@ -646,7 +646,7 @@ class Object(models.Model): """ if not self.scriptlink: if self.is_player(): - script_to_load = 'player/basicplayer' + script_to_load = 'player.basicplayer' else: script_to_load = 'basicobject' self.scriptlink = scripthandler.scriptlink(self, self.get_attribute_value('__parent', script_to_load)) diff --git a/src/scripthandler.py b/src/scripthandler.py index 04647f77b..0eeeb60fd 100644 --- a/src/scripthandler.py +++ b/src/scripthandler.py @@ -40,19 +40,13 @@ def scriptlink(source_obj, scriptname): # 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] - path_split = scriptname.split('/') - script_name = path_split[-1] - script_path = '/'.join(path_split[:-1]) + full_script = "src.scripts.%s" % (scriptname,) + script_name = full_script.split('.')[-1] try: # Change the working directory to the location of the script and import. - os.chdir('%s/%s/' % (settings.SCRIPT_ROOT, script_path)) - logger.log_infomsg("SCRIPT: Caching and importing %s." % (script_name)) - modreference = __import__(script_name) + 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 except ImportError: @@ -64,8 +58,5 @@ def scriptlink(source_obj, scriptname): os.chdir(settings.BASE_PATH) return - # Change back to the original working directory. - os.chdir(settings.BASE_PATH) - # The new script module has been cached, return the reference. return modreference.class_factory(source_obj)