From 402a4c8aa0757c4933b28b935f1bd62313611d85 Mon Sep 17 00:00:00 2001 From: Griatch Date: Sat, 1 Oct 2016 13:47:23 +0200 Subject: [PATCH] Add support to has_cmdset for cmdset instances as suggested in #1083. --- evennia/commands/cmdsethandler.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/evennia/commands/cmdsethandler.py b/evennia/commands/cmdsethandler.py index 9f701e977b..656d94ab4f 100644 --- a/evennia/commands/cmdsethandler.py +++ b/evennia/commands/cmdsethandler.py @@ -537,12 +537,13 @@ class CmdSetHandler(object): self.obj.cmdset_storage = storage self.update() - def has_cmdset(self, cmdset_key, must_be_default=False): + def has(self, cmdset, must_be_default=False): """ - checks so the cmdsethandler contains a cmdset with the given key. + checks so the cmdsethandler contains a given cmdset Args: - cmdset_key (str): Cmdset key to check + cmdset (str or Cmdset): Cmdset key, pythonpath or + Cmdset to check the existence for. must_be_default (bool, optional): Only return True if the checked cmdset is the default one. @@ -550,11 +551,27 @@ class CmdSetHandler(object): has_cmdset (bool): Whether or not the cmdset is in the handler. """ - if must_be_default: - return self.cmdset_stack and self.cmdset_stack[0].key == cmdset_key + if callable(cmdset) and hasattr(cmdset, 'path'): + # try it as a callable + print "Try callable", cmdset + if must_be_default: + return self.cmdset_stack and (self.cmdset_stack[0].path == cmdset.path) + else: + print [cset.path for cset in self.cmdset_stack], cmdset.path + return any([cset for cset in self.cmdset_stack + if cset.path == cmdset.path]) else: - return any([cmdset.key == cmdset_key or cmdset.path == cmdset_key - for cmdset in self.cmdset_stack]) + # try it as a path or key + if must_be_default: + return self.cmdset_stack and ( + self.cmdset_stack[0].key == cmdset or + self.cmdset_stack[0].path == cmdset) + else: + return any([cset for cset in self.cmdset_stack + if cset.path == cmdset or cset.key == cmdset]) + + # backwards-compatability alias + has_cmdset = has def reset(self): """