3
6ßL]Gh  ã               @   s$  d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ ddlmZmZmZ ddlmZ eeƒG d	d
„ d
e	ƒƒZG dd„ deƒZG dd„ de	ƒZ G dd„ de ƒZ!G dd„ de ƒZ"G dd„ de ƒZ#G dd„ deƒZ$G dd„ de	ƒZ%dS )é    N)ÚAnyÚBoolÚCallableÚDictÚEventÚ	HasTraitsÚ
implementsÚInstanceÚIntÚListÚPropertyÚStrÚUnicodeé   )Ú	BindEvent)Ú
IBindEvent)ÚIScriptManager)Úadd_to_namespaceÚFactoryWrapperÚLazyNamespace)Úmake_object_scriptablec               @   s    e Zd ZdZedƒZeZeZ	dS )Ú_BoundObjectz:The base class for any object that can be bound to a name.TN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úexplicitly_boundr   Únamer   Úobj© r   r   úJ/tmp/pip-install-5sebvq2j/apptools/apptools/appscripting/script_manager.pyr   !   s   r   c               @   s0   e Zd ZdZeZeZeZ	e
ZeZeZdd„ ZdS )Ú_ScriptObjectz9The _ScriptObject class encapsulates a scriptable object.c             C   s   | j ƒ S )zThe property getter.)Úobj_ref)Úselfr   r   r    Ú_get_objR   s    z_ScriptObject._get_objN)r   r   r   r   r   r   r   Úargsr   Úkwargsr
   Úobj_idr   r"   Úscripted_typer$   r   r   r   r    r!   1   s   r!   c               @   s    e Zd ZdZeZeZdd„ ZdS )Ú_ScriptCallz? The _ScriptCall class is the base class for all script calls. c             C   s   t ‚dS )zv Return the string equivalent of the call, updated the list of
        needed scriptable objects if required.
        N)ÚNotImplementedError)r#   ÚsmÚ	so_neededr   r   r    Úas_strg   s    z_ScriptCall.as_strN)	r   r   r   r   r   r   r   Úsor-   r   r   r   r    r)   X   s   r)   c               @   s$   e Zd ZdZedƒZeZdd„ ZdS )Ú_ScriptTraitGetz` The _ScriptTraitGet class encapsulates a single call to the get of a
    scriptable trait.
    Fc             C   sx   | j jsdS | jdkrd}n*|jt| jƒ \}}|dkrBd| }nd}| j rZ|dkrZdS |j| j |ƒ}d||| jf S )zv Return the string equivalent of the call, updated the list of
        needed scriptable objects if required.
        NÚ r   zr%d = z%s%s.%s)r.   r   ÚresultÚ_resultsÚidÚhas_side_effectsÚarg_as_string)r#   r+   r,   ÚrstrÚnrÚ_r.   r   r   r    r-   �   s    

