Package horizons :: Package command
[hide private]
[frames] | no frames]

Source Code for Package horizons.command

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  import logging 
 23   
 24  from horizons.network.packets import SafeUnpickler 
 25  from horizons.util.python import get_all_subclasses 
 26  from horizons.util.worldobject import WorldObject 
27 28 29 -class Command:
30 """Base class for every Command.""" 31 log = logging.getLogger("command") 32 33 @classmethod
34 - def allow_network(cls, klass):
35 """ 36 NOTE: this is a security related method and may lead to 37 execution of arbritary code if used in a wrong way 38 see documentation inside horizons.network.packets.SafeUnpickler 39 """ 40 SafeUnpickler.add('server', klass)
41
42 - def execute(self, session, local=False):
43 """Execute command. 44 @param session: Execute command on this session's manager. 45 @param local: Execute the command only locally (only used in multiplayer manager) 46 @return: Propagated return value of manager's execute function. 47 """ 48 return session.manager.execute(self, local)
49 50 @classmethod
51 - def get_all_commands(cls):
52 return list(get_all_subclasses(cls))
53
54 55 -class GenericCommand(Command):
56 """Code generator for trivial commands on an object. 57 It saves an object's world id, and executes a method specified as string on it in __call__ 58 59 NOTE: Do not use floats! 2.6 and 2.7 handle them differently. 60 Use like this to call obj.mymethod(42, 1337): 61 62 class MyCommand(GenericCommand): 63 def __init__(self, obj): 64 super(MyCommand, self).__init__(obj, "mymethod", 42, 1337) 65 """
66 - def __init__(self, obj, method, *args, **kwargs):
67 self.obj_id = obj.worldid 68 self.method = method 69 self.args = args 70 self.kwargs = kwargs
71
72 - def __call__(self, issuer):
73 return getattr(self._get_object(), self.method)(*self.args, **self.kwargs)
74
75 - def _get_object(self):
76 return WorldObject.get_object_by_id(self.obj_id)
77
78 - def __str__(self):
79 return "GenericCommand({}, {}, {}, {}, {})".format( 80 self.__class__, self._get_object(), self.method, self.args, self.kwargs)
81
82 83 -class GenericComponentCommand(Command):
84 """Code generator for trivial commands on a component."""
85 - def __init__(self, component, method, *args, **kwargs):
86 self.obj_id = component.instance.worldid 87 self.method = method 88 self.component_name = component.NAME 89 self.args = args 90 self.kwargs = kwargs
91
92 - def __call__(self, issuer):
93 return getattr(self._get_object().get_component_by_name(self.component_name), self.method)(*self.args, **self.kwargs)
94
95 - def _get_object(self):
96 return WorldObject.get_object_by_id(self.obj_id)
97
98 - def __str__(self):
99 return "GenericCompCommand({}, {}, {}, {}, {}, {})".format( 100 self.__class__, self._get_object(), self.component_name, 101 self.method, self.args, self.kwargs)
102