o
    ;i                    @   s  U d dl Z d dlZd dl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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 d
dlmZ d
dlmZ d
dlm Z  d
dl!m"Z"m#Z# d
dl$m%Z%m&Z&m'Z'm(Z( d
dl)m*Z*m+Z+ d
dl,m-Z- d
dl.m/Z/m0Z0 d
dl1m2Z2m3Z3m4Z4m5Z5 d
dl6m7Z7 d
dl8m9Z9m:Z: d
dl;m<Z< d
dl=m>Z> d
dl?m@Z@mAZA d
dlBmCZC d
dlDmEZEmFZF d
dlGmHZH d
dlImJZJ d
dlKmLZL d
dlMmNZN d
dlOmPZP d
d lQmRZR d
d!lSmTZT d
d"lUmVZV d
d#lWmXZX d
d$lYmZZZ d
d%l[m\Z\ d
d&l]m^Z^ eL_ Z`eLead'< G d(d) d)Zbe-ebZcd*ejej d+edej d,eed-dfd.d/Zfejgd0ede d1Zheid2Zjegd3Zkegd4ZlG d5d6 d6Zme	 G d7d8 d8ZnG d9d: d:Zoe-eoZpdS );    N)AsyncGenerator
Collection	CoroutineMappingSequence)	dataclass)PurePosixPath)dedent)AnyCallableClassVarOptionalUnionoverload)Message)asynccontextmanager)api_pb2   )	_Function)is_interactive_ipython)LoadContext)_get_environment_name_Object)"_find_partial_methods_for_user_cls_PartialFunction_PartialFunctionFlagsverify_concurrent_params)_Servervalidate_http_server_config)synchronize_api)deprecation_warning#warn_on_renamed_autoscaler_settings)FunctionInfois_flash_objectis_global_objectis_method_fn)validate_volumes)check_object_namecheck_tag_dict)_Client)_CloudBucketMount)_Cls	parameter)logger)ExecutionErrorInvalidError)Function)GPU_T)_Image)_NetworkFileSystem)PartialFunction)_Proxy)Retries)
RunningApp)Schedule)SchedulerPlacement)_Secret)_Volume_default_imagec                   @   sj   e Zd ZU eed< ded< deddddfdd	Zd
ededefddZedefddZ	edddZ
dS )_LocalEntrypoint_info_App_appinfoappreturnNc                 C   s   || _ || _d S N)r>   r@   )selfrA   rB    rF   =/home/ubuntu/.local/lib/python3.10/site-packages/modal/app.py__init__H   s   
z_LocalEntrypoint.__init__argskwargsc                 O   s   | j j|i |S rD   )r>   raw_f)rE   rI   rJ   rF   rF   rG   __call__L   s   z_LocalEntrypoint.__call__c                 C      | j S rD   )r>   rE   rF   rF   rG   rA   O      z_LocalEntrypoint.infoc                 C   rM   rD   )r@   rN   rF   rF   rG   rB   S   rO   z_LocalEntrypoint.app)rC   r?   )__name__
__module____qualname__r"   __annotations__rH   r
   rL   propertyrA   rB   rF   rF   rF   rG   r=   D   s   
 r=   items	item_type	error_msgrC   c                    s8   t | ttfst|t fdd| D st|d S )Nc                 3   s    | ]}t | V  qd S rD   )
isinstance).0vrV   rF   rG   	<genexpr>^   s    z!check_sequence.<locals>.<genexpr>)rX   listtupler/   all)rU   rV   rW   rF   r[   rG   check_sequence[   s
   r`   CLS_T)boundP
ReturnTypeOriginalReturnTypec                   @   s   e Zd Zedeeeef deeeef fddZ	ede
eeeeef f deeeeeeef f fddZ	ede
eef deeeef fddZ	dd Z	dS )	_FunctionDecoratorTypefuncrC   c                 C      d S rD   rF   rE   rg   rF   rF   rG   rL   k      z_FunctionDecoratorType.__call__c                 C   rh   rD   rF   ri   rF   rF   rG   rL   p   rj   c                 C   rh   rD   rF   ri   rF   rF   rG   rL   u   rj   c                 C   rh   rD   rF   ri   rF   rF   rG   rL   z   s    N)rP   rQ   rR   r   r4   rc   rd   re   r0   rL   r   r   r
   rF   rF   rF   rG   rf   j   s(    
rf   c                   @   s   e Zd ZU dZeeef ed< eeef ed< e	e
 ed< ee ed< eeef ed< eeef ed< eed< ee ed	< eeeef ef ed
< dS )_LocalAppStatez<All state for apps that's part of the local/definition state	functionsclassesimage_defaultweb_endpointslocal_entrypointstagsinclude_source_defaultsecrets_defaultvolumes_defaultN)rP   rQ   rR   __doc__dictstrr   rS   r+   r   r2   r]   r=   boolr   r:   r   r   r;   rF   rF   rF   rG   rk   }   s   
 rk   c                W   @   s	  e Zd ZU dZi Zeeee e	d  f  e
