How to use the hl7apy.load_reference function in hl7apy

To help you get started, we’ve selected a few hl7apy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github crs4 / hl7apy / hl7apy / core.py View on Github external
def _set_datatype(self, datatype):
        if Validator.is_strict(self.validation_level) and self.datatype and \
                datatype != self.datatype:
            raise OperationNotAllowed("Cannot change datatype using STRICT validation")

        # This will change the structure of the Field/Component so it is done only if the structure
        # is really changed. That's because the first time the datatype is set by the Element._find_structure method
        if not is_base_datatype(datatype, self.version) and \
                datatype not in ('varies', None, self.datatype) and self.datatype is not None:
            reference = load_reference(datatype, 'Datatypes_Structs', self.version)
            new_ref = [ref_item for ref_item in self.reference]
            new_ref[1] = reference
            new_ref[2] = datatype
            structure = ElementFinder.get_structure(self, new_ref)
            for k, v in iteritems(structure):
                if k != 'datatype':  # avoid maximum recursion
                    setattr(self, k, v)

        if hasattr(self, 'children') and len(self.children) >= 1:
            if is_base_datatype(self.datatype, self.version):
                self._datatype = datatype
                if is_base_datatype(datatype, self.version):
                    self.children[0].datatype = datatype
            else:
                raise OperationNotAllowed("Cannot change datatype: the Element already contains children")
        else:
github crs4 / hl7apy / hl7apy / validation.py View on Github external
def _check_z_element(el, errs, warns):
            if el.classname == 'Field':
                if is_base_datatype(el.datatype, el.version) or \
                        el.datatype == 'varies':
                    return True
                elif el.datatype is not None:
                    # if the datatype the is a complex datatype, the z element must follow the correct
                    # structure of that datatype
                    # Component just to search in the datatypes....
                    dt_struct = load_reference(el.datatype, 'Datatypes_Structs', el.version)
                    ref = ('sequence', dt_struct, el.datatype, None, None, -1)
                    _check_known_element(el, ref, errs, warns)
            for c in el.children:
                _is_valid(c, None, errs, warns)
            return True
github crs4 / hl7apy / hl7apy / core.py View on Github external
if datatype == 'varies' and reference is None:
            reference = ('leaf', None, 'varies', None, None, -1)

        try:
            Element.__init__(self, name, parent, reference, version,
                             validation_level, traversal_parent)
        except InvalidName:
            if _valid_z_field_name(name):
                datatype = datatype or 'ST'
                if is_base_datatype(datatype, version):
                    reference = ('leaf', None, datatype, None, None, -1)
                else:
                    if version is None:
                        version = get_default_version()
                    dt_struct = load_reference(datatype, "Datatypes_Structs", version)
                    reference = ('sequence', dt_struct, datatype, None, None, -1)
                Element.__init__(self, name, parent, reference, version,
                                 validation_level, traversal_parent)
            else:
                raise

        if datatype is not None and Validator.is_strict(validation_level) and \
                datatype != 'varies' and datatype != self.datatype:
            raise OperationNotAllowed("Cannot assign a different datatype with strict validation")

        if datatype is not None:  # force the datatype to be the one chosen by the user
            self.datatype = datatype
        elif self.name is None:  # if it is unknown and no datatype has been given
            self.datatype = None
github crs4 / hl7apy / hl7apy / validation.py View on Github external
z_children = [c for c in el.children if c.is_z_element()]
                for c in z_children:
                    _is_valid(c, None, errs, warns)
            else:
                _check_table_compliance(el, ref, warns)

                _check_length(el, ref, warns)

                if el.datatype == 'varies':  # TODO: it should check the real rule
                    return True
                _check_datatype(el, ref, errs)

                # For complex datatypes element, the reference is the one of the datatype
                if not is_base_datatype(el.datatype, el.version):
                    # Component just to search in the datatypes....
                    ref = load_reference(el.datatype, 'Datatypes_Structs', el.version)
                    _is_valid(el, ref, errs, warns)
github crs4 / hl7apy / hl7apy / validation.py View on Github external
def _check_table_compliance(el, ref, warns):
            table = ref[4]
            if table is not None:
                try:
                    table_ref = load_reference(table, 'Table', el.version)
                except ChildNotFound:
                    pass
                else:
                    table_children = table_ref[1]
                    if el.to_er7() not in table_children:
                        warns.append(ValidationWarning("Value {} not in table {} in element {}.{}".
                                                       format(el.to_er7(), table, el.parent.name,
                                                              el.name)))
github crs4 / hl7apy / hl7apy / core.py View on Github external
def get_structure(element, reference=None):
        """
        Get the element structure

        :type element: :class:`Element `
        :param element: element having the given reference structure

        :param reference: the element structure from :func:`load_reference ` or from a
            message profile

        :return: a dictionary containing the structure data
        """
        if reference is None:
            try:
                reference = load_reference(element.name, element.classname, element.version)
            except (ChildNotFound, KeyError):
                raise InvalidName(element.classname, element.name)
        if not isinstance(reference, Sequence):
            raise Exception
        return ElementFinder._parse_structure(element, reference)
github crs4 / hl7apy / hl7apy / validation.py View on Github external
def _check_known_element(el, ref, errs, warns):
            if ref is None:
                try:
                    ref = load_reference(el.name, el.classname, el.version)
                except ChildNotFound:
                    errs.append(ValidationError("Invalid element found: {}".format(el)))

            if ref[0] in ('sequence', 'choice'):
                element_children = {c.name for c in el.children if not c.is_z_element()}
                valid_children, valid_children_refs = _get_valid_children_info(ref)

                # check that the children are all allowed children
                if not element_children <= valid_children:
                    errs.append(ValidationError("Invalid children detected for {}: {}".
                                                format(el, list(element_children - valid_children))))

                # iterates the valid children
                for child_ref in valid_children_refs:
                    # it gets the structure of the children to check
                    child_name, cardinality = _get_child_reference_info(child_ref)
github crs4 / hl7apy / hl7apy / core.py View on Github external
def __init__(self, name=None, datatype=None, parent=None, reference=None,
                 version=None, validation_level=None, traversal_parent=None):

        if self.__class__ == CanBeVaries:
            raise OperationNotAllowed("Cannot instantiate a CanBeVaries")

        if datatype == 'varies' and reference is None:
            reference = ('leaf', None, 'varies', None, None, -1)

        if not Validator.is_strict(validation_level) and datatype not in (None, 'varies') \
                and not is_base_datatype(datatype, version):
            version = version or get_default_version()
            children_refs = load_reference(datatype, 'Datatypes_Structs', version)
            if name is not None:
                # first we get the original reference for the long_name, table etc
                orig_ref = load_reference(name, 'Component', version)
                reference = ('sequence', children_refs, datatype, orig_ref[3], orig_ref[4], orig_ref[5])
            else:
                reference = ('sequence', children_refs, datatype, None, None, -1)

        if name is not None and _valid_child_name(name, 'VARIES'):
            # Set name to None because with a VARIES name the Element would raise an Exception
            Element.__init__(self, None, parent, reference, version,
                             validation_level, traversal_parent)
            self.name = name.upper()
        else:
            try:
                Element.__init__(self, name, parent, reference, version,
                                 validation_level, traversal_parent)
            except ChildNotFound:
                raise InvalidName(self.classname, self.name)