
    5iSZ                         d Z ddlmZmZmZmZ ddl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 ddlmZ  G d d      Zg dZddgZddgZg dZd Z G d dee      Zy)z
DXApplet Handler
++++++++++++++++

Applets are data objects that store application logic, including
specifications for executing it, and (optionally) input and output
signatures. They can be run by calling the :func:`DXApplet.run` method.

    )print_functionunicode_literalsdivisionabsolute_importN   )DXDataObjectDXJob   )merge)is_project_id)SystemRequirementsDictDXError)
basestringc                   n    e Zd ZdZd Zed        Zed        Zd Zd Z	d Z
d Zd	 Zd
 Z	 	 	 	 	 	 ddZy)DXExecutablezMethods in :class:`!DXExecutable` are used by
    :class:`~dxpy.bindings.dxapp.DXApp`,
    :class:`~dxpy.bindings.dxapplet.DXApplet`,
    :class:`~dxpy.bindings.dxworkflow.DXWorkflow`, and
    :class:`~dxpy.bindings.dxworkflow.DXGlobalWorkflow`
    c                     t        d      )Nz6This class is a mix-in. Use DXApp or DXApplet instead.NotImplementedError)selfargskwargss      x/home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/dxpy/bindings/dxapplet.py__init__zDXExecutable.__init__,   s    !"Z[[    c                     j                  d      xs t        j                  }d| i}dD ]  }j                  |      |   ||<    t        fddD              rt	        j
                  j                  d            }t	        j                  d            }t	        j                  d            }t	        j                  d	            }||z   |z   |z   j                         |d
<   j                  d      j                  d      |d
<   j                  d      j                  d      |d<   j                  d      g |d<   t        d   t              rd   D ]  }	t        |	t              st        |	t              r>|	j                         t        d      |d   j                  |	j                                at        |	t              r|d   j                  |	       t        d       nt        d      j                  d      d   |d<   j                  d      d   |d<   j                  d      d   |d<   j                  d      d   |d<   j                  d      d   |d<   t        j                   j                  d      du r|t!        |      r||d<   j                  d      t#        |d          j                  d      d   |d<   j                  d      d   |d<   j                  d       d    |d <   j                  d!      d!   |d"<   j                  d#      d#   |d$<   j                  d%      d%   |d&<   j                  d'      }
|
|
d(k7  r|
dk(  ri n|
|d)<   |S )*ae  
        Takes the same arguments as the run method. Creates an input hash for the /executable-xxxx/run method,
        translating ONLY the fields that can be handled uniformly across all executables: project, folder, name, tags,
        properties, details, depends_on, allow_ssh, debug, delay_workspace_destruction, ignore_reuse, and extra_args.
        projectinput)foldernametags
