
    Ti;                    ,   S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKJ	r	  S SK
Jr  S SKrS SKJrJr  S SKJrJr  S SKJrJrJr   \" 5       r " S S	5      r " S
 S\5      rSS jr " S S\5      rSS jrSSS jjrS rSS jr SS jr!SS jr"SS jr#g)    )annotationsN)Path)Lock)
networkingsettings)BPETokenizerSentencePieceTokenizer)errorinfowarningc                      \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S\S	'   S\S
'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S rSS jrS rS r	S r
S rSrg) IPackage(   aA  A package, can be either installed locally or available from a remote package index.

Attributes:
    package_path: The path to the installed package. None if not installed.

    package_version: The version of the package.

    argos_version: The version of Argos Translate the package is intended for.

    from_code: The code of the language the package translates from.

    from_name: Human readable name of the language the package translates from.

    to_code: The code of the language the package translates to.

    to_name: Human readable name of the language the package translates to.

    links: A list of links to download the package


Packages are a zip archive of a directory with metadata.json
in its root the .argosmodel file extension. By default, an
OpenNMT CTranslate2 directory named model/ is expected in the root directory
along with a sentencepiece model named sentencepiece.model or a bpe.model
for tokenizing and Stanza data for sentence boundary detection.
Packages may also optionally have a README.md in the root.

from_code and to_code should be ISO 639 codes if applicable.

Example metadata.json
{
    "package_version": "1.0",
    "argos_version": "1.0",
    "from_code": "en",
    "from_name": "English",
    "to_code": "es",
    "to_name": "Spanish",
    "links": ["https://example.com/en_es.argosmodel"]
}
strcoder   package_pathpackage_versionargos_version	from_code	from_namelist
from_codesto_codeto_codesto_namelinkstype	languagesdependenciessource_languagestarget_languagesz	list[str]c                   UR                  S5      U l        UR                  SS5      U l        UR                  SS5      U l        UR                  S5      U l        UR                  SS5      U l        UR                  S[        5       5      U l        UR                  S5      U l        UR                  S	[        5       5      U l	        UR                  S
S5      U l
        UR                  S[        5       5      U l        UR                  SS5      U l        UR                  S[        5       5      U l        UR                  S[        5       5      U l        UR                  S[        5       5      U l        UR                  S[        5       5      U l        UR                  SS5      U l        U R                  c  U R
                  b]  [%        5       nU R                  b  U R                  US'   U R
                  b  U R
                  US'   U R                  R'                  U5        U R                  c  U R                  b]  [%        5       nU R                  b  U R                  US'   U R                  b  U R                  US'   U R                  R'                  U5        U =R                  [(        R*                  " U R                  5      -  sl        U =R                   [(        R*                  " U R                  5      -  sl        g)z^Loads package metadata from a JSON object.

Args:
    metadata: A json object from json.load

r   r    r   r   r   r   r   r   r   r   r   	translater   r   r    r!   target_prefixNname)getr   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r%   dictappendcopydeepcopy)selfmetadata	from_langto_langs       h/var/www/html/backend/Backoffice_Marketplace/venv/lib/python3.13/site-packages/argostranslate/package.pyload_metadata_from_json IPackage.load_metadata_from_jsond   s-    LL(	'||,=rB%\\/2>!k2!k26",,|TV<||I. Z8||Ir2\\'462
LL5	!k46:$LL@ (-? H (-? H%\\/2> >>%)CI~~)$(NN	&!~~)$(NN	&!!!((3<<#t||'?fG||'"&,,||'"&,,!!((1t~~!>>t~~!>>    c                    [        5       e)Returns the text of the README.md in this package.

Returns:
    The text of the package README.md, None
        if README.md can't be read

NotImplementedErrorr,   s    r0   
get_readmeIPackage.get_readme   s     "##r3   c                    [        5       eNr6   r8   s    r0   get_descriptionIPackage.get_description   s    !##r3   c                t   U R                   UR                   :H  =(       a    U R                  UR                  :H  =(       ay    U R                  UR                  :H  =(       aY    U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R
                  UR
                  :H  $ r<   )r   r   r   r   r   r   )r,   others     r0   __eq__IPackage.__eq__   s      E$9$99 .""e&9&99.%//1. %//1. -	.
 -	
r3   c                    [        U R                  5      S:  a?  [        U R                  5      S:  a&  SR                  U R                  U R                  5      $ U R                  (       a  U R                  $ g)Nr   z{} -> {}r#   )lenr   r   formatr   r8   s    r0   __repr__IPackage.__repr__   sR    t~~"s4<<'81'<$$T^^T\\BBYY99r3   c                8    [        U 5      R                  SS5      $ )Nz->u   →)reprreplacer8   s    r0   __str__IPackage.__str__   s    Dz!!$..r3   )r   r   r   r   r   r   r   r   r   r    r!   r%   r   r   r   r   N)returnr   )__name__
__module____qualname____firstlineno____doc____annotations__r1   r9   r=   rA   rF   rK   __static_attributes__ r3   r0   r   r   (   s    'R INNLNLK
IO)?V$$
/r3   r   c                  8    \ rS rSrSrS	S jrS rS
S jrS rSr	g)Package   zAn installed packagec                   [        U5      [        :X  a  [        U5      nXl        US-  nUR	                  5       (       d  [        S[        U5      -   S-   5      e[        U5       n[        R                  " U5      nU R                  U5        SSS5         US-  nUS-  nUS-  nUR	                  5       (       a  XPl
        O?UR	                  5       (       a  X`l
        O#UR	                  5       (       a  Xpl
        OSU l
        US-  nUS	-  n	UR	                  5       (       a  [        U5      U l        gU	R	                  5       (       a&  [        XR                  U R                  5      U l        gg! , (       d  f       N= f)
z_Create a new Package from path.

Args:
    package_path: Path to installed package directory.

zmetadata.jsonzError opening package at z no metadata.jsonNminisbdstanzaspacyzsentencepiece.modelz	bpe.model)r   r   r   r   existsFileNotFoundErroropenjsonloadr1   packaged_sbd_pathr	   	tokenizerr   r   r   )
r,   r   metadata_pathmetadata_filer-   minisbd_packagestanza_packagespacy_packagesp_model_pathbpe_model_paths
             r0   __init__Package.__init__   sU    $-L($6##%%#+c-.@@CVV  - Myy/H((2 ! 	m&2%0$w.!!##%4"""$$%3"  ""%2"%)D"$'<<%3!!3MBDN""$$)...$,,WDN %3 ! s   $(E++
E9c                   [        5        H  nUR                  U R                  :X  d  M  UR                  U R                  :X  d  M;  [        R                  R                  UR                  5      [        R                  R                  U R                  5      :  d  M  UR                  5       n[        U 5        [        U5        M     g)z3Update the package if a newer version is available.N)
get_available_packagesr   r   	packagingversionparser   download	uninstallinstall_from_path)r,   available_packagenew_package_paths      r0   updatePackage.update   s    !7!9!++t~~=%--=$$**%55%%++D,@,@AB (9'A'A'C$dO%&67 ":r3   c                    U R                   S-  nUR                  5       (       d  g[        US5       nUR                  5       sSSS5        $ ! , (       d  f       g= f)r5   z	README.mdNr)r   r]   r_   read)r,   readme_pathreadme_files      r0   r9   Package.get_readme   sJ     ''+5!!##+s#{##% $##s   A
Ac                "    U R                  5       $ r<   )r9   r8   s    r0   r=   Package.get_description   s      r3   )r   rb   rc   N)r   r   )rM   z
str | None)
rN   rO   rP   rQ   rR   rk   rw   r9   r=   rT   rU   r3   r0   rW   rW      s    *XX8&!r3   rW   c                X   [            [        R                  " U 5      (       d  [        S5      e[        R                  " U S5       nUR                  [        R                  S9  SSS5        SSKJ	n  UR                  5         SSS5        g! , (       d  f       N-= f! , (       d  f       g= f)z{Install a package file (zip archive ending in .argosmodel).

Args:
    path: The path to the .argosmodel file to install.

z/Not a valid Argos Model (must be a zip archive)rz   )pathNr   get_installed_languages)package_lockzipfile
is_zipfile	ExceptionZipFile
extractallr   package_data_dirargostranslate.translater   cache_clear)r   zipfr   s      r0   rt   rt      sw     
!!$''MNN__T3'4OO!:!:O; ( 	E++- 
 (' 
s#   >BB
#B

B	B
B)c                  4    \ rS rSrSrS rS	S jrS rS rSr	g)
AvailablePackagei  z1A package available for download and installationc                &    U R                  U5        g)z5Creates a new AvailablePackage from a metadata objectN)r1   )r,   r-   s     r0   rk   AvailablePackage.__init__  s    $$X.r3   c                \   [        U 5      S-   n [        R                  U-  nUR                  5       (       db  [        R
                  " U R                  5      nUc  [        S[        U 5       35      e[        US5       nUR                  U5        SSS5        U$ U$ ! , (       d  f       U$ = f)z3Downloads the AvailablePackage and returns its pathz.argosmodelNzDownload failed for wb)argospm_package_namer   downloads_dirr]   r   get_fromr   r   r   r_   write)r,   filenamefilepathdatafs        r0   rr   AvailablePackage.download  s    '-=	 ))H4  &&tzz2D|"6s4yk BCCh% &x &%s   >B
B+c                Z    U R                  5       n[        U5        UR                  5         g r<   )rr   rt   unlink)r,   download_paths     r0   installAvailablePackage.install5  s!    -(r3   c                N    SR                  U R                  U R                  5      $ )Nu	   {} → {})rE   r   r   r8   s    r0   r=    AvailablePackage.get_description:  s    !!$..$,,??r3   rU   N)rM   r   )
rN   rO   rP   rQ   rR   rk   rr   r   r=   rT   rU   r3   r0   r   r     s    ;/6
@r3   r   c                    [            [        R                  " U R                  5        SSKJn  UR                  5         SSS5        g! , (       d  f       g= f)z@Uninstalls a package.

Args:
    pkg: The package to uninstall

r   r   N)r   shutilrmtreer   r   r   r   )pkgr   s     r0   rs   rs   >  s5     
c&&' 	E++- 
s   7A
Ac                   [            / nU c  [        R                  OU nU HK  nUR                  5        H4  n U R	                  5       (       d  M  UR                  [        U 5      5        M6     MM     UsSSS5        $ ! , (       d  f       g= f)aP  Return a list of installed Packages

Looks for packages in <home>/.argos-translate/local/share/packages by
default. Will also look in the directory specified
in the ARGOS_PACKAGE_DIR environment variable
if it is set.

Args:
    path: Path to look for installed package directories in.
        Defaults to the path in settings module.

N)r   r   package_dirsiterdiris_dirr)   rW   )r   	to_returnpackages_path	directorys       r0   get_installed_packagesr   N  sf     
	15--4&I!))+;;==$$WT]3 , '  
s   AA;#A;;
B	c                    [             [        R                  R                  [        R
                  5      n U R                  5       n[        [        R                  S5       nUR                  U5        SSS5        SSS5        g! [         a  n[        U5         SnASSS5        gSnAff = f! , (       d  f       NA= f! , (       d  f       g= f)zDownloads remote package indexNr   )r   urllibrequesturlopenr   remote_package_indexr   r
   r{   r_   local_package_indexr   )responseerrr   r   s       r0   update_package_indexr   e  s    		~~--h.K.KLH }}(..5GGDM 6 
  	#J 
	 65 
sK   B>-B*B> B-2B>
B*B%B>%B**B>-
B;	7B>>
Cc                 4    [        [        R                  5       n [        R                  " U 5      n/ nU H  n[        U5      nUR                  U5        M!      UsSSS5        $ ! , (       d  f       g= f! [         a    [        5         [        5       s $ f = f)z;Returns a list of AvailablePackages from the package index.N)
r_   r   r   r`   ra   r   r)   r^   r   rn   )
index_fileindexpackagesr-   packages        r0   rn   rn   r  s    ((../:IIj)EH!*84( "" / 0//0  (%''(s/   A6 A A%	A6 %
A3/A6 3A6 6BBc                    U R                   nU R                  (       a3  U R                  (       a"  USU R                  -   S-   U R                  -   -  nU$ )ztGets argospm name of an IPackage.

Args:
    The package to get the name of.

Returns:
    Package name for argospm
-_)r   r   r   )r   r   s     r0   r   r     sA     I
}}S3==(3.<<	r3   c                   ^ ^ [        5       n [        [        U U4S jU5      5      n[	        UR                  5       5        g! [         a     gf = f)a\  Installs the necessary package to translate between a pair of languages

Args:
    from_code (str): The ISO 639 code for the language being translated from
    to_code (str): The ISO 639 code for the language being translated to

Returns:
    True if the package was installed successfully,
    False if the installation failed or was not possible
c                N   > U R                   T:H  =(       a    U R                  T:H  $ r<   )r   r   )xr   r   s    r0   <lambda>3install_package_for_language_pair.<locals>.<lambda>  s    !++2KqyyG7KKr3   FT)rn   nextfilterStopIterationrt   rr   )r   r   available_packagespackage_to_installs   ``  r0   !install_package_for_language_pairr     sW     01!K"
 (1134	  s   A 
AAc                 ,    [        S5        [        5       $ )z*Deprecated 1.2, use get_available_packageszUUsing deprecated function load_available_packages, use get_available_packages instead)r   rn   rU   r3   r0   load_available_packagesr     s    _ "##r3   )r   r   )r   rW   r<   )r   r   rM   list[Package])rM   zlist[AvailablePackage])r   r   rM   r   )r   r   r   r   rM   bool)rM   r   )$
__future__r   r*   r`   r   urllib.requestr   r   pathlibr   	threadingr   packaging.versionro   argostranslater   r   argostranslate.tokenizerr   r	   argostranslate.utilsr
   r   r   r   r   rW   rt   r   rs   r   r   rn   r   r   r   rU   r3   r0   <module>r      s    "         / I 5 5( vF/ F/RL!h L!^.&(@x (@V. .
(B4$r3   