o
    yAf7>                     @  s   d dl m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mZmZ d dlmZmZ d dlmZmZ d dlmZ er\d d	lmZ d d
lmZ d dlm Z  G dd dZ!G dd deeeef Z"dS )    )annotationsN)ABCabstractmethod)TYPE_CHECKINGAnyCallable	GeneratorGenericIterablecast)BuilderConfigBuilderConfigBoundenv_var_enabled)EXCLUDED_DIRECTORIESBuildEnvVars)get_relative_path	safe_walk)PluginManagerBoundApplication)BuildHookInterfaceProjectMetadatac                   @  s   e Zd ZdZddd	Zd
S )IncludedFilepathrelative_pathdistribution_pathr   strr   r   returnNonec                 C  s   || _ || _|| _d S Nr   )selfr   r   r    r#   H/usr/lib/python3.10/site-packages/hatchling/builders/plugin/interface.py__init__   s   
zIncludedFile.__init__N)r   r   r   r   r   r   r   r    )__name__
__module____qualname__	__slots__r%   r#   r#   r#   r$   r      s    r   c                   @  sl  e Zd ZdZdZ	 				d^d_ddZdddddddd`ddZdad!d"Zdad#d$Zdad%d&Z	dbd)d*Z
dbd+d,Zedcd-d.Zeddd0d1Zeded3d4Zedfd6d7Zedgd9d:Zedgd;d<Zedgd=d>Zedhd@dAZedgdBdCZedgdDdEZedcdFdGZdidIdJZedjdLdMZdkdOdPZdgdQdRZdldTdUZdmdVdWZedndYdZZ e!dod\d]Z"dS )pBuilderInterfacea  
    Example usage:

    ```python tab="plugin.py"
    from hatchling.builders.plugin.interface import BuilderInterface


    class SpecialBuilder(BuilderInterface):
        PLUGIN_NAME = 'special'
        ...
    ```

    ```python tab="hooks.py"
    from hatchling.plugin import hookimpl

    from .plugin import SpecialBuilder


    @hookimpl
    def hatch_register_builder():
        return SpecialBuilder
    ```
     Nrootr   plugin_managerPluginManagerBound | Noneconfigdict[str, Any] | NonemetadataProjectMetadata | NoneappApplication | Noner   r    c                 C  sX   || _ tt|| _|| _|| _|| _ttd | _d | _	d | _
d | _d | _d | _d | _d S r!   )_BuilderInterface__rootr   r   !_BuilderInterface__plugin_manager_BuilderInterface__raw_config_BuilderInterface__metadata_BuilderInterface__appr   _BuilderInterface__config!_BuilderInterface__project_config_BuilderInterface__hatch_config_BuilderInterface__build_configZ _BuilderInterface__build_targets _BuilderInterface__target_config_BuilderInterface__project_id)r"   r,   r-   r/   r1   r3   r#   r#   r$   r%   8   s   
zBuilderInterface.__init__F)	directoryversions
hooks_onlycleanclean_hooks_after
clean_onlyr@   
str | NonerA   list[str] | NonerB   bool | NonerC   rD   rE   Generator[str, None, None]c             	   c  s*   | j   |d u rtjtjv r| jtjtj n| jj}tj	
|s)t| |  }|p2| jj}|rTt|t| }|rTd| j ddttt| }	t|	|d u r]ttj}| |}
t|
 }|rmd}n	|d u rvttj}|r|s| || |D ]}|| q|rd S |d u rttj}|D ]y}| jd| j d| d |  }|  | t!|
