3 (\;3@sdZddlZddlZddlZddlmZddlmZmZddl m Z m Z m Z m Z ddlmZmZddlmZerddlmZmZmZmZmZmZmZejZed eZejeZ d d Z!d d Z"eddddddZ#Gddde$Z%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) configparser)ConfigurationError!ConfigurationFileCouldNotBeLoaded)global_config_fileslegacy_config_filenew_config_filesite_config_file) ensure_direnum)MYPY_CHECK_RUNNING)AnyDictIterableListNewTypeOptionalTupleKindcCs*|jjdd}|jdr&|dd}|S)zFMake a name consistent regardless of source (environment or file) _-z--N)lowerreplace startswith)namern/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-build-k1a8l1r5/pip/pip/_internal/configuration.py_normalize_name)s  rcCs |jddS)N.)split)rrrr_disassemble_key3sr!userglobalsiteenvzenv-var)USERGLOBALSITEENVENV_VARcseZdZdZd)fdd ZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e ddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(ZZS)* 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. Ncstt|jtjtjtjdg}||krJtdjdj t t |dd ||_ ||_ tjtjtjtjtjg|_ddg|_dd|jD|_dd|jD|_g|_dS) Nz5Got invalid value for load_only - should be one of {}z, rversionhelpcSsi|] }g|qSrr).0variantrrr fsz*Configuration.__init__..cSsi|] }i|qSrr)r.r/rrrr0is)superr+__init__kindsr&r'r(rformatjoinmapreprisolated load_onlyr)r*_override_order_ignore_env_names_parsers_config_modified_parsers)selfr9r:Z_valid_load_only) __class__rrr3Ps   zConfiguration.__init__cCs|j|js|jdS)zELoads configuration from configuration files and environment N)_load_config_filesr9_load_environment_vars)r@rrrloadnszConfiguration.loadc Cs8|jdk stdy |jdStk r2dSXdS)z@Returns the file with highest priority in configuration Nz)Need to be specified a file to be editingr)r:AssertionError_get_parser_to_modify IndexError)r@rrrget_file_to_editvs   zConfiguration.get_file_to_editcCs |jjS)z`Returns key-value pairs like dict.items() representing the loaded configuration ) _dictionaryitems)r@rrrrJszConfiguration.itemsc Cs2y |j|Stk r,tdj|YnXdS)z,Get a value from the configuration. zNo such key - {}N)rIKeyErrorrr5)r@keyrrr get_values zConfiguration.get_valuecCsj|j|j\}}|dk rJt|\}}|j|s<|j||j|||||j|j|<|j||dS)z-Modify a value in the configuration. N) _ensure_have_load_onlyrFr! has_section add_sectionsetr>r:_mark_as_modified)r@rLvaluefnameparsersectionrrrr set_values    zConfiguration.set_valuec Cs|j||j|jkr&tdj||j\}}|dk rt|\}}d}|j|r`|j||}|rt |j |}y t |}Wnt k rd}YnX|dkr|j ||j||ntd|j|j|=dS)z,Unset a value in the configuration. zNo such key - {}NFz4Fatal Internal error [id=1]. Please report as a bug.)rNr>r:rr5rFr!rO remove_optioniterrJnext StopIterationremove_sectionrR) r@rLrTrUrVrZmodified_somethingZ section_itervalrrr 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) rNr?loggerinfor ospathdirnameopenwrite)r@rTrUfrrrsaves   zConfiguration.savecCs$|jdkrtdtjd|jdS)Nz'Needed a specific file to be modifying.z$Will be working with %s variant only)r:rr`debug)r@rrrrNs z$Configuration._ensure_have_load_onlycCs(i}x|jD]}|j|j|q W|S)z)r@retvalr/rrrrIs zConfiguration._dictionarycCst|j}|tjddtjgkr4tjddSxf|jD]Z\}}xP|D]H}|j dk rt||j krttjd||qL|j ||}|j |j ||fqLWq>WdS)z5Loads configuration from configuration files rrzZSkipping loading configuration files due to environment's PIP_CONFIG_FILE being os.devnullNz Skipping file '%s' (variant: %s)) dict_iter_config_filesr4r)rbdevnullr`rirJr: _load_filer=append)r@Z config_filesr/filesrTrUrrrrBs    z Configuration._load_config_filescCsPtjd|||j|}x2|jD]&}|j|}|j|j|j||q"W|S)Nz'For variant '%s', will try loading '%s')r`ri_construct_parsersectionsrJr>rj_normalized_keys)r@r/rTrUrVrJrrrros   zConfiguration._load_filecCstj}tjj|r|y|j|WnXtk rNtdjt j d|dYn.tj k rz}zt|dWYdd}~XnX|S)Nzcontains invalid {} charactersF)reasonrT)error) rRawConfigParserrbrcexistsreadUnicodeDecodeErrorrr5localegetpreferredencodingError)r@rTrUrvrrrrrs   zConfiguration._construct_parsercCs"|jtjj|jd|jdS)z7Loads configuration from environment variables z:env:N)r>r4r*rjrt_get_environ_vars)r@rrrrC0s z$Configuration._load_environment_varscCs2i}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)r@rVrJ normalizedrr]rLrrrrt8s  zConfiguration._normalized_keysccsVxPtjjD]B\}}|jdo2|ddj|jk}|r |ddj|fVq WdS)z@Returns a generator with all environmental vars with prefix PIP_ZPIP_N)rbenvironrJrrr<)r@rLr]Zshould_be_yieldedrrrr~Es  zConfiguration._get_environ_varsccstjjdd}|dk r&tj|gfVn tjgfVtjttfV|j oZ|oXtj j | }|rptj t t gfVtjtgfVdS)zYields variant and configuration files associated with it. This should be treated like items of a dictionary. PIP_CONFIG_FILEN)rbrgetr4r)r'listrr9rcrxr&rrr(r)r@ config_fileZshould_load_user_configrrrrmQs  z Configuration._iter_config_filescCs |j|j}|std|dS)Nz4Fatal Internal error [id=2]. Please report as a bug.rr1)r=r:r)r@parsersrrrrFns  z#Configuration._get_parser_to_modifycCs"||f}||jkr|jj|dS)N)r?rp)r@rTrUZfile_parser_tuplerrrrR|s zConfiguration._mark_as_modified)N)__name__ __module__ __qualname____doc__r3rDrHrJrMrWr^rhrNpropertyrIrBrorrrCrtr~rmrFrR __classcell__rr)rArr+Bs(   '    r+)&rr{loggingrbpip._vendor.six.movesrZpip._internal.exceptionsrrZpip._internal.locationsrrrrZpip._internal.utils.miscr r Zpip._internal.utils.typingr Ztypingr r rrrrrrwstrr getLoggerrr`rr!r4objectr+rrrr s,  $