U
    ŻZßf_  ć                   @   sb  d dl mZmZmZmZ d dlZd dlmZ d dlm	Z	 d dl
mZmZmZmZmZmZmZ d dlmZ d dlmZmZmZmZmZmZmZmZ dZer d dlZd dlZe  e!d	”Z"d
Z#e $d”Z%dZ&e"j'dddZ(e(j)ddde!dd e(j)ddde!dd e(j)ddde!dd e(j)ddde!dd e(j)d d!e!d"d# e(j)d$d%d&e!g d'd( G d)d* d*eZ*dS )+é    )Śabsolute_importŚdivisionŚprint_functionŚunicode_literalsN)ŚCodeBackend)Śfmt_underscores)Ścheck_route_name_conflictŚ	fmt_classŚfmt_funcŚfmt_namespaceŚfmt_objŚfmt_typeŚfmt_var)Śclass_name_for_data_type)Śis_nullable_typeŚis_list_typeŚis_map_typeŚis_struct_typeŚ
is_tag_refŚis_union_typeŚis_user_defined_typeŚis_void_typeFŚargparsez# -*- coding: utf-8 -*-
# Auto-generated by Stone, do not modify.
# flake8: noqa
# pylint: skip-file

from abc import ABCMeta, abstractmethod
z:(?P<tag>[A-z]*):`(?P<val>.*?)`a+  If you do not consume the entire response body, then you must call close on the
response object, otherwise you will max out your available connections. We
recommend using the `contextlib.closing
<https://docs.python.org/2/library/contextlib.html#contextlib.closing>`_
context manager to ensure this.zpython-client-backendzŲGenerates a Python class with a method for each route. Extend the generated class and implement the abstract request() method. This class assumes that the python_types backend was used with the same output directory.)ŚprogŚdescriptionz-mz--module-nameTzQThe name of the Python module to generate. Please exclude the .py file extension.)ŚrequiredŚtypeŚhelpz-cz--class-namezBThe name of the Python class that contains each route as a method.z-tz--types-packagez6The output Python package of the python_types backend.z-ez--error-class-pathz.exceptions.ApiErrorz}The path to the class that's raised when a route returns an error. The class name is inserted into the doc for route methods.)Śdefaultr   r   z-wz--auth-typez(The auth type of the client to generate.)r   r   z-az--attribute-commentŚappendzļRoute attributes that the backend will have access to and presumably expose in generated code. Use ":all" to select all attributes defined in stone_cfg.Route. Attributes will be exposed in the documentation, as the client doesn't use them.)Śactionr   r   r   c                   @   sr   e Zd ZeZdZdd Zdd Zdd Zdd	 Z	dddZ
dddZdd ZdddZdd Zdd Zdd ZdS )ŚPythonClientBackendNc                 C   s  |   d| jj ”ś |  t” d}|j ” D ]0}|jD ]}|jr6|  	d” d} qTq6|r, q^q,|  	”  |  
|j ” ” |  	”  |  	”  |  	d| jj ” |  ” b |  	d” |  	”  |  	d” |  	d” |  ”  |  	d	” W 5 Q R X |  	”  |  |j ” ” W 5 Q R X W 5 Q R X d
S )zGenerates a module called "base".

        The module will contain a base class that will have a method for
        each route across all namespaces.
        z%s.pyFzimport warningsTzclass %s(object):z__metaclass__ = ABCMetaz@abstractmethodz:def request(self, route, namespace, arg, arg_binary=None):ŚpassN)Zoutput_to_relative_pathŚargsŚmodule_nameZemit_rawŚbaseŚ
namespacesŚvaluesŚroutesŚ
deprecatedŚemitŚ_generate_importsŚ
class_nameŚindentŚ_generate_route_methods)ŚselfŚapiZfound_deprecatedŚ	namespaceŚroute© r3   ś@/tmp/pip-unpacked-wheel-vqb3mfeu/stone/backends/python_client.pyŚgenerate~   s6    





