o
    Ì^½^J  ã                   @   sˆ   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 e  
d¡ZG dd„ deƒZG d	d
„ d
eƒZdd„ Zddd„ZdS )é    N)Ú	YAMLError)ÚUserDict)Ú
exceptions)Úutilszmkdocs.configc                   @   s   e Zd ZdZdS )ÚValidationErrorz=Raised during the validation process of the config on errors.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú4/usr/lib/python3/dist-packages/mkdocs/config/base.pyr      s    r   c                   @   sR   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚConfigz®
    MkDocs Configuration dict

    This is a fairly simple extension of a standard dictionary. It adds methods
    for running validation on the structure and contents.
    Nc                 C   st   || _ tt|ƒ ¡ ƒ| _|dur+t|tƒs+z
|jt 	¡ d�}W n t
y*   tdƒ‚w || _i | _g | _|  ¡  dS )zX
        The schema is a Python dict which maps the config name to a validator.
        N)Úencodingz)config_file_path is not a Unicode string.)Ú_schemaÚsetÚdictÚkeysÚ_schema_keysÚ
isinstanceÚstrÚdecodeÚsysÚgetfilesystemencodingÚUnicodeDecodeErrorr   Úconfig_file_pathÚdataÚuser_configsÚset_defaults)ÚselfÚschemar   r   r   r   Ú__init__   s   ÿzConfig.__init__c                 C   s   | j D ]	\}}|j| |< qdS )zt
        Set the base config by going through each validator and getting the
        default if it has one.
        N)r   Údefault)r   ÚkeyÚconfig_optionr   r   r   r   .   s   ÿzConfig.set_defaultsc                    s¾   g g }}| j D ]<\‰ }z|  ˆ ¡}| |¡| ˆ < | ‡ fdd„|jD ƒ¡ | ¡  W q tyD } z| ˆ |f¡ W Y d }~qd }~ww t|  	¡ ƒ| j
 D ]‰ | ˆ d ˆ ¡f¡ qN||fS )Nc                    ó   g | ]}ˆ |f‘qS r   r   ©Ú.0Úw©r"   r   r   Ú
<listcomp>?   ó    z$Config._validate.<locals>.<listcomp>z#Unrecognised configuration name: {})r   ÚgetÚvalidateÚextendÚwarningsÚreset_warningsr   Úappendr   r   r   Úformat)r   Úfailedr.   r#   ÚvalueÚer   r(   r   Ú	_validate7   s    

€ÿ
ÿzConfig._validatec                    óˆ   g g }}| j D ]7\‰ }z|j| ˆ d� | ‡ fdd„|jD ƒ¡ | ¡  W q ty? } z| ˆ |f¡ W Y d }~qd }~ww ||fS )N©Úkey_namec                    r$   r   r   r%   r(   r   r   r)   R   r*   z(Config._pre_validate.<locals>.<listcomp>)r   Úpre_validationr-   r.   r/   r   r0   ©r   r2   r.   r#   r4   r   r(   r   Ú_pre_validateK   ó   
€ÿzConfig._pre_validatec                    r6   )Nr7   c                    r$   r   r   r%   r(   r   r   r)   `   r*   z)Config._post_validate.<locals>.<listcomp>)r   Úpost_validationr-   r.   r/   r   r0   r:   r   r(   r   Ú_post_validateY   r<   zConfig._post_validatec                 C   s`   |   ¡ \}}|  ¡ \}}| |¡ | |¡ t|ƒdkr,|  ¡ \}}| |¡ | |¡ ||fS )Nr   )r;   r5   r-   Úlenr>   )r   r2   r.   Ú
run_failedÚrun_warningsÚpost_failedÚpost_warningsr   r   r   r,   g   s   



zConfig.validatec                 C   s:   t |tƒst d t|ƒ¡¡‚| j |¡ | j 	|¡ d S )NzxThe configuration is invalid. The expected type was a key value mapping (a python dict) but we got an object of type: {})
r   r   r   ÚConfigurationErrorr1   Útyper   r0   r   Úupdate)r   Úpatchr   r   r   Ú	load_dicty   s   

ýzConfig.load_dictc              
   C   s<   z	|   t |¡¡W S  ty } zt d |¡¡‚d }~ww )Nz>MkDocs encountered as error parsing the configuration file: {})rH   r   Ú	yaml_loadr   r   rD   r1   )r   Úconfig_filer4   r   r   r   Ú	load_file„   s   ÿ€þzConfig.load_file©N)r   r   r	   r
   r    r   r5   r;   r>   r,   rH   rK   r   r   r   r   r      s    
	r   c                 C   sz   | d u r
t j d¡} t| dƒr| jr| j} t d | ¡¡ t	| t
ƒr6t j | ¡r.t| dƒ} nt d | ¡¡‚|  d¡ | S )Nz
mkdocs.ymlÚclosedzLoading configuration file: {}Úrbz Config file '{}' does not exist.r   )ÚosÚpathÚabspathÚhasattrrM   ÚnameÚlogÚdebugr1   r   r   ÚexistsÚopenr   rD   Úseek)rJ   r   r   r   Ú_open_config_fileŽ   s   
ÿ
rY   c                 K   s*  |  ¡ }|  ¡  ¡ D ]\}}|du r| |¡ q
t| ƒ} t| ddƒ|d< ddlm} t|j|d d�}| 	| ¡ | 
|¡ | ¡ \}}|D ]\}	}
t d|	|
¡ qE|D ]\}	}t d	|	|¡ qS| ¡ D ]\}}t d
||¡ qct|ƒdkrt d t|ƒ¡¡‚|d r“t|ƒdkr“t d t|ƒ¡¡‚|S )a8  
    Load the configuration for a given file object or name

    The config_file can either be a file object, string or None. If it is None
    the default `mkdocs.yml` filename will loaded.

    Extra kwargs are passed to the configuration to replace any default values
    unless they themselves are None.
    NrS   Ú r   r   )Úconfig)r   r   zConfig value: '%s'. Warning: %szConfig value: '%s'. Error: %szConfig value: '%s' = %rz%Aborted with {} Configuration Errors!Ústrictz8Aborted with {} Configuration Warnings in 'strict' mode!)ÚcopyÚitemsÚpoprY   ÚgetattrÚmkdocsr[   r   ÚDEFAULT_SCHEMArK   rH   r,   rT   ÚwarningÚerrorrU   r?   r   rD   r1   )rJ   ÚkwargsÚoptionsr"   r3   r[   ÚcfgÚerrorsr.   Úconfig_namerc   rd   r   r   r   Úload_config¨   s6   

€

ÿÿrj   rL   )ÚloggingrO   r   Úyamlr   Úcollectionsr   ra   r   r   Ú	getLoggerrT   Ú	Exceptionr   r   rY   rj   r   r   r   r   Ú<module>   s    
|