B
    rnd                 @   s   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mZ d dlm	Z
 edddZG dd dZG dd	 d	eZed
ddZe Zee e ZdS )    N)EasyDict)textc             C   s   t |  }| S )z*Calculate the md5 value of the input text.)hashlibmd5encode	hexdigest)r   Zmd5code r   4/tmp/pip-unpacked-wheel-a87ls_x8/paddlehub/config.pyr      s    r   c               @   s   e Zd ZdZdd Zdd Zdd Zedd	 Zej	e
d
dd	Zedd Zej	edddZedd Zej	e
dddZdd Zdd ZdS )	HubConfiga[  
    PaddleHub configuration management class. Each time the PaddleHub package is loaded, PaddleHub will set the
    corresponding functions according to the configuration obtained in HubConfig, such as the log level of printing,
    server address and so on. When the configuration is modified, PaddleHub needs to be reloaded to take effect.
    c          	   C   s~   |    tjtjd| _tj| js4|   d S t	| jd4}y t
j|t
jd}| j| W n   Y nX W d Q R X d S )Nzconfig.yamlr)Loader)_initializeospathjoinhubenv	CONF_HOMEfileexistsflushopenyamlload
FullLoaderdataupdate)selfr   cfgr   r   r	   __init__(   s    zHubConfig.__init__c             C   s2   t  | _d| j_t  | j_d| jj_d| jj_d S )Nz$http://paddlepaddle.org.cn/paddlehubTDEBUG)r   r   serverlogenablelevel)r   r   r   r	   r   7   s
    

zHubConfig._initializec             C   s   |    |   dS )zReset configuration to default.N)r   r   )r   r   r   r	   reset?   s    zHubConfig.resetc             C   s
   | j jjS )z
        The lowest output level of PaddleHub logger. Logs below the specified level will not be displayed. The default
        is Debug.
        )r   r!   r#   )r   r   r   r	   	log_levelD   s    zHubConfig.log_level)r#   c             C   sJ   ddl m} ||j kr4td|t|j || jj_| 	  d S )Nr   )r!   z-Unknown log level {}. The valid values are {})
Zpaddlehub.utilsr!   Z
log_configkeys
ValueErrorformatlistr   r#   r   )r   r#   r!   r   r   r	   r%   L   s
    
c             C   s
   | j jjS )zKWhether to enable the PaddleHub logger to take effect. The default is True.)r   r!   r"   )r   r   r   r	   
log_enableU   s    zHubConfig.log_enable)r"   c             C   s   || j j_|   d S )N)r   r!   r"   r   )r   r"   r   r   r	   r*   Z   s    
c             C   s   | j jS )zPaddleHub Module server url.)r   r    )r   r   r   r	   r    _   s    zHubConfig.server)urlc             C   s   || j _|   d S )N)r   r    r   )r   r+   r   r   r	   r    d   s    c          	   C   s:   t | jd$}tt| j}t|| W dQ R X dS )z<Flush the current configuration into the configuration file.wN)r   r   jsonloadsdumpsr   r   dump)r   r   r   r   r   r	   r   i   s    zHubConfig.flushc             C   s   t t | j}t|S )N)r-   r.   r/   r   r   r0   )r   r   r   r   r	   __str__p   s    zHubConfig.__str__N)__name__
__module____qualname____doc__r   r   r$   propertyr%   setterstrr*   boolr    r   r1   r   r   r   r	   r
   !   s   r
   c               @   sL   e Zd Zdd Zdd Zedd ZejedddZd	d
 Z	dd Z
dS )CacheConfigc          	   C   s~   |    tjtjd| _tj| js4|   d S t	| jd4}y t
j|t
jd}| j| W n   Y nX W d Q R X d S )Nz
cache.yamlr   )r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r	   r   v   s    zCacheConfig.__init__c             C   s@   t  | _ttt dd  d ttt  }|| j_d S )Ni-)	r   r   r   r8   uuidZuuid1inttimehub_name)r   r?   r   r   r	   r      s    ,zCacheConfig._initializec             C   s   | j jS )N)r   r?   )r   r   r   r	   r?      s    zCacheConfig.hub_name)r+   c             C   s   || j _|   d S )N)r   r    r   )r   r+   r   r   r	   r?      s    c          	   C   s:   t | jd$}tt| j}t|| W dQ R X dS )z<Flush the current configuration into the configuration file.r,   N)r   r   r-   r.   r/   r   r   r0   )r   r   r   r   r   r	   r      s    zCacheConfig.flushc             C   s   t t | j}t|S )N)r-   r.   r/   r   r   r0   )r   r   r   r   r	   r1      s    zCacheConfig.__str__N)r2   r3   r4   r   r   r6   r?   r7   r8   r   r1   r   r   r   r	   r:   u   s   r:   )configc          	   C   sv   t jtjd}t j|rrt|>}y*t|	 }|d d | _
|d | _W n   Y nX W d Q R X t | d S )Nzconfig.jsonZ
server_urlr   r%   )r   r   r   r   r   r   r   r-   r.   readr    r%   remove)r@   Zold_cfg_filer   r   r   r   r	   _load_old_config   s    
rC   )r   r   r>   r-   r<   r   Zeasydictr   Zpaddlehub.envenvr   r8   r   r
   objectr:   rC   r@   Zcache_configr   r   r   r	   <module>   s   T*