B
    6d,              	   @   s$  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ ddlm	Z	 g Z
d*ddZd+dd	ZG d
d dZG dd deZejdkree G dd deZejdkre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ejdkr~edr.ee ed rBee ed!rVee ed"rjee ed#r~ee G d$d% d%eZyd d&lmZ W n ek
r   Y n
X ee ed'kr eejd(k re d) e!  e"ejd "Z#e ee#fejd(d   W dQ R X dS ),    N)quote)Image   )	deprecatec             C   sN   yt | tr|  } W n tk
r(   Y nX |dkr>t|  ntd|  dS )a$  
    The :py:func:`register` function is used to register additional viewers::

        from PIL import ImageShow
        ImageShow.register(MyViewer())  # MyViewer will be used as a last resort
        ImageShow.register(MySecondViewer(), 0)  # MySecondViewer will be prioritised
        ImageShow.register(ImageShow.XVViewer(), 0)  # XVViewer will be prioritised

    :param viewer: The viewer to be registered.
    :param order:
        Zero or a negative integer to prepend this viewer to the list,
        a positive integer to append it.
    r   N)
issubclassViewer	TypeError_viewersappendinsert)viewerorder r   1/tmp/pip-unpacked-wheel-lxgm_btu/PIL/ImageShow.pyregister   s    

r   c             K   s,   x&t D ]}|j| fd|i|rdS qW dS )a  
    Display a given image.

    :param image: An image object.
    :param title: Optional title. Not all viewers can display the title.
    :param \**options: Additional viewer options.
    :returns: ``True`` if a suitable viewer was found, ``False`` otherwise.
    titleTF)r	   show)imager   optionsr   r   r   r   r   4   s    	
r   c               @   sJ   e Zd ZdZdd ZdZi Zdd Zdd Zd	d
 Z	dd Z
dddZdS )r   zBase class for viewers.c             K   sL   |j dks>| jdkr|j dks>t|j }|j |kr>||}| j|f|S )z
        The main function for displaying an image.
        Converts the given image to the target format and displays it.
        )1ZRGBAPNG)zI;16ZLA)modeformatr   Zgetmodebaseconvert
show_image)selfr   r   baser   r   r   r   H   s    


zViewer.showNc             C   s   | j S )z3Return format name, or ``None`` to save as PGM/PPM.)r   )r   r   r   r   r   
get_format_   s    zViewer.get_formatc             K   s   t dS )zj
        Returns the command used to display the file.
        Not implemented in the base class.
        N)NotImplementedError)r   filer   r   r   r   get_commandc   s    zViewer.get_commandc             C   s   |j f d| |i| jS )z+Save to temporary file and return filename.r   )_dumpr   r   )r   r   r   r   r   
save_imagej   s    zViewer.save_imagec             K   s   | j | |f|S )zDisplay the given image.)	show_filer"   )r   r   r   r   r   r   r   n   s    zViewer.show_imagec             K   sL   |dkr4d|kr(t ddd |d}nd}t|t| j|f| dS )z
        Display given file.

        Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
        and will be removed in Pillow 10.0.0 (2023-07-01). ``path`` should be used
        instead.
        Nr   zThe 'file' argument
   z'path'z!Missing required argument: 'path'r   )r   popr   ossystemr    )r   pathr   msgr   r   r   r#   r   s    zViewer.show_file)N)__name__
__module____qualname____doc__r   r   r   r   r    r"   r   r#   r   r   r   r   r   C   s   r   c               @   s&   e Zd ZdZdZdddZdd ZdS )	WindowsViewerzNThe default viewer on Windows is the default system application for PNG files.r   r   T)compress_levelsave_allc             K   s   d| d| dS )Nzstart "Pillow" /WAIT "z)" && ping -n 4 127.0.0.1 >NUL && del /f ""r   )r   r   r   r   r   r   r       s    zWindowsViewer.get_commandN)r*   r+   r,   r-   r   r   r    r   r   r   r   r.      s   
r.   win32c               @   s0   e Zd ZdZdZdddZdd Zdd	d
ZdS )	MacViewerz2The default viewer on macOS using ``Preview.app``.r   r   T)r/   r0   c             K   s(   d}d| dt | dt | d}|S )Nzopen -a Preview.app( z; sleep 20; rm -f z)&)r   )r   r   r   commandr   r   r   r       s     zMacViewer.get_commandNc             K   sp   |dkr4d|kr(t ddd |d}nd}t|tddd	|g tjpTtd
}|rlt	|dd|g dS )z
        Display given file.

        Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
        and will be removed in Pillow 10.0.0 (2023-07-01). ``path`` should be used
        instead.
        Nr   zThe 'file' argumentr$   z'path'z!Missing required argument: 'path'openz-azPreview.apppython3z-cz<import os, sys, time; time.sleep(20); os.remove(sys.argv[1])r   )
r   r%   r   
subprocesscallsys
executableshutilwhichPopen)r   r(   r   r)   r<   r   r   r   r#      s    zMacViewer.show_file)N)r*   r+   r,   r-   r   r   r    r#   r   r   r   r   r3      s
   