’
zPythonClientBackend.generatec                 C   s0   |D ]&}|j r|  d | jjt|j”” qd S )Nzfrom {} import {})Ś
data_typesr*   Śformatr#   Śtypes_packager   Śname©r/   r&   r1   r3   r3   r4   r+   ”   s    z%PythonClientBackend._generate_importsc                 C   sH   d| _ |D ]8}|jr
|  d” |  d |j”” |  ”  |  |” q
dS )zvCreates methods for the routes in each namespace. All data types
        and routes are represented as Python classes.Nz,# ------------------------------------------z# Routes in {} namespace)Ścur_namespacer(   r*   r7   r9   Ś_generate_routesr:   r3   r3   r4   r.   §   s    
z+PythonClientBackend._generate_route_methodsc                 C   sō   || _ | jjdk	r,dd | jj d”D | _t| |jD ]“}| jdkrt|  ||” |j 	d”dkrī|  ||d” q:d}|jdk	r|j 	d”}|dkrq:d	d | d”D }| jD ]:}||kr²|  ||” |j 	d”dkrč|  ||d”  q:q²q:dS )
zV
        Generates Python methods that correspond to routes in the namespace.
        Nc                 S   s   g | ]}|  ”  ” qS r3   ©ŚstripŚlower)Ś.0Ś	auth_typer3   r3   r4   Ś
<listcomp>¼   s     z8PythonClientBackend._generate_routes.<locals>.<listcomp>ś,ŚstyleŚdownloadTŚauthc                 S   s   g | ]}|  ”  ” qS r3   r=   )r@   Śmoder3   r3   r4   rB   Ķ   s     )
r;   r#   rA   ŚsplitŚsupported_auth_typesr   r(   Ś_generate_route_helperŚattrsŚget)r/   r1   r2   Zroute_auth_attrZroute_auth_modesZbase_auth_typer3   r3   r4   r<   ²   s*    



z$PythonClientBackend._generate_routesFc                 C   s  |j }|j}|j d”dk}|j d”dk}|rV|s<td| j||||ddgd n|  ||||” |  ”  d}d}	d}
|rd	g}n
|rd
g}|r¤|s¤d}	t}
|jr¼|  	|j| j
”}nd}| j||||j|||	|
|jd	 |  |” t|rž|  d” nRt|r:| jdd |jD d t|jjt|j”d nt|sPtd| d t|jt|j|jd”d |j”dg}|r| d” n
| d” | j|ddd |rŚ|  d” t|rĪ|  d” n
|  d” n t|rš|  d” n
|  d” W 5 Q R X |  ”  dS ) a  Generate a Python method that corresponds to a route.

        :param namespace: Namespace that the route belongs to.
        :param stone.ir.ApiRoute route: IR node for the route.
        :param bool download_to_file: Whether a special version of the route
            that downloads the response body to a file should be generated.
            This can only be used for download-style routes.
        rD   ŚuploadrE   z;download_to_file can only be set for download-style routes.Z_to_fileŚdownload_path)Śmethod_name_suffixŚ
