U
    /e                     @  s   d Z ddlmZ ddlZeeZddlZddlZddl	m
Z
 ddlmZ ddlmZmZ ddlmZ d	d
lmZmZmZmZ d	dlmZmZ dZG dd deZdS )zI Abstract base class for subcommands that output to a file (or stdout).

    )annotationsN)abstractmethodsplitext)ListUnion   )Document   )ArgArgsArgument
Subcommand)!build_single_handler_applicationsdie)FileOutputSubcommandc                   @  s   e Zd ZU dZded< edddddZedd	d
dZddddddZdddddZ	dddddddZ
dddddddZeddddddZdS )r   zG Abstract subcommand to output applications as some type of file.

    str	extensionr   )output_type_namereturnc                 C  s   dt ddd| ddfS )a   Returns a positional arg for ``files`` to specify file inputs to
        the command.

        Subclasses should include this to their class ``args``.

        Example:

            .. code-block:: python

                class Foo(FileOutputSubcommand):

                    args = (

                        FileOutputSubcommand.files_arg("FOO"),

                        # more args for Foo

                    ) + FileOutputSubcommand.other_args()

        fileszDIRECTORY-OR-SCRIPT+z1The app directories or scripts to generate %s forN)metavarnargshelpdefault)r   )clsr    r   I/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/command/subcommands/file_output.py	files_arg@   s    zFileOutputSubcommand.files_argr   )r   c                 C  s&   dt ddtddfdt ddd	d
ffS )a.   Return args for ``-o`` / ``--output`` to specify where output
        should be written, and for a ``--args`` to pass on any additional
        command line args to the subcommand.

        Subclasses should append these to their class ``args``.

        Example:

            .. code-block:: python

                class Foo(FileOutputSubcommand):

                    args = (

                        FileOutputSubcommand.files_arg("FOO"),

                        # more args for Foo

                    ) + FileOutputSubcommand.other_args()

        )z-oz--outputZFILENAMEappendz1Name of the output file or - for standard output.)r   actiontyper   z--argszCOMMAND-LINE-ARGSz...zMAny command line arguments remaining are passed on to the application handler)r   r   r   )r   r   )r   r   r   r   
other_args]   s    zFileOutputSubcommand.other_args)routeextr   c                 C  s&   |dkrd}n|dd }d||f S )


        /index   Nz%s.%sr   )selfr$   r%   baser   r   r   filename_from_route   s    z(FileOutputSubcommand.filename_from_routezargparse.NamespaceNone)argsr   c           	        s    fdd j D }t j |} jdkr0g }n
t j}t|t|krbtdt|t|f  | D ]D\}}| }t|dkr|d}n| 	|| j
}|  || qjdS )r&   c                   s   i | ]}| j qS r   r.   ).0fr/   r   r   
<dictcomp>   s      z/FileOutputSubcommand.invoke.<locals>.<dictcomp>NzC--output/-o was given too many times (%d times for %d applications)r   )r   r   outputlistlenr   itemsZcreate_documentpopr,   r   
write_file)	r*   r.   ZargvsZapplicationsoutputsr$   Zappdocfilenamer   r/   r   invoke   s    

zFileOutputSubcommand.invoker	   )r.   r;   r:   r   c           
        s   dddd fdd}dddd fdd}  }t|trV|| nt|trl|| nd	kst|d
krdddfdd}ndddfdd}t|D ]:\}}	t|	tr||	|| qt|	tr||	|| qdS )r&   r   r-   )contentr;   r   c              	     sH   |dkrt |  n$t|ddd}||  W 5 Q R X  | d S )N-wzutf-8)encoding)printopenwriteafter_write_file)r=   r;   filer.   r:   r*   r   r   	write_str   s
    
z2FileOutputSubcommand.write_file.<locals>.write_strbytesc              	     sJ   |dkrt jj|  n t|d}||  W 5 Q R X  | d S )Nr>   wb)sysstdoutbufferrC   rB   rD   )r=   r;   r1   rF   r   r   write_bytes   s
    z4FileOutputSubcommand.write_file.<locals>.write_bytesr>   r)   int)ir   c                   s    S )Nr   )rO   r;   r   r   indexed   s    z0FileOutputSubcommand.write_file.<locals>.indexedc                   s   t  \}}| d|  | S )N_r   )rO   rootr%   rP   r   r   rQ      s    N)file_contents
isinstancer   rH   r5   	enumerate)
r*   r.   r;   r:   rG   rM   contentsrQ   rO   r=   r   )r.   r:   r;   r*   r   r8      s    



zFileOutputSubcommand.write_filec                 C  s   dS )r&   Nr   )r*   r.   r;   r:   r   r   r   rD      s    z%FileOutputSubcommand.after_write_filez)Union[str, bytes, List[str], List[bytes]])r.   r:   r   c                 C  s
   t  dS )a    Subclasses must override this method to return the contents of the output file for the given doc.
        subclassed methods return different types:
        str: html, json
        bytes: SVG, png

        Raises:
            NotImplementedError

        N)NotImplementedError)r*   r.   r:   r   r   r   rT      s    z"FileOutputSubcommand.file_contentsN)__name__
__module____qualname____doc____annotations__classmethodr   r#   r,   r<   r8   rD   r   rT   r   r   r   r   r   8   s   
%*r   )r\   
__future__r   logging	getLoggerrY   logargparserJ   abcr   os.pathr   typingr   r   documentr	   
subcommandr   r   r   r   utilr   r   __all__r   r   r   r   r   <module>   s   
