o
    wi(                     @   sd  U d dl Z d dlZd dlZd dlZd dlZd dlmZ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 d dlmZ d dlmZmZmZ d dlmZmZ d	d
lmZ ddlmZ d	dl m!Z! d	dl"m#Z#m$Z$m%Z% ddl"m&Z&m'Z' d	dl(m)Z) d	dl*m+Z+m,Z, ddl-m.Z.m/Z/m0Z0 d	dl1m2Z2 d	dlm3Z3 ddl4m5Z5m6Z6m7Z7 dZ8e9e:Z;ej<e=d< ee>j?j?Z@dZAdhZBeddeee>j?d edZCe2eCj1d< edg dZDeee!  e=d< G d d! d!eee. ZEG d"d# d#eEe, ZFG d$d% d%eEe+ ZGdd&d'ed(ee d)dfd*d+ZHdd&d'ed,eId-e
eIe	f d(ee d)df
d.d/ZJd,eId0eId)dfd1d2ZKdS )3    N)ABCabstractmethod)AnyDictListTypeGenericOptionalcast)Path)
ContextVar)override)EnvironmentStrictUndefinedFileSystemLoader)	BaseModelValidationError   )jsonrpc   )__version__)PartialModel)	copy_treeis_same_pathresolve_template_path)DEBUGDEBUG_GENERATOR)PartialTypeGeneratorError)
PythonDataDefaultData)
BaseModelTInheritsGenericget_args)quote)Manifest)
model_jsonmodel_parsecached_property)BASE_PACKAGE_DIRGenericGeneratorBaseGenerator	Generatorrender_templatecleanup_templatespartial_models_ctxlogz+prisma.generator.generator.GenericGeneratorzpartials.py.jinjaTZ	templates)Ztrim_blocksZlstrip_blocksloader	undefinedr#   r.   )defaultc                   @   sv   e Zd ZedefddZededdfddZeddd	Z	dd
dZ
dejddfddZedee fddZdS )r)   returnc                 C   s   dS )u   Get the metadata for this generator

        This is used by prisma to display the post-generate message e.g.

        ✔ Generated Prisma Client Python to ./.venv/lib/python3.9/site-packages/prisma
        N selfr4   r4   Q/home/app/Keep/.python/lib/python3.10/site-packages/prisma/generator/generator.pyget_manifest@   s   zGenericGenerator.get_manifestdataNc                 C   s   d S )Nr4   )r6   r9   r4   r4   r7   generateJ   s   zGenericGenerator.generatec                 C   s   |  }|   dS )z+Shorthand for calling BaseGenerator().run()N)run)cls	generatorr4   r4   r7   invokeM   s   zGenericGenerator.invokec              
   C   s   t jds
tdd}z	 t }|du rtd W dS t|}| 	| q t
yy } zE|du r7|dts;tr?t  t|trIt|}nt|trWdt   }nt  }tj|jd|i dd	}t| W Y d}~dS d}~ww )
zRun the generation loop

        This can only be called from a prisma generation, e.g.

        ```prisma
        generator client {
            provider = "python generator.py"
        }
        ```
        ZPRISMA_GENERATOR_INVOCATIONz<Attempted to invoke a generator outside of Prisma generationNTzPrisma invocation endingz>An exception ocurred while running the partial type generator
i )codemessager9   )iderror)osenvirongetRuntimeErrorr   readliner/   debugparse_on_request	Exceptionr   r   	traceback	print_exc
isinstancer   strr   
format_excstripZErrorResponserA   reply)r6   requestlineexcr@   responser4   r4   r7   r;   S   sB   





	zGenericGenerator.runrS   c                 C   s   d }|j dkrtj|jt|  dd}nC|j dkrP|jd u r#tdtr0t	dt
j|jdd t| j|j}trBt	d	t|dd | | tj|jd d}ntd
|j  t| d S )NZgetManifest)manifest)rA   resultr:   z-Prisma JSONRPC did not send data to generate.paramsr   )indentr9   z%JSON RPC received unexpected method: )methodr   ZSuccessResponserA   dictr8   rY   rF   r   _write_debug_datajsondumpsr&   
data_classr%   r:   rR   )r6   rS   rV   r9   r4   r4   r7   rJ      s(   



zGenericGenerator._on_requestc                 C   s   t t| j}t|tstdd}|jD ]}|jtkr|} nq|du r/td| dt dt	|}|s<td| |d }t
|tsPtdt d	| d
t tt |S )z2Return the BaseModel used to parse the Prisma DMMFz)Could not resolve generic type arguments.NzICould not find the GenericGenerator type;
This should never happen;
Does z inherit from z ?z/Could not resolve generic arguments from type: r   zBExpected first generic type argument argument to be a subclass of z	 but got z	 instead.)r
   object	__class__rN   r!   rF   __orig_bases__
