Ejemplo n.º 1
0
    def getAgreementsId(self, json=None):
        from jsonpath_rw import parse as parse_jsonpath

        results = parse_jsonpath('$..AgreementId').find(json)

        if (self.debug):
            print("AgreementId: {}".format(results[0].value))

        return results[0].value
Ejemplo n.º 2
0
 def _get_external_value(self, external_data):
     jsonpath = parse_jsonpath(self.jsonpath)
     matches = jsonpath.find(external_data)
     values = [m.value for m in matches]
     if not values:
         return None
     elif len(values) == 1:
         return values[0]
     else:
         return values
Ejemplo n.º 3
0
    def getCompanies(self, json=None):
        from jsonpath_rw import parse as parse_jsonpath

        if (json is None):
            print(">> Getting Agreements...")
            json = self.getAgreements()

        results = parse_jsonpath('$..CompanyId').find(json)

        if (self.debug):
            print("Companies: {}".format(results[0].value))

        return results[0].value
Ejemplo n.º 4
0
    def getUsersIdList(self, json=None):
        from jsonpath_rw import parse as parse_jsonpath

        if (json is None):
            print(">> Getting Users...")
            json = self.getUsers()

        results = parse_jsonpath('$..UserId').find(json)

        if (self.debug):
            print("Users: {}".format(results[0].value))

        return results[0].value
Ejemplo n.º 5
0
    def apply(self, data: dict) -> Any:
        """Parse then apply a Reference Path on some data.

        Args:
            data: The data to use the Reference Path expression on.

        Returns:
            The queried data.
        """
        parsed_reference_path = parse_jsonpath(self.reference_path)
        if matches := [match.value for match in parsed_reference_path.find(data)]:
            assert len(matches) == 1, "There should only be one match possible"
            return matches[0]
Ejemplo n.º 6
0
 def get_external_value(self, external_data):
     if self.jsonpath is not None:
         try:
             jsonpath = parse_jsonpath(self.jsonpath)
         except Exception as err:
             raise JsonpathError from err
         matches = jsonpath.find(external_data)
         values = [m.value for m in matches]
         if not values:
             return None
         elif len(values) == 1:
             return values[0]
         else:
             return values
     raise ConfigurationError(f"{self} is not configured to parse external data")
Ejemplo n.º 7
0
def get_property_map(case_config):
    """
    Returns a map of case properties to OpenMRS patient properties and
    attributes, and a value source dict to deserialize them.
    """
    property_map = {}

    for person_prop, value_source_dict in case_config[
            'person_properties'].items():
        if 'case_property' in value_source_dict:
            jsonpath = parse_jsonpath('person.' + person_prop)
            property_map[value_source_dict['case_property']] = (
                jsonpath, value_source_dict)

    for attr_type_uuid, value_source_dict in case_config[
            'person_attributes'].items():
        # jsonpath_rw offers programmatic JSONPath expressions. For details on how to create JSONPath
        # expressions programmatically see the
        # `jsonpath_rw documentation <https://github.com/kennknowles/python-jsonpath-rw#programmatic-jsonpath>`__
        #
        # The `Where` JSONPath expression "*jsonpath1* `where` *jsonpath2*" returns nodes matching *jsonpath1*
        # where a child matches *jsonpath2*. `Cmp` does a comparison in *jsonpath2*. It accepts a
        # comparison operator and a value. The JSONPath expression for matching simple attribute values is::
        #
        #     (person.attributes[*] where attributeType.uuid eq attr_type_uuid).value
        #
        # This extracts the person attribute values where their attribute type UUIDs match those configured in
        # case_config['person_attributes'].
        #
        # Person attributes with Concept values have UUIDs. The following JSONPath uses Union to match both simple
        # values and Concept values.
        if 'case_property' in value_source_dict:
            jsonpath = Union(
                # Simple values: Return value if it has no children.
                # (person.attributes[*] where attributeType.uuid eq attr_type_uuid).(value where not *)
                Child(
                    Where(
                        Child(Child(Fields('person'), Fields('attributes')),
                              Slice()),
                        Cmp(Child(Fields('attributeType'), Fields('uuid')), eq,
                            attr_type_uuid)),
                    WhereNot(Fields('value'), Fields('*'))),
                # Concept values: Return value.uuid if value.uuid exists:
                # (person.attributes[*] where attributeType.uuid eq attr_type_uuid).value.uuid
                Child(
                    Where(
                        Child(Child(Fields('person'), Fields('attributes')),
                              Slice()),
                        Cmp(Child(Fields('attributeType'), Fields('uuid')), eq,
                            attr_type_uuid)),
                    Child(Fields('value'), Fields('uuid'))))
            property_map[value_source_dict['case_property']] = (
                jsonpath, value_source_dict)

    for name_prop, value_source_dict in case_config[
            'person_preferred_name'].items():
        if 'case_property' in value_source_dict:
            jsonpath = parse_jsonpath('person.preferredName.' + name_prop)
            property_map[value_source_dict['case_property']] = (
                jsonpath, value_source_dict)

    for addr_prop, value_source_dict in case_config[
            'person_preferred_address'].items():
        if 'case_property' in value_source_dict:
            jsonpath = parse_jsonpath('person.preferredAddress.' + addr_prop)
            property_map[value_source_dict['case_property']] = (
                jsonpath, value_source_dict)

    for id_type_uuid, value_source_dict in case_config[
            'patient_identifiers'].items():
        if 'case_property' in value_source_dict:
            if id_type_uuid == 'uuid':
                jsonpath = parse_jsonpath('uuid')
            else:
                # The JSONPath expression below is the equivalent of::
                #
                #     (identifiers[*] where identifierType.uuid eq id_type_uuid).identifier
                #
                # Similar to `person_attributes` above, this will extract the person identifier values where
                # their identifier type UUIDs match those configured in case_config['patient_identifiers']
                jsonpath = Child(
                    Where(
                        Child(Fields('identifiers'), Slice()),
                        Cmp(Child(Fields('identifierType'), Fields('uuid')),
                            eq, id_type_uuid)), Fields('identifier'))
            property_map[value_source_dict['case_property']] = (
                jsonpath, value_source_dict)

    return property_map
