ó *¨óQc@sRdZddlZddlZddlmZmZmZmZmZddl m Z m Z m Z ddl mZmZddlmZdZi dd6dd 6dd 6dd 6d d6d!d6d"d6dd6d$d6Zd%gZdZd„Zdeded&ded„Zed'ded„Zed(ded„Zd„Zd„Zd„ZdS()sa babel.messages.extract ~~~~~~~~~~~~~~~~~~~~~~ Basic infrastructure for extracting localizable messages from source files. This module defines an extensible system for collecting localizable message strings from a variety of sources. A native extractor for Python source files is builtin, extractors for other sources can be added using very simple plugins. The main entry points into the extraction functionality are the functions `extract_from_dir` and `extract_from_file`. :copyright: (c) 2013 by the Babel Team. :license: BSD, see LICENSE for more details. iÿÿÿÿN(tgenerate_tokenstCOMMENTtNAMEtOPtSTRING(tparse_encodingt pathmatchtrelpath(tPY2t text_type(tdedentsbabel.extractorst_tgettextiitngettexttugettextt ungettexttdgettextit dngettexttN_tctpgettexts**.pytpythons‹%s: warning: Empty msgid. It is reserved by GNU gettext: gettext("") returns the header entry with meta information, not the empty string.cs#‡fd†}t||ƒ|(dS(s‹Helper function for `extract` that strips comment tags from strings in a list of comment lines. This functions operates in-place. cs8x1ˆD])}|j|ƒr|t|ƒjƒSqW|S(N(t startswithtlentstrip(tlinettag(ttags(s@/usr/local/lib/python2.7/site-packages/babel/messages/extract.pyt_strip6s N(tmap(tcommentsRR((Rs@/usr/local/lib/python2.7/site-packages/babel/messages/extract.pyt_strip_comment_tags2sccsß|dkrtjƒ}n|dkr0i}ntjj|ƒ}x–tj|ƒD]…\}} } x<| D]4} | jdƒsŒ| jdƒrh| j| ƒqhqhW| jƒ| jƒx | D]} t tjj || ƒj tj dƒ|ƒ} xâ|D]Ú\} }t | | ƒrõtjj || ƒ}i}x2|jƒD]$\}}t || ƒr8|}q8q8W|ry|| ||ƒnxOt||d|d|d|d|ƒD]&\}}}}| ||||fVq¡WPqõqõWq»WqRWdS( s8 Extract messages from any source files found in the given directory. This function generates tuples of the form ``(filename, lineno, message, comments, context)``. Which extraction method is used per file is determined by the `method_map` parameter, which maps extended glob patterns to extraction method names. For example, the following is the default mapping: >>> method_map = [ ... ('**.py', 'python') ... ] This basically says that files with the filename extension ".py" at any level inside the directory should be processed by the "python" extraction method. Files that don't match any of the mapping patterns are ignored. See the documentation of the `pathmatch` function for details on the pattern syntax. The following extended mapping would also use the "genshi" extraction method on any file in "templates" subdirectory: >>> method_map = [ ... ('**/templates/**.*', 'genshi'), ... ('**.py', 'python') ... ] The dictionary provided by the optional `options_map` parameter augments these mappings. It uses extended glob patterns as keys, and the values are dictionaries mapping options names to option values (both strings). The glob patterns of the `options_map` do not necessarily need to be the same as those used in the method mapping. For example, while all files in the ``templates`` folders in an application may be Genshi applications, the options for those files may differ based on extension: >>> options_map = { ... '**/templates/**.txt': { ... 'template_class': 'genshi.template:TextTemplate', ... 'encoding': 'latin-1' ... }, ... '**/templates/**.html': { ... 'include_attrs': '' ... } ... } :param dirname: the path to the directory to extract messages from. If not given the current working directory is used. :param method_map: a list of ``(pattern, method)`` tuples that maps of extraction method names to extended glob patterns :param options_map: a dictionary of additional options (optional) :param keywords: a dictionary mapping keywords (i.e. names of functions that should be recognized as translation functions) to tuples that specify which of their arguments contain localizable strings :param comment_tags: a list of tags of translator comments to search for and include in the results :param callback: a function that is called for every file that message are extracted from, just before the extraction itself is performed; the function is passed the filename, the name of the extraction method and and the options dictionary as positional arguments, in that order :param strip_comment_tags: a flag that if set to `True` causes all comment tags to be removed from the collected comments. :see: `pathmatch` t.R t/tkeywordst comment_tagstoptionststrip_comment_tagsN(tNonetostgetcwdtpathtabspathtwalkRtremovetsortRtjointreplacetsepRtitemstextract_from_file(tdirnamet method_mapt options_mapR"R#tcallbackR%tabsnametroottdirnamest filenamestsubdirtfilenametpatterntmethodtfilepathR$topatterntodicttlinenotmessageRtcontext((s@/usr/local/lib/python2.7/site-packages/babel/messages/extract.pytextract_from_dir>s@E       !   cCsDt|dƒ}z#tt||||||ƒƒSWd|jƒXdS(szExtract messages from a specific file. This function returns a list of tuples of the form ``(lineno, funcname, message)``. :param filename: the path to the file to extract messages from :param method: a string specifying the extraction method (.e.g. "python") :param keywords: a dictionary mapping keywords (i.e. names of functions that should be recognized as translation functions) to tuples that specify which of their arguments contain localizable strings :param comment_tags: a list of translator tags to search for and include in the results :param strip_comment_tags: a flag that if set to `True` causes all comment tags to be removed from the collected comments. :param options: a dictionary of additional options (optional) trbN(topentlisttextracttclose(R>R<R"R#R$R%tfileobj((s@/usr/local/lib/python2.7/site-packages/babel/messages/extract.pyR2¨s ccsOd}d|ksd|kr‘d|krU|jdƒ}|| ||d}} n|jddƒ\}} tt|ii| gƒ| ƒ}n‘yddlm} Wntk r¸n1Xx-| jt |ƒD]} | j dt ƒ}PqÌW|dkr"it d6t d6td 6} | j|ƒ}n|dkrAtd |ƒ‚n|||jƒ|d |p_iƒ} xã| D]Û\}}}}|r—||p‘d}nd}t|ttfƒs¾|g}n|sÊqlnd}g}t}t|ƒ}x||D]t}t|tƒr||d d}qïn||kr2t }Pn||d}|dkrVt }Pn|j|ƒqïW|rsqlnt|d tƒr—|dd}n|d d}||sõd t|dƒrÊ|jpÍd|f}tjjt|dƒqlnt|ƒ}t|ƒdkr |d }n|r6t||ƒn||||fVqlWdS(sÒExtract messages from the given file-like object using the specified extraction method. This function returns tuples of the form ``(lineno, message, comments)``. The implementation dispatches the actual extraction to plugins, based on the value of the ``method`` parameter. >>> source = '''# foo module ... def run(argv): ... print _('Hello, world!') ... ''' >>> from StringIO import StringIO >>> for message in extract('python', StringIO(source)): ... print message (3, u'Hello, world!', [], None) :param method: a string specifying the extraction method (.e.g. "python"); if this is a simple name, the extraction function will be looked up by entry point; if it is an explicit reference to a function (of the form ``package.module:funcname`` or ``package.module.funcname``), the corresponding function will be imported and used :param fileobj: the file-like object the messages should be extracted from :param keywords: a dictionary mapping keywords (i.e. names of functions that should be recognized as translation functions) to tuples that specify which of their arguments contain localizable strings :param comment_tags: a list of translator tags to search for and include in the results :param options: a dictionary of additional options (optional) :param strip_comment_tags: a flag that if set to `True` causes all comment tags to be removed from the collected comments. :raise ValueError: if the extraction method is not registered t:R iiÿÿÿÿ(t working_settrequiretignoreRt javascriptsUnknown extraction method %rR$is%s:%itnames (unknown)s N(i(i(R&trfindtsplittgetattrt __import__t pkg_resourcesRMt ImportErrortiter_entry_pointst GROUP_NAMEtloadtTruetextract_nothingtextract_pythontextract_javascripttgett ValueErrortkeyst isinstanceRHttupletFalseRtappendthasattrRQtsyststderrtwritetempty_msgid_warningR(R>RKR"R#R$R%tfunctlastdottmoduletattrnameRMt entry_pointtbuiltintresultsRBtfuncnametmessagesRtspecRDtmsgstinvalidt last_indextindexRCtfirst_msg_indextwhere((s@/usr/local/lib/python2.7/site-packages/babel/messages/extract.pyRIÃs‚& $              cCsgS(sdPseudo extractor that does not actually extract anything, but simply returns an empty list. ((RKR"R#R$((s@/usr/local/lib/python2.7/site-packages/babel/messages/extract.pyR\Asc#sYd}}}d}g}g} g} t} } d} tˆƒpQ|jddƒ‰trfˆj}n‡‡fd†}t|ƒ}xÎ|D]Æ\}}\}}}}|dkrÓ|tkrÓ|dkrÓt} q‹|t kr|dkr| rýt} q‹n|rQ|}|d7}qQq‹| rC|t krC|d krCt} q‹q‹|dkrú|t krútrs|j ˆƒ}n|dj ƒ}| rº| dd |dkrº| j ||fƒq‹nx”|D]2} |j| ƒrÁt} | j ||fƒPqÁqÁWq‹|rä|d krä|t kr÷|d kr÷|rG| j d j|ƒƒ|2n | j dƒt| ƒdkrut| ƒ} n | d } | r¦| dd |dkr¦g} n||| g| D]}|d^q¶fVd}}}d}g} g} t} qQ|tkrdtd tˆƒ|fiid6iƒ}trTt|tƒ rT|j ˆƒ}n|j |ƒqQ|t krQ|dkrQ|rŸ| j d j|ƒƒ|2n | j dƒ| rá| jƒ\}}| j |d|fƒqáqQq‹|d kr|t kr|d kr|d8}q‹|r0|dkr0d}q‹|tkr‹||kr‹|}q‹q‹WdS(scExtract messages from Python source code. It returns an iterator yielding tuples in the following form ``(lineno, funcname, message, comments)``. :param fileobj: the seekable, file-like object the messages should be extracted from :param keywords: a list of keywords (i.e. function names) that should be recognized as translation functions :param comment_tags: a list of translator tags to search for and include in the results :param options: a dictionary of additional options (optional) :rtype: ``iterator`` iÿÿÿÿtencodings iso-8859-1csˆjƒjˆƒS(N(treadlinetdecode((R{RK(s@/usr/local/lib/python2.7/site-packages/babel/messages/extract.pytdstdeftclasst(iRLit)ts# coding=%s %st __builtins__t,N(RR€(R&RdRR_RR|RRR[RRR}RReRR.RRcRtevaltstrRbR tpop(RKR"R#R$RrRBtmessage_linenot call_stacktbufRsttranslator_commentstin_deftin_translator_commentst comment_tagt next_linettokensttoktvalueR tcommentt old_linenot old_comment((R{RKs@/usr/local/lib/python2.7/site-packages/babel/messages/extract.pyR]Hsš   "$           $  ccs¿ddlm}m}d}}g}d} g} t} |jddƒ} d} d}xb||jƒj| ƒƒD]E}|jdkrµ|j dkrµ|r±|j }|d7}q±nü|dkrc|jdkrc|j d j ƒ}| r | dd |j dkr | j |j |fƒqrnxŽ|D]5}|j |ƒr'| j |j |j ƒfƒPq'q'WnN|jd kr8g} |j d d !j ƒ}x |D]œ}|j |ƒr•|jƒ}|r-|d j ƒ|d s<  (   h}  y