
    -PhD                       d Z ddlmZ 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
 ej        rddlmZmZmZ ddlmZ ddlmZmZmZ d	d
gZddZ ej        d           G d d	                      Z ej        d           G d d
                      Z ej        d          Zej         G d de
                      ZdS )z
This module focues on reading pyproject.toml fields with error collection. It is
mostly internal, except for License and Readme classes, which are re-exported in
the top-level package.
    )annotationsN   )ErrorCollector)	GeneratorIterableSequence)Requirement)ContactTableDynamicProjectTableLicenseReadmereturn	list[str]c                     t           S )N)__all__     \/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyproject_metadata/pyproject.py__dir__r   "   s    Nr   T)frozenc                  (    e Zd ZU dZded<   ded<   dS )r   z
    This represents a classic license, which contains text, and optionally a
    file path. Modern licenses are just SPDX identifiers, which are strings.
    strtextpathlib.Path | NonefileN__name__
__module____qualname____doc____annotations__r   r   r   r   r   &   s3          
 IIIr   c                  2    e Zd ZU dZded<   ded<   ded<   dS )r   zk
    This represents a readme, which contains text and a content type, and
    optionally a file path.
    r   r   r   r   content_typeNr   r   r   r   r   r   1   s?          
 IIIr   Tc                  r    e Zd ZdZd)dZd*dZd+dZd,dZd-dZd.dZ	d/dZ
d0dZd1dZd2d!Zd3d#Zd4d'Zd(S )5PyProjectReadera  Class for reading pyproject.toml fields with error collection.

    Unrelated errors are collected and raised at once if the `collect_errors`
    parameter is set to `True`. Some methods will return None if an error was
    raised. Most of them expect a non-None value as input to enforce the caller
    to handle missing vs. error correctly. The exact design is based on usage,
    as this is an internal class.
    valuer   keyr   
str | Nonec                    t          |t                    r|S d}|                     ||t          |                     dS )z Ensure that a value is a string.3Field {key} has an invalid type, expecting a stringr)   got_typeN)
isinstancer   config_errortype)selfr(   r)   msgs       r   
ensure_strzPyProjectReader.ensure_strK   sD    eS!! 	LC#3e===tr   vallist[T]list[T] | Nonec                
   t          |t                    s)d}|                     ||t          |                     dS |D ]A}t          |t                    s*d}|                     ||t          |                      dS B|S )z)Ensure that a value is a list of strings.z<Field {key} has an invalid type, expecting a list of stringsr-   Nz?Field {key} contains item with invalid type, expecting a string)r/   listr0   r1   r   )r2   r5   r)   r3   items        r   ensure_listzPyProjectReader.ensure_listT   s    #t$$ 	PCcsT#YY???4 	 	DdC(( W!!#3d!DDDtt
 
r   dict[str, str]dict[str, str] | Nonec                >   t          |t                    s)d}|                     ||t          |                     dS |                                D ]I\  }}t          |t
                    s/d}|                     || d| t          |                      dS J|S )z/Ensure that a value is a dictionary of strings.z=Field {key} has an invalid type, expecting a table of stringsr-   Nr,   .)r/   dictr0   r1   itemsr   )r2   r5   r)   r3   subkeyr:   s         r   ensure_dictzPyProjectReader.ensure_dictb   s    #t$$ 	QCcsT#YY???4IIKK 	 	LFDdC(( K!!#c+<+<F+<+<tDzz!RRRtt 
r   Sequence[ContactTable]list[tuple[str, str | None]]c                   t          |t                    s)d}|                     ||t          |                     g S |D ]}t          |t                    s0d}|                     ||t          |          j                   g c S |                                D ]I}t          |t                    s2d}|                     ||t          |          j                   g c c S Jt          |          ddhz
  }|rHd}|                     ||d		                    t          d
 |D                                            g c S d |D             S )zNEnsure that a value is a list of tables with optional "name" and "email" keys.zeField {key} has an invalid type, expecting a list of tables containing the "name" and/or "email" keysr-   zField {key} has an invalid type, expecting a list of tables containing the "name" and/or "email" keys (got list with {type_name}))r)   	type_namezField {key} has an invalid type, expecting a list of tables containing the "name" and/or "email" keys (got list with dict with {type_name})nameemailzField {key} has an invalid type, expecting a list of tables containing the "name" and/or "email" keys (got list with dict with extra keys {extra_keys})z, c              3  "   K   | ]
}d | d V  dS )"Nr   ).0ks     r   	<genexpr>z0PyProjectReader.ensure_people.<locals>.<genexpr>   s*      /M/MQA/M/M/M/M/M/Mr   )r)   
extra_keysc                d    g | ]-}|                     d d          |                     d          f.S )rH   UnknownrI   )get)rL   entrys     r   
<listcomp>z1PyProjectReader.ensure_people.<locals>.<listcomp>   s7    TTTu69--uyy/A/ABTTTr   )r/   r9   r0   r1   r@   r   valuesr   setjoinsorted)r2   r5   r)   r3   eachr(   rO   s          r   ensure_peoplezPyProjectReader.ensure_peopleo   s    #t$$ 	C  csT#YY???I 	 	DdD)) 3 
 !!#3$t**:M!NNN			  !%-- A 
 %%csd5kk>R%SSSIIIII Tfg%66J I 
 !!#yy/M/M*/M/M/M)M)MNN "   
 			 UTPSTTTTr   projectr   project_dirpathlib.PathLicense | str | Nonec                   |                     d          }|dS t          |t                    r|S t          |t                    r|                     |d          }|dS n)d}|                     |dt          |                     dS |D ]%}|dvrd}|                     |d| 	            dS &d}|                     d
          }|                     d          }	|r|	s|s|	sd}|                     |d|           dS |r^|                    |          }|                                sd|d}|                     |d	           dS |	                    d          }	|	J t          |	|          S )zGet the license field from the project table. Handles PEP 639 style license too.

        None is returned if the license field is not present or if an error occurred.
        licenseNzproject.licensezGField {key} has an invalid type, expecting a string or table of stringsr-   )r   r   Unexpected field {key}zproject.license.r)   r   r   zFInvalid {key} contents, expecting a string or one key "file" or "text"r)   gotzLicense file not found ()zproject.license.fileutf-8encoding)rR   r/   r   r@   rC   r0   r1   joinpathis_file	read_textr   )
