U
    P’“e(  ã                   @   s„   d Z dZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ d	d
„ Zdd„ ZG dd„ deƒZG dd„ deƒZdS )a  
Interactive launcher
====================

.. versionadded:: 1.3.0

.. deprecated:: 1.10.0
    The interactive launcher has been deprecated.

The :class:`InteractiveLauncher` provides a user-friendly python shell
interface to an :class:`App` so that it can be prototyped and debugged
interactively.

.. note::

    The Kivy API intends for some functions to only be run once or before the
    main EventLoop has started. Methods that can normally be called during the
    course of an application will work as intended, but specifically overriding
    methods such as :meth:`on_touch` dynamically leads to trouble.

Creating an InteractiveLauncher
-------------------------------

Take your existing subclass of :class:`App` (this can be production code) and
pass an instance to the :class:`InteractiveLauncher` constructor. ::

    from kivy.interactive import InteractiveLauncher
    from kivy.app import App
    from kivy.uix.button import Button

    class MyApp(App):
        def build(self):
            return Button(text='Hello Shell')

    launcher = InteractiveLauncher(MyApp())
    launcher.run()

After pressing *enter*, the script will return. This allows the interpreter to
continue running. Inspection or modification of the :class:`App` can be done
safely through the InteractiveLauncher instance or the provided
:class:`SafeMembrane` class instances.

.. note::

    If you want to test this example, start Python without any file to have
    already an interpreter, and copy/paste all the lines. You'll still have the
    interpreter at the end + the kivy application running.

Interactive Development
-----------------------

IPython provides a fast way to learn the Kivy API. The :class:`App` instance
and all of its attributes, including methods and the entire widget tree,
can be quickly listed by using the '.' operator and pressing 'tab'. Try this
code in an Ipython shell. ::

    from kivy.interactive import InteractiveLauncher
    from kivy.app import App
    from kivy.uix.widget import Widget
    from kivy.graphics import Color, Ellipse

    class MyPaintWidget(Widget):
        def on_touch_down(self, touch):
            with self.canvas:
                Color(1, 1, 0)
                d = 30.
                Ellipse(pos=(touch.x - d/2, touch.y - d/2), size=(d, d))


    class TestApp(App):
        def build(self):
            return Widget()


    i = InteractiveLauncher(TestApp())
    i.run()
    i.       # press 'tab' to list attributes of the app
    i.root.  # press 'tab' to list attributes of the root widget

    # App is boring. Attach a new widget!
    i.root.add_widget(MyPaintWidget())

    i.safeIn()
    # The application is now blocked.
    # Click on the screen several times.
    i.safeOut()
    # The clicks will show up now

    # Erase artwork and start over
    i.root.canvas.clear()

.. note::

    All of the proxies used in the module store their referent in the
    :attr:`_ref` attribute, which can be accessed directly if needed, such as
    for getting doc strings. :func:`help` and :func:`type` will access the
    proxy, not its referent.

Directly Pausing the Application
--------------------------------

Both the :class:`InteractiveLauncher` and :class:`SafeMembrane` hold internal
references to the :class:`EventLoop`'s 'safe' and 'confirmed'
:class:`threading.Event` objects. You can use their safing methods to control
the application manually.

:meth:`SafeMembrane.safeIn` will cause the application to pause and
:meth:`SafeMembrane.safeOut` will allow a paused application
to continue running. This is potentially useful for scripting actions into
functions that need the screen to update etc.

.. note::

    The pausing is implemented via the
    :class:`Clocks' <kivy.clock.Clock>`
    :meth:`~kivy.clock.ClockBase.schedule_once` method
    and occurs before the start of each frame.

Adding Attributes Dynamically
-----------------------------

.. note::

    This module uses threading and object proxies to encapsulate the running
    :class:`App`. Deadlocks and memory corruption can occur if making direct
    references inside the thread without going through the provided proxy(s).

