o
    6de                     @   sb  d dl mZmZmZmZ d dlZd dlmZmZm	Z	m
Z
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mZmZmZ d dlmZm Z m!Z!m"Z"m#Z# dZ$e$rZd dl%Z%d dl&Z&e&'e(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e(dd e+j,ddde(dd e+j,ddde(dd G d d! d!e!Z-dS )"    )absolute_importdivisionprint_functionunicode_literalsN)is_nullable_typeis_struct_typeis_union_typeis_void_typeunwrap_nullable)fmt_alloc_callfmt_camel_upper	fmt_classfmt_class_prefixfmt_funcfmt_func_argsfmt_func_args_declarationfmt_func_call
fmt_importfmt_property_strfmt_route_obj_classfmt_route_funcfmt_route_varfmt_routes_classfmt_signaturefmt_typefmt_var)base_file_commentcomment_prefixObjCBaseBackendstone_warningundocumentedFargparsezobjc-client-backendzGenerates a ObjC class with an object for each namespace, and in each namespace object, a method for each route. This class assumes that the obj_c_types backend was used with the same output directory.)progdescriptionz-mz--module-nameTzSThe name of the ObjC module to generate. Please exclude the {.h,.m} file extension.)requiredtypehelpz-cz--class-namez}The name of the ObjC class that contains an object for each namespace, and in each namespace object, a method for each route.z-tz--transport-client-namez:The name of the ObjC class that manages network API calls.z-wz--auth-typez(The auth type of the client to generate.)r%   r&   z-yz--client-argszFThe client-side route arguments to append to each route by style type.z-zz--style-to-requestz>The dict that maps a style type to a ObjC request object name.c                   @   s   e Zd ZdZeZi Zi Zdd Zdd Z	dd Z
dd	 Zd
d Zdd Zd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S )!ObjCBackendz<Generates ObjC client base that implements route interfaces.c              
   C   sp  |j  D ]}d| j|< |jr!|jD ]}| |r d| j|<  nqq|j  D ]}| D ]
}t|| j|j< q-q'|j  D ]}|jr| j| rt	|j| j
jt|jd| j
jddg}| dt	|j| j
j" | t || | |g  }| | | | W d    n1 sw   Y  | dt	|j| j
jI | t | d |   | td	 |   d
g}|| | j|ddd }| | | d| j
j |   | | W d    n1 sw   Y  q>| d| j
j | | W d    n	1 sw   Y  | d| j
j | | W d    d S 1 s1w   Y  d S )NFT
{}ProtocolZDBStoneBaseDBRequestErrorszRoutes/{}.mzRoutes/{}.h!#import <Foundation/Foundation.h>DBTasksZDBNilObject)Zinclude_route_argsZinclude_route_deep_args@protocol {};zClient/{}.mzClient/{}.h)
namespacesvaluesnamespace_to_has_routesroutes_should_generate_routeZlinearize_data_typesr   obj_name_to_namespacenamer   args	auth_typer   formattransport_client_nameZoutput_to_relative_pathemit_rawr   Z_get_imports_mZ_get_namespace_route_imports_generate_imports_m_generate_routes_mr   emitr   Z_get_imports_hZ_generate_imports_h_generate_routes_hmodule_name_generate_client_m_generate_client_h)selfapi	namespaceroute	data_typeimport_classesZimports_classes_mZimport_classes_h rF   rC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\stone/backends/obj_c_client.pygeneratef   s   









$zObjCBackend.generatec                    s^    t  jjg}| fdd|j D 7 }|d jj  	|  
 jjw tdd jjfg} jd|ddJ  d	   1  d
 |j D ]}|jrv j| rvd} |t|jt|j jj qXW d   n1 sw   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )zGenerates client base implementation file. For each namespace, the client will
        have an object field that encapsulates each route in the particular namespace.c                    ,   g | ]}|j r j| rt|j jjqS rF   r0   r/   r   r3   r4   r5   .0nsr@   rF   rG   
<listcomp>       z2ObjCBackend._generate_client_m.<locals>.<listcomp>r(   clientid<{}>initWithTransportClientinstancetypefuncr4   return_typeself = [super init];z_transportClient = client;z%_{}Routes = [[{} alloc] init:client];N)r8   r   r4   r=   r-   r.   appendr6   r7   r9   block_m
class_namer   
block_funcr;   
block_initr0   r/   r   r3   r   r5   )r@   rA   rE   client_argsrB   Zbase_stringrF   rN   rG   r>      sT   





"zObjCBackend._generate_client_mc           
         s    t  d  fdd|j D }|d |d  |     d     d jj	     t
 d} j|t
d	  t
  j jjd
