<s> from pysphere import VIProperty , VITask , VIMor <EOL> from pysphere . resources import VimService_services as VI <EOL> from pysphere import VIException , VIApiException , FaultTypes <EOL> class Recursion : <EOL> ALL = "<STR_LIT:all>" <EOL> CHILDREN = "<STR_LIT>" <EOL> SELF = "<STR_LIT>" <EOL> class States : <EOL> ERROR = "<STR_LIT:error>" <EOL> QUEUED = "<STR_LIT>" <EOL> RUNNING = "<STR_LIT>" <EOL> SUCCESS = "<STR_LIT:success>" <EOL> class VITaskHistoryCollector ( object ) : <EOL> """<STR_LIT>""" <EOL> RECURSION = Recursion <EOL> STATES = States <EOL> def __init__ ( self , server , entity = None , recursion = None , states = None ) : <EOL> """<STR_LIT>""" <EOL> self . _server = server <EOL> self . _mor = None <EOL> if entity and not VIMor . is_mor ( entity ) : <EOL> raise VIException ( "<STR_LIT>" , <EOL> FaultTypes . PARAMETER_ERROR ) <EOL> if entity and not recursion in [ Recursion . ALL , Recursion . CHILDREN , <EOL> Recursion . SELF ] : <EOL> raise VIException ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> FaultTypes . PARAMETER_ERROR ) <EOL> try : <EOL> task_manager = server . _do_service_content . TaskManager <EOL> request = VI . CreateCollectorForTasksRequestMsg ( ) <EOL> _this = request . new__this ( task_manager ) <EOL> _this . set_attribute_type ( task_manager . get_attribute_type ( ) ) <EOL> request . set_element__this ( _this ) <EOL> _filter = request . new_filter ( ) <EOL> if states and not isinstance ( states , list ) : <EOL> states = [ states ] <EOL> if states : <EOL> _filter . set_element_state ( states ) <EOL> if entity : <EOL> entity_filter = _filter . new_entity ( ) <EOL> mor_entity = entity_filter . new_entity ( entity ) <EOL> mor_entity . set_attribute_type ( entity . get_attribute_type ( ) ) <EOL> entity_filter . set_element_entity ( mor_entity ) <EOL> entity_filter . set_element_recursion ( recursion ) <EOL> _filter . set_element_entity ( entity_filter ) <EOL> request . set_element_filter ( _filter ) <EOL> resp = server . _proxy . CreateCollectorForTasks ( request ) . _returnval <EOL> except ( VI . ZSI . FaultException ) , e : <EOL> raise VIApiException ( e ) <EOL> self . _mor = resp <EOL> self . _props = VIProperty ( self . _server , self . _mor ) <EOL> def get_latest_tasks ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _props . _flush_cache ( ) <EOL> if not hasattr ( self . _props , "<STR_LIT>" ) : <EOL> return [ ] <EOL> ret = [ ] <EOL> for task in self . _props . latestPage : <EOL> ret . append ( VITask ( task . task . _obj , self . _server ) ) <EOL> return ret <EOL> def read_next_tasks ( self , max_count ) : <EOL> """<STR_LIT>""" <EOL> return self . __read_tasks ( max_count , True ) <EOL> def read_previous_tasks ( self , max_count ) : <EOL> """<STR_LIT>""" <EOL> return self . __read_tasks ( max_count , False ) <EOL> def __read_tasks ( self , max_count , next_page ) : <EOL> if not isinstance ( max_count , int ) : <EOL> raise VIException ( "<STR_LIT>" , <EOL> FaultTypes . PARAMETER_ERROR ) <EOL> if next_page : <EOL> request = VI . ReadNextTasksRequestMsg ( ) <EOL> else : <EOL> request = VI . ReadPreviousTasksRequestMsg ( ) <EOL> _this = request . new__this ( self . _mor ) <EOL> _this . set_attribute_type ( self . _mor . get_attribute_type ( ) ) <EOL> request . set_element__this ( _this ) <EOL> request . set_element_maxCount ( max_count ) <EOL> try : <EOL> if next_page : <EOL> resp = self . _server . _proxy . ReadNextTasks ( request ) . _returnval <EOL> else : <EOL> resp = self . _server . _proxy . ReadPreviousTasks ( request ) . _returnval <EOL> ret = [ ] <EOL> for task in resp : <EOL> ret . append ( VITask ( task . Task , self . _server ) ) <EOL> except ( VI . ZSI . FaultException ) , e : <EOL> raise VIApiException ( e ) <EOL> return ret </s>
