PageRenderTime 34ms CodeModel.GetById 16ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/sample_tracking/external_service_types.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 113 lines | 108 code | 3 blank | 2 comment | 4 complexity | a83d8c68ea4e6f9f69bb86da6501430a MD5 | raw file
  1import logging, os, sys
  2from galaxy.util.odict import odict
  3from galaxy.util.bunch import Bunch
  4from galaxy import util, jobs, model
  5from galaxy.forms.forms import form_factory
  6from galaxy.external_services.service import ExternalServiceActionsGroup
  7from elementtree.ElementTree import XML
  8from galaxy.sample_tracking.data_transfer import data_transfer_factories
  9log = logging.getLogger( __name__ )
 10
 11class ExternalServiceTypeNotFoundException( Exception ):
 12    pass
 13
 14class ExternalServiceTypesCollection( object ):
 15
 16    def __init__( self, config_filename, root_dir, app ):
 17        self.all_external_service_types = odict()
 18        self.root_dir = root_dir
 19        self.app = app
 20        try:
 21            self.load_all( config_filename )
 22        except:
 23            log.exception( "ExternalServiceTypesCollection error reading %s", config_filename )
 24    def load_all( self, config_filename ):
 25        self.visible_external_service_types = []
 26        tree = util.parse_xml( config_filename )
 27        root = tree.getroot()
 28        for elem in root:
 29            try:
 30                if elem.tag == 'external_service_type':
 31                    file_path = elem.get( "file" )
 32                    visible = util.string_as_bool( elem.get( "visible" ) )
 33                    external_service_type = self.load_external_service_type( os.path.join( self.root_dir, file_path ), visible )
 34                    self.all_external_service_types[ external_service_type.id ] = external_service_type
 35                    log.debug( "Loaded external_service_type: %s %s" % ( external_service_type.name, external_service_type.config_version ) )
 36                    if visible:
 37                        self.visible_external_service_types.append( external_service_type.id )
 38            except:
 39                log.exception( "error reading external_service_type from path: %s" % file_path )
 40    def load_external_service_type( self, config_file, visible=True ):
 41        # Parse XML configuration file and get the root element
 42        tree = util.parse_xml( config_file )
 43        root = tree.getroot()
 44        return ExternalServiceType( config_file, root, visible )
 45    def reload( self, external_service_type_id ):
 46        """
 47        Attempt to reload the external_service_type identified by 'external_service_type_id', if successful
 48        replace the old external_service_type.
 49        """
 50        if external_service_type_id not in self.all_external_service_types.keys():
 51            raise ExternalServiceTypeNotFoundException( "No external_service_type with id %s" % external_service_type_id )
 52        old_external_service_type = self.all_external_service_types[ external_service_type_id ]
 53        new_external_service_type = self.load_external_service_type( old_external_service_type.config_file )
 54        self.all_external_service_types[ external_service_type_id ] = new_external_service_type
 55        log.debug( "Reloaded external_service_type %s" %( external_service_type_id ) )
 56        return new_external_service_type
 57
 58class ExternalServiceType( object ):
 59    def __init__( self, external_service_type_xml_config, root, visible=True ):
 60        self.config_file = external_service_type_xml_config
 61        self.parse( root )
 62        self.visible = visible
 63        root.clear()
 64    def parse( self, root ):
 65        # Get the name
 66        self.name = root.get( "name" )
 67        if not self.name:
 68            raise Exception, "Missing external_service_type 'name'"
 69        # Get the UNIQUE id for the tool
 70        self.id = root.get( "id" )
 71        if not self.id:
 72            raise Exception, "Missing external_service_type 'id'"
 73        self.config_version = root.get( "version" )
 74        if not self.config_version:
 75            self.config_version = '1.0.0'
 76        self.description = util.xml_text(root, "description")
 77        self.version = util.xml_text( root.find( "version" ) )
 78        # parse the form
 79        self.form_definition = form_factory.from_elem( root.find( 'form' ) )
 80        self.parse_data_transfer_settings( root )
 81        self.parse_run_details( root )
 82        #external services actions
 83        self.actions = ExternalServiceActionsGroup.from_elem( root.find( 'actions' ), parent=self )
 84    def parse_data_transfer_settings( self, root ):
 85        self.data_transfer = {}
 86        data_transfer_settings_elem = root.find( 'data_transfer_settings' )
 87        # Currently only data transfer using scp or http is supported.
 88        for data_transfer_elem in data_transfer_settings_elem.findall( "data_transfer" ):
 89            if data_transfer_elem.get( 'protocol' ) == model.ExternalService.data_transfer_protocol.SCP:
 90                scp_data_transfer = data_transfer_factories[ model.ExternalService.data_transfer_protocol.SCP ]
 91                scp_data_transfer.parse( self.config_file, data_transfer_elem  )
 92                self.data_transfer[ model.ExternalService.data_transfer_protocol.SCP ] = scp_data_transfer
 93            if data_transfer_elem.get( 'protocol' ) == model.ExternalService.data_transfer_protocol.HTTP:
 94                http_data_transfer = data_transfer_factories[ model.ExternalService.data_transfer_protocol.HTTP ]
 95                http_data_transfer.parse( self.config_file, data_transfer_elem  )
 96                self.data_transfer[ model.ExternalService.data_transfer_protocol.HTTP ] = http_data_transfer
 97    def parse_run_details( self, root ):
 98        self.run_details = {}
 99        run_details_elem = root.find( 'run_details' )
100        if run_details_elem:
101            results_elem = run_details_elem.find( 'results' )
102            if results_elem:
103                # Get the list of resulting datatypes
104                # TODO: the 'results_urls' attribute is only useful if the transfer protocol is http(s), so check if that is the case.
105                self.run_details[ 'results' ], self.run_details[ 'results_urls' ] = self.parse_run_details_results( results_elem )
106    def parse_run_details_results( self, root ):
107        datatypes_dict = {}
108        urls_dict = {}
109        for datatype_elem in root.findall( "dataset" ):
110            name = datatype_elem.get( 'name' )
111            datatypes_dict[ name ] = datatype_elem.get( 'datatype' )
112            urls_dict[ name ] = datatype_elem.get( 'url', None )
113        return datatypes_dict, urls_dict