d< dZeed   e
d< ee e
d< ee e
d< dZee e
d< ee e
d< ee e
d	< ee e
d
< ee
d< edefddZ	dddg i dddee deeeef  dee dee deeeef ef deddfddZedee fddZedefddZedee fddZedee fdd Zeddd!d"ded#ee d$ee d%edd f
d&d'Z d(efd)d*Z!d+ed,e"fd-d.Z#edefd/d0Z$e$j%d1d0 Z$d2d3 Z&e'd#ed4ede(d5 fd6d7Z)e'dd!d!dd8d#ee d9ed:ed$ee de(d; f
d<d=Z*ddd>dd?dee d$ee d@ed#ee de+j,f
dAdBZ-dCdD Z.dEdF Z/dGe0dHefdIdJZ1d@edKe2fdLdMZ3d#ed4efdNdOZ4edeee0f fdPdQZ5edeee2f fdRdSZ6edeee7f fdTdUZ8ede	e fdVdWZ9	dddXdYe"dee de:e:dZe"f ge7f fd[d\Z;e<	ddddddd!i i dddddddddd]dddddd!d!d!d!d!dddddd!dddddddd^)dee d_ee= d`eeeee f  dee>e  daee?e	e? f dbedceeeef e@f deeeef eeeAf f ddeeeBeCeBeBf f  deeeeDeCeDeDf f  dfeeD dgeeD dheeD dieeD djeeD dkeeE dleeeDeFf  dmeDdneeD dee doee dpee dqeeeee f  dredsedteduedvedwee dee dxeeee"f  dyee dzeeB d{ed|eeD d}eeD d~eeD deeD deeD deeD deeG deHfTddZIe+jJeKfdde<	dddddd!i i dddddddddd]dddd!d!d!d!d!dddddd!dddddddd&dee d`eeeee f  dee>e  daee?e	e? f dbedceeeef e@f deeeef eeeAf f ddeeeBeCeBeBf f  deeeeDeCeDeDf f  dfeeD dgeeD dheeD dieeD djeeD dkeeE dleeeDeFf  dmeDdneeD dpee dqeeeee f  dredsedteduedvedwee dee dxeeee"f  dyee dzeeB d{ed|eeD d}eeD d~eeD deeD deeD deeD deeG de:eeLeMf geLf fNddZN	dddddd!i ddddddddddddgd!dddd!d!dddddee d`eeeee f  dee>e  daee?e	e? f dbedeeeef eeeAf f ddeeeBeCeBeBf f  deeeeDeCeDeDf f  dfeeD dgeeD dheeD dieeD djeeD dkeeE deDdneDdeDdeee  dedeeD dpee dqeeeee f  dredsedwee dee dxeeee"f  de:eeLeMf geOf f8ddZPddd dede+j,fddZQdddeReef d#ee ddfddZSddd#ee deeef fddZTdd#ee de(edf fddZUeVded  fddZWeVdd ZXdS )r?   a  A Modal App is a group of functions and classes that are deployed together.

    The app serves at least three purposes:

    * A unit of deployment for functions and classes.
    * Syncing of identities of (primarily) functions and classes across processes
      (your local Python interpreter and every Modal container active in your application).
    * Manage log collection for everything that happens inside your code.

    **Registering functions with an app**

    The most common way to explicitly register an Object with an app is through the
    `@app.function()` decorator. It both registers the annotated function itself and
    other passed objects, like schedules and secrets, with the app:

    ```python
    import modal

    app = modal.App()

    @app.function(
        secrets=[modal.Secret.from_name("some_secret")],
        schedule=modal.Period(days=1),
    )
    def foo():
        pass
    ```

    In this example, the secret and schedule are registered with the app.
    	_all_appsN_container_app_name_description_local_state_attr_app_id_running_app_client_root_load_contextrC   c                 C   s   | j du r	td| j S )z9For internal use only. Do not use this property directly.Nz=Local state is not initialized - app is not locally available)r}   AttributeErrorrN   rF   rF   rG   _local_state   s   
