Example #1
0
def create_action(element: Element):
    if "id" in element.attrib:
        name = element.attrib["id"]
    else:
        name = str(element.sourceline)
        for parent in element.iterancestors():
            if "id" in parent.attrib:
                name += parent.attrib["id"]
                break
    return game_models.Action(list(parse_step(step) for step in element), name)
Example #2
0
    def get_root_path(self, tree_elem: etree) -> List[str]:
        """ return a NeXus path (e.g. `entry/instrument/detector_group`) from an lxml tree element

        The trickery is that we compare the path's elements with the group names which we want to rename
        and substitute those path's elements accordingly.
        And if for a certain element we need to create several groups, then paths get duplicated with those names:

        Example:
            we have `entry/instrument/detector_group` in the schema, and
            `detector_group` are [`DET_1`, `DET_2`]. Then we've got two paths as the result:
            [`entry/instrument/DET_1`, `entry/instrument/DET_2`]

        """
        ancestor_list = list(x.attrib["type"]
                             for x in tree_elem.iterancestors())
        for j, elem in enumerate(ancestor_list):
            # an `elem` might occur in the `group_rules` no more than once!
            if elem in self.group_rules:
                # for NXname, local_names in self.group_rules.items():
                # check if we use our own names
                if "names" not in self.group_rules[elem]:
                    continue
                local_names = self.group_rules[elem]["names"]
                if len(local_names) == 1:
                    ancestor_list[j] = [local_names[0]]
                else:
                    ancestor_list[j] = []
                    for local_name in local_names:
                        # iterate through many identical groups
                        ancestor_list[j] += [local_name]
            else:
                ancestor_list[j] = [elem]

        ancestor_list = ancestor_list[:
                                      -1][::
                                          -1]  # drop the last element and reverse the order
        root_path = [
            "/" + "/".join(path).replace("NX", "")
            for path in product(*ancestor_list)
        ]  # concatenate the values
        return root_path