The :class:`InteractiveLauncher` can have attributes added to it exactly like a
normal object and if these were created from outside the membrane, they will
not be threadsafe because the external references to them in the python
interpreter do not go through InteractiveLauncher's membrane behavior,
inherited from :class:`SafeMembrane`.

To threadsafe these external references, simply assign them to
:class:`SafeMembrane` instances of themselves like so::

    from kivy.interactive import SafeMembrane

    interactiveLauncher.attribute = myNewObject
    # myNewObject is unsafe
    myNewObject = SafeMembrane(myNewObject)
    # myNewObject is now safe. Call at will.
    myNewObject.method()

TODO
====

Unit tests, examples, and a better explanation of which methods are safe in a
running application would be nice. All three would be excellent.

Could be re-written with a context-manager style i.e. ::

    with safe:
        foo()

Any use cases besides compacting code?

)ÚSafeMembraneÚInteractiveLauncheré    N)ÚThreadÚEvent)ÚApp)Ú	EventLoop)ÚClock)Ú
deprecatedc                 C   s"   t j ¡  t j ¡  t j ¡  d S ©N)r   Ú	confirmedÚsetÚsafeÚwaitÚclear)Údt© r   ú4/tmp/pip-unpacked-wheel-xzebddm3/kivy/interactive.pyÚsafeWait«   s    

r   c                 C   s   t | ƒtkr| j} q | S r
   )Útyper   Ú_ref)Úobr   r   r   Úunwrap±   s    r   c                   @   sÄ   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
ejfdd„Zejfdd„Zejfdd„Zejfdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)S )*r   a`  
    This help is for a proxy object. Did you want help on the proxy's referent
    instead? Try using help(<instance>._ref)

    The SafeMembrane is a threadsafe proxy that also returns attributes as new
    thread-safe objects
    and makes thread-safe method calls, preventing thread-unsafe objects
    from leaking into the user's environment.
    )r   r   r   c                 O   s   t j| _t j| _|| _d S r
   )r   r   r   r   )Úselfr   ÚargsÚkwargsr   r   r   Ú__init__Ä   s    zSafeMembrane.__init__c                 C   s$   | j  ¡  t td¡ | j ¡  dS )z=Provides a thread-safe entry point for interactive launching.éÿÿÿÿN)r   r   r   Zschedule_oncer   r   r   ©r   r   r   r   ÚsafeInÉ   s    
zSafeMembrane.safeInc                 C   s   | j  ¡  dS )z<Provides a thread-safe exit point for interactive launching.N)r   r   r   r   r   r   ÚsafeOutÏ   s    zSafeMembrane.safeOutc                 C   s
   t  |¡S r
   )ÚinspectÚismethod)r   Úfnr   r   r   ÚisMethodÓ   s    zSafeMembrane.isMethodc                 C   s
   | j  ¡ S r
   )r   Ú__repr__r   r   r   r   r$   Û   s    zSafeMembrane.__repr__c                 O   s`   |   ¡  ttt|ƒƒ}t| ¡ ƒD ]}t|| ƒ||< q"| j||Ž}|  ¡  |d k	r\t|ƒS d S r
   )r   ÚlistÚmapr   Úkeysr   r   r   )r   r   ÚkwÚkÚrr   r   r   Ú__call__Þ   s    zSafeMembrane.__call__c                 C   s<   |  d¡s|dkr2|| dƒ}|dkr(|S t||ƒS || |ƒS )NÚ__r   )Ú
startswithÚgetattr)r   ÚattrÚogaÚsubjectr   r   r   Ú__getattribute__è   s    

