B /H@s,yddlZejZWnek r.ddZYnXedZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZdZdgadtZdZed Zd%d d Zd d ZddZddZddZddZddZddZddZGdddZddZe d kr(eed!d"d#ied$dS)&NcCs|S)N)srr9/opt/alt/python37/lib64/python3.7/Tools/i18n/pygettext.pyrapygettext -- Python equivalent of xgettext(1) Many systems (Solaris, Linux, Gnu) provide extensive tools that ease the internationalization of C programs. Most of these tools are independent of the programming language and can be used from within Python programs. Martin von Loewis' work[1] helps considerably in this regard. There's one problem though; xgettext is the program that scans source code looking for message strings, but it groks only C (or C++). Python introduces a few wrinkles, such as dual quoting characters, triple quoted strings, and raw strings. xgettext understands none of this. Enter pygettext, which uses Python's standard tokenize module to scan Python source code, generating .pot files identical to what GNU xgettext[2] generates for C and C++ code. From there, the standard GNU tools can be used. A word about marking Python strings as candidates for translation. GNU xgettext recognizes the following keywords: gettext, dgettext, dcgettext, and gettext_noop. But those can be a lot of text to include all over your code. C and C++ have a trick: they use the C preprocessor. Most internationalized C source includes a #define for gettext() to _() so that what has to be written in the source is much less. Thus these are both translatable strings: gettext("Translatable String") _("Translatable String") Python of course has no preprocessor so this doesn't work so well. Thus, pygettext searches only for _() by default, but see the -k/--keyword flag below for how to augment this. [1] http://www.python.org/workshops/1997-10/proceedings/loewis.html [2] http://www.gnu.org/software/gettext/gettext.html NOTE: pygettext attempts to be option and feature compatible with GNU xgettext where ever possible. However some options are still missing or are not fully implemented. Also, xgettext's use of command line switches with option arguments is broken, and in these cases, pygettext just defines additional switches. Usage: pygettext [options] inputfile ... Options: -a --extract-all Extract all strings. -d name --default-domain=name Rename the default output file from messages.pot to name.pot. -E --escape Replace non-ASCII characters with octal escape sequences. -D --docstrings Extract module, class, method, and function docstrings. These do not need to be wrapped in _() markers, and in fact cannot be for Python to consider them docstrings. (See also the -X option). -h --help Print this help message and exit. -k word --keyword=word Keywords to look for in addition to the default set, which are: %(DEFAULTKEYWORDS)s You can have multiple -k flags on the command line. -K --no-default-keywords Disable the default set of keywords (see above). Any keywords explicitly added with the -k/--keyword option are still recognized. --no-location Do not write filename/lineno location comments. -n --add-location Write filename/lineno location comments indicating where each extracted string is found in the source. These lines appear before each msgid. The style of comments is controlled by the -S/--style option. This is the default. -o filename --output=filename Rename the default output file from messages.pot to filename. If filename is `-' then the output is sent to standard out. -p dir --output-dir=dir Output files will be placed in directory dir. -S stylename --style stylename Specify which style to use for location comments. Two styles are supported: Solaris # File: filename, line: line-number GNU #: filename:line The style name is case insensitive. GNU style is the default. -v --verbose Print the names of the files being processed. -V --version Print the version of pygettext and exit. -w columns --width=columns Set width of output to columns. -x filename --exclude-file=filename Specify a file that contains a list of strings that are not be extracted from the input files. Each string to be excluded must appear on a line by itself in the file. -X filename --no-docstrings=filename Specify a file that contains a list of files (one per line) that should not have their docstrings extracted. This is only useful in conjunction with the -D option above. If `inputfile' is -, standard input is read. z1.5_z, a# SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR ORGANIZATION # FIRST AUTHOR , YEAR. # msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "POT-Creation-Date: %(time)s\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=%(charset)s\n" "Content-Transfer-Encoding: %(encoding)s\n" "Generated-By: pygettext.py %(version)s\n" cCs4ttttjd|r&t|tjdt|dS)N)file)print__doc__globalssysstderrexit)codemsgrrrusagesrcCs|rd}tand}taddt|DaxtddD]}t|t|<q4Wdttd<d ttd <d ttd <d ttd<dttd<dS)NcSsg|] }d|qS)z\%03or).0irrr sz make_escapes.. z\\\z\t z\r z\n z\"") escape_asciiescapeescape_nonasciirangeescapeschrord)Z pass_nonasciimodrrrr make_escapess    r'cCsddd|DS)Nrcss*|]"}t|dkrtt|n|VqdS)rN)r%r#)rcrrr szescape_ascii..)join)rencodingrrrrsrcCsddd||DS)Nrcss|]}t|VqdS)N)r#)rbrrrr)sz"escape_nonascii..)r*encode)rr+rrrr!sr!cCs$|ddkp"|ddko"|ddkS)Nrz'"ZrRuUr)rrrris_literal_stringsr/cCst|diiiS)N __builtins__)eval)rrrr safe_evalsr2cCs|d}t|dkr*dt||d}n\|dsH|d=|dd|d<x&tt|D]}t|||||<qVWd}d||d}|S)Nrr.rz\n" "z"" ")splitlenr r"r*)rr+linesrZlinetermrrr normalizes  r7csdfdd|DkS)z6Check whether 'str' contains ANY of the chars in 'set'r.csg|] }|kqSrr)rr()strrrrszcontainsAny..r)r8setr)r8r containsAnysr:cstj|s|t|drDt|}g}x|D]}|t|q*W|Sytj |}|j }Wnt k rrd}YnX|s|gStj |rg}tj jdxDt|D]6\}}d|kr|d|fdd|DqW|Stj|r|gSgS)z]Get a list of module files for a filename, a module or package name, or a directory. z*?[]NrZCVScs.g|]&}tj|dkrtj|qS)r.)ospathsplitextr*)rr )_py_extrootrrr+sz#getFilesForName..)r;r<existsr:globextendgetFilesForName importlibutil find_specorigin ImportErrorisdir machinerySOURCE_SUFFIXESwalkremove)namefileslistr specdirsr)r>r?rrC s4           rCc@s^eZdZddZddZddZddZd d Zd d Zd dZ dddZ ddZ ddZ dS) TokenEatercCs6||_i|_|j|_g|_d|_d|_d|_d|_dS)Nr3r.r) _TokenEater__options_TokenEater__messages_TokenEater__waiting_TokenEater__state_TokenEater__data_TokenEater__lineno_TokenEater__freshmodule_TokenEater__curfile_TokenEater__enclosurecount)selfoptionsrrr__init__7szTokenEater.__init__cCs||||ddS)Nr)rW)r]ttypetstringZstupZetuplinerrr__call__AszTokenEater.__call__cCs|j}|jr|j|js|jrh|tjkrNt|rN|j t ||ddd|_n|tj tj fkrdd|_dS|tj kr|dkr|j|_dS|tj kr||jkr|j|_dS)Nr.) isdocstringr)classdef)rT docstrings nodocstringsgetr[rZtokenizeSTRINGr/_TokenEater__addentryr2COMMENTNLNAME_TokenEater__suiteseenrWkeywords_TokenEater__keywordseen)r]r`ralinenooptsrrrZ __waitingHszTokenEater.__waitingcCsX|tjkrT|dkr&|jdkr&|j|_n.|dkr>|jd7_n|dkrT|jd8_dS)N:rz([{r.z)]})rjOPr\_TokenEater__suitedocstringrW)r]r`rarsrrrZ __suiteseen[s  zTokenEater.__suiteseencCsP|tjkr0t|r0|jt||dd|j|_n|tjtjtj fkrL|j|_dS)Nr.)rd) rjrkr/rlr2rVrWNEWLINEINDENTrm)r]r`rarsrrrZ__suitedocstringfs    zTokenEater.__suitedocstringcCs4|tjkr(|dkr(g|_||_|j|_n|j|_dS)N()rjrvrXrY_TokenEater__openseenrWrV)r]r`rarsrrrZ __keywordseenps  zTokenEater.__keywordseencCs|tjkr4|dkr4|jr*|t|j|j|_nj|tjkrXt |rX|j t |nF|tj t jt jt jtjgkrttd||j|jdtjd|j|_dS)N)z:*** %(file)s:%(lineno)s: Seen unexpected token "%(token)s")tokenr rs)r )rjrvrXrl EMPTYSTRINGr*rVrWrkr/appendr2rmr}ryDEDENTrxrnr rr[rYr r)r]r`rarsrrrZ __openseenxs   zTokenEater.__openseenNrcCs:|dkr|j}||jjkr6|j|f}||j|i|<dS)N)rYrT toexcluder[rU setdefault)r]rrsrdentryrrrZ __addentrys   zTokenEater.__addentrycCs||_d|_dS)Nr.)r[rZ)r]filenamerrr set_filenameszTokenEater.set_filenamecCs|j}td}|jr|jnd}tt|t|dd|di}x<|jD].\}}t | }| t |g ||fqJWt | } xR| D]H} || } | x0| D]&\}}t|} t | }|jsn|j|jkrx|D]&\} }| |d}ttd||dqWn|j|jkrd}x^|D]V\} }| |d}td |}t|t||jkrp||}nt||dd|}q0Wt|d krt||d| rtd |dtd t|||dtd |dqWqWdS)Nz%Y-%m-%d %H:%M%zzUTF-8Z8bit)timeversioncharsetr+)r )rrsz&# File: %(filename)s, line: %(lineno)dz#:z %(filename)s:%(lineno)dz #, docstringZmsgidz msgstr "" )rTrZstrftimer+r pot_header __version__rUitemssortedkeysrtuplersortanyvalueswritelocations locationstyleSOLARISrGNUr5widthr7)r]fpr^Z timestampr+reversekvrZrkeysZrkeyZrentriesrdrrsdZloclinerrrrwritesN            zTokenEater.write)Nr) __name__ __module__ __qualname__r_rcrVrprwrrr{rlrrrrrrrS6s    rScCsyBttjdddddddddd d d d d ddddddg\}}Wn.tjk rp}ztd|Wdd}~XYnXGddd}|}|j|jd}x|D]\}}|dkrtdq|dkrd|_q|dkr|d|_q|dkrd|_ q|dkrd|_ q|dkr|j |q|dkr&ga q|d kr8d|_q|d!krJd|_q|d"kr|||_|jdkrtdtd#|q|d$kr||_q|d%kr||_q|d&krd|_q|d'krttd(ttdq|d)kr$yt||_Wn(tk r tdtd*|YnXq|d+kr6||_q|d,krt|}z.x(|} | sZPd|j| dd-<qJWWd|XqWt |j |j !t |jry t|j}|"|_#|Wn8t$k rttd.|jtj%d/tdYnXng|_#g} x2|D]*}|d0kr*| |n| !t&|qW| }t'|} x|D]} | d0kr|jrtttd1tj(j)}d} n&|jrttd2| t| d3}d} z| *| y(t++|j}x|D]}| |qWWnZt+j,k r6}z8td4|j-d| |j-dd|j-ddftj%d/Wdd}~XYnXWd| rJ|XqRW|jd0krjtj.}d} n,|jrt/j01|j|j|_t|jd5}d} z| 2|Wd| r|XdS)6Nr.zad:DEhk:Kno:p:S:Vvw:x:X:z extract-allzdefault-domain=r helpzkeyword=zno-default-keywordsz add-locationz no-locationzoutput=z output-dir=zstyle=verboserzwidth=z exclude-file=rgz no-docstringsc@sDeZdZdZdZdZdZgZdZdZ dZ eZ dZ dZ dZdZiZdS)zmain..Optionsr.rrrz messages.potNN)rrrrr extractallr rqoutpathoutfilerrrrexcludefilenamergrhrrrrOptionssr)ZgnuZsolaris)z-hz--helpr)z-az --extract-all)z-dz--default-domainz.pot)z-Ez--escape)z-Dz --docstrings)z-kz --keyword)z-Kz--no-default-keywords)z-nz--add-location)z --no-location)z-Sz--stylezInvalid value for --style: %s)z-oz--output)z-pz --output-dir)z-vz --verbose)z-Vz --versionz%pygettext.py (xgettext for Python) %s)z-wz--widthz'--width argument must be an integer: %s)z-xz--exclude-file)z-Xz--no-docstringsr3zCan't read --exclude-file: %s)r -zReading standard inputz Working on %srbz%s: %s, line %d, column %dw)3getoptr argverrorrrrrrr rgrqrdefault_keywordsrrilowerrrrrr rrintr ValueErrorropenreadlinerhcloser'rB readlinesrIOErrorrrCrSstdinbufferrrj TokenErrorargsstdoutr;r<r*r)rtrrrr^Z locationsZoptargrrbZexpandedZeaterrZcloseptokensZ_tokenerrrmains                                  &   r__main__z%*** Seen unexpected token "%(token)s"r}ZtestZmorethanonestring)r)!ZfintlgettextrrHr r;importlib.machineryrDimportlib.utilr rArrr}rjrrr*ZDEFAULTKEYWORDSr~rrr'rr!r/r2r7r:rCrSrrrrrrsN   ,"