r2   r[   r\   r5   _licenser3   fieldr   filenamer   s
             r   get_licensezPyProjectReader.get_license   s    kk)$$;4c3 	Jc4   	''->??Ht   \Cc'8499MMM4 	 	E,,,.!!#+Ee+E+E!FFFtt -
 %)<<''||F## 	 	x 	 	X  c'8hGGG4 	4''11D<<>> >>>>!!#+A!BBBt>>7>33DtT"""r   list[pathlib.Path] | Nonec                    |                     d          }|dS |                     |d          dS t          |                     ||                    S )zGet the license-files list of files from the project table.

        Returns None if an error occurred (including invalid globs, etc) or if
        not present.
        zlicense-filesNproject.license-files)rR   r;   r9   _get_files_from_globs)r2   r[   r\   license_filess       r   get_license_filesz!PyProjectReader.get_license_files   sZ      O44 4M+BCCK4D..{MJJKKKr   Readme | Nonec                V   d|vrdS d}d}d}d}|d         }t          |t                    rRd}|}|                    d          rd}np|                    d          rd}nWd}|                     |d|	           dS t          |t                    r|D ]%}	|	d
vrd}|                     |d|	             dS &|                    d          }
|
|                     |
d          }|dS |                    d          }||                     |d          }|dS |                    d          }||                     |d          }|dS |r|s|s|sd}|                     |d|           dS |sd}|                     |d           dS n)d}|                     |dt          |                     dS |r[|                    |          }|	                                sd}|                     |d|	           dS |
                    d          }|J t          |||          S )zGet the text of the readme from the project table.

        Returns None if an error occurred or if the readme field is not present.
        readmeNz.mdztext/markdownz.rstz
text/x-rstz9Could not infer content type for readme file {filename!r}zproject.readme)r)   rn   )content-typer   r   ra   zproject.readme.rb   ry   zproject.readme.content-typer   zproject.readme.filer   zproject.readme.textz9Invalid {key} contents, expecting either "file" or "text"rc   zField {key} missingzNField {key} has an invalid type, expecting either a string or table of stringsr-   z$Readme file not found ({filename!r})rf   rg   )r/   r   endswithr0   r@   rR   r4   r1   ri   rj   rk   r   )r2   r[   r\   rn   r   r   r$   rx   r3   rm   content_type_rawfilename_rawtext_raws                r   
get_readmezPyProjectReader.get_readme   s    7""4#$(#'"fc"" 2	DH  '' .""6** +Q!!#+;h!OOOt%% &	     @@@2C%%c/H/H/H%III44 A
  &zz.99+#$&C     '4!::f--L'??<9NOO#4zz&))H#x1FGG<4 T 8 D Q!!#+;!HHHt +!!#+H!IIIt
 cCc'7$v,,OOO4 	4''11D<<>> <!!#+@8!TTTt>>7>33DdD,///r   list[Requirement]c                `   d}|                     d          }||                     |d          }|g S g }|D ]u}	 |                    t          j                            |                     6# t          j        j        $ r)}d}|                     |d||           g cY d}~c S d}~ww xY w|S )z,Get the dependencies from the project table.Ndependencieszproject.dependenciesNField {key} contains an invalid PEP 508 requirement string {req!r} ({error!r})r)   reqerror)rR   r;   append	packagingrequirementsr	   InvalidRequirementr0   )r2   r[   requirement_stringsrequirement_strings_rawr   r   er3   s           r   get_dependenciesz PyProjectReader.get_dependencies,  s     15")++n"="="."&"2"2')?# # &I*,& 	 	C##I$:$F$Fs$K$KLLLL)<   f!!#+AsRS!TTT								 s   2A..B+B&B+&B+dict[str, list[Requirement]]c           	     |   |                     d          }|si S i }t          |t                    s)d}|                     |dt	          |                     i S |                                                                D ]-\  }}t          |t                    sJ t          |t                    s.d}|                     |d| t	          |                     i c S g ||<   |D ]}t          |t                    s0d}|                     |d| t	          |                     i c c S 	 ||         	                    t          j                            |                     # t          j        j        $ r.}d}|                     |d| ||           i cY d	}~c c S d	}~ww xY w/t          |          S )