z_App._local_stateT)rq   imagesecretsvolumesinclude_sourcenamerq   r   r   r   r   c                C   s   |durt |tstd|durt| || _|| _t|td t| |dur2t |t	s2tdt
i i ||||g i |p>i d	| _d| _d| _d| _t | _tj| jg |  dS )ax  Construct a new app, optionally with default image, mounts, secrets, or volumes.

        ```python notest
        image = modal.Image.debian_slim().pip_install(...)
        secret = modal.Secret.from_name("my-secret")
        volume = modal.Volume.from_name("my-data")
        app = modal.App(image=image, secrets=[secret], volumes={"/mnt/data": volume})
        ```
        N)Invalid value for `name`: Must be string.z>`secrets=` has to be a list or tuple of `modal.Secret` objectsz)`image=` has to be a `modal.Image` object)	rl   rm   rn   rs   rt   rr   ro   rp   rq   )rX   rw   r/   r(   r{   r|   r`   r:   r&   r2   rk   r}   r~   r   r   r   emptyr   r?   ry   
setdefaultappend)rE   r   rq   r   r   r   r   rF   rF   rG   rH      s4   
z_App.__init__c                 C   rM   )z"The user-provided name of the App.)r{   rN   rF   rF   rG   r         z	_App.namec                 C   s   | j r| j jS dS )zmdmd:hidden
        Whether the current app for the app is running in interactive mode.

        Note: this method will likely be deprecated in the future.

        F)r   interactiverN   rF   rF   rG   is_interactive  s   	z_App.is_interactivec                 C   rM   )z.Return the app_id of a running or stopped app.)r~   rN   rF   rF   rG   app_id  r   z_App.app_idc                 C   rM   )zEThe App's `name`, if available, or a fallback descriptive identifier.r|   rN   rF   rF   rG   description  r   z_App.descriptionF)clientenvironment_namecreate_if_missingr   r   r   c                   s   t | d |du rt I dH }t|}tj| ||rtjndd}|j|I dH }t	| }d|_
|j|_||_t|||jd|_t|jdd|_|S )a  Look up an App with a given name, creating a new App if necessary.

        Note that Apps created through this method will be in a deployed state,
        but they will not have any associated Functions or Classes. This method
        is mainly useful for creating an App to associate with a Sandbox:

        ```python
        app = modal.App.lookup("my-app", create_if_missing=True)
        modal.Sandbox.create("echo", "hi", app=app)
        ```
        AppN)app_namer   object_creation_type)r   r   r   F)r   )r'   r)   from_envr   r   AppGetOrCreateRequest&OBJECT_CREATION_TYPE_CREATE_IF_MISSINGstubAppGetOrCreater?   r}   r   r~   r   r   r   r7   r   )r   r   r   r   requestresponserB   rF   rF   rG   lookup  s$   
z_App.lookupr   c                 C   s
   || _ dS )zmdmd:hidden
        Set the description of the App before it starts running.

        Note: we don't recommend using the method and may deprecate it in the future.
        Nr   )rE   r   rF   rF   rG   set_descriptionG  s   
z_App.set_descriptionkeyvaluec                 C   s$   t |tstd| d|dd S )NzApp attribute `z` with value z is not a valid Modal object)rX   r   r/   )rE   r   r   rF   rF   rG   _validate_blueprint_valueO  s   
z_App._validate_blueprint_valuec                 C      | j jS )aC  mdmd:hidden
        Retrieve the Image that will be used as the default for any Functions registered to the App.

        Note: This property is only relevant in the build phase and won't be populated on a deployed
        App that is retrieved via `modal.App.lookup`. It is likely to be deprecated in the future.

        r   rn   rN   rF   rF   rG   r   S     	z
_App.imagec                 C   s   || j _dS )zmdmd:hiddenNr   )rE   r   rF   rF   rG   r   ^  s   c                 C   s:   | j }|j D ]}|  q|j D ]}|  qd S rD   )r   rl   values
_unhydraterm   )rE   local_stateobjrF   rF   rG   _uncreate_all_objectsc  s   

z_App._uncreate_all_objectsrunning_app)NNc                 C  sN   |j | _|| _|| _zd V  W d | _d | _|   d S d | _d | _|   w rD   )r   r~   r   r   r   )rE   r   r   rF   rF   rG   _set_local_appk  s   
z_App._set_local_appr   detachr   r   r   r   )r?   Nc             	   C  s`   ddl m} || ||||d4 I dH  | V  W d  I dH  dS 1 I dH s)w   Y  dS )a  Context manager that runs an ephemeral app on Modal.

        Use this as the main entry point for your Modal application. All calls
        to Modal Functions should be made within the scope of this context
        manager, and they will correspond to the current App.

        **Example**

        ```python notest
        with app.run():
            some_modal_function.remote()
        ```

        To enable output printing (i.e., to see App logs), use `modal.enable_output()`:

        ```python notest
        with modal.enable_output():
            with app.run():
                some_modal_function.remote()
        ```

        Note that you should not invoke this in global scope of a file where you have
        Modal Functions or Classes defined, since that would run the block when the Function
        or Cls is imported in your containers as well. If you want to run it as your entrypoint,
        consider protecting it:

        ```python
        if __name__ == "__main__":
            with app.run():
                some_modal_function.remote()
        ```

        You can then run your script with:

        ```shell
        python app_module.py
        ```

        r   )_run_appr   N)runnerr   )rE   r   r   r   r   r   rF   rF   rG   runw  s   0