d jj	fgdx    |j D ]9}|jr j| rdt|j} j|t
d	 dt|j}dt|j jj} t||d    qntdd jj	fg}d} j| jjt
d	 td|dd}	 d|	    W d   n1 sw   Y      d dS )zGenerates client base header file. For each namespace, the client will
        have an object field that encapsulates each route in the particular namespace.r*   c                    rI   rF   rJ   rK   rN   rF   rG   rO      rP   z2ObjCBackend._generate_client_h.<locals>.<listcomp>r)   r+   NS_ASSUME_NONNULL_BEGINr,   zBase client object that contains an instance field for each namespace, each of which contains references to all routes within that namespace. Fully-implemented API clients will inherit this class.prefixZtransportClientrR   )Z	protectedz!Routes within the `{}` namespace.z{}Routes{} *proptyprQ   z5Initializes the `{}` object with a networking client.rS   rT   rU   {};NNS_ASSUME_NONNULL_END)r8   r   r;   r-   r.   rY   r9   r6   r4   r7   r   emit_wrapped_textblock_hr[   r0   r/   r   r3   r   r5   r   r   r   )
r@   rA   rE   description_strrB   Z	class_docrd   re   r^   init_signaturerF   rN   rG   r?      sx   









"zObjCBackend._generate_client_hc                 C   s.   |j ddD ]}| |kr dS q	dS )Nauth,TF)attrsgetsplitstrip)r@   rC   Zdesired_auth_typer5   rF   rF   rG   _auth_type_in_route  s
   zObjCBackend._auth_type_in_routec                 C   s   |  |do| jjdkS )NZnoauthuser)rr   r4   r5   r@   rC   rF   rF   rG   _route_is_special_noauth_case  s   z)ObjCBackend._route_is_special_noauth_casec                 C   s   |  || jjp| |S )N)rr   r4   r5   ru   rt   rF   rF   rG   r1     s   z"ObjCBackend._should_generate_routec                 C   s  |  t|j| jj tdd| jjfg}| jd|dd& | 	d | 
  | 	d W d   n1 s:w   Y  W d   n1 sIw   Y  | 	  t| jj}|jD ]}| |sdq\|jd	}t| jj}|| v r|| D ]H}t|\}}	|| }
|	d
 }dd |	d D }t|jr| |jr| ||\}}| |||||
| | ||\}}| |||||
| q{q\|| }
t|jr| |jr| ||\}}| |||g |
d | ||\}}| |||g |
d q\W d   dS 1 sw   Y  dS )zoGenerates implementation file for namespace object that has as methods
        all routes within the namespace.rQ   rR   initrT   rU   rX   z_client = client;Nstyler   c                 S      g | ]
}t |d d qS NtuplerL   Z	type_datarF   rF   rG   rO   :      z2ObjCBackend._generate_routes_m.<locals>.<listcomp>    )rZ   r   r3   r4   r5   r   r6   r7   r\   r;   r]   jsonloadsstyle_to_requestr0   r1   rn   ro   r^   keysr|   r   arg_data_type_struct_has_defaults_get_default_route_args_generate_route_m_get_route_args)r@   rB   Z	init_argsr   rC   
route_typer^   	args_datatask_type_keytype_data_dicttask_type_namefunc_suffix
extra_args
route_args_rF   rF   rG   r:     s|   









$zObjCBackend._generate_routes_mc              	   C   sp  t |}ddt|jsdndfg}|D ]\}	}
}||	|f ||	|
f q| jdt||t|d|dp | dt	|j
t|j
| t|jra| dt|jt|jj
 n(t|jsttt|jd	| |td
d |D d}| dt|j| tddt|jdt|d}| d| W d   n1 sw   Y  |   dS )z:Generates route method implementation for the given route.)rC   rC   argnil{}{}rb   rU   zDBRoute *route = {}.{};z{} *arg = {};)callerc                 S   s   g | ]
}|d  |d  fqS )r   rF   rL   frF   rF   rG   rO   v  s    z1ObjCBackend._generate_route_m.<locals>.<listcomp>)r   Zcalleer4   zself.clientz	request{}rw   z
return {};N)listr	   r   rY   r\   r6   r   r   r;   r   r3   r   r   r   r   r   r   Z_cstor_name_from_fields_namesr   r   rn   ro   )r@   rC   rB   r   r   r   r   Z	user_argsZtransport_argsr3   valuere   Z	init_callZrequest_callrF   rF   rG   r   X  sT   




