3 t^?@sddlmZmZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z mZddlmZdd d Zd d Zdd dZGdddeZGdddeZGdddeZdS))absolute_importunicode_literalsN) defaultdict)partial) import_module)DistutilsOptionErrorDistutilsFileError) string_typesFc Csddlm}m}tjj|}tjj|s4td|tj}tj tjj |zJ|}|rb|j ng}||krx|j ||j ||dt||j|d}Wdtj |Xt|S)a,Read given configuration file and returns options from it as a dict. :param str|unicode filepath: Path to configuration file to get options from. :param bool find_others: Whether to search for other configuration files which could be on in various places. :param bool ignore_option_errors: Whether to silently ignore options, values of which could not be resolved (e.g. due to exceptions in directives such as file:, attr:, etc.). If False exceptions are propagated as expected. :rtype: dict r) Distribution _Distributionz%Configuration file %s does not exist.) filenames)ignore_option_errorsN)Zsetuptools.distr r ospathabspathisfilergetcwdchdirdirnameZfind_config_filesappendZparse_config_filesparse_configurationcommand_optionsconfiguration_to_dict) filepathZ find_othersr r r Zcurrent_directoryZdistr handlersr /config.pyread_configuration s$      rcCsltt}x^|D]V}|j}|j}xD|jD]:}t|d|d}|dkrNt||}n|}||||<q&WqW|S)zReturns configuration data gathered by given handlers as a dict. :param list[ConfigHandler] handlers: Handlers list, usually from parse_configuration() :rtype: dict zget_%sN)rdictsection_prefix target_obj set_optionsgetattr)rZ config_dictZhandlerZ obj_aliasr Zoptiongettervaluerrrr<s   rcCs2t|j||}|jt|||}|j||fS)aPerforms additional parsing of configuration options for a distribution. Returns a list of used option handlers. :param Distribution distribution: :param dict command_options: :param bool ignore_option_errors: Whether to silently ignore options, values of which could not be resolved (e.g. due to exceptions in directives such as file:, attr:, etc.). If False exceptions are propagated as expected. :rtype: list )ConfigMetadataHandlermetadataparseConfigOptionsHandler)Z distributionrr metaoptionsrrrrYs  rc@seZdZdZdZiZd!ddZeddZdd Z e d"d d Z e d dZ e ddZ e ddZeddZeddZe ddZe ddZe d#ddZddZdd ZdS)$ ConfigHandlerz1Handles metadata supplied in configuration files.NFcCsbi}|j}x:|jD].\}}|j|s(q|j|djd}|||<qW||_||_||_g|_dS)N.) ritems startswithreplacestripr r sectionsr!)selfr r*r r2r section_namesection_optionsrrr__init__s  zConfigHandler.__init__cCstd|jjdS)z.Metadata item name to parser function mapping.z!%s must provide .parsers propertyN)NotImplementedError __class____name__)r3rrrparsersszConfigHandler.parsersc Cst}|j}|jj||}t|||}||kr6t||r>dSd}|jj|}|ry ||}Wn tk r~d}|jszYnX|rdSt|d|d}|dkrt |||n|||j j |dS)NFTzset_%s) tupler aliasesgetr"KeyErrorr: Exceptionr setattrr!r) r3Z option_namer$unknownr Z current_valueZ skip_optionparsersetterrrr __setitem__s0   zConfigHandler.__setitem__,cCs8t|tr|Sd|kr |j}n |j|}dd|DS)zRepresents value as a list. Value is split either by separator (defaults to comma) or by lines. :param value: :param separator: List items separator character. :rtype: list  cSsg|]}|jr|jqSr)r1).0chunkrrr sz-ConfigHandler._parse_list..) isinstancelist splitlinessplit)clsr$ separatorrrr _parse_lists   zConfigHandler._parse_listcCsTd}i}xF|j|D]8}|j|\}}}||krsz,ConfigHandler._parse_file..rErFc3s2|]*}j|sdrtjj|rj|VqdS)TN) _assert_localrrr _read_file)rGr)rNrrr^ s)rJr r/lenrMjoin)rNr$Zinclude_directivespecZ filepathsr)rNr _parse_files   zConfigHandler._parse_filecCs|jtjstd|dS)Nz#`file:` directive can not access %s)r/rrr)rrrrr_szConfigHandler._assert_localc Cs"tj|dd }|jSQRXdS)Nzutf-8)encoding)ioopenread)rfrrrr`szConfigHandler._read_filec Csd}|j|s|S|j|djjd}|j}dj|}|p@d}tjjdt j zt |}t ||}Wdtjddt_X|S)zRepresents value as a module attribute. Examples: attr: package.attr attr: package.module.attr :param str value: :rtype: str zattr:r,r-r6rN) r/r0r1rMpoprbsysrinsertrrrr")rNr$Zattr_directiveZ attrs_pathZ attr_nameZ module_namemodulerrr _parse_attrs   zConfigHandler._parse_attrcsfdd}|S)zReturns parser function to represents value as a list. Parses a value applying given methods one after another. :param parse_methods: :rtype: callable cs|}xD] }||}q W|S)Nr)r$parsedmethod) parse_methodsrrr'Bs  z1ConfigHandler._get_parser_compound..parser)rNrrr'r)rrr_get_parser_compound9s z"ConfigHandler._get_parser_compoundcCs:i}|pdd}x$|jD]\}\}}||||<qW|S)zParses section options into a dictionary. Optionally applies a given parser to values. :param dict section_options: :param callable values_parser: :rtype: dict cSs|S)Nr)rWrrrWsz6ConfigHandler._parse_section_to_dict..)r.)rNr5Z values_parserr$rU_rWrrr_parse_section_to_dictLs  z$ConfigHandler._parse_section_to_dictc Cs@x:|jD].\}\}}y |||<Wq tk r6Yq Xq WdS)zQParses configuration file section. :param dict section_options: N)r.r>)r3r5namerur$rrr parse_section\s  zConfigHandler.parse_sectioncCsfx`|jjD]R\}}d}|r$d|}t|d|jddd}|dkrVtd|j|f||q WdS)zTParses configuration file items from one or more related sections. r,z_%szparse_section%sr-__Nz0Unsupported distribution option section: [%s.%s])r2r.r"r0rr)r3r4r5Zmethod_postfixZsection_parser_methodrrrr'hszConfigHandler.parse)F)rE)N)r9 __module__ __qualname____doc__rr<r6propertyr:rD classmethodrPrXr]rd staticmethodr_r`rorsrvrxr'rrrrr+ss&  &        r+c@s6eZdZdZdddddZdZedd Zd d Zd S) r%r&Zurl description classifiers platforms)Z home_pageZsummaryZ classifierplatformFc Cs8|j}|j}|j}||||||j||||||j|d S)z.Metadata item name to parser function mapping.) rkeywordsZprovidesZrequiresZ obsoletesrlicenserZlong_descriptionversionZ project_urls)rPrdrXrs_parse_version)r3 parse_listZ parse_file parse_dictrrrr:s zConfigMetadataHandler.parserscCsJ|j|}t|r|}t|tsFt|dr>djtt|}nd|}|S)zSParses `version` option value. :param value: :rtype: str __iter__r-z%s)rocallablerJr hasattrrbmapstr)r3r$rrrrrs   z$ConfigMetadataHandler._parse_versionN) r9rzr{rr<Z strict_moder}r:rrrrrr%s r%c@sTeZdZdZeddZddZddZdd Zd d Z d d Z ddZ ddZ dS)r(r*cCsL|j}t|jdd}|j}|j}|||||||||||||||j|j|dS)z.Metadata item name to parser function mapping.;)rO)Zzip_safeZuse_2to3Zinclude_package_dataZ package_dirZuse_2to3_fixersZuse_2to3_exclude_fixersZconvert_2to3_doctestsscriptsZeager_resourcesZdependency_linksZnamespace_packagesZinstall_requiresZsetup_requiresZ tests_requireZpackages entry_pointsZ py_modules)rPrr]rX_parse_packagesrd)r3rZparse_list_semicolonZ parse_boolrrrrr:s*zConfigOptionsHandler.parserscCsBd}|j|s|j|S|j|jjdi}ddlm}|f|S)zTParses `packages` option value. :param value: :rtype: list zfind:z packages.findr) find_packages)r/rPparse_section_packages__findr2r=Z setuptoolsr)r3r$Zfind_directive find_kwargsrrrrrs   z$ConfigOptionsHandler._parse_packagescsT|j||j}dddgtfdd|jD}|jd}|dk rP|d|d<|S)zParses `packages.find` configuration file section. To be used in conjunction with _parse_packages(). :param dict section_options: whereincludeZexcludecs$g|]\}}|kr|r||fqSrr)rGkv) valid_keysrrrIszEConfigOptionsHandler.parse_section_packages__find..Nr)rvrPrr.r=)r3r5Z section_datarrr)rrrs    z1ConfigOptionsHandler.parse_section_packages__findcCs|j||j}||d<dS)z`Parses `entry_points` configuration file section. :param dict section_options: rN)rvrP)r3r5rprrrparse_section_entry_pointssz/ConfigOptionsHandler.parse_section_entry_pointscCs.|j||j}|jd}|r*||d<|d=|S)N*r,)rvrPr=)r3r5rprootrrr_parse_package_data s  z(ConfigOptionsHandler._parse_package_datacCs|j||d<dS)z`Parses `package_data` configuration file section. :param dict section_options: Z package_dataN)r)r3r5rrrparse_section_package_datasz/ConfigOptionsHandler.parse_section_package_datacCs|j||d<dS)zhParses `exclude_package_data` configuration file section. :param dict section_options: Zexclude_package_dataN)r)r3r5rrr"parse_section_exclude_package_datasz7ConfigOptionsHandler.parse_section_exclude_package_datacCs"t|jdd}|j|||d<dS)zbParses `extras_require` configuration file section. :param dict section_options: r)rOZextras_requireN)rrPrv)r3r5rrrrparse_section_extras_require%sz1ConfigOptionsHandler.parse_section_extras_requireN) r9rzr{rr}r:rrrrrrrrrrrr(s  r()FF)F)Z __future__rrrfrrl collectionsr functoolsr importlibrZdistutils.errorsrrZsetuptools.extern.sixr rrrobjectr+r%r(rrrrs      . ;