PageRenderTime 51ms CodeModel.GetById 9ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/galaxy/external_services/service.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 228 lines | 208 code | 12 blank | 8 comment | 40 complexity | 37211f64a034e3ea19a8ed0dff5af5da MD5 | raw file
  1#Contains objects for accessing external services applications
  2import logging
  3from parameters import ExternalServiceParameter
  4from actions import ExternalServiceAction
  5from galaxy.util.bunch import Bunch
  6
  7log = logging.getLogger( __name__ )
  8
  9
 10class ExternalServiceActionsGroup( object ):
 11    def __init__( self, parent, name, label=None ):
 12        self.name = name
 13        self.label = label
 14        self.parent = parent
 15        self.items = []
 16    @classmethod
 17    def from_elem( self, elem, parent = None ):
 18        """
 19        Return ExternalServiceActionsGroup created from an xml element.
 20        """
 21        if elem is not None:
 22            name = elem.get( 'name' )
 23            label = elem.get( 'label' )
 24            rval = ExternalServiceActionsGroup( parent, name, label=label )
 25            rval.load_sub_elems( elem )
 26        else:
 27            rval = ExternalServiceActionsGroup( None, None )
 28        return rval
 29    def load_sub_elems( self, elem ):
 30        for sub_elem in elem:
 31            if sub_elem.tag == 'param':
 32                self.add_item( ExternalServiceParameter.from_elem( sub_elem, self ) )
 33            elif sub_elem.tag == 'action':
 34                self.add_item( ExternalServiceAction.from_elem( sub_elem, self ) )
 35            elif sub_elem.tag == 'section':
 36                self.add_item( ExternalServiceActionsGroup.from_elem( sub_elem, self ) )
 37            elif sub_elem.tag == 'conditional':
 38                self.add_item( ExternalServiceActionsConditional( sub_elem, self ) )
 39            else:
 40                raise ValueError( 'Unknown tag: %s' % sub_elem.tag )
 41    def add_item( self, item ):
 42        self.items.append( item )
 43    def populate( self, service_instance, item = None, param_dict = None ):
 44        return PopulatedExternalService( self, service_instance, item, param_dict )
 45    def prepare_actions( self, param_dict, parent_dict, parent_section ):
 46        group = Bunch()
 47        group_section = ActionSection( self.name, self.label )
 48        parent_section.append( group_section )
 49        parent_dict[ self.name ] = group
 50        for item in self.items:
 51            if isinstance( item, ExternalServiceParameter ):
 52                group[ item.name ] = item.get_value( param_dict )
 53            elif isinstance( item, ExternalServiceActionsGroup ):
 54                group[ item.name ] = item.prepare_actions( param_dict, group, group_section )
 55            elif isinstance( item, ExternalServiceAction ):
 56                group_section.append( item.populate_action( param_dict ) )
 57            elif isinstance( item, ExternalServiceActionsConditional ):
 58                conditional_group = Bunch()
 59                conditional_group_section = ActionSection( item.name, item.label )#[]
 60                group_section.append( conditional_group_section )
 61                group[ item.name ] = conditional_group
 62                for case in item.get_current_cases( param_dict ):
 63                    conditional_group[ case.name ] = case.prepare_actions( param_dict, conditional_group, conditional_group_section )
 64            else:
 65                raise TypeError( 'unknown item type found: %s' % item )
 66        return group
 67
 68class ExternalServiceActionsGroupWhen( ExternalServiceActionsGroup ):
 69    type="when"
 70    @classmethod
 71    def from_elem( self, parent, elem ):
 72        """Loads the proper when by attributes of elem"""
 73        when_type = elem.get( 'type' )
 74        assert when_type in when_type_to_class, TypeError( "When type not implemented: %s" % when_type )
 75        return when_type_to_class[ when_type ].from_elem( parent, elem )
 76    def is_case( self, param_dict ):
 77        raise TypeError( "Abstract method" )
 78    def get_ref( self, param_dict ):
 79        ref = param_dict
 80        for ref_name in self.parent.ref:
 81            assert ref_name in ref, "Required dependency '%s' not found in incoming values" % ref_name
 82            ref = ref.get( ref_name )
 83        return ref
 84
 85class ValueExternalServiceActionsGroupWhen( ExternalServiceActionsGroupWhen ):
 86    type = "value"
 87    def __init__( self, parent, name, value, label=None ):
 88        super( ValueExternalServiceActionsGroupWhen, self ).__init__( parent, name, label )
 89        self.value = value
 90    @classmethod
 91    def from_elem( self, parent, elem ):
 92        """Returns an instance of this when"""
 93        rval = ValueExternalServiceActionsGroupWhen( parent, elem.get( 'name' ), elem.get( 'value' ), elem.get( 'label' ) )
 94        rval.load_sub_elems( elem )
 95        return rval
 96    def is_case( self, param_dict ):
 97        ref = self.get_ref( param_dict )
 98        return bool( str( ref ) == self.value )
 99
