def call_method(self,method,req_dict,tc,export_dict,log_line): """ call_method converts the res_dict delivered from an interface to the type of arguments expected by the service method. tc is the TypeConverter associated to the service method. """ global rx_cid,rx_cidx args = [] for arg in method.args(): if arg['name'] not in req_dict['args']: if 'default' in arg: args += [arg['default']] else: raise UndefinedServiceMethod(self.iface._interface_name(),self.sinst.servicename,'Parameter "%s" is not optional' % arg['name']) else: if type(arg['type'])==list: arg_list = [] type_info = get_type_info(arg['type'][0]) if type_info: for item in req_dict['args'][arg['name']]: arg_list += [arg['type'][0](prime_dict=item,tc=tc,export_dict=export_dict)] elif arg['type'][0]==attachment: for item in req_dict['args'][arg['name']]: arg_list += [extract_attachment_reference(item,export_dict,self.response_encoding,self.iface._interface_name(),self.sinst.servicename)] else: for item in req_dict['args'][arg['name']]: arg_list += [tc.from_unicode_string(item,arg['type'][0])] args += [arg_list] else: type_info = get_type_info(arg['type']) val = req_dict['args'][arg['name']] if type_info: args += [arg['type'](prime_dict=val,tc=tc,export_dict=export_dict)] elif arg['type']==attachment: args += [extract_attachment_reference(val,export_dict,self.response_encoding,self.iface._interface_name(),self.sinst.servicename)] else: args += [tc.from_unicode_string(val,arg['type'])] #path,fname = os.path.split(method.sinfo.sourcefile) #mname = os.path.splitext(fname)[0] mname = method.sinfo.modulename service_module = __import__(mname, globals(), locals(), '*') #file, pathname, description = imp.find_module(mname,[path]) #service_module = imp.load_module(mname,file, pathname, description) service_class_instance = getattr(service_module,method.sinfo.servicename)() if self.logging & LOG_REQUEST_ACCESS: log_line += ['Method:%s.%s' % (method.sinfo.servicename,req_dict['methodname'])] if self.logging & LOG_REQUEST_DICT: log_line += ['RequestDict:%s' % (str(req_dict))] if self.logging & LOG_EXECUTION_TIME: start = time.time() if method._has_keywords: kw = {'LADON_METHOD_TC':tc} kw.update(export_dict) result = getattr(service_class_instance,req_dict['methodname'])(*args,**kw) else: result = getattr(service_class_instance,req_dict['methodname'])(*args) if self.logging & LOG_EXECUTION_TIME: log_line.insert(0,'ExecutionTime:%s' % str(time.time()-start)) return result
def result_to_dict(self, method, result, tc, response_attachments, log_line): """ Convert the result of a method call to it's dictionary representation. tc is a TypeConverter """ res_dict = { 'servicename': method.sinfo.servicename, 'servicenumber': method.sinfo.servicenumber, 'method': method.name() } typ = method._rtype type_info = get_type_info(typ) if type_info == None: if [list, tuple].count(type(typ)): result_list = [] res_dict['result'] = result_list type_info = get_type_info(typ[0]) if result == typ: # Assumption list attributes are always optional return if type_info: for item in result: result_list += [ item.__dict__(tc, response_attachments) ] elif typ[0] == attachment: for item in result: if not type(item) == attachment: raise AttachmentExpected( self.iface._interface_name(), self.sinst.servicename, 'Attachment expected got: %s' % type(item)) result_list += [ response_attachments.add_attachment(item) ] else: for item in result: result_list += [tc.to_unicode_string(item, typ[0])] elif typ == attachment: res_dict['result'] = response_attachments.add_attachment( result) else: res_dict['result'] = tc.to_unicode_string(result, typ) else: res_dict['result'] = result.__dict__(tc, response_attachments) if self.logging & LOG_RESPONSE_DICT: log_line['ResponseDict'] = res_dict return res_dict
def call_method(self,method,req_dict,tc,export_dict): """ call_method converts the res_dict delivered from an interface to the type of arguments expected by the service method. tc is the TypeConverter associated to the service method. """ global rx_cid,rx_cidx args = [] for arg in method.args(): if arg['name'] not in req_dict['args']: if 'default' in arg: args += [arg['default']] else: raise UndefinedServiceMethod(self.iface._interface_name(),self.sinst.servicename,'Parameter "%s" is not optional' % arg['name']) else: if type(arg['type'])==list: arg_list = [] type_info = get_type_info(arg['type'][0]) if type_info: for item in req_dict['args'][arg['name']]: arg_list += [arg['type'][0](prime_dict=item,tc=tc,export_dict=export_dict)] elif arg['type'][0]==attachment: for item in req_dict['args'][arg['name']]: arg_list += [extract_attachment_reference(item,export_dict,self.response_encoding,self.iface._interface_name(),self.sinst.servicename)] else: for item in req_dict['args'][arg['name']]: arg_list += [tc.from_unicode_string(item,arg['type'][0])] args += [arg_list] else: type_info = get_type_info(arg['type']) val = req_dict['args'][arg['name']] if type_info: args += [arg['type'](prime_dict=val,tc=tc,export_dict=export_dict)] elif arg['type']==attachment: args += [extract_attachment_reference(val,export_dict,self.response_encoding,self.iface._interface_name(),self.sinst.servicename)] else: args += [tc.from_unicode_string(val,arg['type'])] path,fname = os.path.split(method.sinfo.sourcefile) mname = os.path.splitext(fname)[0] service_module = __import__(mname, globals(), locals(), '*') service_class_instance = getattr(service_module,method.sinfo.servicename)() if method._has_keywords: kw = {'LADON_METHOD_TC':tc} kw.update(export_dict) result = getattr(service_class_instance,req_dict['methodname'])(*args,**kw) else: result = getattr(service_class_instance,req_dict['methodname'])(*args) return result
def result_to_dict(self,method,result,tc,response_attachments,log_line): """ Convert the result of a method call to it's dictionary representation. tc is a TypeConverter """ res_dict = { 'servicename': method.sinfo.servicename, 'servicenumber': method.sinfo.servicenumber, 'method': method.name()} typ = method._rtype type_info = get_type_info(typ) if type_info==None: if [list,tuple].count(type(typ)): result_list = [] res_dict['result'] = result_list type_info = get_type_info(typ[0]) if result == typ: # Assumption list attributes are always optional return if type_info: for item in result: result_list += [item.__dict__(tc,response_attachments)] elif typ[0]==attachment: for item in result: if not type(item) == attachment: raise AttachmentExpected(self.iface._interface_name(),self.sinst.servicename,'Attachment expected got: %s' % type(item)) result_list += [response_attachments.add_attachment(item)] else: for item in result: result_list += [tc.to_unicode_string(item,typ[0])] elif typ==attachment: res_dict['result'] = response_attachments.add_attachment(result) else: res_dict['result'] = tc.to_unicode_string(result,typ) else: res_dict['result'] = result.__dict__(tc,response_attachments) if self.logging & LOG_RESPONSE_DICT: log_line += ['ResponseDict:%s' % (str(res_dict))] return res_dict
def __init__(self, prime_dict=None, tc=None, export_dict=None): global rx_cid if type(prime_dict) == dict: type_dict = get_type_info(self.__class__) for attr_name, attr_type, props in type_dict['attributes']: if type(attr_type) == list: # Assumption list attributes are always optional attr_val = [] if attr_name in prime_dict: if prime_dict[attr_name] == None: # Null lists are accepted ans parsed as empty list prime_dict[attr_name] = [] elif type(prime_dict[attr_name]) != list: raise ListAttributeMismatch( self,prime_dict, 'Expected list type for attribute "%s" got "%s"' % \ (attr_name,str(type(prime_dict[attr_name]))), attr_name) type_info = get_type_info(attr_type[0]) try: if type_info: for item in prime_dict[attr_name]: attr_val += [ attr_type[0](prime_dict=item, tc=tc, export_dict=export_dict) ] elif attr_type[0] == attachment: for item in prime_dict[attr_name]: try: attr_val += [ extract_attachment_reference( item, export_dict, tc.encoding) ] except Exception as e: raise NonExistingAttachment( self, prime_dict, str(e), attr_name) else: for item in prime_dict[attr_name]: attr_val += [ tc.from_unicode_string( item, attr_type[0], attr_name=attr_name) ] except Exception as e: raise SubitemTypeMismatch( self,prime_dict, 'Type mismatch for subitem of attibute "%s", expected "%s" got "%s"\nDetails:%s' % \ (attr_name,str(attr_type[0]),str(item),str(e)), attr_name) else: filters = props.get('filters') # Description of incoming filters: # PORTABLE_BYTES = Python 2: str, Python 3: bytes # * 'incoming_raw': # list functions that recieve the raw value (PORTABLE_BYTES). The function can for # instance do business logic validation and raise a client fault if the value makes # no sense. # Or it can return a new value (PORTABLE_BYTES) and thereby acting as a raw modifier. # * 'incoming': # list functions that recieve the value in the service specified type. # The function can do validation raise a client fault if the value makes no sense. # Or it can return a new value in the service specified type acting as a modifier. prefilters = None postfilters = None if filters and filters.get('incoming_raw'): prefilters = filters.get('incoming_raw') if filters and filters.get('incoming'): postfilters = filters.get('incoming') # Need some kind of check for optional in type definition class if attr_name in prime_dict: type_info = get_type_info(attr_type) val = prime_dict[attr_name] if val == None and props.get('nullable') == True: attr_val = val ## Run filters on incoming value if value was None #if filters and filters.get('incoming'): #for func in filters.get('incoming'): #attr_val = func(attr_val) elif type_info: attr_val = attr_type(prime_dict=val, tc=tc, export_dict=export_dict) elif attr_type == attachment: try: attr_val = extract_attachment_reference( val, export_dict, tc.encoding) except Exception as e: raise NonExistingAttachment( self, prime_dict, str(e), attr_name) else: attr_val = tc.from_unicode_string( val, attr_type, prefilters, postfilters, attr_name=attr_name) else: attr_val = props.get('default') if attr_val == None and props.get('nullable') != True: raise MandatoryAttributeMissing( self, prime_dict, 'Missing prime_dict attribute is not nullable "%s"' % attr_name, attr_name) ## Run filters on incoming value if value was None #if filters and filters.get('incoming'): #for func in filters.get('incoming'): #attr_val = func(attr_val) setattr(self, attr_name, attr_val) elif prime_dict: type_dict = TypeManager.global_type_dict[self.__class__] raise LadonTypePrimerMismatch( self,prime_dict, 'Dictionary expected for prime_dict got "%s" of value "%s"' % \ (type(prime_dict), str(prime_dict)))
def call_method(self, method, req_dict, tc, export_dict): """ call_method converts the res_dict delivered from an interface to the type of arguments expected by the service method. tc is the TypeConverter associated to the service method. """ global rx_cid, rx_cidx args = [] for arg in method.args(): if arg['name'] not in req_dict['args']: if 'default' in arg: args += [arg['default']] else: raise UndefinedServiceMethod( self.iface._interface_name(), self.sinst.servicename, 'Parameter "%s" is not optional' % arg['name']) else: if type(arg['type']) == list: arg_list = [] type_info = get_type_info(arg['type'][0]) if type_info: for item in req_dict['args'][arg['name']]: arg_list += [ arg['type'][0](prime_dict=item, tc=tc, export_dict=export_dict) ] elif arg['type'][0] == attachment: for item in req_dict['args'][arg['name']]: arg_list += [ extract_attachment_reference( item, export_dict, self.response_encoding, self.iface._interface_name(), self.sinst.servicename) ] else: for item in req_dict['args'][arg['name']]: arg_list += [ tc.from_unicode_string(item, arg['type'][0]) ] args += [arg_list] else: type_info = get_type_info(arg['type']) val = req_dict['args'][arg['name']] if type_info: args += [ arg['type'](prime_dict=val, tc=tc, export_dict=export_dict) ] elif arg['type'] == attachment: args += [ extract_attachment_reference( val, export_dict, self.response_encoding, self.iface._interface_name(), self.sinst.servicename) ] else: args += [tc.from_unicode_string(val, arg['type'])] path, fname = os.path.split(method.sinfo.sourcefile) mname = os.path.splitext(fname)[0] service_module = __import__(mname, globals(), locals(), '*') service_class_instance = getattr(service_module, method.sinfo.servicename)() if method._has_keywords: kw = {'LADON_METHOD_TC': tc} kw.update(export_dict) result = getattr(service_class_instance, req_dict['methodname'])(*args, **kw) else: result = getattr(service_class_instance, req_dict['methodname'])(*args) return result
def __init__(self,prime_dict=None,tc=None,export_dict=None): global rx_cid if type(prime_dict)==dict: type_dict = get_type_info(self.__class__) for attr_name,attr_type,props in type_dict['attributes']: if type(attr_type)==list: # Assumption list attributes are always optional attr_val = [] if attr_name in prime_dict: if prime_dict[attr_name]==None: # Null lists are accepted ans parsed as empty list prime_dict[attr_name] = [] elif type(prime_dict[attr_name])!=list: raise ListAttributeMismatch( self,prime_dict, 'Expected list type for attribute "%s" got "%s"' % \ (attr_name,str(type(prime_dict[attr_name]))), attr_name) type_info = get_type_info(attr_type[0]) try: if type_info: for item in prime_dict[attr_name]: attr_val += [attr_type[0](prime_dict=item,tc=tc,export_dict=export_dict)] elif attr_type[0]==attachment: for item in prime_dict[attr_name]: try: attr_val += [extract_attachment_reference(item,export_dict,tc.encoding)] except Exception as e: raise NonExistingAttachment( self,prime_dict, str(e), attr_name) else: for item in prime_dict[attr_name]: attr_val += [tc.from_unicode_string(item,attr_type[0])] except Exception as e: raise SubitemTypeMismatch( self,prime_dict, 'Type mismatch for subitem of attibute "%s", expected "%s" got "%s"\nDetails:%s' % \ (attr_name,str(attr_type[0]),str(item),str(e)), attr_name) else: filters = props.get('filters') # Description of incoming filters: # PORTABLE_BYTES = Python 2: str, Python 3: bytes # * 'incoming_raw': # list functions that recieve the raw value (PORTABLE_BYTES). The function can for # instance do business logic validation and raise a client fault if the value makes # no sense. # Or it can return a new value (PORTABLE_BYTES) and thereby acting as a raw modifier. # * 'incoming': # list functions that recieve the value in the service specified type. # The function can do validation raise a client fault if the value makes no sense. # Or it can return a new value in the service specified type acting as a modifier. prefilters = None postfilters = None if filters and filters.get('incoming_raw'): prefilters = filters.get('incoming_raw') if filters and filters.get('incoming'): postfilters = filters.get('incoming') # Need some kind of check for optional in type definition class if attr_name in prime_dict: type_info = get_type_info(attr_type) val = prime_dict[attr_name] if val==None and props.get('nullable')==True: attr_val = val ## Run filters on incoming value if value was None #if filters and filters.get('incoming'): #for func in filters.get('incoming'): #attr_val = func(attr_val) elif type_info: attr_val = attr_type(prime_dict=val,tc=tc,export_dict=export_dict) elif attr_type==attachment: try: attr_val = extract_attachment_reference(val,export_dict,tc.encoding) except Exception as e: raise NonExistingAttachment( self,prime_dict, str(e), attr_name) else: attr_val = tc.from_unicode_string(val,attr_type,prefilters,postfilters) else: attr_val=props.get('default') if attr_val==None and props.get('nullable')!=True: raise MandatoryAttributeMissing( self,prime_dict, 'Missing prime_dict attribute is not nullable "%s"' % attr_name, attr_name) ## Run filters on incoming value if value was None #if filters and filters.get('incoming'): #for func in filters.get('incoming'): #attr_val = func(attr_val) setattr(self,attr_name,attr_val) elif prime_dict: type_dict = TypeManager.global_type_dict[self.__class__] raise LadonTypePrimerMismatch( self,prime_dict, 'Dictionary expected for prime_dict got "%s" of value "%s"' % \ (type(prime_dict), str(prime_dict)))