def fire_operation(self, adapter_instance, current_user, project_id, visible=True, **data): """ Launch an operation, specified by AdapterInstance, for CurrentUser, Current Project and a given set of UI Input Data. """ operation_name = str(adapter_instance.__class__.__name__) try: self.logger.info("Starting operation " + operation_name) project = dao.get_project_by_id(project_id) tmp_folder = self.file_helper.get_project_folder( project, self.file_helper.TEMP_FOLDER) result = OperationService().initiate_operation( current_user, project.id, adapter_instance, tmp_folder, visible, **data) self.logger.info("Finished operation:" + operation_name) return result except TVBException as excep: self.logger.exception( "Could not launch operation " + operation_name + " with the given set of input data, because: " + excep.message) raise OperationException(excep.message, excep) except Exception as excep: self.logger.exception("Could not launch operation " + operation_name + " with the given set of input data!") raise OperationException(str(excep))
def fire_operation(self, adapter_instance, current_user, project_id, visible=True, view_model=None): """ Launch an operation, specified by AdapterInstance, for current_user and project with project_id. """ operation_name = str(adapter_instance.__class__.__name__) try: self.logger.info("Starting operation " + operation_name) project = dao.get_project_by_id(project_id) result = self.initiate_operation(current_user, project, adapter_instance, visible, model_view=view_model) self.logger.info("Finished operation launch:" + operation_name) return result except TVBException as excep: self.logger.exception( "Could not launch operation " + operation_name + " with the given set of input data, because: " + excep.message) raise OperationException(excep.message, excep) except Exception as excep: self.logger.exception("Could not launch operation " + operation_name + " with the given set of input data!") raise OperationException(str(excep))
def build_adapter_instance(self, group): """ Having a module and a class name, create an instance of ABCAdapter. """ try: return ABCAdapter.build_adapter(group) except IntrospectionException, excep: if group is None: self.logger.error('The given algorithm group is None.') self.logger.exception(excep) raise OperationException("Could not prepare the algo- group.") self.logger.error('Not found: ' + group.classname + ' in:' + group.module) self.logger.exception(excep) raise OperationException("Could not prepare " + group.classname)
def _listdir(working_dir, base='/'): # type: (Storage, str) -> dict """ We took this code from pyunicore Storage.listdir method and extended it to use a subdirectory. Looking at the method signature, it should have had this behavior, but the 'base' argument is not used later inside the method code. Probably will be fixed soon in their API, so we could delete this. :return: dict of {str: PathFile} objects """ ret = {} try: for path, meta in working_dir.contents(base)['content'].items(): path_url = working_dir.path_urls['files'] + path path = path[1:] # strip leading '/' if meta['isDirectory']: ret[path] = unicore_client.PathDir(working_dir, path_url, path) else: ret[path] = unicore_client.PathFile( working_dir, path_url, path) return ret except HTTPError as http_error: if http_error.response.status_code == 404: raise OperationException( "Folder {} is not present on HPC storage.".format(base)) raise http_error
def fire_operation(self, adapter_instance, current_user, project_id, method_name=ABCAdapter.LAUNCH_METHOD, visible=True, **data): """ Launch an operation, specified by AdapterInstance, for CurrentUser, Current Project and a given set of UI Input Data. """ operation_name = str(adapter_instance.__class__.__name__) + "." + method_name try: # if OperationService.ATT_UID in data: # existent = dao.get_last_data_with_uid(data[OperationService.ATT_UID]) # if existent is not None: # self.create_link(existent, project_id) # return "Created required links." self.logger.info("Starting operation " + operation_name) project = dao.get_project_by_id(project_id) tmp_folder = self.file_helper.get_project_folder(project, self.file_helper.TEMP_FOLDER) result = OperationService().initiate_operation(current_user, project.id, adapter_instance, tmp_folder, method_name, visible, **data) self.logger.info("Finished operation:" + operation_name) return result except TVBException, excep: self.logger.error("Could not launch operation " + operation_name + " with the given set of input data!") self.logger.exception(excep) raise OperationException(excep.message, excep)
def prepare_adapter(self, project_id, algo_group): """ Having a given Adapter, specified by Module and ClassName, create a instance of it and return the instance. Actually return a Tuple: Adapter Instance, Dictionary for Adapter Interface specification. """ adapter_module = algo_group.module.replace('-', '.') adapter_name = algo_group.classname try: # Prepare Adapter Interface, by populating with existent data, # in case of a parameter of type DataType. group = dao.find_group(adapter_module, adapter_name, algo_group.init_parameter) adapter_instance = self.build_adapter_instance(group) interface = adapter_instance.get_input_tree() interface = self.prepare_parameters(interface, project_id, group.fk_category) interface = ABCAdapter.prepare_param_names(interface) return group, interface except Exception, excep: self.logger.exception(excep) self.logger.error('Not found:' + adapter_name + ' in:' + adapter_module) raise OperationException("Could not prepare " + adapter_name)
def get_category_by_id(self, identifier): """ Pass to DAO the retrieve of category by ID operation.""" try: return dao.get_category_by_id(identifier) except Exception, excep: self.logger.warning("Wrong step!") self.logger.exception(excep) raise OperationException(excep.message)
def prepare_adapter(self, stored_adapter): adapter_module = stored_adapter.module adapter_name = stored_adapter.classname try: # Prepare Adapter Interface, by populating with existent data, # in case of a parameter of type DataType. adapter_instance = ABCAdapter.build_adapter(stored_adapter) return adapter_instance except Exception: self.logger.exception('Not found:' + adapter_name + ' in:' + adapter_module) raise OperationException("Could not prepare " + adapter_name)
def prepare_adapter(self, project_id, stored_adapter): """ Having a StoredAdapter, return the Tree Adapter Interface object, populated with datatypes from 'project_id'. """ adapter_module = stored_adapter.module adapter_name = stored_adapter.classname try: # Prepare Adapter Interface, by populating with existent data, # in case of a parameter of type DataType. adapter_instance = ABCAdapter.build_adapter(stored_adapter) interface = adapter_instance.get_input_tree() interface = self.input_tree_manager.fill_input_tree_with_options(interface, project_id, stored_adapter.fk_category) interface = self.input_tree_manager.prepare_param_names(interface) return interface except Exception: self.logger.exception('Not found:' + adapter_name + ' in:' + adapter_module) raise OperationException("Could not prepare " + adapter_name)
def prepare_adapter_form(self, adapter_instance=None, form_instance=None, project_id=None, extra_conditions=None): # type: (ABCAdapter, ABCAdapterForm, int, []) -> ABCAdapterForm form = None if form_instance is not None: form = form_instance elif adapter_instance is not None: form = adapter_instance.get_form()() if form is None: raise OperationException("Cannot prepare None form") form = self._fill_form_with_datatypes(form, project_id, extra_conditions) return form
project = dao.get_project_by_id(project_id) tmp_folder = self.file_helper.get_project_folder(project, self.file_helper.TEMP_FOLDER) result = OperationService().initiate_operation(current_user, project.id, adapter_instance, tmp_folder, method_name, visible, **data) self.logger.info("Finished operation:" + operation_name) return result except TVBException, excep: self.logger.error("Could not launch operation " + operation_name + " with the given set of input data!") self.logger.exception(excep) raise OperationException(excep.message, excep) except Exception, excep: self.logger.error("Could not launch operation " + operation_name + " with the given set of input data!") self.logger.exception(excep) raise OperationException(str(excep)) def parse_version_xml(self, xml_content): """Parse XML content, to retrieve Version number, release date and release notes.""" return self.file_helper.parse_xml_content(xml_content) ########################################################################## ######## Methods below are for connectivity selections ################### ######## Might be good to move them to proper place ###################### ########################################################################## @staticmethod def get_selections_for_project(project_id, connectivity_gid): """
class FlowService: """ Service Layer for all TVB generic Work-Flow operations. """ def __init__(self): self.logger = get_logger(self.__class__.__module__) self.file_helper = FilesHelper() self.input_tree_manager = InputTreeManager() def get_category_by_id(self, identifier): """ Pass to DAO the retrieve of category by ID operation.""" return dao.get_category_by_id(identifier) @staticmethod def get_raw_categories(): """:returns: AlgorithmCategory list of entities that have results in RAW state (Creators/Uploaders)""" return dao.get_raw_categories() @staticmethod def get_visualisers_category(): """Retrieve all Algorithm categories, with display capability""" result = dao.get_visualisers_categories() if not result: raise ValueError("View Category not found!!!") return result[0] @staticmethod def get_algorithm_by_identifier(ident): """ Retrieve Algorithm entity by ID. Return None, if ID is not found in DB. """ return dao.get_algorithm_by_id(ident) @staticmethod def load_operation(operation_id): """ Retrieve previously stored Operation from DB, and load operation.burst attribute""" operation = dao.get_operation_by_id(operation_id) operation.burst = dao.get_burst_for_operation_id(operation_id) return operation @staticmethod def get_operation_numbers(proj_id): """ Count total number of operations started for current project. """ return dao.get_operation_numbers(proj_id) def prepare_adapter(self, project_id, stored_adapter): """ Having a StoredAdapter, return the Tree Adapter Interface object, populated with datatypes from 'project_id'. """ adapter_module = stored_adapter.module adapter_name = stored_adapter.classname try: # Prepare Adapter Interface, by populating with existent data, # in case of a parameter of type DataType. adapter_instance = ABCAdapter.build_adapter(stored_adapter) interface = adapter_instance.get_input_tree() interface = self.input_tree_manager.fill_input_tree_with_options( interface, project_id, stored_adapter.fk_category) interface = self.input_tree_manager.prepare_param_names(interface) return interface except Exception: self.logger.exception('Not found:' + adapter_name + ' in:' + adapter_module) raise OperationException("Could not prepare " + adapter_name) @staticmethod def get_algorithm_by_module_and_class(module, classname): """ Get the db entry from the algorithm table for the given module and class. """ return dao.get_algorithm_by_module(module, classname) @staticmethod def get_available_datatypes(project_id, data_type_cls, filters=None): """ Return all dataTypes that match a given name and some filters. :param data_type_cls: either a fully qualified class name or a class object """ return get_filtered_datatypes(project_id, data_type_cls, filters) @staticmethod def create_link(data_ids, project_id): """ For a list of dataType IDs and a project id create all the required links. """ for data in data_ids: link = model.Links(data, project_id) dao.store_entity(link) @staticmethod def remove_link(dt_id, project_id): """ Remove the link from the datatype given by dt_id to project given by project_id. """ link = dao.get_link(dt_id, project_id) if link is not None: dao.remove_entity(model.Links, link.id) def fire_operation(self, adapter_instance, current_user, project_id, visible=True, **data): """ Launch an operation, specified by AdapterInstance, for CurrentUser, Current Project and a given set of UI Input Data. """ operation_name = str(adapter_instance.__class__.__name__) try: self.logger.info("Starting operation " + operation_name) project = dao.get_project_by_id(project_id) tmp_folder = self.file_helper.get_project_folder( project, self.file_helper.TEMP_FOLDER) result = OperationService().initiate_operation( current_user, project.id, adapter_instance, tmp_folder, visible, **data) self.logger.info("Finished operation:" + operation_name) return result except TVBException, excep: self.logger.exception( "Could not launch operation " + operation_name + " with the given set of input data, because: " + excep.message) raise OperationException(excep.message, excep) except Exception, excep: self.logger.exception("Could not launch operation " + operation_name + " with the given set of input data!") raise OperationException(str(excep))