zSafeMembrane.__getattribute__c                 C   s   t || dƒ|ƒ}t|ƒS )Nr   )r.   r   )r   r/   r0   r*   r   r   r   Ú__getattr__ð   s    zSafeMembrane.__getattr__c                 C   sX   |dks t t| ƒ|ƒr.| d¡s.|| ||ƒ n&|  ¡  t|ƒ}t| j||ƒ |  ¡  d S )Nr   r,   )Úhasattrr   r-   r   r   Úsetattrr   r   )r   r/   ÚvalZosar   r   r   Ú__setattr__ô   s    ÿÿzSafeMembrane.__setattr__c                 C   s    |   ¡  t| j|ƒ |  ¡  d S r
   )r   Údelattrr   r   )r   r/   Zodar   r   r   Ú__delattr__þ   s    zSafeMembrane.__delattr__c                 C   s
   t | jƒS r
   )Úboolr   r   r   r   r   Ú__bool__  s    zSafeMembrane.__bool__c                 C   s   t | j| ƒS r
   ©r   r   ©r   Úargr   r   r   Ú__getitem__  s    zSafeMembrane.__getitem__c                 C   s&   |   ¡  t|ƒ}|| j|< |  ¡  d S r
   ©r   r   r   r   )r   r>   r6   r   r   r   Ú__setitem__	  s    
zSafeMembrane.__setitem__c                 C   s   |   ¡  | j|= |  ¡  d S r
   ©r   r   r   r=   r   r   r   Ú__delitem__  s    zSafeMembrane.__delitem__c                 C   s   t | j||… ƒS r
   r<   ©r   ÚiÚjr   r   r   Ú__getslice__  s    zSafeMembrane.__getslice__c                 C   s*   |   ¡  t|ƒ}|| j||…< |  ¡  d S r
   r@   )r   rE   rF   r6   r   r   r   Ú__setslice__  s    zSafeMembrane.__setslice__c                 C   s    |   ¡  | j||…= |  ¡  d S r
   rB   rD   r   r   r   Ú__delslice__  s    zSafeMembrane.__delslice__c                 O   s   |   ¡  | jj||Ž d S r
   )r   r   Ú	__enter__©r   r   r   r   r   r   rJ   "  s    zSafeMembrane.__enter__c                 O   s   | j j||Ž |  ¡  d S r
   )r   Ú__exit__r   rK   r   r   r   rL   &  s    zSafeMembrane.__exit__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r   r#   r$   r+   Úobjectr2   r3   r7   r9   r;   r?   rA   rC   rG   rH   rI   rJ   rL   r   r   r   r   r   ·   s*   


r   c                   @   s:   e Zd ZdZdZeddd„ƒZdd„ Zdd	„ Zd
d„ Z	dS )r   z’
    Proxy to an application instance that launches it in a thread and
    then returns and acts as a proxy to the application in the thread.
    )r   r   r   ÚthreadÚappNc                 O   sb   |d krt ƒ }tƒ t_tj| _| j ¡  tƒ t_tj| _|| _|fdd„}t|d|i|—Ž| _d S )Nc                 _   s   | j ||Ž d S r
   )Úrun)rT   r   r   r   r   r   ÚstartApp>  s    z.InteractiveLauncher.__init__.<locals>.startAppÚtarget)	r   r   r   r   r   r   rT   r   rS   )r   rT   r   r   rV   r   r   r   r   3  s    
zInteractiveLauncher.__init__c                 C   s   | j  ¡  | j| _d S r
   )rS   ÚstartrT   r   r   r   r   r   rU   C  s    
zInteractiveLauncher.runc                 C   s   dt _| j ¡  d S )NT)r   ÚquitrS   Újoinr   r   r   r   ÚstopI  s    zInteractiveLauncher.stopc                 C   s
   | j  ¡ S r
   )rT   r$   r   r   r   r   r$   N  s    zInteractiveLauncher.__repr__)N)
rM   rN   rO   rP   rQ   r	   r   rU   r[   r$   r   r   r   r   r   +  s   r   )rP   Ú__all__r    Ú	threadingr   r   Zkivy.appr   Z	kivy.baser   Z
kivy.clockr   Z
kivy.utilsr	   r   r   rR   r   r   r   r   r   r   Ú<module>   s     t