From a88afabd6053ab01b6eff920c6595804c5fb4ced Mon Sep 17 00:00:00 2001 From: Griatch Date: Sun, 24 Aug 2014 09:43:55 +0200 Subject: [PATCH] Added **kwargs to cmdhandler and execute_cmd() methods, to set arbitrary flags on commands at run-time. Unused by default Evennia but may be useful to codedly change operation parameters on commands at run-time. --- src/commands/cmdhandler.py | 10 +++++++++- src/objects/models.py | 8 ++++++-- src/objects/objects.py | 6 +++++- src/players/models.py | 9 +++++++-- src/players/player.py | 8 ++++++-- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/commands/cmdhandler.py b/src/commands/cmdhandler.py index 33116d915e..14d7e49a9e 100644 --- a/src/commands/cmdhandler.py +++ b/src/commands/cmdhandler.py @@ -251,7 +251,7 @@ def get_and_merge_cmdsets(caller, session, player, obj, # Main command-handler function @inlineCallbacks -def cmdhandler(called_by, raw_string, testing=False, callertype="session", sessid=None): +def cmdhandler(called_by, raw_string, testing=False, callertype="session", sessid=None, **kwargs): """ This is the main function to handle any string sent to the engine. @@ -268,6 +268,10 @@ def cmdhandler(called_by, raw_string, testing=False, callertype="session", sessi giving them precendence for same-name and same-prio commands. sessid - Relevant if callertype is "player" - the session id will help retrieve the correct cmdsets from puppeted objects. + **kwargs - other keyword arguments will be assigned as named variables on the + retrieved command object *before* it is executed. This is unuesed + in default Evennia but may be used by code to set custom flags or + special operating conditions for a command as it executes. Note that this function returns a deferred! """ @@ -392,6 +396,10 @@ def cmdhandler(called_by, raw_string, testing=False, callertype="session", sessi # only return the command instance returnValue(cmd) + # assign custom kwargs to found cmd object + for key, val in kwargs.items(): + setattr(cmd, key, val) + # pre-command hook yield cmd.at_pre_cmd() diff --git a/src/objects/models.py b/src/objects/models.py index fa04ea8e10..3bb1b9fa4a 100644 --- a/src/objects/models.py +++ b/src/objects/models.py @@ -482,7 +482,7 @@ class ObjectDB(TypedObject): # Execution/action methods # - def execute_cmd(self, raw_string, sessid=None): + def execute_cmd(self, raw_string, sessid=None, **kwargs): """ Do something as this object. This method is a copy of the execute_ cmd method on the session. This is never called normally, it's only @@ -492,6 +492,10 @@ class ObjectDB(TypedObject): Argument: raw_string (string) - raw command input sessid (int) - optional session id to return results to + **kwargs - other keyword arguments will be added to the found command + object instace as variables before it executes. This is + unused by default Evennia but may be used to set flags and + change operating paramaters for commands at run-time. Returns Deferred - this is an asynchronous Twisted object that will not fire until the command has actually finished executing. To @@ -509,7 +513,7 @@ class ObjectDB(TypedObject): raw_string = to_unicode(raw_string) raw_string = self.nicks.nickreplace(raw_string, categories=("inputline", "channel"), include_player=True) - return cmdhandler.cmdhandler(_GA(self, "typeclass"), raw_string, callertype="object", sessid=sessid) + return cmdhandler.cmdhandler(_GA(self, "typeclass"), raw_string, callertype="object", sessid=sessid, **kwargs) def msg(self, text=None, from_obj=None, sessid=0, **kwargs): """ diff --git a/src/objects/objects.py b/src/objects/objects.py index dd0a978243..5d7be5a349 100644 --- a/src/objects/objects.py +++ b/src/objects/objects.py @@ -300,7 +300,7 @@ class Object(TypeClass): return self.player return self.dbobj.search_player(searchdata, quiet=quiet) - def execute_cmd(self, raw_string, sessid=None): + def execute_cmd(self, raw_string, sessid=None, **kwargs): """ Do something as this object. This command transparently lets its typeclass execute the command. This method is @@ -311,6 +311,10 @@ class Object(TypeClass): raw_string (string) - raw command input sessid (int) - id of session executing the command. This sets the sessid property on the command. + **kwargs - other keyword arguments will be added to the found command + object instace as variables before it executes. This is + unused by default Evennia but may be used to set flags and + change operating paramaters for commands at run-time. Returns Deferred - this is an asynchronous Twisted object that will not fire until the command has actually finished executing. To diff --git a/src/players/models.py b/src/players/models.py index 7c45195176..d8c341b814 100644 --- a/src/players/models.py +++ b/src/players/models.py @@ -426,7 +426,7 @@ class PlayerDB(TypedObject, AbstractUser): _GA(self, "aliases").clear() super(PlayerDB, self).delete(*args, **kwargs) - def execute_cmd(self, raw_string, sessid=None): + def execute_cmd(self, raw_string, sessid=None, **kwargs): """ Do something as this player. This method is never called normally, but only when the player object itself is supposed to execute the @@ -434,6 +434,11 @@ class PlayerDB(TypedObject, AbstractUser): eventual puppets. raw_string - raw command input coming from the command line. + sessid - the optional session id to be responsible for the command-send + **kwargs - other keyword arguments will be added to the found command + object instace as variables before it executes. This is + unused by default Evennia but may be used to set flags and + change operating paramaters for commands at run-time. """ raw_string = utils.to_unicode(raw_string) raw_string = self.nicks.nickreplace(raw_string, @@ -448,7 +453,7 @@ class PlayerDB(TypedObject, AbstractUser): # this can happen for bots sessid = None return cmdhandler.cmdhandler(self.typeclass, raw_string, - callertype="player", sessid=sessid) + callertype="player", sessid=sessid, **kwargs) def search(self, searchdata, return_puppet=False, **kwargs): """ diff --git a/src/players/player.py b/src/players/player.py index 8f5298be8b..3615f607a5 100644 --- a/src/players/player.py +++ b/src/players/player.py @@ -133,7 +133,7 @@ class Player(TypeClass): """ return self.dbobj.swap_character(new_character, delete_old_character=delete_old_character) - def execute_cmd(self, raw_string, sessid=None): + def execute_cmd(self, raw_string, sessid=None, **kwargs): """ Do something as this object. This command transparently lets its typeclass execute the command. This method @@ -144,6 +144,10 @@ class Player(TypeClass): raw_string (string) - raw command input sessid (int) - id of session executing the command. This sets the sessid property on the command + **kwargs - other keyword arguments will be added to the found command + object instace as variables before it executes. This is + unused by default Evennia but may be used to set flags and + change operating paramaters for commands at run-time. Returns Deferred - this is an asynchronous Twisted object that will not fire until the command has actually finished executing. To @@ -155,7 +159,7 @@ class Player(TypeClass): be useful for coders intending to implement some sort of nested command structure. """ - return self.dbobj.execute_cmd(raw_string, sessid=sessid) + return self.dbobj.execute_cmd(raw_string, sessid=sessid, **kwargs) def search(self, searchdata, return_puppet=False, **kwargs): """