o
    +wÖiL  ã                   @   s    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Zd dlZej	d  dk r.d dl
Z
nd dlmZ
 d dlmZ dd„ ZG dd„ deƒZeeƒ ƒZejZdS )	é    )Úabsolute_importNé   )Ú	internalsc                  C   sR   t  ¡ } tjdkrdtjv rtjd }ntj d¡}|dkr"tdƒ‚tj 	|d¡S )a®  
    Returns the directory to which packages will be downloaded by default.
    This value can be overriden using the constructor,
    or on a case-by-case basis using the ``download_dir`` argument
    when calling ``download()``.

    On Windows, the default download directory is ``PYTHONHOME/lib/konlpy``,
    where *PYTHONHOME* is the directory containing Python e.g., ``C:\Python27``.

    On all other platforms, the default directory is the first of the following
    which exists or which can be created with write permission:
    ``/usr/share/konlpy_data``, ``/usr/local/share/konlpy_data``,
    ``/usr/lib/konlpy_data``, ``/usr/local/lib/konlpy_data``, ``~/konlpy_data``.
    Úwin32ÚAPPDATAz~/z+Could not find a default download directoryÚkonlpy_data)
r   Úget_datadirÚsysÚplatformÚosÚenvironÚpathÚ
expanduserÚ
ValueErrorÚjoin)Ú	konlpydirÚhomedir© r   úN/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/konlpy/downloader.pyÚdefault_download_dir   s   r   c                   @   sp   e Zd ZdZdZdZdZdZdZdZ	dd	d
„Z
ddd„Zddd„Zdd„ Zddd„Zdd„ Zdd„ Zdd„ ZdS )Ú
Downloaderz`
    A class used to access the KoNLPy data server, which can be used to download packages.
    z2http://konlpy.github.io/konlpy-data/packages/%s.%sz2http://konlpy.github.io/konlpy-data/packages/%s.shz.http://konlpy.github.io/konlpy-data/index.jsonÚ	installedznot installedzcorrupt or out of dateNc                 C   s
   || _ d S ©N)Ú_download_dir)ÚselfÚdownload_dirr   r   r   Ú__init__@   s   
zDownloader.__init__c                 C   sV   |du r| j }|du rtdƒ‚|dkrtdƒ‚|  |¡}|  ||¡D ]}t|ƒ q"dS )aä  The KoNLPy data downloader.
        With this module you can download corpora, models and other data packages
        that can be used with KoNLPy.

        Downloading packages
        ====================

        Individual packages can be downloaded by passing a single argument, the package identifier for the package that should be downloaded:

        >>> download('corpus/kobill')
        [konlpy_data] Downloading package 'kobill'...
        [konlpy_data]   Unzipping corpora/kobill.zip.

        To download all packages, simply call ``download`` with the argument 'all':

        >>> download('all')
        [konlpy_data] Downloading package 'kobill'...
        [konlpy_data]   Unzipping corpora/kobill.zip.
        ...

        NzPlease specify a package to download. To download all available packages, pass 'all' to the argument: ``konlpy.download('all')``.ÚallzeThis function is not implemented yet. Please download each package individually until further notice.)r   r   ÚNotImplementedErrorÚ	_get_infoÚ_download_packageÚprint)r   Úidr   ÚinfoÚmsgr   r   r   ÚdownloadC   s   

ÿzDownloader.downloadc                 C   sš   t  t | j¡ ¡  ¡ ¡| _	 |d u rtdƒ‚t	|t
ƒr|}n|}z| j| }W n ty3   tdƒ‚w |d u r;| j}tj ||d |d ¡}|  ||¡S )Nz%Please specify a package to download.z6Package does not exist. Please check the package name.ÚfilepathÚext)ÚjsonÚloadsÚurllibÚurlopenÚ	INDEX_URLÚreadÚdecodeÚindexr   Ú
isinstanceÚdictÚKeyErrorr   r   r   r   Ú_pkg_status)r   Ú
info_or_idr   r#   r"   r&   r   r   r   Ústatuse   s    
ÿzDownloader.statusc                 C   s’   t j |¡s	| jS zt  |¡}W n ty   | j Y S w |jt|d ƒkr)| jS t	 
t|dƒ ¡ ¡ ¡ }||d kr>| jS | d¡rF| jS | jS )NÚsizeÚrbÚchecksumÚinstall)r   r   ÚexistsÚNOT_INSTALLEDÚstatÚOSErrorÚst_sizeÚintÚSTALEÚhashlibÚmd5Úopenr-   Ú	hexdigestÚgetÚ	INSTALLED)r   r#   r&   Úfilestatr8   r   r   r   r3   ~   s   
ÿ
zDownloader._pkg_statusFc              
   c   sŒ   dV  |   ||¡}|s|| jkrd|d  V  d S tj |d|d |d f ¡}tj |¡r<|| jkr7dV  t |¡ tj |¡sGt |¡ tj 	|¡}tj |¡sXt |¡ | j
|d |d f }zd|d  V  t ||¡ W n tyˆ } z
d	V  W Y d }~d S d }~ww d
V  tj |¡d }	|	dv r£d| V  |  ||	¡ | d¡rÁ| j|d  }tj |d|d  ¡}
|  ||
|¡ dV  d S )NzKoNLPy downloaderz'[konlpy_data] '%s' is already installedr"   z%s.%sr&   r'   z [konlpy_data] This file is stalez)[konlpy_data] Downloading package '%s'...z$[konlpy_data] Error downloading filez[konlpy_data] Download finishedéÿÿÿÿ)ú.zipú.tarz[konlpy_data] Unzipping file %sr9   z%s.shÚDone)r5   rF   r   r   r   r:   r@   ÚremoveÚmkdirÚdirnameÚPACKAGE_URLr*   ÚurlretrieveÚIOErrorÚsplitextÚ_unzip_filerE   Ú
SCRIPT_URLÚ_exec_shell)r   r#   r   Úforcer5   r&   ÚsubdirÚurlÚer'   Úshpathr   r   r   r    ™   sF   €



€þ


zDownloader._download_packagec                 C   s*   t  ||¡ t |¡ t d||g¡ d S )NÚsudo)r*   rP   r   ÚchmodÚ
subprocessÚcall)r   rX   rZ   r   r   r   r   rU   É   s   
zDownloader._exec_shellc              
   C   s”   z5|dkrt  |¡}| tj |¡¡ | ¡  W d S |dkr3t |¡}| tj |¡¡ | ¡  W d S W d S  t	yI } zt
d||f ƒ‚d }~ww )NrI   rJ   zError reading file %r!
%s)ÚzipfileÚZipFileÚ
extractallr   r   rN   ÚcloseÚtarfilerC   Ú	Exceptionr   )r   r&   r'   ÚzfÚtfrY   r   r   r   rS   Î   s   

ý€ÿzDownloader._unzip_filec                 C   s<   t  t | j¡ ¡  ¡ ¡| _| j |¡r| j |¡S t	dƒ‚)Nz*Could not find a matching item to download)
r(   r)   r*   r+   r,   r-   r.   r/   rE   r   )r   r"   r   r   r   r   Û   s   zDownloader._get_infor   )NN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rO   rT   r,   rF   r;   r@   r   r%   r5   r3   r    rU   rS   r   r   r   r   r   r   3   s     


"
0r   )Ú
__future__r   rA   r(   r   r]   r	   rc   r_   Úversion_infor*   Úurllib.requestÚrequestÚkonlpyr   r   Úobjectr   Ú_downloaderr%   r   r   r   r   Ú<module>   s"   
 
2