|d< |D ]}|"|| q|r| jd	| j d| d q| j#| || |fi |}W d    n1 sw   Y  |D ]	}|$||| q|r|D ]	}||g q|V  qd S )
NzUnknown versions for target `z`: z, Tz
Building `z` version ``build_hookszOnly ran build hooks for `)%r1   Zvalidate_fieldsr   ZLOCATIONosenvironr/   Znormalize_build_directoryr@   r   isdirmakedirsget_version_apirA   setPLUGIN_NAMEjoinmapr   sorted
ValueErrorr   Z
HOOKS_ONLYget_build_hookslistvaluesZCLEANrC   ZCLEAN_HOOKS_AFTERr3   Zdisplay_debugget_default_build_dataset_build_data_defaultstupleZ
initializeZset_build_datafinalize)r"   r@   rA   rB   rC   rD   rE   Zversion_apiZunknown_versionsmessageconfigured_build_hooksrK   
build_hookversion
build_dataartifactr#   r#   r$   buildO   sl   

 




zBuilderInterface.buildIterable[IncludedFile]c                 c  s*    |   E dH  | | j E dH  dS )a{  
        Returns a consistently generated series of file objects for every file that should be distributed. Each file
        object has three `str` attributes:

        - `path` - the absolute path
        - `relative_path` - the path relative to the project root; will be an empty string for external files
        - `distribution_path` - the path to be distributed as
        N)recurse_selected_project_filesrecurse_forced_filesr/   Zget_force_includer"   r#   r#   r$   recurse_included_files   s   	z'BuilderInterface.recurse_included_filesc                 c  s4    | j jr| | j jE d H  d S |  E d H  d S r!   )r/   Zonly_includerecurse_explicit_filesrecurse_project_filesrh   r#   r#   r$   rf      s   z/BuilderInterface.recurse_selected_project_filesc                 #  s    t jD ]T\}}}t|j t fdd|D |d d < |  d|v }|D ].}tj |}j	|}j
|rAq+jj||drYttj|||j	|V  q+qd S )Nc                 3  s"    | ]}j | s|V  qd S r!   )r/   Zdirectory_is_excluded.0dr   r"   r#   r$   	<genexpr>   s     z9BuilderInterface.recurse_project_files.<locals>.<genexpr>__init__.py)
is_package)r   r,   r   rU   sortrL   r   rS   r/   get_distribution_pathpath_is_reservedinclude_pathr   )r"   r,   dirsfilesrr   frelative_file_pathr   r#   ro   r$   rk      s$    z&BuilderInterface.recurse_project_filesinclusion_mapdict[str, str]c                 c  s   |  D ]\}}|| j }tj|r,t||rdntj|| j| j	|V  qtj
|r|t|D ]D\}}}t||}tdd |D |d d < |  |D ]&}	tj|||	}
| j	|
}| j|syttj||	|rtdn|
|V  qSq6qd| }t|d S )Nr+   c                 s      | ]	}|t vr|V  qd S r!   r   rl   r#   r#   r$   rp          z8BuilderInterface.recurse_forced_files.<locals>.<genexpr>zForced include not found: )items
startswithr,   rL   r   isfiler   relpathr/   rt   rN   r   r   rU   rs   rS   ru   FileNotFoundError)r"   r{   sourcetarget_pathexternalr,   rw   rx   relative_directoryry   rz   r   msgr#   r#   r$   rg      s:   



z%BuilderInterface.recurse_forced_filesc                 c  s0   |  D ]\}}|| j }tj|r8| j|}| j|s7t	||r(dntj
|| j| j|V  qtj|rt|D ]R\}}}t||}	tdd |D |d d < |  d|v }
|D ]0}tj||	|}| j|}| j|rzqc| jj|d|
drt	tj|||rdn||V  qcqBqd S )Nr+   c                 s  r}   r!   r~   rl   r#   r#   r$   rp      r   z:BuilderInterface.recurse_explicit_files.<locals>.<genexpr>rq   T)Zexplicitrr   )r   r   r,   rL   r   r   r/   rt   ru   r   r   rN   r   r   rU   rs   rS   rv   )r"   r{   r   r   r   r   r,   rw   rx   r   rr   ry   rz   r#   r#   r$   rj      s>   

z'BuilderInterface.recurse_explicit_filesc                 C  s   | j S )z/
        The root of the project tree.
        )r5   rh   r#   r#   r$   r,     s   zBuilderInterface.rootr   c                 C  s$   | j d u rddlm} | | _ | j S )Nr   )PluginManager)r6   hatchling.plugin.managerr   )r"   r   r#   r#   r$   r-     s   
zBuilderInterface.plugin_managerr   c                 C  s0   | j d u rddlm} || j| j| j| _ | j S )Nr   r   )r8   hatchling.metadata.corer   r,   r-   r7   )r"   r   r#   r#   r$   r1     s   
zBuilderInterface.metadatar   c                 C  s.   | j du rddlm} t||  | _ | j S )za
        An instance of [Application](../utilities.md#hatchling.bridge.app.Application).
        Nr   r   )r9   hatchling.bridge.appr   r   Zget_safe_application)r"   r   r#   r#   r$   r3      s   
zBuilderInterface.appdict[str, Any]c                 C  s   | j d u r
| jj| _ | j S r!   )r7   r1   r/   rh   r#   r#   r$   
raw_config,  s   

zBuilderInterface.raw_configc                 C     | j d u r| jjj| _ | j S r!   )r;   r1   corer/   rh   r#   r#   r$   project_config3     
zBuilderInterface.project_configc                 C  r   r!   )r<   r1   hatchr/   rh   r#   r#   r$   hatch_config:  r   zBuilderInterface.hatch_configr   c                 C  s.   | j du r|  | | j| j| j| j| _ | j S )zj
        An instance of [BuilderConfig](../utilities.md#hatchling.builders.config.BuilderConfig).
        N)r:   get_config_classr,   rR   build_configtarget_configrh   r#   r#   r$   r/   A  s
   
zBuilderInterface.configc                 C  s   | j du r| jjj| _ | j S )zO
        ```toml config-example
        [tool.hatch.build]
        ```
        N)r=   r1   r   r   rh   r#   r#   r$   r   M  s   
zBuilderInterface.build_configc                 C  sJ   | j du r"| jjj| ji }t|tsd| j d}t||| _ | j S )ze
        ```toml config-example
        [tool.hatch.build.targets.<PLUGIN_NAME>]
        ```
        Nz Field `tool.hatch.build.targets.z` must be a table)	r>   r1   r   Zbuild_targetsgetrR   
isinstancedict	TypeError)r"   r   r^   r#   r#   r$   r   Y  s   

zBuilderInterface.target_configc                 C  s0   | j d u r| | jjj d| jj | _ | j S )N-)r?   normalize_file_name_componentr1   r   namera   rh   r#   r#   r$   
project_idj  s   
 zBuilderInterface.project_iddict[str, BuildHookInterface]c              	   C  st   i }| j j D ]/\}}| jj|}|d u r&ddlm} d| }|||| j|| j | j	|| j
| j||< q|S )Nr   )UnknownPluginErrorzUnknown build hook: )r/   Zhook_configr   r-   r`   r   Zhatchling.plugin.exceptionsr   r,   r1   rR   r3   )r"   r@   r_   	hook_namer/   r`   r   r^   r#   r#   r$   rW   q  s   

