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, excep: self.logger.exception(excep) self.logger.error("Invalid Portlet description File " + file_n + " will continue without it!!")
def _prepare_valid_portlets_list(self, portlet_folder, 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 = importlib.import_module(module_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 = self._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_form = adapter_instance.get_form() adapter_instance.submit_form(adapter_form()) # TODO: implement this for neoforms adapter_form_field_names = {} # 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_form_field_names: self.logger.error("Invalid input %s for adapter %s" % (input_entry[ABCAdapter.KEY_NAME], adapter_instance)) is_valid = False except ImportError: is_valid = False self.logger.error("Invalid adapter declaration %s in portlet %s" % (adapter[ABCAdapter.KEY_TYPE], algo_identifier)) if is_valid: portlets_list.append( 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!!")