def from_unicode_converter(field, value, context=None): if value is not None: if field._type is not None: if not isinstance(value, field._type): if isinstance(value, _type_conversions): # only auto convert primitive types try: value = field._type(value) # convert other types over except ValueError: raise WrongType(value, field._type, field.__name__) else: raise WrongType(value, field._type, field.__name__) else: value = field.from_unicode(value) return value
def patch(self, patch_data): if not isinstance(patch_data, (list, tuple)): raise WrongType( "patch value must be list or tuple type! but got `{0}` type.".format( type(patch_data) ), type(patch_data), None, ) if not bool(self): raise Invalid( "None object cannot be patched! " "Make sure fhir resource value is not empty!" ) try: patcher = jsonpatch.JsonPatch(patch_data) value = patcher.apply(self._resource_obj.as_json()) new_value = self._resource_obj.__class__(value) object.__setattr__(self, "_resource_obj", new_value) except jsonpatch.JsonPatchException as exc: return reraise(Invalid, str(exc))
def _validate_object(self, obj: FhirResourceType = None): # noqa: E999 """ """ if obj is None: return try: verifyObject(IFhirResource, obj, False) except (BrokenImplementation, BrokenMethodImplementation): t, v, tb = sys.exc_info() try: reraise(Invalid(str(v)), None, tb) finally: del t, v, tb except DoesNotImplement: msg = "Object must be derived from valid FHIR resource class!" msg += "But it is found that object is derived from `{0}`".format( obj.__class__.__module__ + "." + obj.__class__.__name__) t, v, tb = sys.exc_info() msg += "\nOriginal Exception: {0!s}".format(str(v)) try: reraise(WrongType(msg), None, tb) finally: del t, v, tb
def _validate(self, value): if self._type is not None and not isinstance(value, self._type): raise WrongType(value, self._type, self.__name__) if not self.constraint(value): raise ConstraintNotSatisfied(value, self.__name__) for validator in self._validators or []: validator(self, value)
def _optimized_lookup(value, field, context): if getattr(field, "_type", None) in _type_conversions: # for primitive types, all we really do is return the value back. # this is costly for all the lookups if not isinstance(value, field._type): if isinstance(value, _type_conversions): try: value = field._type(value) # convert other types over except ValueError: raise WrongType(value, field._type, field.__name__) else: raise WrongType(value, field._type, field.__name__) return value else: try: return schema_compatible(value, field, context) except ValidationError as error: raise ValueDeserializationError(field, value, "Wrong contained type", errors=[error])
def _validate(self, value): if self.allowed_mime_types\ and value.mimeType not in self.allowed_mime_types: raise WrongType(value, self.allowed_mime_types) if self.max_length is not None and len(value.raw) > self.max_length: raise Invalid( _('msg_text_too_long', default=u'Text is too long. (Maximum ${max} characters.)', mapping={'max': self.max_length})) if not self.constraint(value): raise ConstraintNotSatisfied(value)
def _pre_value_validate(self, fhir_json): """ """ if isinstance(fhir_json, str): fhir_dict = parse_json_str(fhir_json).copy() elif isinstance(fhir_json, dict): fhir_dict = fhir_json.copy() else: raise WrongType( "Only dict type data is allowed but got `{0}` type data!". format(type(fhir_json))) if "resourceType" not in fhir_dict.keys( ) or "id" not in fhir_dict.keys(): raise Invalid( "Invalid FHIR resource json is provided!\n{0}".format( fhir_json))
def _validate(self, value): super(InterfaceField, self)._validate(value) if not IInterface.providedBy(value): raise WrongType("An interface is required", value, self.__name__)
def _validate(self, value): super(Date, self)._validate(value) if isinstance(value, datetime): raise WrongType(value, self._type, self.__name__)
def _init( self, resource_class, resource_interface, resource_type, fhir_release, **kw ): """ """ if "default" in kw: if ( isinstance(kw["default"], (str, dict)) or kw["default"] is None ) is False: msg = ( "Only dict or string or None is accepted as " "default value but got {0}".format(type(kw["default"])) ) raise Invalid(msg) field_attributes = get_fields(IFhirField) attribute = field_attributes["resource_class"].bind(self) if resource_class is None: attribute.validate(resource_class) attribute_val = None else: attribute_val = attribute.from_unicode(resource_class) attribute.set(self, attribute_val) attribute = field_attributes["resource_interface"].bind(self) if resource_interface is None: attribute.validate(resource_interface) attribute_val = None else: attribute_val = attribute.from_unicode(resource_interface) attribute.set(self, attribute_val) attribute = field_attributes["resource_type"].bind(self) if resource_type is None: attribute.validate(resource_type) attribute_val = None else: attribute_val = attribute.from_unicode(resource_type) attribute.set(self, attribute_val) attribute = field_attributes["fhir_release"].bind(self) if fhir_release is None: attribute.validate(fhir_release) attribute_val = None else: attribute_val = attribute.from_unicode(fhir_release) # just for ensure correct value FHIR_VERSION[attribute_val] attribute.set(self, attribute_val) if self.resource_type and self.resource_class is not None: raise Invalid( "Either `resource_class` or `resource_type` value is acceptable! " "you cannot provide both!" ) if self.resource_class: try: klass = import_string(self.resource_class) self.ensure_fhir_abstract(klass) except ImportError: msg = ( "Invalid FHIR Resource class `{0}`! " "Please check the module or class name." ).format(self.resource_class) return reraise(Invalid, msg) if not IFhirResource.implementedBy(klass): raise Invalid( "{0!r} must be valid resource class from fhir.resources".format( klass ) ) self._resource_class = klass if self.resource_type: try: self._resource_class = implementer(IFhirResource)( lookup_fhir_class(self.resource_type) ) except ImportError: msg = "{0} is not valid fhir resource type!".format(self.resource_type) return reraise(Invalid, msg) if self.resource_interface: try: klass = implementer(IFhirResource)( import_string(self.resource_interface) ) except ImportError: msg = ( "Invalid FHIR Resource Interface`{0}`! " "Please check the module or class name." ).format(self.resource_interface) return reraise(Invalid, msg) if not IInterface.providedBy(klass): raise WrongType("An interface is required", klass, self.__name__) if klass is not IFhirResource and not issubclass(klass, IFhirResource): msg = "`{0!r}` must be derived from {1}".format( klass, IFhirResource.__module__ + "." + IFhirResource.__class__.__name__, ) raise Invalid(msg) self._resource_interface_class = klass
def _init(self, resource_class, resource_interface, resource_type, **kw): """ """ if "default" in kw: if (isinstance(kw["default"], (str, dict)) or kw["default"] is None) is False: msg = ("Only dict or string or None is accepted as " "default value but got {0}".format(type(kw["default"]))) raise Invalid(msg) field_attributes = get_fields(IFhirField) attribute = field_attributes['resource_class'].bind(self) if resource_class is None: attribute.validate(resource_class) attribute_val = None else: attribute_val = attribute.from_unicode(resource_class) attribute.set(self, attribute_val) attribute = field_attributes['resource_interface'].bind(self) if resource_interface is None: attribute.validate(resource_interface) attribute_val = None else: attribute_val = attribute.from_unicode(resource_interface) attribute.set(self, attribute_val) attribute = field_attributes['resource_type'].bind(self) if resource_type is None: attribute.validate(resource_type) attribute_val = None else: attribute_val = attribute.from_unicode(resource_type) attribute.set(self, attribute_val) if self.resource_type and self.resource_class is not None: raise Invalid( "Either `resource_class` or `resource_type` value is acceptable! you cannot provide both!" ) if self.resource_class: try: klass = import_string(self.resource_class) except ImportError: msg = "Invalid FHIR Resource class `{0}`! Please check the module or class name.".format( self.resource_class) t, v, tb = sys.exc_info() try: reraise(Invalid(msg), None, tb) finally: del t, v, tb if not IFhirResource.implementedBy(klass): raise Invalid( "{0!r} must be valid resource class from fhir.resources". format(klass)) self._resource_class = klass if self.resource_type: try: self._resource_class = resource_type_to_resource_cls( self.resource_type) except ImportError: msg = "{0} is not valid fhir resource type!".format( self.resource_type) t, v, tb = sys.exc_info() try: reraise(Invalid(msg), None, tb) finally: del t, v, tb raise Invalid(msg) if self.resource_interface: try: klass = import_string(self.resource_interface) except ImportError: msg = "Invalid FHIR Resource Interface`{0}`! Please check the module or class name.".format( self.resource_interface) t, v, tb = sys.exc_info() try: reraise(Invalid(msg), None, tb) finally: del t, v, tb if not IInterface.providedBy(klass): raise WrongType("An interface is required", klass, self.__name__) if klass is not IFhirResource and not issubclass( klass, IFhirResource): msg = "`{0!r}` must be derived from {1}".format( klass, IFhirResource.__module__ + "." + IFhirResource.__class__.__name__, ) raise Invalid(msg) self._resource_interface_class = klass