.z_App.run r   r   tagr   r   c                   sL   ddl m} |du r| jdu rtd|| ||||dI dH }|j| _| S )a   Deploy the App so that it is available persistently.

        Deployed Apps will be available for lookup or web-based invocations until they are stopped.
        Unlike with `App.run`, this method will return as soon as the deployment completes.

        This method is a programmatic alternative to the `modal deploy` CLI command.

        Examples:

        ```python notest
        app = App("my-app")
        app.deploy()
        ```

        To enable output printing (i.e., to see build logs), use `modal.enable_output()`:

        ```python notest
        app = App("my-app")
        with modal.enable_output():
            app.deploy()
        ```

        Unlike with `App.run`, Function logs will not stream back to the local client after the
        App is deployed.

        Note that you should not invoke this method in global scope, as that would redeploy
        the App every time the file is imported. If you want to write a programmatic deployment
        script, protect this call so that it only runs when the file is executed directly:

        ```python notest
        if __name__ == "__main__":
            with modal.enable_output():
                app.deploy()
        ```

        Then you can deploy your app with:

        ```shell
        python app_module.py
        ```

        r   )_deploy_appNzYou need to either supply a deployment name or have a name set on the app.

Examples:
app.deploy(name="some-name")

or
app = modal.App("some-name")r   )r   r   r{   r/   r   r~   )rE   r   r   r   r   r   resultrF   rF   rG   deploy  s   2z_App.deployc                 C   s   | j }|jr	|jS tS rD   )r   rn   r<   )rE   r   rF   rF   rG   _get_default_image  s   z_App._get_default_imagec                 C   s<   | j stdg }| j D ]}||j qdd |D S )Nz+`_get_watch_mounts` requires a running app.c                 S   s   g | ]}|  r|qS rF   )is_local)rY   mrF   rF   rG   
<listcomp>  s    z*_App._get_watch_mounts.<locals>.<listcomp>)r   r.   registered_functionsr   extend_serve_mounts)rE   
all_mountsfunctionrF   rF   rG   _get_watch_mounts  s   z_App._get_watch_mountsr   is_web_endpointc                 C   s   | j }|j|jd  }r3||u rd S t s3td|j d|jj d|jj	 d|jj d|jj	 
 |j|j
v rCtd|j d | jrb|j| jjv rb| jj|j }| jj| }||| j| ||j|j< |rs|j|j d S d S )NzWarning: function name 'z+' collision! Overriding existing function [z].z with new function [zWarning: tag z% exists but is overridden by function)r   rl   getr   r   r-   warningr>   module_namefunction_namerm   r   function_idsobject_handle_metadata_hydrater   ro   r   )rE   r   r   r   old_function	object_idmetadatarF   rF   rG   _add_function   s6   
z_App._add_functionclsc                 C   sJ   | j r|| j jv r| j j| }| j j| }||| j| || jj|< d S rD   )r   	class_idsr   r   r   r   rm   )rE   r   r   r   r   rF   rF   rG   
_add_class  s   z_App._add_classc                 C   s   |j | _|| _|| _| t_| j}|j D ]\}}||j	v r/|j	| }|j
| }|||| q|j D ]\}}||jv rO|j| }|j
| }|||| q5d S rD   )r   r~   r   r   r?   rz   r   r   rU   rl   r   r   r   rm   )rE   r   r   r   r   r   r   handle_metadatarF   rF   rG   _init_container*  s$   





z_App._init_containerc                 C   r   )a  mdmd:hidden
        All modal.Function objects registered on the app.

        Note: this property is populated only during the build phase, and it is not
        expected to work when a deplyoed App has been retrieved via `modal.App.lookup`.
        This method is likely to be deprecated in the future in favor of a different
        approach for retrieving the layout of a deployed App.
        )r   rl   rN   rF   rF   rG   r   ?     
