Exemplo n.º 1
0
def test_structured_data_StructuredDataTree_building_tree():
    def plugin_dict():
        node = struct_tree.get_dict("level0_0.level1_dict.")
        for a, b in [("d1", "D1"), ("d2", "D2")]:
            node.setdefault(a, b)

    def plugin_list():
        node = struct_tree.get_list("level0_1.level1_list:")
        for a, b in [("l1", "L1"), ("l2", "L2")]:
            node.append({a: b})

    def plugin_nested_list():
        node = struct_tree.get_list("level0_2.level1_nested_list:")
        for index in range(10):
            array: Dict[str, List[Dict[str, str]]] = {"foo": []}
            for a, b in [("nl1", "NL1"), ("nl2", "NL2")]:
                array["foo"].append({a: "%s-%s" % (b, index)})
            node.append(array)

    struct_tree = StructuredDataTree()
    plugin_dict()
    plugin_list()
    plugin_nested_list()
    struct_tree.normalize_nodes()

    assert struct_tree.has_edge("level0_0")
    assert struct_tree.has_edge("level0_1")
    assert struct_tree.has_edge("level0_2")
    assert not struct_tree.has_edge("foobar")

    level1_dict = struct_tree.get_sub_attributes(["level0_0", "level1_dict"])
    level1_list = struct_tree.get_sub_numeration(["level0_1", "level1_list"])
    level1_nested_list_con = struct_tree.get_sub_container(
        ["level0_2", "level1_nested_list"])
    level1_nested_list_num = struct_tree.get_sub_numeration(
        ["level0_2", "level1_nested_list"])
    level1_nested_list_att = struct_tree.get_sub_attributes(
        ["level0_2", "level1_nested_list"])

    assert isinstance(level1_dict, Attributes)
    assert 'd1' in level1_dict.get_child_data()
    assert 'd2' in level1_dict.get_child_data()

    assert isinstance(level1_list, Numeration)
    known_keys = [key for row in level1_list.get_child_data() for key in row]
    assert 'l1' in known_keys
    assert 'l2' in known_keys
    assert level1_nested_list_num is None
    assert level1_nested_list_att is None

    assert isinstance(level1_nested_list_con, Container)
    assert list(
        level1_nested_list_con._nodes) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Exemplo n.º 2
0
def test_structured_data_StructuredDataTree_building_tree():
    def plugin_dict():
        node = struct_tree.get_dict("level0_0.level1_dict.")
        for a, b in [("d1", "D1"), ("d2", "D2")]:
            node.setdefault(a, b)

    def plugin_list():
        node = struct_tree.get_list("level0_1.level1_list:")
        for a, b in [("l1", "L1"), ("l2", "L2")]:
            node.append({a: b})

    def plugin_nested_list():
        node = struct_tree.get_list("level0_2.level1_nested_list:")
        for index in xrange(10):
            array = {"foo": []}
            for a, b in [("nl1", "NL1"), ("nl2", "NL2")]:
                array["foo"].append({a: "%s-%s" % (b, index)})
            node.append(array)

    struct_tree = StructuredDataTree()
    plugin_dict()
    plugin_list()
    plugin_nested_list()
    struct_tree.normalize_nodes()

    assert struct_tree.has_edge("level0_0")
    assert struct_tree.has_edge("level0_1")
    assert struct_tree.has_edge("level0_2")
    assert not struct_tree.has_edge("foobar")

    level1_dict = struct_tree.get_sub_attributes(["level0_0", "level1_dict"])
    level1_list = struct_tree.get_sub_numeration(["level0_1", "level1_list"])
    level1_nested_list_con = struct_tree.get_sub_container(
        ["level0_2", "level1_nested_list"])
    level1_nested_list_num = struct_tree.get_sub_numeration(
        ["level0_2", "level1_nested_list"])
    level1_nested_list_att = struct_tree.get_sub_attributes(
        ["level0_2", "level1_nested_list"])

    assert 'd1' in level1_dict.get_child_data().keys()
    assert 'd2' in level1_dict.get_child_data().keys()
    assert ['l1'] in [x.keys() for x in level1_list.get_child_data()]
    assert ['l2'] in [x.keys() for x in level1_list.get_child_data()]
    assert level1_nested_list_num is None
    assert level1_nested_list_att is None
    assert level1_nested_list_con._edges.keys() == [
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    ]
Exemplo n.º 3
0
def get_inventory_data(inventory_tree: StructuredDataTree,
                       tree_path: RawInventoryPath) -> InventoryData:
    invdata = None
    parsed_path, attribute_keys = parse_tree_path(tree_path)
    if attribute_keys == []:
        numeration = inventory_tree.get_sub_numeration(parsed_path)
        if numeration is not None:
            invdata = numeration.get_child_data()
    elif attribute_keys:
        attributes = inventory_tree.get_sub_attributes(parsed_path)
        if attributes is not None:
            # In paint_host_inventory_tree we parse invpath and get
            # a path and attribute_keys which may be either None, [], or ["KEY"].
            invdata = attributes.get_child_data().get(attribute_keys[-1])
    return invdata