
    ['                         d Z ddlmZmZ  G d de          Zd ZdaddZ	 	 	 	 dd	Z		 	 	 	 	 dd
Z
d Zd Zd Zd Z G d de          ZdS )z
Task functions

This module provies synchronization of client/server operations since
many VIM operations return 'tasks' which can have varying completion
times.
    )vmodlvimc                       e Zd ZdZdS )TaskBlockedzk
    Exception class to represent when task is blocked (e.g.: waiting
    for an answer to a question.
    N)__name__
__module____qualname____doc__     ,/usr/lib/python3/dist-packages/pyVim/task.pyr   r   &   s          	Dr   r   c           
      
   t          | j        j        t                    rc| j        }t          |t                    sd|j        |j        fz  }t          d|j        j        j        d|j        d|j	        d|           d S d S )Nz	%d%% (%s)Task z (key:z, desc:z) - )

isinstanceinfoprogressintstrstateprintnamekeydescription)taskr   r   s      r   TaskUpdatesVerboser   2   s    $)$c** Hy(C(( 	A"dmTZ%@@HIN4+;+;+;XXG 	H 	H 	H 	H 	H	H Hr   NTc                      | r	t           ad S d ad S N)r   globalTaskUpdate)verboses    r   SetTasksVerboser    >   s"      -r   c                 v   |t          j        d| j                  }||j        j        }t          | |          }|                    d           t          ||           }d\  }}|t           j        j	        j
        t           j        j	        j        fvr	 t          | ||          \  }}|                                 n8# t          j        j        $ r!}	t#          d|	j        z             Y d}	~	n6d}	~	ww xY w|t           j        j	        j
        t           j        j	        j        fv|                                 |dk    rg|                    dt)          | j        j                  z             |r| j        j        t#          dt)          | j        j                  z              n|                    d	           |S )
a  
    Wait for task to complete.

    @type  raiseOnError      : bool
    @param raiseOnError      : Any exception thrown is thrown up to the caller
                               if raiseOnError is set to true.
    @type  si                : ManagedObjectReference to a ServiceInstance.
    @param si                : ServiceInstance to use. If None, use the
                               information from the task.
    @type  pc                : ManagedObjectReference to a PropertyCollector.
    @param pc                : Property collector to use. If None, get it from
                               the ServiceInstance.
    @type  onProgressUpdate  : callable
    @param onProgressUpdate  : Callable to call with task progress updates.

        For example::

            def OnTaskProgressUpdate(task, percentDone):
                print 'Task %s is %d%% complete.' % (task, percentDone)
    NServiceInstancecreatedNNz Task object has been deleted: %serror	error: %szTask reported error: 	completed)r   r"   _stubcontentpropertyCollectorProgressUpdaterUpdateCreateFilterTaskInfoStatesuccessr%   GetTaskStatusUpdateIfNeededr   faultManagedObjectNotFoundr   objDestroyr   r   )
r   raiseOnErrorsipconProgressUpdateprogressUpdaterfilterversionr   es
             r   WaitForTaskr?   X   s   4 
z !2DJ??	zZ)%d,<==O9%%%"d##FNGU
*2CL4F4LM
M
M	*4"==NGU**,,,,{0 	 	 	4qu<===EEEE		 *2CL4F4LM
M
M NN{S-A-AABBB 	B)/!)C	,@,@@AAAA{+++Ls   (B= =C2C--C2c                    | sdS | t          j        d| d         j                  }||j        j        }|g }i }| D ]9}t          ||          }|                    d           ||t          |          <   :t          ||           }		 d\  }
}t          |          r|
                    |
          }|j        D ]w}|j        D ]k}|j        }t          |          }|j        D ]I}|j        dk    r|j        j        }n|j        dk    r|j        }n/|                    |          }|sG|t           j        j        j        k    rJ|                    d           |                    |           |                    |j        j                   |t           j        j        j        k    rn|j        j        }|                    d	t          |          z             |r|t7          d
|dt          |                     |                    |           3|r|                                 Kmy|j        }
t          |          |	r|	                                 n# |	r|	                                 w w xY wdS )z`
    Wait for mulitiple tasks to complete. Much faster than calling WaitForTask
    N times
    Nr"   r   r#   r$   r   z
