3 HXU @sdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl mZddl mZmZddlmZmZmZdd lmZdd lmZdd lmZmZmZmZdd lm Z dd l!m"Z"m#Z#ddl$m%Z%m&Z&ddl'm(Z)ddl*m+Z,ddl-m.Z.m/Z/yddl0m1Z1Wn"e2k rFddl3m1Z1YnXd*ddZ4Gddde,Z+Gddde+Z5Gddde+Z6ddZ7Gddde+Z8Gdd d e+Z9Gd!d"d"e:Z;d#d$Ze?d)kre<dS),z babel.messages.frontend ~~~~~~~~~~~~~~~~~~~~~~~ Frontends for the message extraction functionality. :copyright: (c) 2013 by the Babel Team. :license: BSD, see LICENSE for more details. )print_functionN)datetime)getpreferredencoding) __version__)Locale localedata)StringIO string_types text_type)UnknownLocaleError)Catalog)DEFAULT_KEYWORDSDEFAULT_MAPPINGcheck_and_call_extract_fileextract_from_dir)write_mo)read_powrite_po)LOCALTZodict)log)Command)DistutilsOptionErrorDistutilsSetupError)RawConfigParsercCsg}t|ttfs|g}xV|D]N}|dkr,qt|ttfrN|jt||dq|jddt|j|DqWtdd|Dst|S)a Make a list out of an argument. Values from `distutils` argument parsing are always single strings; values from `optparse` parsing may be lists of strings that may need to be further split. No matter the input, this function returns a flat list of whitespace-trimmed strings, with `None` values filtered out. >>> listify_value("foo bar") ['foo', 'bar'] >>> listify_value(["foo bar"]) ['foo', 'bar'] >>> listify_value([["foo"], "bar"]) ['foo', 'bar'] >>> listify_value([["foo"], ["bar", None, "foo"]]) ['foo', 'bar', 'foo'] >>> listify_value("foo, bar, quux", ",") ['foo', 'bar', 'quux'] :param arg: A string or a list of strings :param split: The argument to pass to `str.split()`. :return: N)splitcss|]}|jVqdS)N)strip).0sr/usr/lib/python3.6/frontend.py Osz listify_value..css|]}t|tVqdS)N) isinstancer )rvalrrr r!Ps) r"listtupleextend listify_valuer rallAssertionError)argroutr#rrr r'*s "r'c@s.eZdZdZfZfZiZiZeZ dddZ dS)rNcCs0||_|jd|_d|_d|_d|_d|_dS)NFr) distributioninitialize_optionsZ_dry_runverboseforcehelpZ finalized)selfdistrrr __init__rszCommand.__init__)N) __name__ __module__ __qualname__as_argsmultiple_value_optionsboolean_optionsoption_aliasesoption_choices distutils_logrr3rrrr rTsrc @sNeZdZdZdZd d!d"d#d$d%d&gZddgZddZddZddZ ddZ dS)'compile_catalogaCatalog compilation command for use in ``setup.py`` scripts. If correctly installed, this command is available to Setuptools-using setup scripts automatically. For projects using plain old ``distutils``, the command needs to be registered explicitly in ``setup.py``:: from babel.messages.frontend import compile_catalog setup( ... cmdclass = {'compile_catalog': compile_catalog} ) .. versionadded:: 0.9 z+compile message catalogs to binary MO filesdomain=D>domains of PO files (space separated list, default 'messages') directory=d.path to base directory containing the catalogs input-file=iname of the input file output-file=oQname of the output file (default '//LC_MESSAGES/.mo')locale=l locale of the catalog to compile use-fuzzyfalso include fuzzy translations statisticsN#print statistics about translationscCs.d|_d|_d|_d|_d|_d|_d|_dS)NmessagesF)domain directory input_file output_filelocale use_fuzzyrP)r1rrr r-sz"compile_catalog.initialize_optionscCs@t|j|_|j r$|j r$td|j r<|j rr?r@)rArBrC)rDrErF)rGrHrI)rJrKrL)rMrNrO)rPNrQ) r4r5r6__doc__ description user_optionsr9r-rYr[rZrrrr r=}s*  r=c@seZdZdZdZdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYgZd dddddd+gZd2ZdZZd[d\d]d^d9Z d:d_iZ d>d?Z d@dAZ dBdCZ dDdEZdS)`extract_messagesaMessage extraction command for use in ``setup.py`` scripts. If correctly installed, this command is available to Setuptools-using setup scripts automatically. For projects using plain old ``distutils``, the command needs to be registered explicitly in ``setup.py``:: from babel.messages.frontend import extract_messages setup( ... cmdclass = {'extract_messages': extract_messages} ) z1extract localizable strings from the project codecharset=N3charset to use in the output file (default "utf-8") keywords=kispace-separated list of keywords to look for in addition to the defaults (may be repeated multiple times)no-default-keywords#do not include the default keywords mapping-file=F&path to the mapping configuration file no-location>do not include location comments with filename and line number add-location=location lines format. If it is not given or "full", it generates the lines with both file name and line number. If it is "file", the line number part is omitted. If it is "never", it completely suppresses the lines (same as --no-location). omit-header'do not include msgid "" entry in header output-file=rHname of the output filewidth=w"set output line width (default 76)no-wrapVdo not break long message lines, longer than the output line width, into several lines sort-output&generate sorted output (default False) sort-by-file,sort output by file location (default False)msgid-bugs-address=set report address for msgidcopyright-holder=set copyright holder in outputproject=set project name in outputversion=set project version in output add-comments=cuplace comment block with TAG (or those preceding keyword lines) in output file. Separate multiple TAGs with commas(,)strip-commentsr)strip the comment TAGs from the comments. input-paths=ofiles or directories that should be scanned for messages. Separate multiple files or directories with commas(,) input-dirs=@alias for input-paths (does allow files as well as directories).z input-paths add-commentskeywords --keyword --mapping--output--strip-comment-tags)rz mapping-filez output-filezstrip-commentsz add-locationfullfilenevercCsd|_d|_d|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_d|_d|_d|_d|_dS)Nzutf-8FT)charsetrno_default_keywords mapping_file no_location add_location omit_headerrV input_dirs input_pathswidthno_wrap sort_output sort_by_filemsgid_bugs_addresscopyright_holderprojectversion add_commentsstrip_commentsinclude_lineno)r1rrr r-Ms*z#extract_messages.initialize_optionscCs|jr|js|j|_ntd|jr*i}ntj}|jtt|j ||_ |j sZtd|j shtd|j r||j r|td|j r|j rd|_ n|j dk rt |j |_ |jr|jrtd|jrt|jtrtjd|j|_n8|jdk rtjdd |jjpfDj|_ng|_|js*td x*|jD] }tjj|s2td |q2Wt|jpbfd |_|jr|js|jj|_|js|jj |_|j!d krd|_"n|j!dkrd|_#dS)Nz1input-dirs and input-paths are mutually exclusivez=you must specify new keywords if you disable the default oneszno output file specifiedz0'--no-wrap' and '--width' are mutually exclusiveLz;'--sort-output' and '--sort-by-file' are mutually exclusivez,\s*cSsg|]}|jdddqS).r^r)r)rr~rrr sz5extract_messages.finalize_options..z'no input files or directories specifiedzInput path: %s does not exist,rTrF)$rrrrr copyupdateparse_keywordsr'rrVrrintrrr"r rerr,dictfromkeysZpackageskeysrbrcrfrrZget_namerZ get_versionrrr)r1rrcrrr rYdsZ            z!extract_messages.finalize_optionscs@j}tjd }tjjjjjd}x|D]\}}fdd}t j j rt j }t |||jjj|}nt||jj|jd}xX|D]P\} } } } } t j j r| }nt j jt j j| }|j| d|| fg| | dqWq:Wjjdjt||jjjjjjdWdQRXdS) Nr`)rrrrrcsl|dkr dStjjr}ntjjtjj|}d}|rXddjdd|jD}jjd||dS)Nignorez (%s)z, cSsg|]\}}d||fqS)z%s="%s"r)rr~vrrr rsz:extract_messages.run..callback..zextracting messages from %s%s)rbrcisfilenormpathrditemsrrk)filenamemethodoptionsfilepathZoptstr)rcr1rr callbacks  z&extract_messages.run..callback)rZ comment_tagsrZstrip_comment_tags)Z auto_commentscontextzwriting PO template file to %s)rrrrrr) _get_mappingsrhrVr rrrrrrbrcrgetcwdrrrrrrrdaddrrkrrrrrrr)r1mappingsrvrs method_map options_maprZ current_dirZ extractedrrnrtZcommentsrrr)rcr1r r[sD       zextract_messages.runc Csg}|jrPt|jd}t|\}}WdQRXx|jD]}|j|||fq6Wnt|jddr|jj}x|jD]l\}}t |t rtt |\}}n8gi}}x,|D]$\}} } |j|| f| pi||<qW|j|||fqpWn x|jD]}|j|t ifqW|S)NUmessage_extractors) rrh parse_mappingrragetattrr,rrr"r rr) r1rfileobjrrrcrmappingpatternrrrrr rs&    zextract_messages._get_mappings)r{Nr|)r}r~r)rNr)rrr)rNr)rNr)rNr)rrHr)rrr)rNr)rNr)rNr)rNr)rNr)rNr)rNr)rrr)rrr)rNr)rNr)rr)r)r)r)r)rrr)r4r5r6rwrxryr9r7r8r:r;r-rYr[rrrrr rzsn  C=rzcCs"|dks tt|tstddS)agValidate the ``message_extractors`` keyword argument to ``setup()``. :param dist: the distutils/setuptools ``Distribution`` object :param name: the name of the keyword argument (should always be "message_extractors") :param value: the value of the keyword argument :raise `DistutilsSetupError`: if the value is not valid rzDthe value of the "message_extractors" parameter must be a dictionaryN)r)r"rr)r2namevaluerrr check_message_extractorss  rc @sDeZdZdZdZddd d!d"d#d$gZdgZddZddZddZ dS)% init_catalogaNew catalog initialization command for use in ``setup.py`` scripts. If correctly installed, this command is available to Setuptools-using setup scripts automatically. For projects using plain old ``distutils``, the command needs to be registered explicitly in ``setup.py``:: from babel.messages.frontend import init_catalog setup( ... cmdclass = {'init_catalog': init_catalog} ) z(create a new catalog based on a POT filedomain=r?&domain of PO file (default 'messages') input-file=rEname of the input file output-dir=rBpath to output directory output-file=rHQname of the output file (default '//LC_MESSAGES/.po')locale=rK$locale for the new localized catalogwidth=r"set output line width (default 76)no-wrapNVdo not break long message lines, longer than the output line width, into several linescCs.d|_d|_d|_d|_d|_d|_d|_dS)NrRF) output_dirrVrUrWrSrr)r1rrr r-3szinit_catalog.initialize_optionscCs|jstd|jstdytj|j|_Wn*tk rX}zt|WYdd}~XnX|j rr|j rrtd|jst j j |j|jd|j d|_t j j t j j|jst jt j j|j|jr|jrtd|j r|j rd|_n|jdk rt|j|_dS)Nzyou must specify the input filez-you must provide a locale for the new catalogz%you must specify the output directoryr\z.poz0'--no-wrap' and '--width' are mutually exclusiver)rUrrWrparse_localer rVrrbrcrdrSrfdirnamemakedirsrrr)r1errr rY<s*  zinit_catalog.finalize_optionscCs|jjd|j|jt|jd}t||jd}WdQRX|j|_tj t |_ d|_ t|jd}t |||jdWdQRXdS)Nzcreating catalog %s based on %sr])rWFr`)r)rrkrVrUrhrrWrrZnowrZ revision_daterlrr)r1rrrsrvrrr r[Xs zinit_catalog.run)rr?r)rrEr)rrBr)rrHr)rrKr)rrr)rNr) r4r5r6rwrxryr9r-rYr[rrrr rs(  rc @sTeZdZdZdZd(d)d*d+d,d-d.d/d0d1d2g Zdd!dddgZd"d#Zd$d%Zd&d'Z dS)3update_catalogaCatalog merging command for use in ``setup.py`` scripts. If correctly installed, this command is available to Setuptools-using setup scripts automatically. For projects using plain old ``distutils``, the command needs to be registered explicitly in ``setup.py``:: from babel.messages.frontend import update_catalog setup( ... cmdclass = {'update_catalog': update_catalog} ) .. versionadded:: 0.9 z'update message catalogs from a POT filedomain=r?&domain of PO file (default 'messages') input-file=rEname of the input file output-dir=rB.path to base directory containing the catalogs output-file=rHQname of the output file (default '//LC_MESSAGES/.po')locale=rK locale of the catalog to compilewidth=r"set output line width (default 76)no-wrapNVdo not break long message lines, longer than the output line width, into several linesignore-obsolete=1whether to omit obsolete messages from the outputno-fuzzy-matchingNdo not use fuzzy matchingupdate-header-commentupdate target header commentprevious+keep previous msgids of translated messageszignore-obsoletecCsFd|_d|_d|_d|_d|_d|_d|_d|_d|_d|_ d|_ dS)NrRF) rSrUrrVrWrrignore_obsoleteno_fuzzy_matchingupdate_header_commentr)r1rrr r-sz!update_catalog.initialize_optionscCs|jstd|j r&|j r&td|jr<|j r}tj|}|||jf} t| jtjjp*tp*ddqWdS|sL|jj d|d} | |j!krp|jj d| |j"| |dd} | j#S)z{Main entry point of the command-line interface. :param argv: list of arguments passed on the command-line Ncommand[args])usagerz--list-locales list_locales store_truez print all known locales and exit)destactionr0z-vz --verbose store_constloglevelzprint as much as possible)r,r+constr0z-qz--quietzprint as little as possibleF)r)r.r^cSsg|] }t|qSr)rj)r identifierrrr r,sz,CommandLineInterface.run..z %%-%ds %%sasciireplacerzQno valid command or option passed. Try the -h/--help option for more information.zunknown command "%s")r&r')$sysargvoptparse OptionParserr(rparserZdisable_interspersed_args_helpZ print_help add_optionloggingDEBUGZERRORZ set_defaultsINFO parse_args_configure_loggingr.r)rZlocale_identifiersmaxsortrrZ english_nameprintencodestdoutencodingrrmcommands_configure_commandr[) r1r4rargsZ identifierslongestformatr0rWoutputcmdnamecmdinstrrr r[sJ             zCommandLineInterface.runcCsdtjd|_|jj||jjr.|jjd}ntj}|jj||j|tjd}|j|dS)Nbabelrz %(message)s) r:Z getLoggerrZsetLevelZhandlersZ StreamHandlerZ addHandlerZ FormatterZ setFormatter)r1r.ZhandlerZ formatterrrr r>Bs     z'CommandLineInterface._configure_loggingcCspt|jjtdtdd|jD}dtd|d}t|jj}x |D]\}}t|||fqPWdS)Nz commands:cSsg|] }t|qSr)rj)rr&rrr rTsz.CommandLineInterface._help..z %%-%ds %%sr^)rAr7Z format_helpr?rEsortedr)r1rHrIrErrxrrr r8QszCommandLineInterface._helpcCs|j|}|}|jr|j|_t|ts,t|jtj|j|df|j |d}t |df}x|j D]\}}} |j d} t || j dd} d| g} |r| jd|| j|jj| f|jj| d } | |kr|jd | 7_qf| |jkr|j| d | d qf| |jkr(|j| d | | dqf|j| | | | dqfW|j|\}}|rht||j dd|x&t|jD]\}}t|||qvWy |jWn2tk r}z|jt|WYd d }~XnX|S)zB :type cmdname: str :type argv: list[str] r)r(rxr7=-_z--%sz-%sNz<%s>r*)r,r0ra)r,r0choices)r0defaultrS)command_classesrr"rr)r-r5r6r(rErryrr2rar&r:getr;r9r9r8r=setattrvarsrZensure_finalizedrrmstr)r1rKr4ZcmdclassrLr7r7ZlongZshortr0rrTZstrsrSrrGkeyrerrrrr rFZsF         z'CommandLineInterface._configure_command)N)r4r5r6rwr(VERSIONrrEr=rzrrrUrr[r>r8rFrrrr r#s  3 r#cCstjtjS)N)r#r[r3r4rrrr mainsr]c Csi}g}i}t}t|j|_|j||xb|jD]V}|dkrPt|j|}q4dd|jddD\}}|j||ft|j|||<q4W|rx4t |D](\} \}}||kr||}||f|| <qW||fS)aParse an extraction method mapping from a file-like object. >>> buf = StringIO(''' ... [extractors] ... custom = mypackage.module:myfunc ... ... # Python source files ... [python: **.py] ... ... # Genshi templates ... [genshi: **/templates/**.html] ... include_attrs = ... [genshi: **/templates/**.txt] ... template_class = genshi.template:TextTemplate ... encoding = latin-1 ... ... # Some custom extractor ... [custom: **/custom/*.*] ... ''') >>> method_map, options_map = parse_mapping(buf) >>> len(method_map) 4 >>> method_map[0] ('**.py', 'python') >>> options_map['**.py'] {} >>> method_map[1] ('**/templates/**.html', 'genshi') >>> options_map['**/templates/**.html']['include_attrs'] '' >>> method_map[2] ('**/templates/**.txt', 'genshi') >>> options_map['**/templates/**.txt']['template_class'] 'genshi.template:TextTemplate' >>> options_map['**/templates/**.txt']['encoding'] 'latin-1' >>> method_map[3] ('**/custom/*.*', 'mypackage.module:myfunc') >>> options_map['**/custom/*.*'] {} :param fileobj: a readable file-like object containing the configuration text to parse :see: `extract_from_directory` extractorscSsg|] }|jqSr)r)rpartrrr rsz!parse_mapping..:r^) rrZ _sectionsZreadfpZsectionsrrrrarg) rrr^rrr7Zsectionrrrqrrr rs$1  rcCsi}x|D]}d|kr&|jd\}}n |d}}||kr |rg}xH|jdD]:}|ddkrx|jt|dddfqL|jt|qLWt|}|||<q W|S)aQParse keywords specifications from the given list of strings. >>> kw = sorted(parse_keywords(['_', 'dgettext:2', 'dngettext:2,3', 'pgettext:1c,2']).items()) >>> for keyword, indices in kw: ... print((keyword, indices)) ('_', None) ('dgettext', (2,)) ('dngettext', (2, 3)) ('pgettext', ((1, 'c'), 2)) r`Nrr^rra)rrarr%)ZstringsrrifuncnameindicesZindsxrrr rs     r__main__)N)N)@rwZ __future__rr:r5rbrr!r3rrrWrrMrr\rrZ babel._compatrr r Z babel.corer Zbabel.messages.catalogr Zbabel.messages.extractr rrrZbabel.messages.mofilerZbabel.messages.pofilerrZ babel.utilrrZ distutilsrr<Z distutils.cmdrZ_CommandZdistutils.errorsrrZ ConfigParserr ImportErrorZ configparserr'r=rzrrrobjectr#r]rrr4rrrr  sV          *)[  I