dcc_quantities.serializers.dcc_element_parser

General parser functions to be applied over XML files.

dcc_type_collector

dcc_type_collector(
    dcc_data: dict | list,
    search_keys: Sequence[DccElementKey],
    recursive: bool = False,
    attribute_matchers: Mapping | None = None,
    compile_attributes: bool = True,
) -> list[tuple[list[str], DccType]]

Searches a JSON-like dict for keys in search_keys.

Parameters:
  • dcc_data (dict | list) –

    Serialized DCC data. This can either be a dictionary (extracted directly from the XML) or a sequence of the DCC elements. Any sequence is then converted to a dictionary, where the keys are the numerical index corresponding to the position of each element in the sequence.

  • search_keys (Sequence[DccElementKey]) –

    Element keys to search within the json dictionary to be extracted.

  • recursive (bool, default: False ) –

    Whether the data to collect is recursive.

  • attribute_matchers (Mapping | None, default: None ) –

    Rules that define how each XML descriptor should be interpreter by the collector. Each rule must be defined as: - Compiled regex patterns (re.Pattern objects) - String patterns (will be compiled to regex) - Callable functions that return bool when given a value E.G.:

    {
        "@refType": [re.compile("basic_([0-9])IndexTable"), "some_pattern"],
        "@tableDimension": [lambda x: isinstance(x, int) and x > 0],
        "@status": [lambda x: x in ["active", "pending"]],
    }

  • compile_attributes ((bool, True), default: True ) –

    Flag to determine if the attributes defined at attribute_matchers should be compiled. Any first call to this function should always set this value to True (its default value).The key should be set to False at any recursive call, as compilation checks are recursive and expensive.

Returns:
  • results( list[tuple[list[str], DccType]] ) –

    Sequence of items as (path, value), where: - path is the hierarchical path (as a list of items) from root to the value - value is the decoded DCC data as the corresponding instance (depending on the

extract_dcc_elements

extract_dcc_elements(
    xml_file_path: str,
    dcc_element_key: DccElementKey = ...,
    with_hierarchy_path: Literal[True] = ...,
) -> list[tuple[list[str], DccType]]
extract_dcc_elements(
    xml_file_path: str,
    dcc_element_key: DccElementKey = ...,
    with_hierarchy_path: Literal[False] = ...,
) -> list[DccType]

Extracts all DCC elements of the same type provided an XML file.

Parameters:
  • xml_file_path (str) –

    Path to the XML file containing the DCC.

  • dcc_element_key (DccElementKey, default: DccElementKey.TABLE ) –

    Element type to extract. When not specified, all the tables are extracted from the XML file.

  • with_hierarchy_path (bool, default: False ) –

    When true, each item at the final sequence is defined as a tuple where the first item is the hierarchical path from the root level to the item and the second item is a dictionary containing the DCC data.

Returns:
  • list

    All extracted elements (defined by the parameter dcc_element_key) from the XML in a sequence.