3 iga3@s>dZddlZddlZddlZddlZddlmZmZmZm Z m Z m Z m Z ddl mZmZddlmZddlmZddlmZddlmZmZejZe d eZerd nd ZdZeddddddZejej ej!ej"ej#fZ$ej ejej!fZ%ee&Z'eedddZ(ee edddZ)eee efdddZ*GdddZ+dS)a Configuration management setup Some terminology: - name As written in config files. - value Value associated with a name - key Name combined with it's section (section.name) - variant A single word describing where the configuration key-value pair came from N)AnyDictIterableListNewTypeOptionalTuple)ConfigurationError!ConfigurationFileCouldNotBeLoaded)appdirs)WINDOWS) getLogger) ensure_direnumKindzpip.inizpip.confversionhelpuserglobalsiteenvzenv-var)USERGLOBALSITEENVENV_VAR)namereturncCs*|jjdd}|jdr&|dd}|S)zAMake a name consistent regardless of source (environment or file)_-z--N)lowerreplace startswith)rr$:/tmp/pip-build-88gy_88q/pip/pip/_internal/configuration.py_normalize_name2s  r&cCs&d|krdj|}t||jddS)N.zbKey does not contain dot separated section and key. Perhaps you wanted to use 'global.{}' instead?)formatr split)r error_messager$r$r%_disassemble_key:s r,)rcCstddtjdD}tjjtjt}tjjtjjdt rFsz+get_configuration_files..pip~z.pip)r site_config_dirsr-r.r/sysprefixr0 expanduserr user_config_dirkindsrrr)Zglobal_config_filesZsite_config_filelegacy_config_fileZnew_config_filer$r$r%get_configuration_filesDs  r<cseZdZdZd6eeeddfdd ZddddZee dd d Z e e e e fdd d Ze e d ddZe e ddddZe dd ddZddddZddddZeee e fdddZddddZee edddZe ed d!d"Zddd#d$Ze e e e e fee e fd%d&d'Ze e e e fdd(d)Ze e eee fdd*d+Zeee e fd,d-d.Z e e efdd/d0Z!e edd1d2d3Z"e dd4d5Z#Z$S)7 ConfigurationaHandles management of configuration. Provides an interface to accessing and managing configuration files. This class converts provides an API that takes "section.key-name" style keys and stores the value associated with it as "key-name" under the section "section". This allows for a clean interface wherein the both the section and the key-name are preserved in an easy to manage form in the configuration files and the data stored is also nice. N)isolated load_onlyrcsjtj|dk r4|tkr4tdjdjttt||_||_ ddt D|_ ddt D|_ g|_ dS)Nz5Got invalid value for load_only - should be one of {}z, cSsi|] }g|qSr$r$)r1variantr$r$r% rsz*Configuration.__init__..cSsi|] }i|qSr$r$)r1r@r$r$r%rAus)super__init__VALID_LOAD_ONLYr r)r/mapreprr>r?OVERRIDE_ORDER_parsers_config_modified_parsers)selfr>r?) __class__r$r%rCes   zConfiguration.__init__)rcCs|j|js|jdS)z_load_environment_vars)rKr$r$r%loadzszConfiguration.loadc Cs8|jdk stdy |jdStk r2dSXdS)z7Returns the file with highest priority in configurationNz)Need to be specified a file to be editingr)r?AssertionError_get_parser_to_modify IndexError)rKr$r$r%get_file_to_edits  zConfiguration.get_file_to_editcCs |jjS)z`Returns key-value pairs like dict.items() representing the loaded configuration ) _dictionaryitems)rKr$r$r%rUszConfiguration.items)keyrc Cs2y |j|Stk r,td|YnXdS)z#Get a value from the configuration.zNo such key - N)rTKeyErrorr )rKrVr$r$r% get_values zConfiguration.get_value)rVvaluercCst|j|jst|j\}}|dk rTt|\}}|j|sF|j||j|||||j|j|<|j ||dS)z$Modify a value in the configuration.N) _ensure_have_load_onlyr?rPrQr, has_section add_sectionsetrI_mark_as_modified)rKrVrYfnameparsersectionrr$r$r% set_values     zConfiguration.set_valuecCs|j|jst||j|jkr0td||j\}}|dk rt|\}}|j|od|j||sntd|j |s|j ||j |||j|j|=dS)z#Unset a value in the configuration.zNo such key - Nz4Fatal Internal error [id=1]. Please report as a bug.) rZr?rPrIr rQr,r[ remove_optionrUremove_sectionr^)rKrVr_r`rarr$r$r% unset_values      zConfiguration.unset_valuec Cs\|jxN|jD]D\}}tjd|ttjj|t|d}|j |WdQRXqWdS)z!Save the current in-memory state.z Writing to %swN) rZrJloggerinforr-r.dirnameopenwrite)rKr_r`fr$r$r%saves   zConfiguration.savecCs$|jdkrtdtjd|jdS)Nz'Needed a specific file to be modifying.z$Will be working with %s variant only)r?r rgdebug)rKr$r$r%rZs z$Configuration._ensure_have_load_onlycCs&i}xtD]}|j|j|q W|S)z3A dictionary representing the loaded configuration.)rGupdaterI)rKretvalr@r$r$r%rTs zConfiguration._dictionarycCst|j}|tjddtjgkr4tjddSxf|jD]Z\}}xP|D]H}|j dk rt||j krttjd||qL|j ||}|j |j ||fqLWq>WdS)z,Loads configuration from configuration filesrr(zZSkipping loading configuration files due to environment's PIP_CONFIG_FILE being os.devnullNz Skipping file '%s' (variant: %s)) dictiter_config_filesr:rr-devnullrgrnrUr? _load_filerHappend)rK config_filesr@filesr_r`r$r$r%rMs   z Configuration._load_config_files)r@r_rcCsPtjd|||j|}x2|jD]&}|j|}|j|j|j||q"W|S)Nz'For variant '%s', will try loading '%s')rgverbose_construct_parsersectionsrUrIro_normalized_keys)rKr@r_r`rarUr$r$r%rts   zConfiguration._load_file)r_rcCstj}tjj|r|y|j|WnXtk rNtdjt j d|dYn.tj k rz}zt|dWYdd}~XnX|S)Nzcontains invalid {} charactersF)reasonr_)error) configparserRawConfigParserr-r.existsreadUnicodeDecodeErrorr r)localegetpreferredencodingError)rKr_r`r}r$r$r%rys   zConfiguration._construct_parsercCs"|jtjj|jd|jdS)z.Loads configuration from environment variablesz:env:N)rIr:rror{get_environ_vars)rKr$r$r%rNs z$Configuration._load_environment_vars)rarUrcCs2i}x(|D] \}}|dt|}|||<q W|S)zNormalizes items to construct a dictionary with normalized keys. This routine is where the names become keys and are made the same regardless of source - configuration files or environment. r')r&)rKrarU normalizedrvalrVr$r$r%r{"s  zConfiguration._normalized_keysccsHxBtjjD]4\}}|jdr |ddj}|tkr ||fVq WdS)z@Returns a generator with all environmental vars with prefix PIP_ZPIP_N)r-environrUr#r!ENV_NAMES_IGNORED)rKrVrrr$r$r%r0s  zConfiguration.get_environ_varsccstjjdd}|dk r&tj|gfVn tjgfVt}tj|tjfV|j ob|o`tjj | }|rztj |tj fVtj |tj fVdS)zYields variant and configuration files associated with it. This should be treated like items of a dictionary. PIP_CONFIG_FILEN) r-rgetr:rr<rr>r.rrr)rK config_filervZshould_load_user_configr$r$r%rr9s zConfiguration.iter_config_files)r@rcCs |j|S)z#Get values present in a config file)rI)rKr@r$r$r%get_values_in_configWsz"Configuration.get_values_in_configcCs*|js t|j|j}|s"td|dS)Nz4Fatal Internal error [id=2]. Please report as a bug.r()r?rPrHr )rKparsersr$r$r%rQ[s   z#Configuration._get_parser_to_modify)r_r`rcCs"||f}||jkr|jj|dS)N)rJru)rKr_r`Zfile_parser_tupler$r$r%r^is zConfiguration._mark_as_modifiedcCs|jjd|jdS)N())rL__name__rT)rKr$r$r%__repr__nszConfiguration.__repr__)N)%r __module__ __qualname____doc__boolrrrCrOstrrSrrrrUrXrbrermrZpropertyrrTrMrrtryrNr{rrrrrrQr^r __classcell__r$r$)rLr%r=Ws0      r=)rr),rr~rr-r6ZtypingrrrrrrrZpip._internal.exceptionsr r Zpip._internal.utilsr Zpip._internal.utils.compatr Zpip._internal.utils.loggingr Zpip._internal.utils.miscrrrrrr0rr:rrrrrrGrDrrgr&r,r<r=r$r$r$r% s6$