z_App.registered_functionsc                 C   r   )a  mdmd:hidden
        All modal.Cls objects registered on the app.

        Note: this property is populated only during the build phase, and it is not
        expected to work when a deplyoed App has been retrieved via `modal.App.lookup`.
        This method is likely to be deprecated in the future in favor of a different
        approach for retrieving the layout of a deployed App.
        )r   rm   rN   rF   rF   rG   registered_classesK  r   z_App.registered_classesc                 C   r   )a8  mdmd:hidden
        All local CLI entrypoints registered on the app.

        Note: this property is populated only during the build phase, and it is not
        expected to work when a deplyoed App has been retrieved via `modal.App.lookup`.
        This method is likely to be deprecated in the future.
        )r   rp   rN   rF   rF   rG   registered_entrypointsW  r   z_App.registered_entrypointsc                 C   r   )a  mdmd:hidden
        Names of web endpoint (ie. webhook) functions registered on the app.

        Note: this property is populated only during the build phase, and it is not
        expected to work when a deplyoed App has been retrieved via `modal.App.lookup`.
        This method is likely to be deprecated in the future in favor of a different
        approach for retrieving the layout of a deployed App.
        )r   ro   rN   rF   rF   rG   registered_web_endpointsb  r   z_App.registered_web_endpoints)r   _warn_parentheses_missing.c                   sJ   |rt d durt tst ddtdtf dtf fdd}|S )	a  Decorate a function to be used as a CLI entrypoint for a Modal App.

        These functions can be used to define code that runs locally to set up the app,
        and act as an entrypoint to start Modal functions from. Note that regular
        Modal functions can also be used as CLI entrypoints, but unlike `local_entrypoint`,
        those functions are executed remotely directly.

        **Example**

        ```python
        @app.local_entrypoint()
        def main():
            some_modal_function.remote()
        ```

        You can call the function using `modal run` directly from the CLI:

        ```shell
        modal run app_module.py
        ```

        Note that an explicit [`app.run()`](https://modal.com/docs/reference/modal.App#run) is not needed, as an
        [app](https://modal.com/docs/guide/apps) is automatically created for you.

        **Multiple Entrypoints**

        If you have multiple `local_entrypoint` functions, you can qualify the name of your app and function:

        ```shell
        modal run app_module.py::app.some_other_function
        ```

        **Parsing Arguments**

        If your entrypoint function take arguments with primitive types, `modal run` automatically parses them as
        CLI options.
        For example, the following function can be called with `modal run app_module.py --foo 1 --bar "hello"`:

        ```python
        @app.local_entrypoint()
        def main(foo: int, bar: str):
            some_modal_function.call(foo, bar)
        ```

        Currently, `str`, `int`, `float`, `bool`, and `datetime.datetime` are supported.
        Use `modal run app_module.py --help` for more information on usage.

        zBDid you forget parentheses? Suggestion: `@app.local_entrypoint()`.Nr   rK   .rC   c                    sR   t | } d ur
 n| j}j}||jv rtd| dt| }|j|< |S )Nz!Duplicate local entrypoint name: z(. Local entrypoint names must be unique.)r"   rR   r   rp   r/   r=   )rK   rA   r   r   
entrypointr   rE   rF   rG   wrapped  s   
z&_App.local_entrypoint.<locals>.wrapped)r/   rX   rw   r   r
   r=   )rE   r   r   r   rF   r   rG   local_entrypointn  s   3 
z_App.local_entrypointi,  ))r   scheduleenvr   gpu
serializednetwork_file_systemsr   cpumemoryephemeral_diskmin_containersmax_containersbuffer_containersscaledown_windowproxyretriestimeoutstartup_timeoutr   is_generatorcloudregionnonpreemptibleenable_memory_snapshotblock_networkrestrict_modal_accesssingle_use_containersi6pnr   experimental_options_experimental_proxy_ip#_experimental_custom_scaling_factor_experimental_restrict_output	keep_warmconcurrency_limitcontainer_idle_timeoutallow_concurrent_inputs
max_inputs_experimental_buffer_containers!_experimental_scheduler_placementr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  c       )   ,   $      s  t |tr	td|rtddu r dur tdd |(durNt |(ts3tdt|(j |(dkr;td|(d	krCtd
tdddd |(d	k|*durutdd dus]ratd|*jj	rkt
|*jj	|*jjru|*jjdkpxg |rg t|jg jdtttdtf df dtf 	
 !"f#dd}+|+S )z9Decorator to register a new Modal Function with this App.zE`image` needs to be a keyword argument: `@app.function(image=image)`.z:Did you forget parentheses? Suggestion: `@app.function()`.N     	   The `allow_concurrent_inputs` parameter is deprecated. Please use the `@modal.concurrent` decorator instead.

See https://modal.com/docs/guide/modal-1-0-migration for more information.!`max_inputs` must be an int, not r   `max_inputs` must be positiver   *Only `max_inputs=1` is currently supportedr        ZThe `max_inputs` parameter is deprecated. Please set `single_use_containers=True` instead.Tpendingr        The `_experimental_scheduler_placement` parameter is deprecated. Please use the `region` and `nonpreemptible` parameters instead.eCannot use `_experimental_scheduler_placement` together with `region` or `nonpreemptible` parameters.	on-demandf.rC   c                    sx  t | r	tdt| trat| jjrtdp| j	t
j@ }| jj}| jj}t| jd}| j}| jj}| jj| jj}| jj}| j	t
j@ r\t| jt	d d | jj}	| jj}
n6}	d }
n1t| jsnsnttdt| jryttdt| d}| }d }d }d }}	d }
d }d }}d u rt |pt |tj|fi ddd	d
dd
ddi j"dddddddddd|	d|
d|d|d!d p!ddd d!|d"d#d$d%d&|d'|d(|d)d urnj d*d+d, 	p%i ! D d- d.}"||d u |S )/NzWThe `@app.function` decorator cannot be used on a class. Please use `@app.cls` instead.zThe `@app.function` decorator cannot be used on class methods. Swap with `@modal.method` or one of the web endpoint decorators. Example: 

