U
    Zf                     @   s  d dl mZmZmZmZ d dlmZ d dlZd dlm	Z	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mZmZmZmZmZmZmZmZmZmZ dZerd dlZed	ed
ededededededededediZ dddddddhZ!edd Z"dd Z#d:ddZ$d;ddZ%d d! Z&d"d# Z'd<d$d%Z(d&d' Z)d(d) Z*d*d+ Z+d,Z,d=d-d.Z-d/d0 Z.d1Z/e/j0d2d3Z1e/j0e,d3Z2d4d5 Z3d>d6d7Z4d?d8d9Z5dS )@    )absolute_importdivisionprint_functionunicode_literals)contextmanagerN)BackendCodeBackend)
fmt_pascalfmt_underscores)ApiNamespace)AnnotationTypeBooleanBytesFloat32Float64Int32Int64ListString	TimestampUInt32UInt64is_user_defined_typeis_aliasFboolbytesfloatintliststrdatetimebreakclasscontinueforpasswhileasyncc                 c   s0   | j }d V  | j }||kr,| d |   d S )Nr%   )linenoemit)ZcodegenZstarting_linenoZending_lineno r*   A/tmp/pip-unpacked-wheel-vqb3mfeu/stone/backends/python_helpers.pyemit_pass_if_nothing_emitted:   s    
r,   c                 C   s   | t kr| d S | S d S )N_)_reserved_keywords)sr*   r*   r+   _rename_if_reservedD   s    r0   c                 C   s   t | }|rt|S |S N)r	   r0   namecheck_reservedr/   r*   r*   r+   	fmt_classJ   s    r5      c                 C   s,   t | } |rt| } |dkr(d| |} | S )Nr6   z{}_v{})r
   r0   format)r3   r4   versionr*   r*   r+   fmt_funcN   s    r9   c                 C   s   t j| ddS )Nr6   )width)pprintpformat)or*   r*   r+   fmt_objV   s    r>   c                 C   s   t | jt| jS r1   )_type_tableget	__class__r5   r3   )	data_typer*   r*   r+   fmt_typeY   s    rC   c                 C   s   t | }|rt|S |S r1   )r
   r0   r2   r*   r*   r+   fmt_var\   s    rD   c                 C   s   d | |t|gS )N.)joinrD   )Zns_nameZdata_type_name
field_namer*   r*   r+   fmt_namespaced_var`   s    rH   c                 C   s   t | S r1   )r0   )r3   r*   r*   r+   fmt_namespacec   s    rI   c                 C   sL   i }| j D ]<}t|j|jd}||kr>|| }td|||||< q
dS )z
    Check name conflicts among generated route definitions. Raise a runtime exception when a
    conflict is encountered.
    )r8   z.There is a name conflict between {!r} and {!r}N)Zroutesr9   r3   r8   RuntimeErrorr7   )	namespaceZroute_by_nameZrouteZ
route_nameZother_router*   r*   r+   check_route_name_conflictf   s    

rL   z  # type: ignorec                 C   sR   |j dd}|sdS |rtnd}|D ] }| dj|t|j|d q$|   dS )z
    Both the true Python backend and the Python PEP 484 Type Stub backend have
    to perform the same imports.

    :param insert_type_ignore: add a MyPy type-ignore comment to the imports in
        the except: clause.
    T)Zconsider_annotation_typesN z;from {package} import {namespace_name}{type_ignore_comment})packageZnamespace_nametype_ignore_comment)Zget_imported_namespacesTYPE_IGNORE_COMMENTr)   r7   rI   r3   )backendrK   rN   Zinsert_type_ignoreZimported_namespacesrO   nsr*   r*   r+   *generate_imports_for_referenced_namespacesw   s    

rS   c                 C   s<   |  d |  d |  dd |  d |  d d S )Nz# -*- coding: utf-8 -*-z)# Auto-generated by Stone, do not modify.z# @{}	generatedz# flake8: noqaz# pylint: skip-file)r)   r7   )rQ   r*   r*   r+   generate_module_header   s
    


rU   zfrom stone.backends.python_rsrc import stone_base as bb{type_ignore_comment}
from stone.backends.python_rsrc import stone_validators as bv{type_ignore_comment}

rM   )rO   c                 C   s   ||kr| S d t|j| S )z
    Returns a name that can be used to reference `name` in namespace `name_ns`
    from `source_ns`.

    If `source_ns` and `name_ns` are the same, that's just `name`. Otherwise
    it's `name_ns`.`name`.
    z{}.{})r7   rI   r3   )r3   Zname_nsZ	source_nsr*   r*   r+   prefix_with_ns_if_necessary   s    	rV   c                 C   s@   t | s t| s tdt|  t| j}|r<t|| j|S |S )a>  
    Returns the name of the Python class that maps to a user-defined type.
    The name is identical to the name in the spec.

    If ``ns`` is set to a Namespace and the namespace of `data_type` does
    not match, then a namespace prefix is added to the returned name.
    For example, ``foreign_ns.TypeName``.
    zExpected composite type, got %r)r   r   AssertionErrortyper5   r3   rV   rK   )rB   rR   r3   r*   r*   r+   class_name_for_data_type   s    	

rY   c                 C   s.   t | tstt| j}|r*t|| j|S |S )zL
    Same as class_name_for_data_type, but works with annotation types.
    )
isinstancer   rW   r5   r3   rV   rK   )Zannotation_typerR   r3   r*   r*   r+   class_name_for_annotation_type   s
    
r[   )F)Fr6   )F)F)N)N)6
__future__r   r   r   r   
contextlibr   r;   Zstone.backendr   r   Zstone.backends.helpersr	   r
   Zstone.irr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z_MYPYtypingr?   r.   r,   r0   r5   r9   r>   rC   rD   rH   rI   rL   rP   rS   rU   Z_validators_import_templater7   Zvalidators_importZ"validators_import_with_type_ignorerV   rY   r[   r*   r*   r*   r+   <module>   s~   @           

	


 


