Package smach :: Module util

Source Code for Module smach.util

  1   
  2  import smach 
  3  import threading 
  4   
  5   
  6  __all__ = ['is_shutdown','set_shutdown_cb',\ 
  7          'cb_interface','has_smach_interface','CBInterface'] 
  8   
9 -def is_shutdown():
10 return False
11
12 -def set_shutdown_check(cb):
13 smach.is_shutdown = cb
14
15 -def has_smach_interface(obj):
16 """Returns True if the object has SMACH interface accessors.""" 17 return hasattr(obj,'get_registered_input_keys')\ 18 and hasattr(obj,'get_registered_output_keys')\ 19 and hasattr(obj,'get_registered_outcomes')
20 21 # Callback decorator for describing userdata
22 -class cb_interface(object):
23 - def __init__(self, outcomes=[], input_keys=[], output_keys=[]):
24 self._outcomes = outcomes 25 self._input_keys = input_keys 26 self._output_keys = output_keys
27
28 - def __call__(self, cb):
29 return CBInterface(cb, self._outcomes, self._input_keys, self._output_keys)
30 -class CBInterface(object):
31 """Decorator to describe the extension of a state's SMACH userdata and outcome interface. 32 33 Some SMACH states can be extended with the use of user callbacks. Since 34 the SMACH interface and SMACH userdata are strictly controlled, the ways in 35 which these callbacks interact with SMACH must be delcared. This decorator 36 allows this information to be attached to a given callback function. 37 38 If a callback adds a potential outcome to a state, suppose 'critical_failure', 39 then one could write this when defining the callback: 40 41 >>> import smach 42 >>> @smach.cb_interface(outcomes=['critical_failure']) 43 >>> def my_cb(x,y,z): 44 >>> # User code 45 >>> return 'critical_failure' 46 47 Suppose a state retrieves data that it passes into a callback. If the user 48 wants to take that data and put some of all of it into userdata, this 49 interface must be declared. In this case, the user could write: 50 51 >>> import smach 52 >>> @smach.cb_interface(output_keys=['processed_res']) 53 >>> def my_cb(ud, data): 54 >>> ud.processed_res = data 55 56 """
57 - def __init__(self, cb, outcomes=[], input_keys=[], output_keys=[], io_keys=[]):
58 """Describe callback SMACH interface. 59 60 @type outcomes: array of strings 61 @param outcomes: Custom outcomes for this state. 62 63 @type input_keys: array of strings 64 @param input_keys: The userdata keys from which this state might read 65 at runtime. 66 67 @type output_keys: array of strings 68 @param output_keys: The userdata keys to which this state might write 69 at runtime. 70 71 @type io_keys: array of strings 72 @param io_keys: The userdata keys to which this state might write or 73 from which it might read at runtime. 74 """ 75 76 self._input_keys = set(input_keys) 77 self._input_keys.union(io_keys) 78 79 self._output_keys = set(output_keys) 80 self._output_keys.union(io_keys) 81 82 self._outcomes = outcomes 83 84 self._cb = cb
85
86 - def __call__(self, *args, **kwargs):
87 return self._cb(*args, **kwargs)
88 89 ### SMACH Interface API
91 """Get a tuple of registered input keys.""" 92 return tuple(self._input_keys)
94 """Get a tuple of registered output keys.""" 95 return tuple(self._output_keys)
96 - def get_registered_outcomes(self):
97 """Get a list of registered outcomes. 98 @rtype: tuple of string 99 @return: Tuple of registered outcome strings. 100 """ 101 return tuple(self._outcomes)
102