```python
@app.cls()
class MyClass:
    @modal.method()
    def f(self, x):
        ...
```
)r   name_overrideparamsis_flasha+  
                            The `@app.function` decorator must apply to functions in global scope,
                            unless `serialized=True` is set.
                            If trying to apply additional decorators, they may need to use `functools.wraps`.
                            a  
                            The `@app.function` decorator cannot be used on class methods.
                            Please use `@app.cls` with `@modal.method` instead. Example:

                            ```python
                            @app.cls()
                            class MyClass:
                                @modal.method()
                                def f(self, x):
                                    ...
                            ```
                            rB   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   max_concurrent_inputstarget_concurrent_inputsbatch_max_sizebatch_wait_msr   r   r   r   r   webhook_configr   r   r   r   i6pn_enabledcluster_sizerdmar   r   c                 S      i | ]	\}}|t |qS rF   rw   rY   krZ   rF   rF   rG   
<dictcomp>      z2_App.function.<locals>.wrapped.<locals>.<dictcomp>r   restrict_output)#inspectisclass	TypeErrorrX   r   r%   rK   rR   r/   flagsr   	CLUSTEREDr  r!  r"  r"   r  r   r  r  
CONCURRENTr   r#   r  r  r$   r	   isgeneratorfunctionisasyncgenfunctionr   
from_localrt   rr   rU   r   )r  r   r!  r"  rA   rK   r  r  r  r  r  r   #r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rE   r   r   r   r   r   rF   rG   r   )  s  




	
 !"#$%&'*z_App.function.<locals>.wrapped)rX   r2   r/   r   r    inttyperP   protoregionsr]   
_lifecycler:   	from_dictr   rs   r   r   r   r
   r   ),rE   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   rF   r3  rG   r     sb   
@
R 
z_App.function)field_specifierskw_only_default)&r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  c       &   )   !      sF  |rt ddurtdd |%dur=t|%ts"t dt|%j |%dkr*t d|%dkr2t d	td
ddd |%dk|'durdtdd dusLrPt d|'jjrZt|'jj|'jj	rd|'jj	dkpgg |rsg t
|dtttf dtf 	
f dd}(|(S )zt
        Decorator to register a new Modal [Cls](https://modal.com/docs/reference/modal.Cls) with this App.
        z5Did you forget parentheses? Suggestion: `@app.cls()`.Nr  r  r  r   r  r   r	  r
  r  Tr  r  r  r  r  wrapped_clsrC   c                    s  j }d }t| trK| j}| jtj@ r| jj}| jtj	@ r3t
