Package brisa :: Package upnp :: Package device :: Module action
[hide private]
[frames] | no frames]

Source Code for Module brisa.upnp.device.action

  1  # Licensed under the MIT license 
  2  # http://opensource.org/licenses/mit-license.php or see LICENSE file. 
  3  # Copyright 2007-2008 Brisa Team <brisa-develop@garage.maemo.org> 
  4   
  5  """ Device-side action class used for implementing UPnP actions. 
  6  """ 
  7   
  8  from brisa.upnp.base_action import BaseAction, BaseArgument 
  9   
 10  from brisa.core import log 
 11   
 12   
13 -class InvalidActionOutput(Exception):
14 pass
15 16
17 -class InvalidActionInput(Exception):
18 pass
19 20
21 -class Argument(BaseArgument):
22 23 IN = "in" 24 OUT = "out" 25
26 - def __init__(self, arg_name, arg_direction, arg_state_var):
27 """ Constructor for the Argument class. 28 29 @param arg_name: argument name 30 @param arg_direction: argument direction 31 @param arg_state_var: argument related state variable 32 33 @type arg_name: string 34 @type arg_direction: string 35 @type arg_state_var: string 36 """ 37 BaseArgument.__init__(self, arg_name, arg_direction, arg_state_var)
38 39
40 -class Action(BaseAction):
41
42 - def __init__(self, service, name, arguments = []):
43 BaseAction.__init__(self, service, name, arguments) 44 self.run_function = self.run
45
46 - def add_argument(self, argument):
47 """ Adds an argument to the action. 48 49 @param argument: the argument 50 @type argument: ArgumentDevice 51 """ 52 if argument: 53 self.arguments.append(argument)
54
55 - def get_in_argument(self, name):
56 """ Returns the in argument with the given name. 57 58 @param name: argument name 59 @type name: string 60 61 @rtype: Argument 62 """ 63 for arg in self.arguments: 64 if arg.direction == Argument.IN and arg.name == name: 65 return arg 66 return None
67
68 - def get_out_argument(self, name):
69 """ Returns the out argument with the given name. 70 71 @param name: argument name 72 @type name: string 73 74 @rtype: Argument 75 """ 76 for arg in self.arguments: 77 if arg.direction == Argument.OUT and arg.name == name: 78 return arg 79 return None
80
81 - def __call__(self, *args, **kwargs):
82 log.debug('Entering at action %s __call__' % self.name) 83 # Update in arguments 84 in_kwargs = {} 85 86 log.debug('Updating IN variables') 87 for arg_name, arg_value in kwargs.iteritems(): 88 arg = self.get_in_argument(arg_name) 89 if not arg: 90 log.error('Input argument "%s" not' \ 91 ' present on action definition.' \ 92 % arg_name) 93 raise InvalidActionInput('Input argument "%s" not' \ 94 ' present on action definition.' \ 95 % arg_name) 96 arg.state_var.update(arg_value) 97 in_kwargs[arg_name] = arg_value 98 99 log.debug('Calling run function') 100 out_args = self.run_function(*(), **in_kwargs) 101 102 if not isinstance(out_args, dict): 103 log.error('output is not a dict.') 104 raise InvalidActionOutput('output is not a dict.') 105 106 # Update out arguments 107 return_args = {} 108 109 log.debug('Updating OUT variables') 110 for arg_name, arg_value in out_args.iteritems(): 111 arg = self.get_out_argument(arg_name) 112 if not arg: 113 log.error('output contains argument "%s" not'\ 114 ' present on action definition' % \ 115 arg_name) 116 raise InvalidActionOutput('output contains argument "%s" not'\ 117 ' present on action definition' % \ 118 arg_name) 119 120 arg.state_var.update(arg_value) 121 return_args[arg_name] = arg_value 122 123 log.debug('Returning from action %s __call__' % self.name) 124 return {self.name + "Response": return_args}
125
126 - def run(self, *args, **kwargs):
127 return {}
128