예제 #1
0
 def new_portlet_configuration(portlet_id,
                               tab_nr=-1,
                               index_in_tab=-1,
                               portlet_name='Default'):
     """
     Return a new portlet configuration entitiy with default parameters.
     
     :param portlet_id: the id of the portlet for which a configuration will
         be stored
     :param tab_nr: the index of the currently selected tab
     :param index_in_tab: the index from the currently selected tab
     
     """
     portlet_entity = dao.get_portlet_by_id(portlet_id)
     if portlet_entity is None:
         raise InvalidPortletConfiguration(
             "No portlet entity located in database with id=%s." %
             portlet_id)
     portlet_configurer = PortletConfigurer(portlet_entity)
     configuration = portlet_configurer.create_new_portlet_configuration(
         portlet_name)
     for wf_step in configuration.analyzers:
         wf_step.tab_index = tab_nr
         wf_step.index_in_tab = index_in_tab
     configuration.visualizer.tab_index = tab_nr
     configuration.visualizer.index_in_tab = index_in_tab
     return configuration
예제 #2
0
 def update_portlet_configuration(portlet_configuration, submited_parameters):
     """
     :param portlet_configuration: the portlet configuration that needs to be updated
     :param submited_parameters: a list of parameters as submitted from the UI. This 
         is a dictionary in the form : 
         {'dynamic' : {name:value pairs}, 'static' : {name:value pairs}}
         
     All names are prefixed with adapter specific generated prefix.
     """
     portlet_entity = dao.get_portlet_by_id(portlet_configuration.portlet_id)
     portlet_configurer = PortletConfigurer(portlet_entity)
     return portlet_configurer.update_portlet_configuration(portlet_configuration, submited_parameters)
예제 #3
0
 def update_portlet_configuration(portlet_configuration,
                                  submited_parameters):
     """
     :param portlet_configuration: the portlet configuration that needs to be updated
     :param submited_parameters: a list of parameters as submitted from the UI. This 
         is a dictionary in the form : 
         {'dynamic' : {name:value pairs}, 'static' : {name:value pairs}}
         
     All names are prefixed with adapter specific generated prefix.
     """
     portlet_entity = dao.get_portlet_by_id(
         portlet_configuration.portlet_id)
     portlet_configurer = PortletConfigurer(portlet_entity)
     return portlet_configurer.update_portlet_configuration(
         portlet_configuration, submited_parameters)
예제 #4
0
 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!!")
예제 #5
0
 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!!")
예제 #6
0
 def new_portlet_configuration(portlet_id, tab_nr=-1, index_in_tab=-1, portlet_name='Default'):
     """
     Return a new portlet configuration entity with default parameters.
     
     :param portlet_id: the id of the portlet for which a configuration will be stored
     :param tab_nr: the index of the currently selected tab
     :param index_in_tab: the index from the currently selected tab
     """
     portlet_entity = dao.get_portlet_by_id(portlet_id)
     if portlet_entity is None:
         raise InvalidPortletConfiguration("No portlet entity located in database with id=%s." % portlet_id)
     portlet_configurer = PortletConfigurer(portlet_entity)
     configuration = portlet_configurer.create_new_portlet_configuration(portlet_name)
     for wf_step in configuration.analyzers:
         wf_step.tab_index = tab_nr
         wf_step.index_in_tab = index_in_tab
     configuration.visualizer.tab_index = tab_nr
     configuration.visualizer.index_in_tab = index_in_tab
     return configuration 
예제 #7
0
    def _get_portlet_configurer(self, portlet_id):

        if portlet_id not in self.cache_portlet_configurators:

            portlet_entity = dao.get_portlet_by_id(portlet_id)
            if portlet_entity is None:
                raise InvalidPortletConfiguration("No portlet entity located in database with id=%s. " % portlet_id)

            self.cache_portlet_configurators[portlet_id] = PortletConfigurer(portlet_entity)
            self.logger.debug("Recently parsed portlet XML:" + str([portlet_entity]))

        return self.cache_portlet_configurators[portlet_id]
