B
    p[                 @   s   d dl Z ddl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mZ ddlmZ d	d
 Ze ZeG dd deeeZeG dd deZeG dd deZeG dd deZdS )    N   )
CoreWidget)	DOMWidget)ValueWidget)register)UnicodeCUnicodeBytesBool)bytes_serializationc               C   s   yt S  tk
r   tS X d S )N)Zunicode	NameErrorstr r   r   >lib/python3.7/site-packages/ipywidgets/widgets/widget_media.py
_text_type   s    r   c                   sr   e Zd ZdZeddjf ddieZedd Z	edd	 Z
d
d Zedd Zedd Z fddZ  ZS )_Mediaa+  Base class for Image, Audio and Video widgets.

    The `value` of this widget accepts a byte string.  The byte string is the
    raw data that you want the browser to display.

    If you pass `"url"` to the `"format"` trait, `value` will be interpreted
    as a URL as bytes encoded in UTF-8.
    z The media data as a byte string.)helpsyncTc             K   s@   |  |}d|kr.| ||}|dk	r.||d< | f d|i|S )aB  
        Create an :class:`Media` from a local file.

        Parameters
        ----------
        filename: str
            The location of a file to read into the value from disk.

        **kwargs:
            The keyword arguments for `Media`

        Returns an `Media` with the value set from the filename.
        formatNvalue)_load_file_value_guess_format)clstagfilenamekwargsr   r   r   r   r   
_from_file&   s    
z_Media._from_filec             K   s    t |tr|d}| |ddS )aq  
        Create an :class:`Media` from a URL.

        :code:`Media.from_url(url)` is equivalent to:

        .. code-block: python

            med = Media(value=url, format='url')

        But both unicode and bytes arguments are allowed for ``url``.

        Parameters
        ----------
        url: [str, bytes]
            The location of a URL to load.
        zutf-8url)r   r   )
isinstancer   encode)r   r   r   r   r   r   from_url>   s    

z_Media.from_urlc             C   s   |  |}|| _dS )z
        Convenience method for reading a file into `value`.

        Parameters
        ----------
        filename: str
            The location of a file to read into value from disk.
        N)r   r   )selfr   r   r   r   r   set_value_from_fileV   s    	
z_Media.set_value_from_filec          	   C   s6   t |dd d k	r| S t|d
}| S Q R X d S )Nreadrb)getattrr#   open)r   r   fr   r   r   r   c   s    z_Media._load_file_valuec             C   sf   t |dd }|p|}y8t|\}}|d|s8d S |td|d  S  tk
r`   d S X d S )Nnamez{}/)r%   	mimetypesZ
guess_type
startswithr   len	Exception)r   r   r   r(   Zmtype_r   r   r   r   k   s    z_Media._guess_formatc       
         s   | j j}g }t| j}|dd\}}|d d }t|dkrRd||dd }|dd|f  x@t|| 	 D ].}|dkrqtt
t| |}	|d	||	f  qtW d
|}d||f S )N'r   d   z	{}'{}...'r   z%s=%sr   z%s=%rz, z%s(%s))	__class____name__reprr   splitr+   r   appendsuperZ
_repr_keysr   r%   join)
r!   r   
class_nameZ	signatureZ	sig_valueprefixrestZcontentkeyr   )r1   r   r   	_get_reprz   s    

z_Media._get_repr)r2   
__module____qualname____doc__r	   r   r   r   classmethodr   r    r"   r   r   r<   __classcell__r   r   )r1   r   r      s   	r   c               @   s|   e Zd ZdZedjddZedjddZedddjddZe	d	djddZ
e	d
djddZedd Zdd ZdS )Imagea  Displays an image as a widget.

    The `value` of this widget accepts a byte string.  The byte string is the
    raw image data that you want the browser to display.  You can explicitly
    define the format of the byte string using the `format` trait (which
    defaults to "png").

    If you pass `"url"` to the `"format"` trait, `value` will be interpreted
    as a URL as bytes encoded in UTF-8.
    Z	ImageViewT)r   Z
ImageModelZpngzThe format of the image.)r   zWidth of the image in pixels.zHeight of the image in pixels.c             K   s   | j d|f|S )NZimage)r   )r   r   r   r   r   r   	from_file   s    zImage.from_filec             C   s
   |  tS )N)r<   rB   )r!   r   r   r   __repr__   s    zImage.__repr__N)r2   r=   r>   r?   r   r   
_view_name_model_namer   r   widthheightr@   rC   rD   r   r   r   r   rB      s   rB   c               @   s   e Zd ZdZedjddZedjddZedddjddZe	d	djddZ
e	d
djddZedddjddZedddjddZedddjddZedd Zdd ZdS )Videoa  Displays a video as a widget.

    The `value` of this widget accepts a byte string.  The byte string is the
    raw video data that you want the browser to display.  You can explicitly
    define the format of the byte string using the `format` trait (which
    defaults to "mp4").

    If you pass `"url"` to the `"format"` trait, `value` will be interpreted
    as a URL as bytes encoded in UTF-8.
    Z	VideoViewT)r   Z
VideoModelZmp4zThe format of the video.)r   zWidth of the video in pixels.zHeight of the video in pixels.z/When true, the video starts when it's displayedzBWhen true, the video will start from the beginning after finishingzSSpecifies that video controls should be displayed (such as a play/pause button etc)c             K   s   | j d|f|S )NZvideo)r   )r   r   r   r   r   r   rC      s    zVideo.from_filec             C   s
   |  tS )N)r<   rI   )r!   r   r   r   rD      s    zVideo.__repr__N)r2   r=   r>   r?   r   r   rE   rF   r   r   rG   rH   r
   autoplayloopcontrolsr@   rC   rD   r   r   r   r   rI      s   rI   c               @   s   e Zd ZdZedjddZedjddZedddjddZe	dd	djddZ
e	dd
djddZe	dddjddZedd Zdd ZdS )Audioa  Displays a audio as a widget.

    The `value` of this widget accepts a byte string.  The byte string is the
    raw audio data that you want the browser to display.  You can explicitly
    define the format of the byte string using the `format` trait (which
    defaults to "mp3").

    If you pass `"url"` to the `"format"` trait, `value` will be interpreted
    as a URL as bytes encoded in UTF-8.
    Z	AudioViewT)r   Z
AudioModelZmp3zThe format of the audio.)r   z/When true, the audio starts when it's displayedzBWhen true, the audio will start from the beginning after finishingzSSpecifies that audio controls should be displayed (such as a play/pause button etc)c             K   s   | j d|f|S )NZaudio)r   )r   r   r   r   r   r   rC      s    zAudio.from_filec             C   s
   |  tS )N)r<   rM   )r!   r   r   r   rD      s    zAudio.__repr__N)r2   r=   r>   r?   r   r   rE   rF   r   r
   rJ   rK   rL   r@   rC   rD   r   r   r   r   rM      s   rM   )r)   Zwidget_corer   Z	domwidgetr   Zvaluewidgetr   Zwidgetr   Z	traitletsr   r   r	   r
   Ztrait_typesr   r   r   rB   rI   rM   r   r   r   r   <module>   s    x