Example #1
0
def parse_dates(tree_to_parse, xpath_map):
    """
    Creates and returns a Dates Dictionary data structure given the parameters provided
    :param tree_to_parse: the XML tree from which to construct the Dates data structure
    :param xpath_map: a map containing the following type-specific XPATHs:
        multiple, range, range_begin, range_end, and single
    """

    # Determine dates to query based on metadata elements

    values = wrap_value(parse_property(tree_to_parse, None, xpath_map, DATE_TYPE_SINGLE))
    if len(values) == 1:
        return {DATE_TYPE: DATE_TYPE_SINGLE, DATE_VALUES: values}
    elif len(values) > 1:
        return {DATE_TYPE: DATE_TYPE_MULTIPLE, DATE_VALUES: values}

    values = wrap_value(parse_property(tree_to_parse, None, xpath_map, DATE_TYPE_MULTIPLE))
    if len(values) == 1:
        return {DATE_TYPE: DATE_TYPE_SINGLE, DATE_VALUES: values}
    elif len(values) > 1:
        return {DATE_TYPE: DATE_TYPE_MULTIPLE, DATE_VALUES: values}

    values = flatten_items(
        d for x in (DATE_TYPE_RANGE_BEGIN, DATE_TYPE_RANGE_END)
        for d in wrap_value(parse_property(tree_to_parse, None, xpath_map, x))
    )
    if len(values) == 1:
        return {DATE_TYPE: DATE_TYPE_SINGLE, DATE_VALUES: values}
    elif len(values) == 2:
        return {DATE_TYPE: DATE_TYPE_RANGE, DATE_VALUES: values}
    elif len(values) > 2:
        return {DATE_TYPE: DATE_TYPE_MULTIPLE, DATE_VALUES: values}

    return {}
Example #2
0
def parse_dates(tree_to_parse, xpath_map):
    """
    Creates and returns a Dates Dictionary data structure given the parameters provided
    :param tree_to_parse: the XML tree from which to construct the Dates data structure
    :param xpath_map: a map containing the following type-specific XPATHs:
        multiple, range, range_begin, range_end, and single
    """

    # Determine dates to query based on metadata elements

    values = wrap_value(
        parse_property(tree_to_parse, None, xpath_map, DATE_TYPE_SINGLE))
    if len(values) == 1:
        return {DATE_TYPE: DATE_TYPE_SINGLE, DATE_VALUES: values}
    elif len(values) > 1:
        return {DATE_TYPE: DATE_TYPE_MULTIPLE, DATE_VALUES: values}

    values = wrap_value(
        parse_property(tree_to_parse, None, xpath_map, DATE_TYPE_MULTIPLE))
    if len(values) == 1:
        return {DATE_TYPE: DATE_TYPE_SINGLE, DATE_VALUES: values}
    elif len(values) > 1:
        return {DATE_TYPE: DATE_TYPE_MULTIPLE, DATE_VALUES: values}

    values = flatten_items(
        d for x in (DATE_TYPE_RANGE_BEGIN, DATE_TYPE_RANGE_END)
        for d in wrap_value(parse_property(tree_to_parse, None, xpath_map, x)))
    if len(values) == 1:
        return {DATE_TYPE: DATE_TYPE_SINGLE, DATE_VALUES: values}
    elif len(values) == 2:
        return {DATE_TYPE: DATE_TYPE_RANGE, DATE_VALUES: values}
    elif len(values) > 2:
        return {DATE_TYPE: DATE_TYPE_MULTIPLE, DATE_VALUES: values}

    return {}
    def _parse_report_item(self, prop):
        """ :return: the text for each element at the configured path if type attribute matches"""

        item_type = None

        if prop == 'attribute_accuracy':
            item_type = 'DQQuanAttAcc'
        elif prop == 'dataset_completeness':
            item_type = 'DQCompOm'

        xroot = self._get_xroot_for(prop)

        parsed = (element_to_dict(e) for e in get_elements(self._xml_tree, xroot))
        parsed = flatten_items(e['children'] for e in parsed if e['attributes'].get('type') == item_type)

        return reduce_value([p['text'] for p in parsed if p['name'] == 'measDesc'])
    def _parse_report_item(self, prop):
        """ :return: the text for each element at the configured path if type attribute matches"""

        item_type = None

        if prop == 'attribute_accuracy':
            item_type = 'DQQuanAttAcc'
        elif prop == 'dataset_completeness':
            item_type = 'DQCompOm'

        xroot = self._get_xroot_for(prop)

        parsed = (element_to_dict(e) for e in get_elements(self._xml_tree, xroot))
        parsed = flatten_items(e['children'] for e in parsed if e['attributes'].get('type') == item_type)

        return reduce_value([p['text'] for p in parsed if p['name'] == 'measDesc'])
Example #5
0
def parse_complex(tree_to_parse, xpath_root, xpath_map, complex_key):
    """
    Creates and returns a Dictionary data structure parsed from the metadata.
    :param tree_to_parse: the XML tree compatible with element_utils to be parsed
    :param xpath_root: the XPATH location of the structure inside the parent element
    :param xpath_map: a dict of XPATHs corresponding to a complex definition
    :param complex_key: indicates which complex definition describes the structure
    """

    complex_struct = {}

    for prop in _complex_definitions.get(complex_key, xpath_map):
        # Normalize complex values: treat values with newlines like values from separate elements
        parsed = parse_property(tree_to_parse, xpath_root, xpath_map, prop)
        parsed = reduce_value(flatten_items(v.split(_COMPLEX_DELIM) for v in wrap_value(parsed)))

        complex_struct[prop] = get_default_for_complex_sub(complex_key, prop, parsed, xpath_map[prop])

    return complex_struct if any(complex_struct.values()) else {}
Example #6
0
def parse_complex(tree_to_parse, xpath_root, xpath_map, complex_key):
    """
    Creates and returns a Dictionary data structure parsed from the metadata.
    :param tree_to_parse: the XML tree compatible with element_utils to be parsed
    :param xpath_root: the XPATH location of the structure inside the parent element
    :param xpath_map: a dict of XPATHs corresponding to a complex definition
    :param complex_key: indicates which complex definition describes the structure
    """

    complex_struct = {}

    for prop in _complex_definitions.get(complex_key, xpath_map):
        # Normalize complex values: treat values with newlines like values from separate elements
        parsed = parse_property(tree_to_parse, xpath_root, xpath_map, prop)
        parsed = reduce_value(
            flatten_items(v.split(_COMPLEX_DELIM) for v in wrap_value(parsed)))

        complex_struct[prop] = get_default_for_complex_sub(
            complex_key, prop, parsed, xpath_map[prop])

    return complex_struct if any(complex_struct.values()) else {}