PageRenderTime 55ms CodeModel.GetById 14ms RepoModel.GetById 4ms app.codeStats 1ms

/lib/galaxy/external_services/service.py

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