예제 #8
0
 def test_portlet_configurable_interface(self):
     """
     A simple test for the get configurable interface method.
     """        
     test_portlet = dao.get_portlet_by_identifier("TA1TA2")
     
     result = PortletConfigurer(test_portlet).get_configurable_interface()
     self.assertEqual(len(result), 2, "Length of the resulting interface not as expected")
     for one_entry in result:
         for entry in one_entry.interface:
             if entry['name'] == 'test1':
                 self.assertTrue(entry['default'] == 'step_0[0]', "Overwritten default not in effect.")
             if entry['name'] == 'test2':
                 self.assertTrue(entry['default'] == '0', "Value that was not overwritten changed.")
예제 #9
0
    def build_portlet_interface(self, portlet_configuration, project_id):
        """
        From a portlet_id and a project_id, first build the portlet
        entity then get it's configurable interface. 
        
        :param portlet_configuration: a portlet configuration entity. It holds at the
            least the portlet_id, and in case any default parameters were saved
            they can be rebuilt from the analyzers // visualizer parameters
        :param project_id: the id of the current project   
            
        :returns: the portlet interface will be of the following form::
            [{'interface': adapter_interface, 
            'prefix': prefix_for_parameter_names, 
            'subalg': {algorithm_field_name: default_algorithm_value},
            'algo_group': algorithm_group,
            'alg_ui_name': displayname},
            ......]
            A list of dictionaries for each adapter that makes up the portlet.
            
        """
        portlet_entity = dao.get_portlet_by_id(
            portlet_configuration.portlet_id)
        if portlet_entity is None:
            raise InvalidPortletConfiguration(
                "No portlet entity located in database with id=%s. "
                "Portlet configuration %s is not valid." %
                (portlet_configuration.portlet_id, portlet_configuration))
        portlet_configurer = PortletConfigurer(portlet_entity)
        portlet_interface = portlet_configurer.get_configurable_interface()
        self.logger.debug("Created interface for portlet " +
                          str([portlet_entity]))

        for adapter_conf in portlet_interface:
            interface = adapter_conf.interface
            interface = FlowService().prepare_parameters(
                interface, project_id, adapter_conf.group.fk_category)
            interface = ABCAdapter.prepare_param_names(interface,
                                                       adapter_conf.prefix)
            adapter_conf.interface = interface

        portlet_configurer.update_default_values(portlet_interface,
                                                 portlet_configuration)
        portlet_configurer.prefix_adapters_parameters(portlet_interface)

        return portlet_interface
예제 #10
0
    def build_portlet_interface(self, portlet_configuration, project_id):
        """
        From a portlet_id and a project_id, first build the portlet
        entity then get it's configurable interface. 
        
        :param portlet_configuration: a portlet configuration entity. It holds at the
            least the portlet_id, and in case any default parameters were saved
            they can be rebuilt from the analyzers // visualizer parameters
        :param project_id: the id of the current project   
            
        :returns: the portlet interface will be of the following form::
            [{'interface': adapter_interface, 
            'prefix': prefix_for_parameter_names, 
            'subalg': {algorithm_field_name: default_algorithm_value},
            'algo_group': algorithm_group,
            'alg_ui_name': displayname},
            ......]
            A list of dictionaries for each adapter that makes up the portlet.
            
        """
        portlet_entity = dao.get_portlet_by_id(portlet_configuration.portlet_id)
        if portlet_entity is None:
            raise InvalidPortletConfiguration(
                "No portlet entity located in database with id=%s. "
                "Portlet configuration %s is not valid." % (portlet_configuration.portlet_id, portlet_configuration)
            )
        portlet_configurer = PortletConfigurer(portlet_entity)
        portlet_interface = portlet_configurer.get_configurable_interface()
        self.logger.debug("Created interface for portlet " + str([portlet_entity]))

        for adapter_conf in portlet_interface:
            interface = adapter_conf.interface
            interface = FlowService().prepare_parameters(interface, project_id, adapter_conf.group.fk_category)
            interface = ABCAdapter.prepare_param_names(interface, adapter_conf.prefix)
            adapter_conf.interface = interface

        portlet_configurer.update_default_values(portlet_interface, portlet_configuration)
        portlet_configurer.prefix_adapters_parameters(portlet_interface)

        return portlet_interface
예제 #11
0
    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 = __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_form = adapter_instance.get_form()
                                adapter_instance.submit_form(adapter_form())
                                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!!")