| jt
p$i |dd | jj}| jj}n}d }| jtj@ rF| jj}| jj}nd }d }n
| }}d }d }d }t|s^tdt|t }|rvt|dkrvtd|j dt|tj}	|	rt|	dkrtd|j dtt|t dkrtd|j dtt|	 }
|
jj}|
jj}nd }d }t|tjrstd	t|tj	 D ]}td
t|tj D ]}td|d urt|tj  D ]}tdt!d |d}p|d u}t"j#|fi ddp$ dg |j%dddi |j&ddd	ddddddd|d|d|d |d!d"pRd#d$d%d&d'd(d)d*|d+|d,|d-|d.d ur|n|j'd/d0d1 
pi ( D d2d3 d4}j)|d5d6 t*#||}|j+( D ]\}}|jj,d ur|j d7| }|j-.| q|j}/|| |S )8N)http_configr  z/The @app.cls decorator must be used on a class.r   zModal class z- cannot have multiple methods when clustered.z$ can only have one batched function.z? with a modal batched function cannot have other modal methods.zRA class must have `enable_memory_snapshot=True` to use `snap=True` on its methods.zXThe `@modal.concurrent` decorator cannot be used on methods; decorate the class instead.zYThe `@modal.http_server` decorator cannot be used on methods; decorate the class instead.zECallable decorators cannot be combined with web interface decorators.)r   user_clsrB   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r   r   r   r   r   r   r   r   r   r=  r   r!  r"  r   r   c                 S   r#  rF   r$  r%  rF   rF   rG   r'    r(  z-_App.cls.<locals>.wrapper.<locals>.<dictcomp>r   r   r)  Fr   .)0r   rX   r   r>  r-  r   HTTP_WEB_INTERFACEr  r=  r/  r   r#   r  r  r.  r!  r"  r*  r+  r,  r   interface_flagslenr/   rP   BATCHEDnextiterr   r  r  ENTER_PRE_SNAPSHOTCALLABLE_INTERFACEr"   r   r2  r   rs   rt   rr   rU   r   r+   _method_partialsr  ro   r   r   )r<  r   r=  r>  r  r  r!  r"  interface_methodsbatch_functionsbatch_functionr  r  methodrA   r   cls_funcr   method_namepartial_function	full_namer    r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rE   r   r   r   r   r   rF   rG   wrapper  s@  





	
 !"#$%&)z_App.cls.<locals>.wrapper)r/   r    rX   r4  r5  rP   r6  r7  r]   r8  r:   r9  r   ra   r   ))rE   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  rS  rF   rR  rG   r     sN   <
\ 	z_App.clsi@     r   zus-east)r   r   r   r   r   r   r   r   r   r   r   r   r   r   portr   exit_grace_periodproxy_regions
h2_enabledtarget_concurrencyr   r   r   r   r   r   r   rU  rV  rW  rX  rY  c                   s   |rt dt|||d durttrdk rt dtj||||d|r/t|ng |r;t	| dt
tttf dtf 	
fd	d
}|S )a  
        Decorator to register a new Modal Server with this App.

        Servers run HTTP servers that are started in an `@enter` method.
        Unlike `@app.cls()`, servers only expose HTTP endpoints and do not
        support `.remote()` method calls.

        Example:

        ```python
        @app._experimental_server(port=8000, proxy_regions=["us-east"])
        class MyServer:
            @modal.enter()
            def start(self):
                self.proc = subprocess.Popen(["python3", "-m", "http.server", "8000"])

            @modal.exit()
            def stop(self):
                self.proc.terminate()
        ```
        zFDid you forget parentheses? Suggestion: `@app._experimental_server()`.)rU  rW  r   rV  Nr   z=The `target_concurrency` argument must be a positive integer.)rU  rW  r   rV  rX  wrapped_user_clsrC   c                    s  t |  t |  d }d }| }t| tr'| j}| jtj@ r'| j	j
}| j	j}j}td ||jd}tj|fi dd	pA dg |jddi di |jdd	d
ddd dddd dd ddd dd dddddddddddp|d ud |d!|d"
d ur
n|jd#d$d% pi  D d&d}j|dd' t | |}|S )(N)r   r>  r  rB   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r   r   r   r   r   r   Fr=  	is_serverTr   r!  r"  r   r   c                 S   r#  rF   r$  r%  rF   rF   rG   r'  4  r(  z>_App._experimental_server.<locals>.wrapper.<locals>.<dictcomp>r)  r?  )r   %_validate_wrapped_user_cls_decorators _validate_construction_mechanismrX   r   r>  r-  r   r.  r  r!  r"  r   r"   rP   r   r2  r   rs   rt   rr   rU   r   _from_local)rZ  r!  r"  r>  r   rA   service_functionserverr   r   r   r   r   r   r   r=  r   r   r   r   r   r   r   r   r   r   secrets_listrE   r   r   rY  r   rF   rG   rS    s   


	

 !"%z*_App._experimental_server.<locals>.wrapper)r/   r   rX   r4  r   
HTTPConfigr]   r   r:   r9  r   ra   r   r   r   )rE   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rU  r   rV  rW  rX  rY  r   r   r   r   r   r   r   rS  rF   ra  rG   _experimental_server  s.   8	N?z_App._experimental_server	other_appinherit_tagsc          	   	   C   s   |j }| j }|j D ]
\}}| |d q|j D ]$\}}|j|}|r9||kr9td| d| d|  | || q|rKi |j	|j	|_	| S )a  Include another App's objects in this one.

        Useful for splitting up Modal Apps across different self-contained files.

        ```python
        app_a = modal.App("a")
        @app.function()
        def foo():
            ...

        app_b = modal.App("b")
        @app.function()
        def bar():
            ...

        app_a.include(app_b)

        @app_a.local_entrypoint()
        def main():
            # use function declared on the included app
            bar.remote()
        ```

        When `inherit_tags=True` any tags set on the other App will be inherited by this App
        (with this App's tags taking precedence in the case of conflicts).

        FzNamed app class z with existing value z+ is being overwritten by a different class )