z5Get the optional dependencies from the project table.zoptional-dependencieszQField {key} has an invalid type, expecting a table of PEP 508 requirement stringszproject.optional-dependenciesr-   zproject.optional-dependencies.zGField {key} has an invalid type, expecting a PEP 508 requirement stringr   r   N)rR   r/   r@   r0   r1   copyrA   r   r9   r   r   r   r	   r   )	r2   r[   r5   requirements_dictr3   extrar   r   r   s	            r   get_optional_dependenciesz)PyProjectReader.get_optional_dependenciesB  sK    kk122 	I:<#t$$ 	eC8499     I#&88::#3#3#5#5 #	 #	E<eS)))))lD11 i!!@@@!,// "   
 			')e$#  !#s++ cC%%DUDD!%c &   
 IIIII%e,33!.::3??    !-@   T  %%DUDD	 &    IIIIIIIIII2 %&&&s   /8E((F*<F%F*%F*dict[str, dict[str, str]]c           	        |                     dd          }|i S t          |t                    s)d}|                     |dt	          |                     i S |                                D ]\  }}t          |t                    sJ t          j        d|          sd}|                     |d|           i c S t          |t                    s.d	}|                     |d
| t	          |                     i c S |                                D ]d\  }}t          |t                    sJ t          |t                    s3d}|                     |d
| d| t	          |                     i c c S e|S )z+Get the entrypoints from the project table.zentry-pointsNzIField {key} has an invalid type, expecting a table of entrypoint sectionszproject.entry-pointsr-   z^\w+(\.\w+)*$znField {key} has an invalid value, expecting a name containing only alphanumeric, underscore, or dot charactersrc   zAField {key} has an invalid type, expecting a table of entrypointszproject.entry-points.r,   r?   )	rR   r/   r@   r0   r1   rA   r   rematch)r2   r[   r5   r3   sectionentrypointsrH   
entrypoints           r   get_entrypointszPyProjectReader.get_entrypointsy  s    kk.$//;I#t$$ 	]Cc'=S		RRRI$'IIKK 	 	 G[gs+++++8,g66 R  !!#+Aw!OOO			k400 	W  !!999!+.. "   
 			$/$5$5$7$7 	 	 j!$,,,,,!*c22 OC%%DGDDdDD!%j!1!1 &   
 IIIII	 
r   list[Dynamic]c                    |                     dg           }|                     |d           d|v rd}|                     |d           g S |S )zGet the dynamic fields from the project table.

        Returns an empty list if the field is not present or if an error occurred.
        dynamiczproject.dynamicrH   z!Unsupported field 'name' in {key}rb   )rR   r;   r0   )r2   r[   r   r3   s       r   get_dynamiczPyProjectReader.get_dynamic  sb    
 ++i,,"3444W5Cc'8999Ir   globsIterable[str]#Generator[pathlib.Path, None, None]c              #  ,  K   |D ]}|                     d          rd}|                     |d|            dS d |                    |          D             }|sd}|                     |d|            dS |D ]}|                    |          V  dS )z,Given a list of globs, get files that match.)z../z\{glob!r} is an invalid {key} glob: the pattern must match files within the project directoryrr   )r)   globc                :    g | ]}|                                 |S r   )rj   )rL   fs     r   rT   z9PyProjectReader._get_files_from_globs.<locals>.<listcomp>  s%    FFF1!))++FQFFFr   zOEvery pattern in {key} must match at least one file: {glob!r} did not match anyN)
startswithr0   r   relative_to)r2   r\   r   r   r3   filesr   s          r   rs   z%PyProjectReader._get_files_from_globs  s      
  	1 	1D{++ t!!#+B!NNNFF 0 0 6 6FFFE g!!#+B!NNN 1 1mmK0000001	1 	1r   N)r(   r   r)   r   r   r*   )r5   r6   r)   r   r   r7   )r5   r<   r)   r   r   r=   )r5   rD   r)   r   r   rE   )r[   r   r\   r]   r   r^   )r[   r   r\   r]   r   rp   )r[   r   r\   r]   r   rv   )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!   r4   r;   rC   rZ   ro   ru   r~   r   r   r   r   rs   r   r   r   r'   r'   @   s                 *U *U *U *UX0# 0# 0# 0#dL L L L M0 M0 M0 M0^   ,5' 5' 5' 5'n' ' ' 'R    1 1 1 1 1 1r   r'   )r   r   )r!   
__future__r   dataclassespathlibr   typingpackaging.requirementsr   errorsr   TYPE_CHECKINGcollections.abcr   r   r   r	   project_tabler
   r   r   r   r   	dataclassr   r   TypeVarr%   r'   r   r   r   <module>r      s    # " " " " "      				      " " " " " "	 C==========222222BBBBBBBBBB     d###       $# d###       $# FN3 A1 A1 A1 A1 A1n A1 A1 A1 A1 A1r   