__origin__r)   r"   
issubclassr   	TypeErrorr   r    )r6   r<   typbaseargsmodelr4   r4   r7   r`      s8   	



zGenericGenerator.data_class)r3   N)__name__
__module____qualname__r   r$   r8   r    r:   classmethodr>   r;   r   RequestrJ   r'   r   r`   r4   r4   r4   r7   r)   ?   s    	
7r)   c                   @   s   e Zd ZdS )r*   N)rk   rl   rm   r4   r4   r4   r7   r*      s    r*   c                   @   sN   e Zd ZedededdfddZedefddZed	eddfd
dZ	dS )r+   ri   kwargsr3   Nc                 O   s   t t dt d)Nz' cannot be subclassed, maybe you meant ?)rf   r+   r*   )r<   ri   rp   r4   r4   r7   __init_subclass__   s   zGenerator.__init_subclass__c                 C   s   t dt dtdgdS )NzPrisma Client Python (v)ZqueryEngine)nameZdefault_outputZrequires_engines)r$   r   r(   r5   r4   r4   r7   r8      s   
zGenerator.get_manifestr9   c                 C   s  |j j}t|j jj}| s|jddd tt|s t	t| |d }t|j
|s0||j | }z<t D ]}|drI|dsI|tv rJq9t||| q9|jr^td |j  t |d< tD ]}t||| qfW n
   t|td  td	 d S )
NTparentsexist_okzschema.prismaz	.py.jinja_zGenerating partial typesZpartial_modelsenvz(Finished generating Prisma Client Python)r=   configr   outputvalueexistsmkdirr   r(   r   Zschema_path
write_textZ	datamodelZ	to_paramsDEFAULT_ENVlist_templatesendswith
startswithDEFERRED_TEMPLATESr,   Zpartial_type_generatorr/   rH   r;   r.   rE   r-   )r6   r9   r{   rootdirZpackaged_schemarY   rt   r4   r4   r7   r:      s4   



zGenerator.generate)
rk   rl   rm   r   r   rr   r$   r8   r   r:   r4   r4   r4   r7   r+      s    	r+   ry   r   rz   r3   c                C   sF   |du rt }| D ]}t| |d}| r td| |  q
dS )z%Revert module to pre-generation stateNr   rt   z Removing rendered template at %s)r   r   r   r~   r/   rH   unlink)r   rz   rt   filer4   r4   r7   r-   
  s   r-   rt   rY   c                C   st   |d u rt }||}|jdi |}t| |d}|j s&|jjddd ||t	
  td|  d S )Nr   Tru   zRendered template to %sr4   )r   Zget_templaterenderr   parentr~   r   write_bytesencodesysgetdefaultencodingr/   rH   absolute)r   rt   rY   rz   templater|   r   r4   r4   r7   r,     s   

r,   r|   c                 C   sb   t tjd|  d}|d}|| W d    n1 s!w   Y  td| |  d S )Nzdebug-z.jsonwzWrote generator %s to %s)	r   __file__r   joinpathopenwriter/   rH   r   )rt   r|   pathr   r4   r4   r7   r]   +  s
   r]   )LrC   r   r^   loggingrL   abcr   r   typingr   r   r   r   r   r	   r
   pathlibr   contextvarsr   Ztyping_extensionsr   Zjinja2r   r   r   Zpydanticr   r    r   r   typesr   utilsr   r   r   r   r   errorsr   modelsr   r   _typesr    r!   r"   filtersr#   r$   _compatr%   r&   r'   __all__	getLoggerrk   r/   Logger__annotations__r   r   r(   ZGENERIC_GENERATOR_NAMEr   r   r.   r)   r*   r+   r-   rO   r,   r]   r4   r4   r4   r7   <module>   sl   
 $


  6

