def gather_all_references_of_view_model(gid, base_dir, ref_files): vm_path = determine_filepath(gid, base_dir) ref_files.append(vm_path) view_model_class = H5File.determine_type(vm_path) view_model = view_model_class() with ViewModelH5(vm_path, view_model) as vm_h5: references = vm_h5.gather_references() uuids = vm_h5.gather_references_by_uuid() for _, gid in references: if not gid: continue if isinstance(gid, (list, tuple)): for list_gid in gid: gather_all_references_of_view_model( list_gid, base_dir, ref_files) else: gather_all_references_of_view_model(gid, base_dir, ref_files) uuid_files = [] for _, gid in uuids: if not gid: continue index = load_entity_by_gid(gid.hex) h5_file = h5_file_for_index(index) uuid_files.append(h5_file.path) gather_all_references_by_index(h5_file, uuid_files) ref_files.extend(uuid_files)
def load_view_model_from_file(filepath): # type: (str) -> ViewModel """ Load a ViewModel object by reading the H5 file specified by filepath. """ base_dir = os.path.dirname(filepath) view_model_class = H5File.determine_type(filepath) view_model = view_model_class() with ViewModelH5(filepath, view_model) as h5_file: h5_file.load_into(view_model) references = h5_file.gather_references() view_model.create_date = string2date(h5_file.create_date.load()) view_model.generic_attributes = h5_file.load_generic_attributes() for trait_attr, gid in references: if not gid: continue if isinstance(gid, list): loaded_ref = [] for idx, sub_gid in enumerate(gid): ref = load_view_model(sub_gid, base_dir) loaded_ref.append(ref) else: loaded_ref = load_view_model(gid, base_dir) setattr(view_model, trait_attr.field_name, loaded_ref) return view_model
def load(self, gid=None, fname=None): # type: (typing.Union[uuid.UUID, str], str) -> ViewModel """ Load a ViewModel object by reading the H5 file with the given GID, from the directory self.base_dir """ if fname is None: if gid is None: raise ValueError("Neither gid nor filename is provided to load!") fname = self.find_file_by_gid(gid) else: fname = os.path.join(self.base_dir, fname) view_model_class = H5File.determine_type(fname) view_model = view_model_class() with ViewModelH5(fname, view_model) as h5_file: h5_file.load_into(view_model) references = h5_file.gather_references() view_model.create_date = string2date(h5_file.create_date.load()) view_model.generic_attributes = h5_file.load_generic_attributes() for trait_attr, gid in references: if not gid: continue if isinstance(gid, list): loaded_ref = [] for idx, sub_gid in enumerate(gid): ref = self.load(sub_gid) loaded_ref.append(ref) else: loaded_ref = self.load(gid) setattr(view_model, trait_attr.field_name, loaded_ref) return view_model
def load(self, gid=None, fname=None): # type: (typing.Union[uuid.UUID, str], str) -> ViewModel """ Load a ViewModel object by reading the H5 file with the given GID, from the directory self.base_dir """ if fname is None: if gid is None: raise ValueError( "Neither gid nor filename is provided to load!") fname = self.find_file_by_gid(gid) else: fname = os.path.join(self.base_dir, fname) view_model_class = H5File.determine_type(fname) view_model = view_model_class() has_traits_h5 = self.registry.get_h5file_for_datatype( view_model.__class__) if has_traits_h5 != H5File: with has_traits_h5(fname) as file: self._load(file, view_model) else: with ViewModelH5(fname, view_model) as h5_file: self._load(h5_file, view_model) return view_model
def gather_reference_files(self, gid, vm_ref_files, dt_ref_files, load_dts=None): vm_path = self.find_file_by_gid(gid) vm_ref_files.append(vm_path) view_model_class = H5File.determine_type(vm_path) view_model = view_model_class() with ViewModelH5(vm_path, view_model) as vm_h5: references = vm_h5.gather_references() for _, gid in references: if not gid: continue if isinstance(gid, (list, tuple)): for list_gid in gid: self.gather_reference_files(list_gid, vm_ref_files, dt_ref_files, load_dts) else: self.gather_reference_files(gid, vm_ref_files, dt_ref_files, load_dts) if load_dts: load_dts(vm_h5, dt_ref_files)
def _edit_data(self, datatype, new_data, from_group=False): # type: (DataType, dict, bool) -> None """ Private method, used for editing a meta-data XML file and a DataType row for a given custom DataType entity with new dictionary of data from UI. """ # 1. First update Operation fields: # Update group field if possible new_group_name = new_data[CommonDetails.CODE_OPERATION_TAG] empty_group_value = (new_group_name is None or new_group_name == "") if from_group: if empty_group_value: raise StructureException("Empty group is not allowed!") group = dao.get_generic_entity(OperationGroup, new_data[CommonDetails.CODE_OPERATION_GROUP_ID]) if group and len(group) > 0 and new_group_name != group[0].name: group = group[0] exists_group = dao.get_generic_entity(OperationGroup, new_group_name, 'name') if exists_group: raise StructureException("Group '" + new_group_name + "' already exists.") group.name = new_group_name dao.store_entity(group) else: operation = dao.get_operation_by_id(datatype.fk_from_operation) operation.user_group = new_group_name dao.store_entity(operation) op_folder = self.structure_helper.get_project_folder(operation.project, str(operation.id)) vm_gid = operation.view_model_gid view_model_file = h5.determine_filepath(vm_gid, op_folder) if view_model_file: view_model_class = H5File.determine_type(view_model_file) view_model = view_model_class() with ViewModelH5(view_model_file, view_model) as f: ga = f.load_generic_attributes() ga.operation_tag = new_group_name f.store_generic_attributes(ga, False) else: self.logger.warning("Could not find ViewModel H5 file for op: {}".format(operation)) # 2. Update GenericAttributes in the associated H5 files: h5_path = h5.path_for_stored_index(datatype) with H5File.from_file(h5_path) as f: ga = f.load_generic_attributes() ga.subject = new_data[DataTypeOverlayDetails.DATA_SUBJECT] ga.state = new_data[DataTypeOverlayDetails.DATA_STATE] ga.operation_tag = new_group_name if DataTypeOverlayDetails.DATA_TAG_1 in new_data: ga.user_tag_1 = new_data[DataTypeOverlayDetails.DATA_TAG_1] if DataTypeOverlayDetails.DATA_TAG_2 in new_data: ga.user_tag_2 = new_data[DataTypeOverlayDetails.DATA_TAG_2] if DataTypeOverlayDetails.DATA_TAG_3 in new_data: ga.user_tag_3 = new_data[DataTypeOverlayDetails.DATA_TAG_3] if DataTypeOverlayDetails.DATA_TAG_4 in new_data: ga.user_tag_4 = new_data[DataTypeOverlayDetails.DATA_TAG_4] if DataTypeOverlayDetails.DATA_TAG_5 in new_data: ga.user_tag_5 = new_data[DataTypeOverlayDetails.DATA_TAG_5] f.store_generic_attributes(ga, False) # 3. Update MetaData in DT Index DB as well. datatype.fill_from_generic_attributes(ga) dao.store_entity(datatype)