Example #1
0
def get_enrollments_errors(response: dict) -> dict:
    # $.enrollments.importSummaries[*][?(@.status='ERROR')].description
    jsonpath_expr = Child(
        Where(
            Child(Child(Fields("enrollments"), Fields("importSummaries")),
                  Slice()), Cmp(Fields("status"), eq, "ERROR")),
        Fields("description"))
    matches = jsonpath_expr.find(response)
    return {str(match.full_path): match.value for match in matches}
Example #2
0
def get_entity_errors(response: dict) -> dict:
    # $[?(@.status='ERROR')].description
    jsonpath_expr = Child(Where(Root(), Cmp(Fields("status"), eq, "ERROR")),
                          Fields("description"))
    # We write the JSONPath expression programmatically because
    # currently jsonpath-ng does not support parsing comparison
    # expressions like ``[?(@.status='ERROR')]``
    matches = jsonpath_expr.find(response)
    return {str(match.full_path): match.value for match in matches}
Example #3
0
from jsonpath_ng import parse, Union, Child

from oasapi.common import OPERATIONS_LOWER, REFERENCE_SECTIONS

# list of JSPATH for different structures

JSPATH_INFO = parse(f"info")
JSPATH_ENDPOINTS = parse(f"paths.*")
JSPATH_OPERATIONS = Child(JSPATH_ENDPOINTS,
                          parse(f"({'|'.join(OPERATIONS_LOWER)})"))
JSPATH_OPERATION_RESPONSES = Child(JSPATH_OPERATIONS, parse("responses"))
JSPATH_OPERATIONID = Child(JSPATH_OPERATIONS, parse("operationId"))
JSPATH_OPERATION_TAGS = Child(JSPATH_OPERATIONS, parse(f"tags"))
JSPATH_SECURITY_OPERATION = Child(JSPATH_OPERATIONS, parse("security.[*].*"))
JSPATH_SECURITY_GLOBAL = parse("security.[*].*")
JSPATH_SECURITY = Union(JSPATH_SECURITY_GLOBAL, JSPATH_SECURITY_OPERATION)
JSPATH_PARAMETERS_GLOBAL = parse("parameters.[*]")
JSPATH_PARAMETERS_PATH = parse(f"paths.*.parameters.[*]")
JSPATH_PARAMETERS_OPERATION = Child(JSPATH_OPERATIONS, parse("parameters.[*]"))
JSPATH_PARAMETERS = Union(
    JSPATH_PARAMETERS_GLOBAL,
    Union(JSPATH_PARAMETERS_PATH, JSPATH_PARAMETERS_OPERATION))
JSPATH_PATHS_REFERENCES = parse("paths..'$ref'")
JSPATH_REFERENCES = parse("$..'$ref'")
JSPATH_COMPONENTS = parse(f"$.({'|'.join(REFERENCE_SECTIONS)}).*")
JSPATH_TAGS = parse("tags.[*].name")
Example #4
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-ng offers programmatic JSONPath expressions. For details on how to create JSONPath
        # expressions programmatically see the
        # `jsonpath-ng documentation <https://github.com/h2non/jsonpath-ng#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
Example #5
0
        ),
        (
            [("b", 2, True), ("a", 1, False), ("b", 1, True)],
            lambda x: x[2],
            [[("a", 1, False)], [("b", 2, True), ("b", 1, True)]],
        ),
    ],
)
def test_group_by(iterable, key, expected):
    assert list(group_by(iterable, key)) == expected


@pytest.mark.parametrize(
    "pointer, expected",
    [
        (Child(Fields("a"), Fields("b")), ("a", "b")),
        (Child(Root(), Fields("a")), ("a", )),
        (Child(Fields("a"), Child(Fields("b"), Fields("c"))), ("a", "b", "c")),
    ],
)
def test__pointer_to_tuple(pointer, expected):
    assert _pointer_to_tuple(pointer) == expected


@pytest.mark.parametrize(
    "text, expected",
    [
        ("$.a[:]", ("a", )),
        ("$.a[*].b", ("a", "b")),
        ("$['a'][*]", ("a", )),
        ("$['a'][*]['b']", ("a", "b")),