def unicode_from_string(self, cls, value): retval = value if isinstance(value, six.binary_type): if cls.Attributes.encoding is None: retval = six.text_type(value, errors=cls.Attributes.unicode_errors) else: retval = six.text_type(value, cls.Attributes.encoding, errors=cls.Attributes.unicode_errors) return retval
def unicode_from_string(self, cls, value): retval = value if isinstance(value, str): if cls.Attributes.encoding is None: retval = six.text_type(value, errors=cls.Attributes.unicode_errors) else: retval = six.text_type(value, cls.Attributes.encoding, errors=cls.Attributes.unicode_errors) return retval
def unicode_from_string(self, cls, value): retval = value cls_attrs = self.get_cls_attrs(cls) if isinstance(value, six.binary_type): if cls_attrs.encoding is None: retval = six.text_type(value, errors=cls_attrs.unicode_errors) else: retval = six.text_type(value, cls_attrs.encoding, errors=cls_attrs.unicode_errors) return retval
def __validate_lxml(self, payload): ret = self.validation_schema.validate(payload) logger.debug("Validated ? %r" % ret) if ret == False: error_text = text_type(self.validation_schema.error_log.last_error) raise SchemaValidationError(error_text.encode("ascii", "xmlcharrefreplace"))
def validate_document(self, payload): """ Validate the document against the KING reference WSDL. """ # TODO [TECH]: For some reason xmllint nor xmlstarlet validates # against the gml XSD properly. I can't find an xml validator which # does support this (my guess, they're all based on libxml2). if payload.xpath('//gml:*', namespaces={'gml': GML_XML_NS}): # Make a copy to prevent removing data. payload_copy = copy.deepcopy(payload) for el in payload_copy.xpath('//gml:*', namespaces={'gml': GML_XML_NS}): el.getparent().remove(el) else: # No need to make a copy. We're not removing anything. payload_copy = payload xsd_path = os.path.join(settings.ZAAKMAGAZIJN_ZDS_PATH, 'zds0120_msg_zs-dms_resolved2017.xsd') xmlschema = etree.XMLSchema(file=xsd_path) ret = xmlschema.validate(payload_copy) logger.debug("Validated ? %r" % ret) if not ret: error_text = text_type(xmlschema.error_log.last_error) raise SchemaValidationError( error_text.encode('ascii', 'xmlcharrefreplace'))
def test_send_out_complex_header(self): out_header = self.client.factory.create('OutHeader') out_header.dt = datetime(year=2000, month=1, day=1) out_header.f = 3.141592653 out_trace_header = self.client.factory.create('OutTraceHeader') out_trace_header.receiptDate = datetime(year=2000, month=1, day=1, hour=1, minute=1, second=1, microsecond=1) out_trace_header.returnDate = datetime(year=2000, month=1, day=1, hour=1, minute=1, second=1, microsecond=100) ret = self.client.service.send_out_complex_header() self.assertTrue(isinstance(ret[0], type(out_header))) self.assertEquals(ret[0].dt, out_header.dt) self.assertEquals(ret[0].f, out_header.f) self.assertTrue(isinstance(ret[1], type(out_trace_header))) self.assertEquals(ret[1].receiptDate, out_trace_header.receiptDate) self.assertEquals(ret[1].returnDate, out_trace_header.returnDate) # Control the reply soap header (in an unelegant way but this is the # only way with suds) soapheaders = self.client.options.plugins[0].reply.getChild("Envelope").getChild("Header") soap_out_header = soapheaders.getChild('OutHeader') self.assertEquals('T'.join((out_header.dt.date().isoformat(), out_header.dt.time().isoformat())), soap_out_header.getChild('dt').getText()) self.assertEquals(six.text_type(out_header.f), soap_out_header.getChild('f').getText()) soap_out_trace_header = soapheaders.getChild('OutTraceHeader') self.assertEquals('T'.join((out_trace_header.receiptDate.date().isoformat(), out_trace_header.receiptDate.time().isoformat())), soap_out_trace_header.getChild('receiptDate').getText()) self.assertEquals('T'.join((out_trace_header.returnDate.date().isoformat(), out_trace_header.returnDate.time().isoformat())), soap_out_trace_header.getChild('returnDate').getText())
def __validate_lxml(self, payload): ret = self.validation_schema.validate(payload) logger.debug("Validated ? %r" % ret) if ret == False: error_text = text_type(self.validation_schema.error_log.last_error) raise SchemaValidationError(error_text.encode('ascii', 'xmlcharrefreplace'))
def to_unicode(cls, value): """ Returns unicode(value). This should be overridden if this is not enough. """ return six.text_type(value)
def _from_dict_value(self, ctx, key, cls, inst, validator): if validator is self.SOFT_VALIDATION: self.validate(key, cls, inst) cls_attrs = self.get_cls_attrs(cls) complex_as = self.get_complex_as(cls_attrs) if complex_as is list or complex_as is tuple: check_complex_as = (list, tuple) else: check_complex_as = complex_as # get native type if issubclass(cls, File): if isinstance(inst, check_complex_as): cls = cls_attrs.type or cls inst = self._parse(cls_attrs, inst) retval = self._doc_to_object(ctx, cls, inst, validator) else: retval = self.from_serstr(cls, inst, self.binary_encoding) else: inst = self._parse(cls_attrs, inst) if issubclass(cls, (Any, AnyDict)): retval = inst elif issubclass(cls, ComplexModelBase): retval = self._doc_to_object(ctx, cls, inst, validator) else: if cls_attrs.empty_is_none and inst in (u'', b''): inst = None if (validator is self.SOFT_VALIDATION and isinstance(inst, six.string_types) and not cls.validate_string(cls, inst)): raise ValidationError([key, inst]) if issubclass(cls, (ByteArray, Uuid)): retval = self.from_serstr(cls, inst, self.binary_encoding) elif issubclass(cls, Unicode): if isinstance(inst, bytearray): retval = six.text_type(inst, encoding=cls_attrs.encoding or 'ascii', errors=cls_attrs.unicode_errors) elif isinstance(inst, memoryview): # FIXME: memoryview needs a .decode() function to avoid # needless copying here retval = inst.tobytes().decode( cls_attrs.encoding or 'ascii', errors=cls_attrs.unicode_errors) elif isinstance(inst, mmap): # FIXME: mmap needs a .decode() function to avoid # needless copying here retval = mmap[:].decode( cls_attrs.encoding, errors=cls_attrs.unicode_errors) else: retval = inst else: retval = self.from_serstr(cls, inst) # validate native type if validator is self.SOFT_VALIDATION: if not cls.validate_native(cls, retval): raise ValidationError([key, retval]) return retval
def _from_dict_value(self, ctx, key, cls, inst, validator): if validator is self.SOFT_VALIDATION: self.validate(key, cls, inst) cls_attrs = self.get_cls_attrs(cls) complex_as = self.get_complex_as(cls_attrs) if complex_as is list or complex_as is tuple: check_complex_as = (list, tuple) else: check_complex_as = complex_as # get native type if issubclass(cls, File): if isinstance(inst, check_complex_as): cls = cls_attrs.type or cls inst = self._parse(cls_attrs, inst) retval = self._doc_to_object(ctx, cls, inst, validator) else: retval = self.from_serstr(cls, inst, self.binary_encoding) else: inst = self._parse(cls_attrs, inst) if issubclass(cls, (Any, AnyDict)): retval = inst elif issubclass(cls, ComplexModelBase): retval = self._doc_to_object(ctx, cls, inst, validator) else: if cls_attrs.empty_is_none and inst in (u'', b''): inst = None if (validator is self.SOFT_VALIDATION and isinstance(inst, six.string_types) and not cls.validate_string(cls, inst)): raise ValidationError([key, inst]) if issubclass(cls, (ByteArray, Uuid)): retval = self.from_serstr(cls, inst, self.binary_encoding) elif issubclass(cls, Unicode): if isinstance(inst, bytearray): retval = six.text_type(inst, encoding=cls_attrs.encoding or 'ascii', errors=cls_attrs.unicode_errors) elif isinstance(inst, memoryview): # FIXME: memoryview needs a .decode() function to avoid # needless copying here retval = inst.tobytes().decode( cls_attrs.encoding or 'ascii', errors=cls_attrs.unicode_errors) elif isinstance(inst, mmap): # FIXME: mmap needs a .decode() function to avoid # needless copying here retval = mmap[:].decode(cls_attrs.encoding, errors=cls_attrs.unicode_errors) else: retval = inst else: retval = self.from_serstr(cls, inst) # validate native type if validator is self.SOFT_VALIDATION: if not cls.validate_native(cls, retval): raise ValidationError([key, retval]) return retval