info.stater'   r&   r   z reported error: )r   r"   r(   r)   r*   r+   r,   r   CreateTasksFilterlenWaitForUpdates	filterSet	objectSetr5   	changeSetr   valr   getr.   r/   r0   popappendr   resultr%   r   r2   r=   r6   )tasksr7   r8   r9   r:   resultsprogressUpdatersr   r;   r<   r=   r   updaterD   objSettaskIdchangeerrs                     r   WaitForTasksrT      s     	z !2E!HNCC	zZ) 6 6)$0@AAy)))&5T##r5))F+# "## $	%&&w//F#-  A  A	'1 A AF!:D YYF"("2 A A!;&00$*J$4EE#[L88$*JEE$*:*>*>v*F*F. %$ CL$6$>>>+22;???,00888 $NN49+;<<<<"cl&8&>>>"&)/C+22;S3IJJJ+ =&)	 %fffcRUhhh&W X X X 0 4 4V < < < </ A / > > @ @ @9AAB nGI "## $	%L  	NN  	NN	
Fs   GI   I9c                     |                     |          }| j        j        }|dk    rO| j        j        C| j        j        j        j        dk    r)| j        j        j        j        dk    rt	          |            |j        |fS )Nrunningr6   Relocate)rC   r   r   r   CheckForQuestionPendingr=   )r   r=   r9   rO   r   s        r   r1   r1      sw    w''FIOEty~9din>Q>VZc>c>cIN$
22%%%>5  r   c                 $    t          | |g          S )z+ Create property collector filter for task )rA   )r9   r   s     r   r-   r-      s    R$(((r   c                    |sdS d |D             }t           j        j                            t          j        g d          }t           j        j                                        }||_        |g|_        | 	                    |d          S )z, Create property collector filter for tasks Nc                 X    g | ]'}t           j        j                            |           (S ))r5   )r   queryPropertyCollector
ObjectSpec).0r   s     r   
<listcomp>z%CreateTasksFilter.<locals>.<listcomp>   s>     # # # -88T8BB # # #r   T)typepathSetall)
r   r\   r]   PropertySpecr   Task
FilterSpecrE   propSetr-   )r9   rL   objspecspropspec
filterspecs        r   rA   rA      s     t# #!# # #H {,99Xrt : - -H .99;;J#J"J ??:t,,,r   c                     | j         j        }|7t          |t          j                  r|j        j        }|t          d          dS dS dS )zE
    Check to see if VM needs to ask a question, throw exception
    Nz(Task blocked, User Intervention required)r   entityr   r   VirtualMachineruntimequestionr   )r   vmqsts      r   rX   rX     sR    
 
	B	~*R);<<~j!?HIII ~~~?r   c                   $    e Zd ZdZd Zd Zd ZdS )r+   zo
    Class that keeps track of task percentage complete and calls a
    provided callback when it changes.
    c                 >    || _         || _        d| _        d| _        d S )Nr   )r   r:   prevProgressr   )selfr   r:   s      r   __init__zProgressUpdater.__init__#  s$    	 0r   c                 Z    t           }| j        r| j        }|r || j        |           d S d S r   )r   r:   r   )ru   r   
taskUpdates      r   r,   zProgressUpdater.Update)  sF    %
  	/.J 	)Jty%(((((	) 	)r   c                     | j         j        j        | _        | j        | j        k    r|                     | j                   | j        | _        d S r   )r   r   r   rt   r,   )ru   s    r   r2   zProgressUpdater.UpdateIfNeeded1  sD    	/=D---KK&&& Mr   N)r   r   r	   r
   rv   r,   r2   r   r   r   r+   r+     sK         
  ) ) )* * * * *r   r+   )T)TNNN)TNNNN)r
   pyVmomir   r   	Exceptionr   r   r   r    r?   rT   r1   r-   rA   rX   objectr+   r   r   r   <module>r}      sA  .         	 	 	 	 	) 	 	 	H H H         6 "!%	9 9 9 9H #"&I I I IX! ! !) ) )
- - -,	J 	J 	J * * * * *f * * * * *r   