zObjCBackend._generate_route_mc                 C   s  |  t | jdt|jtd |  t |    |  d |    | t|j| jj	 d}| j|td |  t
dd| jjd |    tdd| jjfg}td|d	d
}d}| j|t|j| jj	td |  d| |    t| jj}|jD ]}| |sq|jd}t| jj}|| v r|| D ][}	t|	\}
}||
 }|d }dd |d D }dd |d D }t|jr| |jr| j||dd\}}| |||||| || | j||dd\}}| |||||| || qq|| }t|jr*| |jr*| j||dd\}}| |||g ||d | j||dd\}}| |||g ||d qW d   n	1 sKw   Y  |    |  d |    dS )zgGenerates header file for namespace object that has as methods
        all routes within the namespace.zRoutes for the `{}` namespacer`   r_   zRAn instance of the networking client that each route will use to submit a request.rQ   rR   rc   rv   rT   rU   zIInitializes the `{}` namespace container object with a networking client.rf   rw   r   c                 S   rx   ry   r{   r}   rF   rF   rG   rO     r~   z2ObjCBackend._generate_routes_h.<locals>.<listcomp>r   c                 S   s   g | ]
}|d  |d fqS )r   rz   rF   r}   rF   rF   rG   rO     s    Ttagr   Nrg   )r;   r   rh   r6   r   r3   ri   r   r4   r5   r   r7   r   r   r   r   r   r0   r1   rn   ro   r^   r   r|   r   r   r   r   _generate_route_signaturer   )r@   rB   rj   Zroutes_obj_argsrk   r   rC   r   r^   r   r   r   r   r   r   Z
extra_docsr   doc_listrF   rF   rG   r<     s   













R
zObjCBackend._generate_routes_hc                 C   s~  |D ]\}}	}
| ||
f q|jrdnd}dt||}| t |jr/| |j| j}nd|}| j	|| tdd | t |D ]\}}| j	d||rR|nt
tdd qE| t d}|t|jd	d
dt|jd	d
d}| j	|tdd | t t|jst|jnd}t|jst|jnd}d|||}| |}t|t|d|d}| d|| |   dS )z5Generates route method signature for the given route.zDEPRECATED: r   r   zThe {} routex   )ra   widthz@param {} {}zt@return Through the response callback, the caller will receive a `{}` object on success or a `{}` object on failure.FT)r   Zno_ptrzDBNilObject *z{}<{}, {}> *z{}rU   z{}{};N)rY   
deprecatedr6   r   r;   r   docprocess_doc_docfrh   r    r   Zresult_data_typeZerror_data_typer	   _get_deprecation_warningr   r   )r@   rC   rB   r   r   r   r   r   r3   r   re   r   	func_nameZ	route_docr   outputZresult_type_strZerror_type_strrW   Zroute_signaturerF   rF   rG   r     s`   







z%ObjCBackend._generate_route_signaturec                 C   sF   d}|j r!dt|}|j jr|dt|j jj7 }d|}|S )z<Returns a deprecation tag / message, if route is deprecated.r   z{} is deprecated.z Use {}.z __deprecated_msg("{}"))r   r6   r   Zbyr   r3   )r@   rC   resultmsgrF   rF   rG   r     s   
z$ObjCBackend._get_deprecation_warningFc           	   	      s   t |j\}}t|r2g }|jD ]}|t|jt|j||j	df q fdd|j
D }||fS t|r`t|jt|j|dfg}t|j|jrR |j jndt|jfg}||fS g }g }||fS )zfReturns a list of name / value string pairs representing the arguments for
        a particular route.)r   has_defaultc                    s,   g | ]}|j rt|j |j  jfqS rF   )r   r   r3   r   r   r   rN   rF   rG   rO   1  s     z/ObjCBackend._get_route_args.<locals>.<listcomp>r   zThe {} union)r
   r   r   
all_fieldsrY   r   r3   r   rD   r   fieldsr   r   r   r   r6   r   	r@   rB   rC   r   rD   r   arg_listfieldr   rF   rN   rG   r   '  s<   






zObjCBackend._get_route_argsc           	         s   t |j\}}t|r8g }|jD ]}|js)t|js)|t|j	t
|j|df q fdd|jD }||fS g }g }||fS )znReturns a list of name / value string pairs representing the default arguments for
        a particular route.r   c                    s<   g | ]}|j r|jst|jst|j |j  jfqS rF   )r   r   r   rD   r   r3   r   r   r   rN   rF   rG   rO   S  s     z7ObjCBackend._get_default_route_args.<locals>.<listcomp>)r
   r   r   r   r   r   rD   rY   r   r3   r   r   r   rF   rN   rG   r   C  s$   


z#ObjCBackend._get_default_route_argsc                 C   sb   |dkrd t|S |dkr)d|v r%|d\}}d t|| j| S t|S |dv r/|S |S )NrC   z`{}`r   .z`{}` in `{}`)r%   vallink)r6   r   rp   r   r2   )r@   r   r   cls_namer   rF   rF   rG   r   ]  s   zObjCBackend._docfN)F)__name__
__module____qualname____doc___cmdline_parserZcmdline_parserr2   r/   rH   r>   r?   rr   ru   r1   r:   r   r<   r   r   r   r   r   rF   rF   rF   rG   r'   _   s(    C$C;)c9

 
r'   ).
__future__r   r   r   r   r   Zstone.irr   r   r   r	   r
   Zstone.backends.obj_c_helpersr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zstone.backends.obj_cr   r   r   r   r    Z_MYPYtyping	importlibimport_modulestrr!   ArgumentParserr   add_argumentr'   rF   rF   rF   rG   <module>   sp    L