def _update_old_portlets_from_db(portlets_list): stored_portlets = dao.get_available_portlets() # First update old portlets from DB for stored_portlet in stored_portlets: for verified_portlet in portlets_list: if stored_portlet.algorithm_identifier == verified_portlet.algorithm_identifier: stored_portlet.xml_path = verified_portlet.xml_path stored_portlet.last_introspection_check = datetime.datetime.now() stored_portlet.name = verified_portlet.name dao.store_entity(stored_portlet) break
"Invalid adapter declaration %s in portlet %s" % (adapter[ABCAdapter.KEY_TYPE], algo_identifier)) is_valid = False if is_valid: portlets_list.append( model.Portlet( algo_identifier, complete_file_path, portlet_list[algo_identifier]['name'])) except XmlParserException, excep: self.logger.exception(excep) self.logger.error("Invalid Portlet description File " + file_n + " will continue without it!!") self.logger.debug("Refreshing portlets from xml declarations.") stored_portlets = dao.get_available_portlets() #First update old portlets from DB for stored_portlet in stored_portlets: for verified_portlet in portlets_list: if stored_portlet.algorithm_identifier == verified_portlet.algorithm_identifier: stored_portlet.xml_path = verified_portlet.xml_path stored_portlet.last_introspection_check = datetime.datetime.now( ) stored_portlet.name = verified_portlet.name dao.store_entity(stored_portlet) break #Now add portlets that were not in DB at previous run but are valid now for portlet in portlets_list: db_entity = dao.get_portlet_by_identifier( portlet.algorithm_identifier)
def get_available_portlets(): """ :returns: a list of all the available portlet entites """ return dao.get_available_portlets()
self.logger.error("Invalid input %s for adapter %s" % ( input_entry[ABCAdapter.KEY_NAME], adapter_instance)) is_valid = False except ImportError, _: self.logger.error("Invalid adapter declaration %s in portlet %s" % ( adapter[ABCAdapter.KEY_TYPE], algo_identifier)) is_valid = False if is_valid: portlets_list.append(model.Portlet(algo_identifier, complete_file_path, portlet_list[algo_identifier]['name'])) except XmlParserException, excep: self.logger.exception(excep) self.logger.error("Invalid Portlet description File " + file_n + " will continue without it!!") self.logger.debug("Refreshing portlets from xml declarations.") stored_portlets = dao.get_available_portlets() #First update old portlets from DB for stored_portlet in stored_portlets: for verified_portlet in portlets_list: if stored_portlet.algorithm_identifier == verified_portlet.algorithm_identifier: stored_portlet.xml_path = verified_portlet.xml_path stored_portlet.last_introspection_check = datetime.datetime.now() stored_portlet.name = verified_portlet.name dao.store_entity(stored_portlet) break #Now add portlets that were not in DB at previous run but are valid now for portlet in portlets_list: db_entity = dao.get_portlet_by_identifier(portlet.algorithm_identifier) if db_entity is None: self.logger.debug("Will now store portlet %s" % (str(portlet),))
def __get_portlets(self, path_portlets): """ Given a path in the form of a python package e.g.: "tvb.portlets', import the package, get it's folder and look for all the XML files defined there, then read all the portlets defined there and store them in DB. """ portlet_package = __import__(path_portlets, globals(), locals(), ["__init__"]) portlet_folder = os.path.dirname(portlet_package.__file__) portlets_list = [] for file_n in os.listdir(portlet_folder): try: if file_n.endswith('.xml'): complete_file_path = os.path.join(portlet_folder, file_n) portlet_reader = XMLPortletReader(complete_file_path) portlet_list = portlet_reader.get_algorithms_dictionary() self.logger.debug("Starting to verify currently declared portlets in %s." % (file_n,)) for algo_identifier in portlet_list: adapters_chain = portlet_reader.get_adapters_chain(algo_identifier) is_valid = True for adapter in adapters_chain: class_name = adapter[ABCAdapter.KEY_TYPE].split('.')[-1] module_name = adapter[ABCAdapter.KEY_TYPE].replace('.' + class_name, '') try: #Check that module is properly declared module = __import__(module_name, globals(), fromlist=[class_name]) if type(module) != ModuleType: is_valid = False self.logger.error("Wrong module %s in portlet %s" % (module_name, algo_identifier)) continue #Check that class is properly declared if not hasattr(module, class_name): is_valid = False self.logger.error("Wrong class %s in portlet %s." % (class_name, algo_identifier)) continue #Check inputs that refers to this adapter portlet_inputs = portlet_list[algo_identifier][ELEM_INPUTS] adapter_instance = PortletConfigurer.build_adapter_from_declaration(adapter) if adapter_instance is None: is_valid = False self.logger.warning("No group having class=%s stored for " "portlet %s." % (class_name, algo_identifier)) continue adapter_input_names = [entry[ABCAdapter.KEY_NAME] for entry in adapter_instance.flaten_input_interface()] for input_entry in portlet_inputs.values(): if input_entry[ATT_OVERWRITE] == adapter[ABCAdapter.KEY_NAME]: if input_entry[ABCAdapter.KEY_NAME] not in adapter_input_names: self.logger.error("Invalid input %s for adapter %s" % ( input_entry[ABCAdapter.KEY_NAME], adapter_instance)) is_valid = False except ImportError: self.logger.error("Invalid adapter declaration %s in portlet %s" % ( adapter[ABCAdapter.KEY_TYPE], algo_identifier)) is_valid = False if is_valid: portlets_list.append(model.Portlet(algo_identifier, complete_file_path, portlet_list[algo_identifier]['name'])) except XmlParserException as excep: self.logger.exception(excep) self.logger.error("Invalid Portlet description File " + file_n + " will continue without it!!") self.logger.debug("Refreshing portlets from xml declarations.") stored_portlets = dao.get_available_portlets() #First update old portlets from DB for stored_portlet in stored_portlets: for verified_portlet in portlets_list: if stored_portlet.algorithm_identifier == verified_portlet.algorithm_identifier: stored_portlet.xml_path = verified_portlet.xml_path stored_portlet.last_introspection_check = datetime.datetime.now() stored_portlet.name = verified_portlet.name dao.store_entity(stored_portlet) break #Now add portlets that were not in DB at previous run but are valid now for portlet in portlets_list: db_entity = dao.get_portlet_by_identifier(portlet.algorithm_identifier) if db_entity is None: self.logger.debug("Will now store portlet %s" % (str(portlet),)) dao.store_entity(portlet)
def __get_portlets(self, path_portlets): """ Given a path in the form of a python package e.g.: "tvb.portlets', import the package, get it's folder and look for all the XML files defined there, then read all the portlets defined there and store them in DB. """ portlet_package = __import__(path_portlets, globals(), locals(), ["__init__"]) portlet_folder = os.path.dirname(portlet_package.__file__) portlets_list = [] for file_n in os.listdir(portlet_folder): try: if file_n.endswith('.xml'): complete_file_path = os.path.join(portlet_folder, file_n) portlet_reader = XMLPortletReader(complete_file_path) portlet_list = portlet_reader.get_algorithms_dictionary() self.logger.debug( "Starting to verify currently declared portlets in %s." % (file_n, )) for algo_identifier in portlet_list: adapters_chain = portlet_reader.get_adapters_chain( algo_identifier) is_valid = True for adapter in adapters_chain: class_name = adapter[ABCAdapter.KEY_TYPE].split( '.')[-1] module_name = adapter[ABCAdapter.KEY_TYPE].replace( '.' + class_name, '') try: #Check that module is properly declared module = __import__(module_name, globals(), fromlist=[class_name]) if type(module) != ModuleType: is_valid = False self.logger.error( "Wrong module %s in portlet %s" % (module_name, algo_identifier)) continue #Check that class is properly declared if not hasattr(module, class_name): is_valid = False self.logger.error( "Wrong class %s in portlet %s." % (class_name, algo_identifier)) continue #Check inputs that refers to this adapter portlet_inputs = portlet_list[algo_identifier][ ELEM_INPUTS] adapter_instance = PortletConfigurer.build_adapter_from_declaration( adapter) if adapter_instance is None: is_valid = False self.logger.warning( "No group having class=%s stored for " "portlet %s." % (class_name, algo_identifier)) continue adapter_input_names = [ entry[ABCAdapter.KEY_NAME] for entry in adapter_instance.flaten_input_interface() ] for input_entry in portlet_inputs.values(): if input_entry[ATT_OVERWRITE] == adapter[ ABCAdapter.KEY_NAME]: if input_entry[ ABCAdapter. KEY_NAME] not in adapter_input_names: self.logger.error( "Invalid input %s for adapter %s" % (input_entry[ ABCAdapter.KEY_NAME], adapter_instance)) is_valid = False except ImportError: self.logger.error( "Invalid adapter declaration %s in portlet %s" % (adapter[ABCAdapter.KEY_TYPE], algo_identifier)) is_valid = False if is_valid: portlets_list.append( model.Portlet( algo_identifier, complete_file_path, portlet_list[algo_identifier]['name'])) except XmlParserException as excep: self.logger.exception(excep) self.logger.error("Invalid Portlet description File " + file_n + " will continue without it!!") self.logger.debug("Refreshing portlets from xml declarations.") stored_portlets = dao.get_available_portlets() #First update old portlets from DB for stored_portlet in stored_portlets: for verified_portlet in portlets_list: if stored_portlet.algorithm_identifier == verified_portlet.algorithm_identifier: stored_portlet.xml_path = verified_portlet.xml_path stored_portlet.last_introspection_check = datetime.datetime.now( ) stored_portlet.name = verified_portlet.name dao.store_entity(stored_portlet) break #Now add portlets that were not in DB at previous run but are valid now for portlet in portlets_list: db_entity = dao.get_portlet_by_identifier( portlet.algorithm_identifier) if db_entity is None: self.logger.debug("Will now store portlet %s" % (str(portlet), )) dao.store_entity(portlet)