Ejemplo n.º 8
0
def get_property_map(case_config):
    """
    Returns a map of case properties to OpenMRS patient properties and
    attributes, and a ValueSource instance to deserialize them.
    """
    property_map = {}

    for person_prop, value_source in case_config['person_properties'].items():
        if 'case_property' in value_source:
            jsonpath = parse_jsonpath('person.' + person_prop)
            property_map[value_source['case_property']] = (jsonpath, value_source)

    for attr_type_uuid, value_source in case_config['person_attributes'].items():
        # jsonpath_rw offers programmatic JSONPath expressions. For details on how to create JSONPath
        # expressions programmatically see the
        # `jsonpath_rw documentation <https://github.com/kennknowles/python-jsonpath-rw#programmatic-jsonpath>`__
        #
        # The `Where` JSONPath expression "*jsonpath1* `where` *jsonpath2*" returns nodes matching *jsonpath1*
        # where a child matches *jsonpath2*. `Cmp` does a comparison in *jsonpath2*. It accepts a
        # comparison operator and a value. The JSONPath expression for matching simple attribute values is::
        #
        #     (person.attributes[*] where attributeType.uuid eq attr_type_uuid).value
        #
        # This extracts the person attribute values where their attribute type UUIDs match those configured in
        # case_config['person_attributes'].
        #
        # Person attributes with Concept values have UUIDs. The following JSONPath uses Union to match both simple
        # values and Concept values.
        if 'case_property' in value_source:
            jsonpath = Union(
                # Simple values: Return value if it has no children.
                # (person.attributes[*] where attributeType.uuid eq attr_type_uuid).(value where not *)
                Child(
                    Where(
                        Child(Child(Fields('person'), Fields('attributes')), Slice()),
                        Cmp(Child(Fields('attributeType'), Fields('uuid')), eq, attr_type_uuid)
                    ),
                    WhereNot(Fields('value'), Fields('*'))
                ),
                # Concept values: Return value.uuid if value.uuid exists:
                # (person.attributes[*] where attributeType.uuid eq attr_type_uuid).value.uuid
                Child(
                    Where(
                        Child(Child(Fields('person'), Fields('attributes')), Slice()),
                        Cmp(Child(Fields('attributeType'), Fields('uuid')), eq, attr_type_uuid)
                    ),
                    Child(Fields('value'), Fields('uuid'))
                )
            )
            property_map[value_source['case_property']] = (jsonpath, value_source)

    for name_prop, value_source in case_config['person_preferred_name'].items():
        if 'case_property' in value_source:
            jsonpath = parse_jsonpath('person.preferredName.' + name_prop)
            property_map[value_source['case_property']] = (jsonpath, value_source)

    for addr_prop, value_source in case_config['person_preferred_address'].items():
        if 'case_property' in value_source:
            jsonpath = parse_jsonpath('person.preferredAddress.' + addr_prop)
            property_map[value_source['case_property']] = (jsonpath, value_source)

    for id_type_uuid, value_source in case_config['patient_identifiers'].items():
        if 'case_property' in value_source:
            if id_type_uuid == 'uuid':
                jsonpath = parse_jsonpath('uuid')
            else:
                # The JSONPath expression below is the equivalent of::
                #
                #     (identifiers[*] where identifierType.uuid eq id_type_uuid).identifier
                #
                # Similar to `person_attributes` above, this will extract the person identifier values where
                # their identifier type UUIDs match those configured in case_config['patient_identifiers']
                jsonpath = Child(
                    Where(
                        Child(Fields('identifiers'), Slice()),
                        Cmp(Child(Fields('identifierType'), Fields('uuid')), eq, id_type_uuid)
                    ),
                    Fields('identifier')
                )
            property_map[value_source['case_property']] = (jsonpath, value_source)

    return property_map