100class BooleanExternalServiceActionsGroupWhen( ExternalServiceActionsGroupWhen ):
101    type = "boolean"
102    def __init__( self, parent, name, value, label=None ):
103        super( BooleanExternalServiceActionsGroupWhen, self ).__init__( parent, name, label )
104        self.value = value
105    @classmethod
106    def from_elem( self, parent, elem ):
107        """Returns an instance of this when"""
108        rval = BooleanExternalServiceActionsGroupWhen( parent, elem.get( 'name' ), elem.get( 'label' ) )
109        rval.load_sub_elems( elem )
110        return rval
111    def is_case( self, param_dict ):
112        ref = self.get_ref( param_dict )
113        return bool( ref )
114
115class ItemIsInstanceExternalServiceActionsGroupWhen( ExternalServiceActionsGroupWhen ):
116    type = "item_type"
117    def __init__( self, parent, name, value, label=None ):
118        super( ItemIsInstanceExternalServiceActionsGroupWhen, self ).__init__( parent, name, label )
119        self.value = value
120    @classmethod
121    def from_elem( self, parent, elem ):
122        """Returns an instance of this when"""
123        rval = ItemIsInstanceExternalServiceActionsGroupWhen( parent, elem.get( 'name' ), elem.get( 'value' ), elem.get( 'label' ) )
124        rval.load_sub_elems( elem )
125        return rval
126    def is_case( self, param_dict ):
127        ref = self.get_ref( param_dict )
128        return ref.__class__.__name__.lower() in map( lambda x: x.lower(), self.value.split( '.' ) ) #HACK!
129
130when_type_to_class = {}
131for class_type in [ ValueExternalServiceActionsGroupWhen, BooleanExternalServiceActionsGroupWhen, ItemIsInstanceExternalServiceActionsGroupWhen]:
132    when_type_to_class[ class_type.type ] = class_type
133
134class ExternalServiceActionsConditional( object ):
135    type = "conditional"
136    def __init__( self, elem, parent ):
137        self.parent = parent
138        self.name = elem.get( 'name', None )
139        assert self.name is not None, "Required 'name' attribute missing from ExternalServiceActionsConditional"
140        self.label = elem.get( 'label' )
141        self.ref = elem.get( 'ref', None )
142        assert self.ref is not None, "Required 'ref' attribute missing from ExternalServiceActionsConditional"
143        self.ref = self.ref.split( '.' )
144        self.cases = []
145        for when_elem in elem.findall( 'when' ):
146            self.cases.append( ExternalServiceActionsGroupWhen.from_elem( self, when_elem ) )
147    def get_current_cases( self, param_dict ):
148        rval = []
149        for case in self.cases:
150            if case.is_case( param_dict ):
151                rval.append( case )
152        return rval
153
154class ActionSection( list ):
155    def __init__( self, name, label ):
156        list.__init__( self )
157        self.name = name
158        self.label = label
159    def has_action( self ):
160        for item in self:
161            if not isinstance( item, ActionSection ):
162                return True
163            else:
164                if item.has_action():
165                    return True
166        return False
167
168class PopulatedExternalService( object ):
169    def __init__( self, service_group, service_instance, item, param_dict = None ):
170        self.service_group = service_group
171        self.service_instance = service_instance
172        self.item = item
173        self.param_dict = param_dict
174        self.populate()
175    def __getattr__( self, name ):
176        return getattr( self.service_instance, name )#should .service or.service_instance should be here...
177    def populate( self ):
178        param_dict = {}
179        param_dict['fields'] = Bunch( **self.service_instance.form_values.content )
180        param_dict['item'] = self.item
181        param_dict['service'] = self.service_group.parent
182        param_dict['service_instance'] = self.service_instance
183        action_list = ActionSection( self.service_group.name, self.service_group.label )
184        for item in self.service_group.items:
185            if isinstance( item, ExternalServiceParameter ):
186                param_dict[ item.name ] = item.get_value( param_dict )
187            elif isinstance( item, ExternalServiceAction ):
188                action_list.append( item.populate_action( param_dict ) )
189            elif isinstance( item, ExternalServiceActionsGroup ):
190                item.prepare_actions( param_dict, param_dict, action_list )
191            else:
192                raise 'unknown item type found'
193        self.param_dict = param_dict
194        self.actions = action_list
195    def perform_action_by_name( self, actions_list ):
196        action = self.get_action_by_name( actions_list )
197        result = action.perform_action()
198        return action
199    def get_action_by_name( self, actions_list ):
200        action = None
201        actions = self.actions #populated actions
202        for name in actions_list:
203            action_found = False
204            for action in actions:
205                if action.name == name:
206                    action_found = True
207                    actions = action
208                    break
209            assert action_found, 'Action not found: %s in %s' % ( name, actions_list )
210        assert action, 'Action not found: %s' % actions_list
211        return action
212
213    def get_action_links( self ):
214        rval = []
215        param_dict = {}
216        param_dict['fields'] = Bunch( **self.service_instance.form_values.content )
217        param_dict['item'] = self.item
218        for item in self.service.items:
219            if isinstance( item, ExternalServiceParameter ):
220                param_dict[ item.name ] = item.get_value( param_dict )
221            elif isinstance( item, ExternalServiceAction ):
222                rval.append( item.get_action_access_link( self.item, trans, param_dict ) )
223            elif isinstance( item, ExternalServiceActionsGroup ):
224                rval.extend( item.populate( self.service_instance, item, param_dict ).get_action_links() )
225            else:
226                raise 'unknown item type found'
227    def __nonzero__( self ):
228        return self.actions.has_action()