propertiesdetailsc              3   D   K   | ]  }j                  |      d u  y wN)get).0keyr   s     r   	<genexpr>z<DXExecutable._get_run_input_common_fields.<locals>.<genexpr>=   s     xsvzz#d*xs    )instance_typecluster_specfpga_drivernvidia_driverr*   r+   r,   r-   systemRequirementssystem_requirements!system_requirements_by_executablesystemRequirementsByExecutable
depends_on	dependsOnz:A dxpy handler given in depends_on does not have an ID setz`Expected elements of depends_on to only be either instances of DXJob or DXDataObject, or stringsz&Expected depends_on field to be a listdelay_workspace_destructiondelayWorkspaceDestruction	allow_sshallowSSHdebugpriorityignore_reuseignoreReusedetachT
extra_args
cost_limit	costLimitrankmax_tree_spot_wait_timemaxTreeSpotWaitTimemax_job_spot_wait_timemaxJobSpotWaitTimedetailed_job_metricsdetailedJobMetricspreserve_job_outputsFpreserveJobOutputs)r&   dxpyWORKSPACE_IDanyr   from_instance_typeas_dict
isinstancelistr	   r   get_idr   appendr   JOB_IDr   r   )executable_inputr   r   	run_inputarginstance_type_srdcluster_spec_srdfpga_driver_srdnvidia_driver_srditemrG   s    `         r   _get_run_input_common_fieldsz)DXExecutable._get_run_input_common_fields/   s    **Y'<4+<+<./	F 	-Czz#*!'	#	- x6wxx 6 I I&**UdJe f5fjj6PQ4VZZ5NOO 6vzz/7R S/@CS/SVe/ehy/y  /C  /C  /EI*+::+,8.4jj9N.OI*+::9:F:@**Eh:iI67::l#/%'Ik"&.5"<0 JD!$.*T<2P;;=0")*f"gg!+.55dkkmD#D*5!+.55d;%  'I  J  JJ FGG::34@5;<Y5ZI12::k".$*;$7Ij!::g*!'Ig::j!-$*:$6Ij!::n%1'-n'=Im$;;6::h#74#?GDW\ijq\r#*Ii ::l#/)VL12::h+"("2Ih::l#/%+L%9Ik"::f) &vIf::/0</56O/PI+,::./;.45M.NI*+::,-9.45K.LI*+%zz*@A+0D0M4HD4PbVjI*+r   c                     dD ]!  }|j                  |      st        |dz          t        j                  | fi |}|j                  d      |d   |d<   |S )z|
        Takes the same arguments as the run method. Creates an input
        hash for the /applet-xxxx/run method.
        )stage_instance_typesstage_foldersrerun_stagesignore_reuse_stagesz. is not supported for applets (only workflows)head_job_on_demandheadJobOnDemand)r&   r   r   r[   )rS   r   unsupported_argrT   s       r    _get_run_input_fields_for_appletz-DXExecutable._get_run_input_fields_for_applet   ss      p 	bOzz/*o0``aa	b !==>NYRXY	::*+7+12F+GI'(r   c                     t        d      )a)  
        Runs the executable with the specified input and returns a
        handler for the resulting execution object
        (:class:`~dxpy.bindings.dxjob.DXJob` or
        :class:`~dxpy.bindings.dxanalysis.DXAnalysis`).

        Any kwargs are passed on to :func:`~dxpy.DXHTTPRequest`.
        z_run_impl is not implementedr   r   rT   r   s      r   	_run_implzDXExecutable._run_impl   s     ""@AAr   c                     t        d      )z
        Takes the same arguments as the run method. Creates an input
        hash for the /executable-xxxx/run method.
        z!_get_run_input is not implementedr   r   rS   r   s      r   _get_run_inputzDXExecutable._get_run_input   s    
 ""EFFr   c                     t        d      )N
        Abstract method used in executable_unbuilder.dump_executable
        z%_get_required_keys is not implementedr   r   s    r   _get_required_keyszDXExecutable._get_required_keys        ""IJJr   c                     t        d      )rl   z%_get_optional_keys is not implementedr   rm   s    r   _get_optional_keyszDXExecutable._get_optional_keys   ro   r   c                     t        d      )rl   z,_get_describe_output_keys is not implementedr   rm   s    r   _get_describe_output_keysz&DXExecutable._get_describe_output_keys   s     ""PQQr   c                     t        d      )rl   z$_get_cleanup_keys is not implementedr   rm   s    r   _get_cleanup_keyszDXExecutable._get_cleanup_keys   s     ""HIIr   Nc!                     | j                   |f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| }" | j                  |"fi |!S ) a  
        :param executable_input: Hash of the executable's input arguments
        :type executable_input: dict
        :param project: Project ID of the project context
        :type project: string
        :param folder: Folder in which executable's outputs will be placed in *project*
        :type folder: string
        :param name: Name for the new job (default is "<name of the executable>")
        :type name: string
        :param tags: Tags to associate with the job
        :type tags: list of strings
        :param properties: Properties to associate with the job
        :type properties: dict with string values
        :param details: Details to set for the job
        :type details: dict or list
        :param instance_type: Instance type on which the jobs will be run, or a dict mapping function names to instance type requests
        :type instance_type: string or dict
        :param depends_on: List of data objects or jobs to wait that need to enter the "closed" or "done" states, respectively, before the new job will be run; each element in the list can either be a dxpy handler or a string ID
        :type depends_on: list
        :param allow_ssh: List of hostname or IP masks to allow SSH connections from
        :type allow_ssh: list
        :param debug: Configuration options for job debugging
        :type debug: dict
        :param delay_workspace_destruction: Whether to keep the job's temporary workspace around for debugging purposes for 3 days after it succeeds or fails
        :type delay_workspace_destruction: boolean
        :param priority: Priority level to request for all jobs created in the execution tree, "low", "normal", or "high"
        :type priority: string
        :param head_job_on_demand: If true, the job will be run on a demand instance.
        :type head_job_on_demand: bool
        :param ignore_reuse: Disable job reuse for this execution
        :type ignore_reuse: boolean
        :param ignore_reuse_stages: Stages of a workflow (IDs, names, or indices) or "*" for which job reuse should be disabled
        :type ignore_reuse_stages: list
        :param detach: If provided, job will not start as subjob if run inside of a different job.
        :type detach: boolean
        :param cost_limit: Maximum cost of the job before termination.
        :type cost_limit: float
        :param rank: Rank of execution
        :type rank: int
        :param max_tree_spot_wait_time: Number of seconds allocated to each path in the root execution's tree to wait for Spot
        :type max_tree_spot_wait_time: int
        :param max_job_spot_wait_time: Number of seconds allocated to each job in the root execution's tree to wait for Spot
        :type max_job_spot_wait_time: int
        :param preserve_job_outputs: Copy cloneable outputs of every non-reused job entering "done" state in this root execution to a folder in the project. If value is True it will place job outputs into the "intermediateJobOutputs" subfolder under the output folder for the root execution. If the value is dict, it may contains "folder" key with desired folder path. If the folder path starts with '/' it refers to an absolute path within the project, otherwise, it refers to a subfolder under root execution's output folder.
        :type preserve_job_outputs: boolean or dict
        :param detailed_job_metrics: Enable detailed job metrics for this root execution
        :type detailed_job_metrics: boolean
        :param extra_args: If provided, a hash of options that will be merged into the underlying JSON given for the API call
        :type extra_args: dict
        :returns: Object handler of the newly created job
        :param fpga_driver: a dict mapping function names to fpga driver requests
        :type fpga_driver: dict
        :param system_requirements: System requirement single mapping
        :type system_requirements: dict
        :param system_requirements_by_executable: System requirement by executable double mapping
        :type system_requirements_by_executable: dict
        :param nvidia_driver: a dict mapping function names to nvidia driver requests
        :type nvidia_driver: dict
        :rtype: :class:`~dxpy.bindings.dxjob.DXJob`

        Creates a new job that executes the function "main" of this executable with
        the given input *executable_input*.

        r   r   r    r!   r"   r#   r*   r]   r^   r_   r+   r2   r6   r:   r`   r8   r4   r9   ra   r<   r>   r@   rA   rC   rG   rE   r=   r,   r/   r0   r-   )rj   rg   )#r   rS   r   r   r    r!   r"   r#   r*   r]   r^   r_   r+   r2   r6   r8   r4   r9   ra   r:   r`   r<   r>   r@   rA   rC   rG   rE   r=   r,   r/   r0   r-   r   rT   s#                                      r   runzDXExecutable.run   s   T (D''(8 E07E/5E .2E .2	E
 4>E 18E 7DE >RE 7DE 6BE 6BE 4>E 3<E 6BE =PE  /4!E" E`#E$ 2:%E& >P'E( 06)E* 4>+E, .2-E. AX/E0 @V1E2 >R3E4 >R5E6 4>7E8 5@9E: =P;E< Kl=E> 7D?E	@ t~~i2622r   )NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN)__name__
__module____qualname____doc__r   staticmethodr[   rd   rg   rj   rn   rq   rs   ru   rw    r   r   r   r   %   s|    \ T Tl  "	BGKKRJ osos ~Bz~jnnrj3r   r   )r    titlesummarytypesr!   httpsAppr"   dxapi	inputSpec
outputSpecrunSpecaccessr#   r;   treeTurnaroundTimeThresholdr"   r#   )	r    r~   r   r   r!   r"   r   r   r#   c                      d fd	}|S )Nc                 &    t        dz   dz         )NzWrapper for z does not existr   )	object_idinput_paramsalways_retryr   methods       r   nonexistentAPIWrapperz9_makeNonexistentAPIWrapper.<locals>.nonexistentAPIWrapper;  s    nv-0AABBr   )NNr}   )r   r   s   ` r   _makeNonexistentAPIWrapperr   :  s    C  r   c                   j    e Zd ZdZdZ eej                  j                        Z	 e e
d            Z e e
d            Z eej                  j                        Z e e
d            Z e e
d            Z eej                  j"                        Z eej                  j&                        Z eej                  j*                        Z eej                  j.                        Z e e
d            Z eej                  j4                        Zd Zd	 Zd
 Zd Zd Z d Z!d Z"d Z# fdZ$ xZ%S )DXAppleta  
    Remote applet object handler.

    .. py:attribute:: runSpec

       The applet's run specification (a dict indicating, among other things, how the code of the
       applet is to be interpreted). See `the API docs for Run Specification
       <https://documentation.dnanexus.com/developer/api/running-analyses/io-and-run-specifications#run-specification>`_
       for more information.

    .. py:attribute:: dxapi

       String containing the version of the DNAnexus API that the applet should run against.

    .. py:attribute:: access

       The applet's access requirements hash (a dict indicating any nonstandard permissions, such
       as requiring access to the internet, that are needed by the applet). See `the API docs for
       Access Requirements
       <https://documentation.dnanexus.com/developer/api/running-analyses/io-and-run-specifications#access-requirements>`_
       for more information.

    .. py:attribute:: title

       String containing the (human-readable) title of the app

    .. py:attribute:: summary

       String containing a short, one-line summary of the applet's purpose

    .. py:attribute:: description

       String of free-form text (`Markdown <http://daringfireball.net/projects/markdown/>`_ syntax
       is supported) containing a description of the applet. The description is presented to users
       to help them understand the purpose of the app and how to invoke it.

    .. py:attribute:: developerNotes

       String of free-form text (`Markdown <http://daringfireball.net/projects/markdown/>`_ syntax
       is supported) containing information about the internals or implementation details of the
       applet, suitable for developers or advanced users.

    .. automethod:: _new
    appletz/applet-xxxx/addTypesz/applet-xxxx/removeTypesz/applet-xxxx/setDetailsz/applet-xxxx/setVisibilityz/applet-xxxx/closec                    dD ]6  }||vr%t        | j                  j                  d|d      ||   ||<   ||= 8 dD ]  }||v s||   ||<   ||=  t        j                  j
                  |fi |}| j                  |d   |d          y)a  
        :param dx_hash: Standard hash populated in :func:`dxpy.bindings.DXDataObject.new()` containing attributes common to all data object classes.
        :type dx_hash: dict
        :param runSpec: Run specification
        :type runSpec: dict
        :param dxapi: API version string
        :type dxapi: string
        :param inputSpec: Input specification (optional)
        :type inputSpec: dict
        :param outputSpec: Output specification (optional)
        :type outputSpec: dict
        :param access: Access specification (optional)
        :type access: dict
        :param title: Title string (optional)
        :type title: string
        :param summary: Summary string (optional)
        :type summary: string
        :param description: Description string (optional)
        :type description: string

        .. note:: It is highly recommended that the higher-level module
           :mod:`dxpy.app_builder` or (preferably) its frontend `dx build
           <https://documentation.dnanexus.com/user/helpstrings-of-sdk-command-line-utilities#build>`_
           be used instead for applet creation.

        Creates an applet with the given parameters. See the API
        documentation for the `/applet/new
        <https://documentation.dnanexus.com/developer/api/running-analyses/applets-and-entry-points#api-method-applet-new>`_
        method for more info. The applet is not run until :meth:`run()`
        is called.

        )r   r   z: Keyword argument z is required)r   r   r   r~   r   descriptionidr   N)r   	__class__rx   rI   api
applet_newset_ids)r   dx_hashr   fieldresps        r   _newzDXApplet._new|  s    B ( 	EF"t~~G^G^`efgg#E]GENu		
 \ 	"E!'5M	"
 xx""75f5T$Z!34r   c                 V    t        j                  j                  | j                  fi |S )a  
        :returns: Full specification of the remote applet object
        :rtype: dict

        Returns the contents of the applet. The result includes the
        key-value pairs as specified in the API documentation for the
        `/applet-xxxx/get
        <https://documentation.dnanexus.com/developer/api/running-analyses/applets-and-entry-points#api-method-applet-xxxx-get>`_
        method.
        )rI   r   
applet_get_dxid)r   r   s     r   r&   zDXApplet.get  s"     xx""4::888r   c                 p    t        t        j                  j                  | j                  |fi |d         S )Nr   )r	   rI   r   
applet_runr   rf   s      r   rg   zDXApplet._run_impl  s,    TXX((YI&I$OPPr   c                 .    t        j                  |fi |S r%   )r   rd   ri   s      r   rj   zDXApplet._get_run_input  s    <<=MXQWXXr   c                     t         S r%   )_applet_required_keysrm   s    r   rn   zDXApplet._get_required_keys      $$r   c                     t         S r%   )_applet_optional_keysrm   s    r   rq   zDXApplet._get_optional_keys  r   r   c                     t         S r%   )_applet_describe_output_keysrm   s    r   rs   z"DXApplet._get_describe_output_keys  s    ++r   c                     t         S r%   )_applet_cleanup_keysrm   s    r   ru   zDXApplet._get_cleanup_keys  s    ##r   c                 2    t        t        | 
  |g|i |S )z
        Creates a new job that executes the function "main" of this applet with
        the given input *applet_input*.

        See :meth:`dxpy.bindings.dxapplet.DXExecutable.run` for the available
        args.
        )superr   rw   )r   applet_inputr   r   r   s       r   rw   zDXApplet.run  s!     Xt(GGGGr   )&rx   ry   rz   r{   _classr|   rI   r   applet_describe	_describer   
_add_types_remove_typesapplet_get_details_get_details_set_details_set_visibilityapplet_rename_renameapplet_set_properties_set_propertiesapplet_add_tags	_add_tagsapplet_remove_tags_remove_tags_closeapplet_list_projects_list_projectsr   r&   rg   rj   rn   rq   rs   ru   rw   __classcell__)r   s   @r   r   r   ?  s(   +Z FTXX556I89PQRJ !;<V!WXM ; ;<L :;T UVL"#=>Z#[\O488112G"488#A#ABOTXX556I ; ;<L45IJKF!$(("?"?@N,5\9QY%%,$
H 
Hr   r   )r{   
__future__r   r   r   r   rI    r   r	   utilsr   utils.resolverr   r/   r   
exceptionsr   compatr   r   r   r   r   r   r   r   r}   r   r   <module>r      sv   " S R  !  * 8   G3 G3Z9  '(EF  ,i8 F !
TH|\ THr   