z_ScriptTraitGet.as_strN)	r   r   r   r   r   r4   r   r1   r-   r   r   r   r    r/   o   s   r/   c               @   s   e Zd ZdZeZdd„ ZdS )Ú_ScriptTraitSetz` The _ScriptTraitSet class encapsulates a single call to the set of a
    scriptable trait.
    c             C   s8   | j jsdS |j| j |ƒ}|j| j|ƒ}d|| j|f S )zv Return the string equivalent of the call, updated the list of
        needed scriptable objects if required.
        Nz
%s.%s = %s)r.   r   r5   Úvalue)r#   r+   r,   r.   r:   r   r   r    r-   ¬   s
    z_ScriptTraitSet.as_strN)r   r   r   r   r   r:   r-   r   r   r   r    r9   ž   s   r9   c               @   s$   e Zd ZdZeZeZeZ	dd„ Z
dS )Ú_ScriptMethodzT The _ScriptMethod class encapsulates a single call to a scriptable
    method.
    c             C   s  | j r| j j rdS | jdkr$d}n¦t| jƒtf ƒkr g }d}xF| jD ]<}|jt|ƒ \}}|dkrx|jd| ƒ d}qF|jdƒ qFW |ršdj|ƒd	 }qÊd}n*|jt| jƒ \}}|dkrÆd
| }nd}| j rä|j| j |ƒd }	nd}	|j	| j
| j|ƒ}
d||	| jdj|
ƒf S )zv Return the string equivalent of the call, updated the list of
        needed scriptable objects if required.
        Nr0   Fr   zr%dTr8   z, z = zr%d = Ú.z
%s%s%s(%s))r.   r   r1   Útyper2   r3   ÚappendÚjoinr5   Úargs_as_string_listr%   r&   )r#   r+   r,   r6   ZrlistÚneededÚrr7   r8   r.   r%   r   r   r    r-   Ô   s2    

z_ScriptMethod.as_strN)r   r   r   r   r   r%   r   r&   r   r1   r-   r   r   r   r    r;   »   s
   r;   c               @   s,   e Zd ZdZeZeZeZ	eZ
eZdd„ ZdS )Ú_FactoryObjectzZ The _FactoryObject class wraps a factory that lazily creates
    scriptable objects.
    c             C   s   t | j| j| j| jd�S )zThe property getter.)ÚfactoryÚapiÚincludesÚexcludes)r   rD   rE   rF   rG   )r#   r   r   r    r$     s    
z_FactoryObject._get_objN)r   r   r   r   r   r   r   rE   r   rD   rF   rG   r$   r   r   r   r    rC     s   rC   c               @   s,  e Zd ZdZeeƒZedƒZe	e
ƒZeeƒZeeeƒƒZeZeZeZeZeZeZeZd6dd„Zd7dd„Zd	d
„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd„ Z#dd„ Z$d8dd„Z%e&d9dd„ƒZ'e&dd„ ƒZ(dd„ Z)d d!„ Z*d"d#„ Z+d$d%„ Z,d&d'„ Z-d(d)„ Z.e&d*d+„ ƒZ/d,d-„ Z0d.d/„ Z1d0d1„ Z2d2d3„ Z3d4d5„ Z4dS ):ÚScriptManagerzR The ScriptManager class is the default implementation of
    IScriptManager.
    FNÚuniquec             C   s(   | j ||j||d� t||||d� dS )a   Bind obj to name and make (by default) its public methods and
        traits (ie. those not beginning with an underscore) scriptable.  The
        default value of name is the type of obj with the first character
        forced to lower case.  name may be a dotted name (eg. 'abc.def.xyz').

        bind_policy determines what happens if the name is already bound.  If
        the policy is 'auto' then a numerical suffix will be added to the name,
        if necessary, to make it unique.  If the policy is 'unique' then an
        exception is raised.  If the policy is 'rebind' then the previous
        binding is discarded.  The default is 'unique'

        If api is given then it is a class, or a list of classes, that define
        the attributes that will be made scriptable.

        Otherwise if includes is given it is a list of names of attributes that
        will be made scriptable.

        Otherwise all the public attributes of scripted_type will be made
        scriptable except those in the excludes list.
        )r   Úbind_policy)rE   rF   rG   N)Ú
new_objectÚ	__class__r   )r#   r   r   rJ   rE   rF   rG   r   r   r    Úbindf  s    zScriptManager.bindc             C   s(   | j ||ƒ}t|||||d�| j|< dS )a&   Bind factory to name.  This does the same as the bind() method
        except that it uses a factory that will be called later on to create
        the object only if the object is needed.

        See the documentation for bind() for a description of the remaining
        arguments.
        )r   rD   rE   rF   rG   N)Ú_unique_namerC   Ú
_namespace)r#   rD   r   rJ   rE   rF   rG   r   r   r    Úbind_factory„  s    
zScriptManager.bind_factoryc             C   s@   t ƒ }x*| jjƒ D ]\}}|jrt|j||ƒ qW t||ƒ dS )zF Run the given script, either a string or a file-like object.
        N)r   rO   Úitemsr   r   r   Úexec)r#   ÚscriptZnspacer   Úbor   r   r    Úrun’  s
    zScriptManager.runc             C   s   t |ƒ}| j|ƒ |jƒ  dS )z$ Run the given script file.
        N)ÚopenrU   Úclose)r#   Ú	file_nameÚfr   r   r    Úrun_filež  s    
zScriptManager.run_filec             C   s"   g | _ d| _i | _d| _| | _dS )zÀ Start the recording of user actions.  The 'script' trait is cleared
        and all subsequent actions are added to 'script'.  The 'recording'
        trait is updated appropriately.
        r   TN)Ú_callsÚ_next_result_nrr2   Ú	recordingÚscript_updated)r#   r   r   r    Ústart_recording¦  s
    zScriptManager.start_recordingc             C   s
   d| _ dS )zf Stop the recording of user actions.  The 'recording' trait is
        updated appropriately.
        FN)r]   )r#   r   r   r    Ústop_recording³  s    zScriptManager.stop_recordingc             C   s@   | j r2| j|||ƒ}|||Ž}| j||ƒ | | _n
|||Ž}|S )zª Record the call of a method of a ScriptableObject instance and
        return the result.  This is intended to be used only by the scriptable
        decorator.
        )r]   Ú_new_methodÚ_add_methodr^   )r#   Úfuncr%   r&   Zsrecr1   r   r   r    Úrecord_method¾  s    

zScriptManager.record_methodc             C   s"   | j r| j|||ƒ}|r| | _dS )z… Record the get of a trait of a scriptable object.  This is intended
        to be used only by the Scriptable trait getter.
        N)r]   Ú_add_trait_getr^   )r#   r   r   r1   Úside_effectsr   r   r    Úrecord_trait_getÐ  s    zScriptManager.record_trait_getc             C   s   | j r| j|||ƒ | | _dS )z… Record the set of a trait of a scriptable object.  This is intended
        to be used only by the Scriptable trait getter.
        N)r]   Ú_add_trait_setr^   )r#   r   r   r:   r   r   r    Úrecord_trait_setÜ  s    zScriptManager.record_trait_setÚautoc                sÌ   |s"|j }|d jƒ |dd…  }ˆ j||ƒ}t|ƒ}tj|ˆ jƒ}t||||d�}	|dk	rœ‡ fdd„|D ƒ|	_x$|j	ƒ D ]\}
}ˆ j
|ƒ|	j|
< qzW d|	_|	ˆ j|< |	ˆ j|< |	ˆ j|< t||d�ˆ _dS )	z« Register a scriptable object and the arguments used to create it.
        If no arguments were provided then assume the object is being
        explicitly bound.
        r   r   N)r   r'   r"   r(   c                s   g | ]}ˆ j |ƒ‘qS r   )Ú_scriptable_object_as_string)Ú.0Úa)r#   r   r    ú
<listcomp>ÿ  s    z,ScriptManager.new_object.<locals>.<listcomp>F)r   r   )r   ÚlowerrN   r3   ÚweakrefÚrefÚ_gc_script_objr!   r%   rQ   rk   r&   r   Ú	_so_by_idÚ
_so_by_refrO   r   Ú
bind_event)r#   r   r(   r%   r&   r   rJ   r'   r"   r.   Únr:   r   )r#   r    rK   æ  s"    


zScriptManager.new_objectc             C   sl   |dkrg }g }x"| D ]}t j||ƒ}|j|ƒ qW x2|jƒ D ]&\}}t j||ƒ}|jd||f ƒ q>W |S )zÃ Return a complete argument list from sets of positional and keyword
        arguments.  Update the optional so_needed list for those arguments that
        refer to a scriptable object.
        Nz%s=%s)rH   r5   r>   rQ   )r%   r&   r,   Zall_argsÚargÚsr   r:   r   r   r    r@     s    
z!ScriptManager.args_as_string_listc             C   sJ   t | tƒr| ‚t | tƒrF| js.td| jƒ  ƒ‚| |kr@|j| ƒ | j} | S )zÇ Return the string representation of an argument.  Update the
        so_needed list if the argument refers to a scriptable object.  Any
        delayed conversion exception is handled here.
        z/%s has been unbound but is needed by the script)Ú
isinstanceÚ	Exceptionr!   r   Ú	NameErrorr"   r>   )rw   r,   r   r   r    r5   %  s    


zScriptManager.arg_as_stringc       	         st   ‡ fdd„|D ƒ}t |ƒtjkr&d}n|d }|dd… }i }x"|jƒ D ]\}}ˆ j|ƒ||< qHW t|j|||d�S )z‘ Return an object that encapsulates a call to a scriptable method.
        _add_method() must be called to add it to the current script.
        c                s   g | ]}ˆ j |ƒ‘qS r   )Ú_object_as_string)rl   rw   )r#   r   r    rn   G  s    z-ScriptManager._new_method.<locals>.<listcomp>Nr   r   )r   r.   r%   r&   )r=   ÚtypesÚFunctionTyperQ   r|   r;   r   )	r#   rc   r%   r&   Únargsr.   Únkwargsr   r:   r   )r#   r    ra   @  s    
zScriptManager._new_methodc             C   sZ   | j ƒ  |dk	rJt|ƒtf ƒkr:x"|D ]}| j|ƒ q&W n
| j|ƒ ||_| jj|ƒ dS )z{ Add a method call (returned by _new_method()), with it's associated
        result and ID, to the current script.
        N)Ú_start_scriptr=   Ú_save_resultr1   r[   r>   )r#   Úentryr1   rB   r   r   r    rb   V  s    

zScriptManager._add_methodc             C   sX   | j ƒ  |j|ƒj}|dkr d}| j|ƒ}|dk	r<| j|ƒ | jjt||||d�ƒ |S )z” Add a call to a trait getter, with it's associated result and ID,
        to the current script.  Return True if the get had side effects.
        NF)r.   r   r1   r4   )r�   Ztraitr4   r|   r‚   r[   r>   r/   )r#   r   r   r1   rf   r.   r   r   r    re   k  s    


zScriptManager._add_trait_getc             C   s6   | j ƒ  | j|ƒ}| j|ƒ}| jjt|||d�ƒ dS )zi Add a call to a trait setter, with it's associated value and ID,
        to the current script.
        )r.   r   r:   N)r�   r|   r[   r>   r9   )r#   r   r   r:   r.   r   r   r    rh   �  s    

zScriptManager._add_trait_setc             C   sr   | j j|ƒ}|dkr d| j|< nN|dkrN| j| }|d | j|< d||f }n |dkrb| j|ƒ ntd| ƒ‚|S )z] Return a name that is guaranteed to be unique according to the bind
        policy.
        Nr   rj   z%s%dZrebindz,"%s" is already bound to a scriptable object)rO   ÚgetÚ_namesÚ_unbindr{   )r#   r   rJ   rT   Úsuffr   r   r    rN   �  s    
zScriptManager._unique_namec             C   s.   t |tƒst|jdd�| _| j|j= d|_dS )zUnbind the given bound object.N)r   r   r0   )ry   rC   r   r   ru   rO   )r#   rT   r   r   r    r†   £  s    

zScriptManager._unbindc             C   sD   ddl m} |ƒ }|j|  }|jr,|j|ƒ |j|j= |j|j= dS )zM The callback invoked when a scriptable object is garbage collected.
        r   )Úget_script_managerN)Zpackage_globalsrˆ   rt   r   r†   rs   r'   r"   )r"   rˆ   r+   r.   r   r   r    rr   °  s    


zScriptManager._gc_script_objc             C   s$   t | jƒdkr tjjƒ jdƒ| _dS )z* Save when a script recording is started. r   z%cN)Úlenr[   ÚdatetimeÚnowÚstrftimeÚ_when_started)r#   r   r   r    r�   Á  s    zScriptManager._start_scriptc             C   s^   t |ƒ}| jj|dƒ\}}|dk	rT|dk rL| j}|  jd7  _||f| j|< d| S | j|ƒS )zª Convert an object to a string as it will appear in a script.  An
        exception may be returned (not raised) if there was an error in the
        conversion.
        Nr   r   zr%d)NN)r3   r2   r„   r\   rk   )r#   r   r'   r7   r8   r   r   r    r|   Ç  s    zScriptManager._object_as_stringc             C   sD   t |ƒ}| jj|ƒ}|dk	r |S t|ƒ}t|ƒ|kr8|S td| ƒS )zª Convert an object to a string as it will appear in a script.  An
        exception may be returned (not raised) if there was an error in the
        conversion.
        Nz.unable to create a script representation of %s)r3   rs   r„   ÚreprÚhexÚ
ValueError)r#   r   r'   r.   rx   r   r   r    rk   ß  s    z*ScriptManager._scriptable_object_as_stringc             C   s$   t |ƒ| jkr d|f| jt |ƒ< dS )zf Save the result of a call to a scriptable method so that it can be
        recognised later.
        r   Néÿÿÿÿ)r3   r2   )r#   r1   r   r   r    r‚   û  s    zScriptManager._save_resultc             C   s  t | jƒdkrdS d| j }g }g }x(| jD ]}|j| |ƒ}|r,|j|ƒ q,W dj|ƒ}g }g }xZ|D ]R}|jrrqf|j}	| j|j	|j
ƒ}
|jd|j|	jdj|
ƒf ƒ |	|krf|j|	ƒ qfW dj|ƒ}g }x"|D ]}	|jd|	j|	jf ƒ qÐW dj|ƒ}dj||||gƒd S )	z0 Convert the current list of calls to a script. r   r0   z# Script generated %sÚ
z%s = %s(%s)z, zfrom %s import %sz

)r‰   r[   r�   r-   r>   r?   r   r(   r@   r%   r&   r   r   r   )r#   Úheaderr,   ZcallsÚcallrx   Ztypes_neededZctorsr.   Zso_typer%   Zimportsr   r   r    Ú_get_script  s4    





zScriptManager._get_script)NrI   NNN)rI   NNN)NNNrj   )N)5r   r   r   r   r   r   ru   r   r]   r   r   rS   r   r^   r   r	   r)   r[   r   r…   rO   r
   r\   r2   rs   rt   r   r�   rM   rP   rU   rZ   r_   r`   rd   rg   ri   rK   Ústaticmethodr@   r5   ra   rb   re   rh   rN   r†   rr   r�   r|   rk   r‚   r•   r   r   r   r    rH   %  sR    
 

 
(rH   )&rŠ   r}   rp   Z
traits.apir   r   r   r   r   r   r   r	   r
   r   r   r   r   ru   r   Zi_bind_eventr   Zi_script_managerr   Zlazy_namespacer   r   r   Zscriptable_typer   Úprovidesr   r!   r)   r/   r9   r;   rC   rH   r   r   r   r    Ú<module>   s"   <'/G#