extra_argsN)ŚfŚbyteszContents to upload.)rN   Śstrz#Path on local machine to save file.z!:class:`requests.models.Response`)ŚoverviewŚextra_request_argsŚextra_return_argŚfooterrK   z
arg = Nonec                 S   s   g | ]
}|j qS r3   )r9   )r@   rQ   r3   r3   r4   rB     s     z>PythonClientBackend._generate_route_helper.<locals>.<listcomp>zarg = {}.{})ŚbeforezUnhandled request type %rz{}.{}©Śversionś'{}'ŚargrQ   ŚNonezr = self.requestF)Ścompactz,self._save_body_to_file(download_path, r[1])zreturn Nonezreturn r[0]zreturn r)Śarg_data_typeŚresult_data_typerK   rL   ŚAssertionErrorŚ_generate_route_method_declr-   ŚDOCSTRING_CLOSE_RESPONSEŚdocŚprocess_docŚ_docfŚ_generate_docstring_for_funcŚerror_data_typeŚ#_maybe_generate_deprecation_warningr   r*   r   Śgenerate_multiline_listŚ
all_fieldsr7   r   r1   r9   r	   r   r
   rZ   r   )r/   r1   r2   Zdownload_to_filer_   r`   Śrequest_binary_bodyZresponse_binary_bodyrU   rV   rW   Zfunc_docstringr#   r3   r3   r4   rJ   Õ   s    	ūż÷


žž
’’
ü



z*PythonClientBackend._generate_route_helperŚ c                 C   s  dg}|r||7 }|r |  d” t|r¢|jD ]p}t|jrP|  d |j”” q.|jrt|jrj|jj	}	nd}	d |j|  
|	|j””}
|  |
” q.|  |j” q.n(t|r¶|  d” nt|sŹtd| t|j| |jd}t|j}|  |d	 ||”d
” dS )z+Generates the method prototype for a route.r/   rQ   z{}=NoneNz{}={}r\   zUnhandled request type: %rrY   z	def {}_{}ś:)r   r   rk   r   Ś	data_typer7   r9   Zhas_defaultr   r1   Ś_generate_python_valuer   r   r   ra   r
   rZ   r   rj   )r/   r1   r2   r_   rl   rO   rP   r#   ŚfieldŚnsr\   Śmethod_nameZnamespace_namer3   r3   r4   rb   =  s8    




ž’
z/PythonClientBackend._generate_route_method_declc                 C   sR   |j rNd |j”}|j jr.|d |j jj”7 }d |”dg}| j|dddd d S )	Nz{} is deprecated.z Use {}.r[   ŚDeprecationWarningzwarnings.warn)ś(ś)F)rX   Śdelimr^   )r)   r7   r9   Zbyrj   )r/   r2   Śmsgr#   r3   r3   r4   ri   c  s    üz7PythonClientBackend._maybe_generate_deprecation_warningc
              
   C   sn  t |rg n|j}
g }| jjrZ|	rZ| jjD ].}||	kr*|	| dk	r*| d ||	| ”” q*|
sj|sj|sjdS |  d” |r|  |” |r²|r|  ”  |  d” |D ]}|  |d” q |s¼|
r0|sÄ|rĢ|  ”  |r|D ]D\}}}|rd |||”}| j|dd qÖ| jd ||”dd qÖt|rę|
D ]ø}|j	r¾t
|jr^d |j|  |j	| j””}n&d |  ||j”|j|  |j	| j””}| j|dd t
|jrą|  d	 |j|  ||j””” n"d	 |j|  ||j””}|  |” q*nJt|r0|j	r| jd
 |  |j	| j””dd |  d |  ||””” |s<|rP|sP|
sP|  ”  |rg }t |rp| d” n|  ||”}| |” | |” |  |d” n2t |r“|  d” n|  ||”}|  d |”” t |sH|jrH|  d | jj”” |  ”  | jj dd”d }|  d |”” |  ”  |  |  ||”” W 5 Q R X |r`|  ”  |  |” |  d” dS )a  
        Generates a docstring for a function or method.

        This function is versatile. It will create a docstring using all the
        data that is provided.

        :param arg_data_type: The data type describing the argument to the
            route. The data type should be a struct, and each field will be
            treated as an input parameter of the method.
        :param result_data_type: The data type of the route result.
        :param error_data_type: The data type of the route result in the case
            of an error.
        :param str overview: A description of the route that will be located
            at the top of the docstring.
        :param extra_request_args: [(field name, field type, field doc), ...]
            Describes any additional parameters for the method that aren't a
            field in arg_data_type.
        :param str extra_return_arg: Name of an additional return type that. If
            this is specified, it is assumed that the return of the function
            will be a tuple of return_data_type and extra_return-arg.
        :param str footer: Additional notes at the end of the docstring.
        Nz{}: {}z"""zRoute attributes:z    z:param {} {}: {})Zsubsequent_prefixz:param {}: {}z:type {}: {}z:param arg: {}z:type arg: {}r]   z:rtype: z:rtype: Nonez
:rtype: {}z:raises: :class:`{}`Ś.é   é’’’’z If this raises, {} will contain:)r   Śfieldsr#   Zattribute_commentr   r7   r*   Zemit_wrapped_textr   rd   r   ro   r9   re   rf   Ś_format_type_in_docr   rj   Zerror_class_pathŚrsplitr-   )r/   r1   r_   r`   rh   rT   rU   rV   rW   rK   r|   Zattrs_linesŚ	attributeŚar9   Zdata_type_namerd   Z	field_docrq   r#   ZrtypeZerror_class_namer3   r3   r4   rg   p  sÄ    



 ’’
ž
 ’ż ’žž
’ž
’
’




z0PythonClientBackend._generate_docstring_for_funcc                 C   s  |dkr4|}d|kr$| j jd | }d | jj|”S |dkrd|kr^| dd”\}}t|}nd}d|kr|d t||d”S d	 | j jt||d”S n||d
krŗ| dd”\}}d ||”S |dkrņ|dkrĪdS |dksŽ|dkrģd | 	” ”S |S n |dkrd |”S t
d| dS )z
        Callback used as the handler argument to process_docs(). This converts
        Babel doc references to Sphinx-friendly annotations.
        r   ry   z:class:`{}.{}`r2   rn   rz   z
:meth:`{}`rY   z:meth:`{}_{}`Ślinkś z
`{} <{}>`_ŚvalŚnullr]   ŚtrueŚfalsez``{}``rq   zUnknown doc ref tag %rN)r;   r9   r7   r#   r8   rH   Śintr
   r~   Ś
capitalizeŚRuntimeError)r/   Śtagr   Zfq_valrZ   Śanchorr   r3   r3   r4   rf     s8    
 
’

zPythonClientBackend._docfc                 C   s   t |rdS t|r,d | jj|jt|”S t|rHd |  ||j	””S t
|rdd |  ||j	””S t|rd |  ||j”|  ||j””S t|S dS )zo
        Returns a string that can be recognized by Sphinx as a type reference
        in a docstring.
        r]   z:class:`{}.{}.{}`zNullable[{}]zList[{}]zMap[{}, {}]N)r   r   r7   r#   r8   r9   r   r   r}   ro   r   r   Zkey_data_typeZvalue_data_type)r/   r1   ro   r3   r3   r4   r}   )  s,      ’’’žz'PythonClientBackend._format_type_in_docc                 C   s4   t |r(d t|jt|jt|j”S t|S d S )Nz{}.{}.{})	r   r7   r   r9   r   Zunion_data_typer   Ztag_namer   )r/   r1   Śvaluer3   r3   r4   rp   C  s    żz*PythonClientBackend._generate_python_value)F)rm   N)NNNNNNN)Ś__name__Ś
__module__Ś__qualname__Ś_cmdline_parserZcmdline_parserrI   r5   r+   r.   r<   rJ   rb   ri   rg   rf   r}   rp   r3   r3   r3   r4   r!   y   s.   ##
j   ž
&           ż
 $r!   )+Ś
__future__r   r   r   r   ŚreZstone.backendr   Zstone.backends.helpersr   Zstone.backends.python_helpersr   r	   r
   r   r   r   r   Zstone.backends.python_typesr   Zstone.irr   r   r   r   r   r   r   r   Z_MYPYŚtypingŚ	importlibŚimport_modulerS   r   r%   ŚcompileZdoc_sub_tag_rerc   ŚArgumentParserr   Śadd_argumentr!   r3   r3   r3   r4   Ś<module>   sz   $	(

żūūūś	üś