Added settings.CMDSET_PATHS to allow to give cmdsets without the full path.

This commit is contained in:
Griatch 2014-05-10 19:51:46 +02:00
parent 63c099c22f
commit 3be02c8f03

View file

@ -63,6 +63,7 @@ can then implement separate sets for different situations. For
example, you can have a 'On a boat' set, onto which you then tack on example, you can have a 'On a boat' set, onto which you then tack on
the 'Fishing' set. Fishing from a boat? No problem! the 'Fishing' set. Fishing from a boat? No problem!
""" """
from django.conf import settings
from src.utils import logger, utils from src.utils import logger, utils
from src.commands.cmdset import CmdSet from src.commands.cmdset import CmdSet
from src.server.models import ServerConfig from src.server.models import ServerConfig
@ -71,7 +72,7 @@ from django.utils.translation import ugettext as _
__all__ = ("import_cmdset", "CmdSetHandler") __all__ = ("import_cmdset", "CmdSetHandler")
_CACHED_CMDSETS = {} _CACHED_CMDSETS = {}
_CMDSET_PATHS = utils.make_iter(settings.CMDSET_PATHS)
class _ErrorCmdSet(CmdSet): class _ErrorCmdSet(CmdSet):
"This is a special cmdset used to report errors" "This is a special cmdset used to report errors"
@ -84,12 +85,12 @@ class _EmptyCmdSet(CmdSet):
priority = -101 priority = -101
mergetype = "Union" mergetype = "Union"
def import_cmdset(python_path, cmdsetobj, emit_to_obj=None, no_logging=False): def import_cmdset(path, cmdsetobj, emit_to_obj=None, no_logging=False):
""" """
This helper function is used by the cmdsethandler to load a cmdset This helper function is used by the cmdsethandler to load a cmdset
instance from a python module, given a python_path. It's usually accessed instance from a python module, given a python_path. It's usually accessed
through the cmdsethandler's add() and add_default() methods. through the cmdsethandler's add() and add_default() methods.
python_path - This is the full path to the cmdset object. path - This is the full path to the cmdset object on python dot-form
cmdsetobj - the database object/typeclass on which this cmdset is to be cmdsetobj - the database object/typeclass on which this cmdset is to be
assigned (this can be also channels and exits, as well as players assigned (this can be also channels and exits, as well as players
but there will always be such an object) but there will always be such an object)
@ -101,7 +102,10 @@ def import_cmdset(python_path, cmdsetobj, emit_to_obj=None, no_logging=False):
function returns None if an error was encountered or path not found. function returns None if an error was encountered or path not found.
""" """
try: python_paths = [path] + ["%s.%s" % (prefix, path)
for prefix in _CMDSET_PATHS]
errstring = ""
for python_path in python_paths:
try: try:
#print "importing %s: _CACHED_CMDSETS=%s" % (python_path, _CACHED_CMDSETS) #print "importing %s: _CACHED_CMDSETS=%s" % (python_path, _CACHED_CMDSETS)
wanted_cache_key = python_path wanted_cache_key = python_path
@ -120,22 +124,20 @@ def import_cmdset(python_path, cmdsetobj, emit_to_obj=None, no_logging=False):
return cmdsetclass return cmdsetclass
except ImportError: except ImportError:
errstring = _("Error loading cmdset: Couldn't import module '%s'.") errstring += _("Error loading cmdset: Couldn't import module '%s'.")
errstring = errstring % modulepath errstring = errstring % modulepath
raise
except KeyError: except KeyError:
errstring = _("Error in loading cmdset: No cmdset class '%(classname)s' in %(modulepath)s.") errstring += _("Error in loading cmdset: No cmdset class '%(classname)s' in %(modulepath)s.")
errstring = errstring % {"classname": classname, errstring = errstring % {"classname": classname,
"modulepath": modulepath} "modulepath": modulepath}
raise
except Exception: except Exception:
errstring = _("Compile/Run error when loading cmdset '%s'. Error was logged.") errstring += _("Compile/Run error when loading cmdset '%s'. Error was logged.")
errstring = errstring % (python_path) errstring = errstring % (python_path)
raise
except Exception: if errstring:
# returning an empty error cmdset # returning an empty error cmdset
if not no_logging: if not no_logging:
logger.log_trace(errstring) logger.log_errmsg(errstring)
if emit_to_obj and not ServerConfig.objects.conf("server_starting_mode"): if emit_to_obj and not ServerConfig.objects.conf("server_starting_mode"):
emit_to_obj.msg(errstring) emit_to_obj.msg(errstring)
err_cmdset = _ErrorCmdSet() err_cmdset = _ErrorCmdSet()