z BuilderInterface.get_build_hooksdict[str, Callable]c                 C     dS )a3  
        A mapping of `str` versions to a callable that is used for building.
        Each callable must have the following signature:

        ```python
        def ...(build_dir: str, build_data: dict) -> str:
        ```

        The return value must be the absolute path to the built artifact.
        Nr#   rh   r#   r#   r$   rP         z BuilderInterface.get_version_api	list[str]c                 C  s   t |  S )zh
        A list of versions to build when users do not specify any, defaulting to all versions.
        )rX   rP   rh   r#   r#   r$   get_default_versions  s   z%BuilderInterface.get_default_versionsc                 C  s   i S )z
        A mapping that can be modified by [build hooks](../build-hook/reference.md) to influence the behavior of builds.
        r#   rh   r#   r#   r$   rZ     s   z'BuilderInterface.get_default_build_datarb   c                 C  s   | dg  | di  d S )NZ	artifactsZforce_include)
setdefault)r"   rb   r#   r#   r$   r[     s   z(BuilderInterface.set_build_data_defaultsc                 C  r   )z
        Called before builds if the `-c`/`--clean` flag was passed to the
        [`build`](../../cli/reference.md#hatch-build) command.
        Nr#   )r"   r@   rA   r#   r#   r$   rC     r   zBuilderInterface.cleantype[BuilderConfig]c                 C  s   t S )zu
        Must return a subclass of [BuilderConfig](../utilities.md#hatchling.builders.config.BuilderConfig).
        )r   )clsr#   r#   r$   r     s   z!BuilderInterface.get_config_class	file_namec                 C  s   t jdd| t jdS )zH
        https://peps.python.org/pep-0427/#escaping-and-unicode
        z	[^\w\d.]+_)flags)resubUNICODE)r   r#   r#   r$   r     s   z.BuilderInterface.normalize_file_name_component)NNNN)r,   r   r-   r.   r/   r0   r1   r2   r3   r4   r   r    )r@   rF   rA   rG   rB   rH   rC   rH   rD   rH   rE   rH   r   rI   )r   re   )r{   r|   r   re   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r@   r   r   r   )r   r   )r   r   )rb   r   r   r    )r@   r   rA   r   r   r    )r   r   )r   r   r   r   )#r&   r'   r(   __doc__rR   r%   rd   ri   rf   rk   rg   rj   propertyr,   r-   r1   r3   r   r   r   r/   r   r   r   rW   r   rP   r   rZ   r[   rC   classmethodr   staticmethodr   r#   r#   r#   r$   r*      sl    
X








r*   )#
__future__r   rL   r   abcr   r   typingr   r   r   r   r	   r
   r   Zhatchling.builders.configr   r   r   Zhatchling.builders.constantsr   r   Zhatchling.builders.utilsr   r   r   r   r   r   Z)hatchling.builders.hooks.plugin.interfacer   r   r   r   r*   r#   r#   r#   r$   <module>   s    $	