def __new__(cls, scheme, hostname, port, is_wan): is_wan = bool(is_wan) if is_wan is not None else None port = int(port) if port is not None else None scheme = str(scheme) if scheme is not None else None hostname = uc(hostname) if hostname is not None else None return super(ConnectorSpec, cls).__new__(cls, scheme, hostname, port, is_wan)
def __new__(cls, scheme, hostname, port): assert scheme and hostname port = int(port) if port is not None else None scheme = str(scheme) hostname = uc(hostname) return super(ConnectorInfo, cls).__new__(cls, scheme, hostname, port)
def encode_error(self, error, pretty=False, encoding="utf-8"): try: statuscode = error.statusCode except AttributeError: status = "STATUS_INTERNAL_SERVER_ERROR" else: _, status = get_status_message_safe(statuscode) return binding.ErrorInfo( statusCode=status, additionalInfo=uc(error)).toDOM( element_name="ns1:errorInfo").toxml(encoding=encoding)
def _build_elementtree(self, representation, id_attribute=[]): resource_name, representation_values = representation.items()[0] self.logger.debug( "building elementtree from: resource_name: %s and representation_value: %s", resource_name, representation_values) tagname = QName(XMLNS, resource_name) e = ET.Element(tagname) if isinstance(representation_values, dict): for k, v in representation_values.iteritems(): if k in id_attribute: e.set(namespace_url + k, v) elif isinstance(v, list): for i in v: s = self._build_elementtree({k: i}, id_attribute=id_attribute) e.append(s) elif isinstance( v, dict ): #check if instance is a dict: for example searchStrings :{ 'searchString': [] } #if k == "searchStrings": # v = {'searchString': ["XML serializer test searchstring", "this is just a test"]} s = self._build_elementtree( {k: v}, id_attribute=id_attribute) #create a subelement e.append(s) #append the result elif k == "$t": self.logger.debug("hve: %s", v) e.text = v else: s = ET.SubElement(e, namespace_url + k) s.text = str(v) elif isinstance(representation_values, list): for i in representation_values: #we have a list if isinstance(i, str): e.text = str(i) else: s = self._build_elementtree({resource_name: i}, id_attribute=id_attribute) e.append(s) elif isinstance(representation_values, (basestring, int, float)): e.text = uc(representation_values) elif isinstance(representation_values, datetime): e.text = representation_values.isoformat() else: self.logger.debug( "building elementtree: Unknown representation value, %s", type(representation_values)) return e
def encode_error(self, error, pretty=False, encoding="utf-8"): try: statuscode = error.statusCode except AttributeError: status = "STATUS_INTERNAL_SERVER_ERROR" else: _, status = get_status_message_safe(statuscode) representation = { "errorInfo": { "statusCode": status, "additionalInfo": error.payload if hasattr(error, "payload") else uc(error) } } if not pretty: return dumps(representation) return self.encoder.encode(representation)
def _set_mandatory_create_attributes(self, values): values["creationTime"] = values["lastModifiedTime"] = self.now # set values for parentID and resourceID values["parentID"] = self.parent.resourceID short_name = get_short_resource_name(self.resource_type.typename) try: values["resourceID"] = short_name + str( _resource_id_counter[short_name]) _resource_id_counter[short_name] += 1 except KeyError: _resource_id_counter[short_name] = 0 values["resourceID"] = short_name + str( _resource_id_counter[short_name]) _resource_id_counter[short_name] += 1 try: name = uc(values["resourceName"]) except KeyError: name = "%s-%s" % (self.resource_type.typename, self._create_id()) self.name = values["resourceName"] = name values["resourceType"] = ResourceTypeE[self.resource_type.typename]