def __delattr__(self, item): """ """ if self._finalized: raise TypeError("Modification of attribute value is not allowed!") try: del self.__storage__[item] except KeyError: msg = "Object from {0!s} has no attribute `{1}`".format( self.__class__.__name__, item) reraise(AttributeError, msg)
def __getattr__(self, item): """ :param item: :return: """ try: return self.__storage__[item] except KeyError: msg = "Object from {0!s} has no attribute `{1}`".format( self.__class__.__name__, item) reraise(AttributeError, msg)
def fql(obj): """ """ try: func = obj.__fql__ try: func.__self__ except AttributeError: reraise( ValueError, "__fql__ is not bound method, make sure class initialized!" ) return func() except AttributeError: raise AttributeError("Object must have __fql__ method available")
def _validate(self, value): """ """ super(FhirField, self)._validate(value) if self.resource_interface: try: verifyObject( self._resource_interface_class, value.foreground_origin(), False ) except ( BrokenImplementation, BrokenMethodImplementation, DoesNotImplement, ) as exc: return reraise(Invalid, str(exc)) if self.resource_type and value.resource_type != self.resource_type: msg = ( "Resource type must be `{0}` but we got {1} " "which is not allowed!" ).format(self.resource_type, value.resource_type) raise ConstraintNotSatisfied(msg, field_name=self.getName()) if self.resource_class: klass = self._resource_class if value.foreground_origin() is not None and not isinstance( value.foreground_origin(), klass ): msg = ( "Wrong fhir resource value is provided! " "Value should be object of {0!r} but got {1!r}".format( klass, value.foreground_origin().__class__ ) ) raise WrongContainedType(msg, field_name=self.getName()) if value.foreground_origin() is not None: try: value.foreground_origin().as_json() except (FHIRValidationError, TypeError) as exc: msg = ( "There is invalid element inside " "fhir model object.\n{0!s}" ).format(exc) return reraise(Invalid, msg)
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 create_connection(conn_string, klass=None, **extra): """ """ mod_pattern = "fhirpath.connectors.factory.{driver_mod}.create" if isinstance(conn_string, (tuple, list)): url = [make_url(conn) for conn in conn_string] url_ = url[0] else: url_ = url = make_url(conn_string) driver_mod = url_.drivername.split("+")[0] try: factory = import_string(mod_pattern.format(driver_mod=driver_mod)) return factory(url, klass, **extra) except ImportError: reraise( Invalid, "Invalid ({0}) drivername or not supported yet!.".format( url_.drivername), )
def _validate_object(self, obj: FhirResourceType = None): # noqa: E999 """ """ if obj is None: return try: verifyObject(IFhirResource, obj, False) except ( BrokenImplementation, BrokenMethodImplementation, MultipleInvalid, ) as exc: return reraise(Invalid, str(exc)) except DoesNotImplement as exc: 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__ ) msg += "\nOriginal Exception: {0!s}".format(exc) return reraise(WrongType, msg, field_name=self.getName())
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