def test_set_item_with_existing_component_in_overwrites_if_name_is_same():
    comp1_name = "component1"
    comp1 = Component(comp1_name)

    comp2 = Component(comp1_name)
    nx_class = "NXaperture"
    comp2.nx_class = nx_class

    list_of_components = [comp1]

    _set_item(None, list_of_components, comp1_name, comp2)

    assert list_of_components[0].nx_class == nx_class
예제 #2
0
    def _read_json_object(self, json_object: Dict, parent_node: Group = None):
        """
        Tries to create a component based on the contents of the JSON file.
        :param json_object: A component from the JSON dictionary.
        :param parent_name: The name of the parent object. Used for warning messages if something goes wrong.
        """
        nexus_object: Union[Group, FileWriterModule] = None
        use_placeholder = False
        if isinstance(json_object,
                      str) and json_object in PLACEHOLDER_WITH_NX_CLASSES:
            json_object = self._replace_placeholder(json_object)
            if not json_object:
                return
            use_placeholder = True
        if (CommonKeys.TYPE in json_object
                and json_object[CommonKeys.TYPE] == NodeType.GROUP):
            try:
                name = json_object[CommonKeys.NAME]
            except KeyError:
                self._add_object_warning(CommonKeys.NAME, parent_node)
                return None
            nx_class = _find_nx_class(json_object.get(CommonKeys.ATTRIBUTES))
            if nx_class == SAMPLE_CLASS_NAME:
                self.sample_name = name
            if not self._validate_nx_class(name, nx_class):
                self._add_object_warning(f"valid Nexus class {nx_class}",
                                         parent_node)
            if nx_class in COMPONENT_TYPES:
                nexus_object = Component(name=name, parent_node=parent_node)
                children_dict = json_object[CommonKeys.CHILDREN]
                self._add_transform_and_shape_to_component(
                    nexus_object, children_dict)
                self.model.append_component(nexus_object)
            else:
                nexus_object = Group(name=name, parent_node=parent_node)
            nexus_object.nx_class = nx_class
            if CommonKeys.CHILDREN in json_object:
                for child in json_object[CommonKeys.CHILDREN]:
                    node = self._read_json_object(child, nexus_object)
                    if node and isinstance(node, StreamModule):
                        nexus_object.children.append(node)
                    elif node and node.name not in nexus_object:
                        nexus_object[node.name] = node
        elif CommonKeys.MODULE in json_object and NodeType.CONFIG in json_object:
            module_type = json_object[CommonKeys.MODULE]
            if (module_type == WriterModules.DATASET.value
                    and json_object[NodeType.CONFIG][CommonKeys.NAME]
                    == CommonAttrs.DEPENDS_ON):
                nexus_object = None
            elif module_type in [x.value for x in WriterModules]:
                nexus_object = create_fw_module_object(
                    module_type, json_object[NodeType.CONFIG], parent_node)
                nexus_object.parent_node = parent_node
            else:
                self._add_object_warning("valid module type", parent_node)
                return None
        elif json_object == USERS_PLACEHOLDER:
            self.model.entry.users_placeholder = True
            return None
        else:
            self._add_object_warning(
                f"valid {CommonKeys.TYPE} or {CommonKeys.MODULE}", parent_node)

        # Add attributes to nexus_object.
        if nexus_object:
            json_attrs = json_object.get(CommonKeys.ATTRIBUTES)
            if json_attrs:
                attributes = Attributes()
                for json_attr in json_attrs:
                    if not json_attr[CommonKeys.VALUES]:
                        self._add_object_warning(
                            f"values in attribute {json_attr[CommonKeys.NAME]}",
                            parent_node,
                        )
                    elif CommonKeys.DATA_TYPE in json_attr:
                        attributes.set_attribute_value(
                            json_attr[CommonKeys.NAME],
                            json_attr[CommonKeys.VALUES],
                            json_attr[CommonKeys.DATA_TYPE],
                        )
                    elif CommonKeys.NAME in json_attr:
                        attributes.set_attribute_value(
                            json_attr[CommonKeys.NAME],
                            json_attr[CommonKeys.VALUES])
                nexus_object.attributes = attributes
            if (parent_node and isinstance(nexus_object, Dataset)
                    and parent_node.nx_class == ENTRY_CLASS_NAME):
                self.model.entry[nexus_object.name] = nexus_object
            if isinstance(nexus_object, Group) and not nexus_object.nx_class:
                self._add_object_warning(
                    f"valid {CommonAttrs.NX_CLASS}",
                    parent_node,
                )
            elif isinstance(nexus_object,
                            Group) and nexus_object.nx_class == "NXuser":
                self.model.entry[nexus_object.name] = nexus_object
            if isinstance(nexus_object, Group):
                nexus_object.group_placeholder = use_placeholder

        return nexus_object
예제 #3
0
def get_component():
    component = Component("test_1")
    component.nx_class = "NXslit"
    return component