r   rl   rU   r   rm   r   r-   r   r   rq   )	rE   re  rf  other_app_local_statethis_local_stater   r   r   existing_clsrF   rF   rG   include>  s    z_App.include)r   c                   sZ   | j du r
tdt| tj| j |d}|p!| jp!t I dH }|j	|I dH  dS )a  Attach key-value metadata to the App.

        Tag metadata can be used to add organization-specific context to the App and can be
        included in billing reports and other informational APIs. Tags can also be set in
        the App constructor.

        Any tags set on the App before calling this method will be removed if they are not
        included in the argument (i.e., this method does not have `.update()` semantics).

        Nz:`App.set_tags` cannot be called before the App is running.)r   rq   )
r~   r/   r(   r   AppSetTagsRequestr   r)   r   r   
AppSetTags)rE   rq   r   reqrF   rF   rG   set_tagso  s   
z_App.set_tagsc                   sV   | j du r
tdtj| j d}|p| jpt I dH }|j|I dH }t	|j
S )z4Get the tags that are currently attached to the App.Nz:`App.get_tags` cannot be called before the App is running.)r   )r~   r/   r   AppGetTagsRequestr   r)   r   r   
AppGetTagsrv   rq   )rE   r   rm  resprF   rF   rG   get_tags  s   

z_App.get_tagsc                 C  s   | j std|p| jpt I dH }d}	 tj| j d|d}|jj	|2 z3 dH W }|j
r3|j
}|jr9 dS |jD ]	}|jrE|jV  q<q'6 q)zStream logs from the app.

        This method is considered private and its interface may change - use at your own risk!
        z+`app._logs` requires a running/stopped app.NT7   )r   r   last_entry_id)r~   r/   r   r)   r   r   AppGetLogsRequestr   
AppGetLogsunary_streamentry_idapp_donerU   data)rE   r   last_log_batch_entry_idr   	log_batchlogrF   rF   rG   _logs  s.   
z
_App._logsc                 C   rM   )zlReturns the `App` running inside a container.

        This will return `None` outside of a Modal container.rz   r   rF   rF   rG   _get_container_app  s   z_App._get_container_appc                 C   s
   d| _ dS )zOnly used for tests.Nr  r  rF   rF   rG   _reset_container_app  s   
z_App._reset_container_apprD   )T)YrP   rQ   rR   ru   ry   r   rv   r   rw   r]   rS   rz   r}   rk   r7   r)   r   rT   r   r2   r   r:   r   r   r;   rx   rH   r   r   r   r   staticmethodr   r   r
   r   r   setterr   r   r   r   r   typing_extensionsSelfr   r   r   r   r   r+   r   r   r   r   r=   r   r   r   r   r!   r8   r   r1   r3   r*   floatr^   r4  r5   r6   r9   rf   r   dataclass_transformr,   ra   r   r   r   rd  rj  r   rn  rr  r~  classmethodr  r  rF   rF   rF   rG   r?      s  
  		
:)

9
A


D
	
 !$%&'()*+./12356789:;=  
	 !"#$%&'(*+,./012346 v
	
!"
 (1$ 	r?   )qr*  typingcollections.abcr   r   r   r   r   dataclassesr   pathlibr   textwrapr	   r
   r   r   r   r   r   r  google.protobuf.messager   synchronicity.async_wrapr   modal_protor   
_functionsr   _ipythonr   _load_contextr   _objectr   r   _partial_functionr   r   r   r   _serverr   r   _utils.async_utilsr   _utils.deprecationr    r!   _utils.function_utilsr"   r#   r$   r%   _utils.mount_utilsr&   _utils.name_utilsr'   r(   r   r)   cloud_bucket_mountr*   r   r+   r,   configr-   	exceptionr.   r/   rl   r0   r   r1   r   r2   network_file_systemr3   rP  r4   r   r5   r   r6   r   r7   r   r8   scheduler_placementr9   secretr:   volumer;   debian_slimr<   rS   r=   LocalEntrypointr5  rw   r`   TypeVarra   	ParamSpecrc   rd   re   rf   rk   r?   r   rF   rF   rF   rG   <module>   s|   
 	(


          7