r3   darwinc               @   s"   e Zd ZdZdddZdd ZdS )
UnixViewerr   r   T)r/   r0   c             K   s&   | j |f|d }d| dt| S )Nr   r4   r5   )get_command_exr   )r   r   r   r6   r   r   r   r       s    zUnixViewer.get_commandN)r*   r+   r,   r   r   r    r   r   r   r   rA      s   
rA   c               @   s"   e Zd ZdZdd ZdddZdS )	XDGViewerz3
    The freedesktop.org ``xdg-open`` command.
    c             K   s   d }}||fS )Nzxdg-openr   )r   r   r   r6   r<   r   r   r   rB      s    zXDGViewer.get_command_exNc             K   sF   |dkr4d|kr(t ddd |d}nd}t|td|g dS )	z
        Display given file.

        Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
        and will be removed in Pillow 10.0.0 (2023-07-01). ``path`` should be used
        instead.
        Nr   zThe 'file' argumentr$   z'path'z!Missing required argument: 'path'zxdg-openr   )r   r%   r   r9   r?   )r   r(   r   r)   r   r   r   r#      s    zXDGViewer.show_file)N)r*   r+   r,   r-   rB   r#   r   r   r   r   rC      s   rC   c               @   s$   e Zd ZdZdddZdddZdS )	DisplayViewerz`
    The ImageMagick ``display`` command.
    This viewer supports the ``title`` parameter.
    Nc             K   s&   d }}|r|dt | 7 }||fS )Ndisplayz -title )r   )r   r   r   r   r6   r<   r   r   r   rB      s    zDisplayViewer.get_command_exc             K   sl   |dkr4d|kr(t ddd |d}nd}t|dg}|d}|rT|d	|g7 }|| t| d
S )z
        Display given file.

        Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
        and ``path`` should be used instead.
        Nr   zThe 'file' argumentr$   z'path'z!Missing required argument: 'path'rE   r   z-titler   )r   r%   r   getr
   r9   r?   )r   r(   r   r)   argsr   r   r   r   r#      s    


zDisplayViewer.show_file)N)N)r*   r+   r,   r-   rB   r#   r   r   r   r   rD      s   
rD   c               @   s"   e Zd ZdZdd ZdddZdS )GmDisplayViewerz*The GraphicsMagick ``gm display`` command.c             K   s   d}d}||fS )Ngmz
gm displayr   )r   r   r   r<   r6   r   r   r   rB     s    zGmDisplayViewer.get_command_exNc             K   sH   |dkr4d|kr(t ddd |d}nd}t|tdd|g d	S )
z
        Display given file.

        Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
        and ``path`` should be used instead.
        Nr   zThe 'file' argumentr$   z'path'z!Missing required argument: 'path'rI   rE   r   )r   r%   r   r9   r?   )r   r(   r   r)   r   r   r   r#     s    zGmDisplayViewer.show_file)N)r*   r+   r,   r-   rB   r#   r   r   r   r   rH     s   rH   c               @   s"   e Zd ZdZdd ZdddZdS )	EogViewerz'The GNOME Image Viewer ``eog`` command.c             K   s   d}d}||fS )Neogzeog -nr   )r   r   r   r<   r6   r   r   r   rB   .  s    zEogViewer.get_command_exNc             K   sH   |dkr4d|kr(t ddd |d}nd}t|tdd|g d	S )
z
        Display given file.

        Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
        and ``path`` should be used instead.
        Nr   zThe 'file' argumentr$   z'path'z!Missing required argument: 'path'rK   z-nr   )r   r%   r   r9   r?   )r   r(   r   r)   r   r   r   r#   3  s    zEogViewer.show_file)N)r*   r+   r,   r-   rB   r#   r   r   r   r   rJ   +  s   rJ   c               @   s$   e Zd ZdZdddZdddZdS )	XVViewerzX
    The X Viewer ``xv`` command.
    This viewer supports the ``title`` parameter.
    Nc             K   s&   d }}|r|dt | 7 }||fS )Nxvz -name )r   )r   r   r   r   r6   r<   r   r   r   rB   K  s    zXVViewer.get_command_exc             K   sl   |dkr4d|kr(t ddd |d}nd}t|dg}|d}|rT|d	|g7 }|| t| d
S )z
        Display given file.

        Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
        and ``path`` should be used instead.
        Nr   zThe 'file' argumentr$   z'path'z!Missing required argument: 'path'rM   r   z-namer   )r   r%   r   rF   r
   r9   r?   )r   r(   r   r)   rG   r   r   r   r   r#   S  s    


zXVViewer.show_file)N)N)r*   r+   r,   r-   rB   r#   r   r   r   r   rL   E  s   
rL   )r2   r@   zxdg-openrE   rI   rK   rM   c               @   s   e Zd ZdZdd ZdS )IPythonViewerz!The viewer for IPython frontends.c             K   s   t | dS )Nr   )ipython_display)r   r   r   r   r   r   r   {  s    zIPythonViewer.show_imageN)r*   r+   r,   r-   r   r   r   r   r   rN   x  s   rN   )rE   __main__   z.Syntax: python3 ImageShow.py imagefile [title])r   )N)$r&   r=   r9   r;   shlexr   ZPILr   Z
_deprecater   r	   r   r   r   r.   platformr3   rA   rC   rD   rH   rJ   rL   r>   rN   ZIPython.displayrE   rO   ImportErrorr*   lenargvprintexitr7   Zimr   r   r   r   <module>   sZ   

E
*
	$&
