Mc@s|dZddlZyeWn!ek r=ddlmZnXddlZddlmZm Z m Z m Z m Z ddl mZmZmZddlmZddd gZd Zd Zidd 6dd 6d!d6dd6d"d6d#d6d$d6dd6Zd%gZdZdZejeded&dedZed'dedZed(dedZdZ dZ!d Z"dS()sBasic 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`. iN(tSet(tgenerate_tokenstCOMMENTtNAMEtOPtSTRING(tparse_encodingt pathmatchtrelpath(tdedenttextracttextract_from_dirtextract_from_filesrestructuredtext ensbabel.extractorst_tgettextiitngettexttugettextt ungettexttdgettextit dngettexttN_s**.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(sHelper function for `extract` that strips comment tags from strings in a list of comment lines. This functions operates in-place. cs8x1D])}|j|r|t|jSqW|S(N(t startswithtlentstrip(tlinettag(ttags(s:/usr/lib/python2.7/site-packages/babel/messages/extract.pyt_strip>s N(tmap(tcommentsRR((Rs:/usr/lib/python2.7/site-packages/babel/messages/extract.pyt_strip_comment_tags:sccs|dkri}ntjj|}xtj|D]\}} } x<| D]4} | jdsq| jdrM| j| qMqMW| j| jx| D]} ttjj || j tj d|} x|D]\} }t | | rtjj || }i}x2|j D]$\}}t || r|}qqW|r^|| ||nxIt||d|d|d|d|D] \}}}| |||fVqWPqqWqWq7WdS( sR Extract messages from any source files found in the given directory. This function generates tuples of the form: ``(filename, lineno, message, comments)`` 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 :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. :return: an iterator over ``(filename, lineno, funcname, message)`` tuples :rtype: ``iterator`` :see: `pathmatch` t.R t/tkeywordst comment_tagstoptionststrip_comment_tagsN(tNonetostpathtabspathtwalkRtremovetsortRtjointreplacetsepRtitemsR (tdirnamet method_mapt options_mapR"R#tcallbackR%tabsnametroottdirnamest filenamestsubdirtfilenametpatterntmethodtfilepathR$topatterntodicttlinenotmessageR((s:/usr/lib/python2.7/site-packages/babel/messages/extract.pyR Fs<G      !   cCsDt|d}z#tt||||||SWd|jXdS(sExtract 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) :return: the list of extracted messages :rtype: `list` tUN(topentlistR tclose(R<R:R"R#R$R%tfileobj((s:/usr/lib/python2.7/site-packages/babel/messages/extract.pyR s c csd}d|ksd|krd|krU|jd}|| ||d}} n|jdd\}} tt|ii| g| }n{yddlm} Wn4tk ritd6t d6} | j |}n1Xx-| j t |D]} | j dt}PqW|dkr+td |n|||j|d |pIi} x| D]\}}}}|r||p{d}nd}t|ttfs|g}n|sqVng}t}t|}xU|D]M}||krt}Pn||d}|dkrt}Pn|j|qW|r0qVn|d d}||sd t|d rc|jpfd|f}tjt|IJqVnt|}t|dkr|d }n|rt||n|||fVqVWdS(sExtract messages from the given file-like object using the specified extraction method. This function returns a list of 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!', []) :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. :return: the list of extracted messages :rtype: `list` :raise ValueError: if the extraction method is not registered t:R ii(t working_settignoreRtrequiresUnknown extraction method %rR$is%s:%itnames (unknown)N(i(i(R&trfindtsplittgetattrt __import__t pkg_resourcesRHt ImportErrortextract_nothingtextract_pythontgettiter_entry_pointst GROUP_NAMEtloadtTruet ValueErrortkeyst isinstanceRDttupletFalseRtappendthasattrRKtsyststderrtempty_msgid_warningR(R<RFR"R#R$R%tfunctlastdottmoduletattrnameRHtbuiltint entry_pointtresultsR@tfuncnametmessagesRtspectmsgstinvalidt last_indextindexRAtfirst_msg_indextwhere((s:/usr/lib/python2.7/site-packages/babel/messages/extract.pyR sl* $            cCsgS(sdPseudo extractor that does not actually extract anything, but simply returns an empty list. ((RFR"R#R$((s:/usr/lib/python2.7/site-packages/babel/messages/extract.pyRR@sccsd}}}d}g}g} g} t} } d} t|pQ|jdd}t|j}x|D]\}}\}}}}|dkr|tkr|dkrt} qj|tkr|dkr| rt} qjn|r|}|d7}qqj| r"|tkr"|dkr"t} qjqj|dkr|t kr|j |dj }| r| dd |dkr| j ||fqjnx|D]2} |j | rt} | j ||fPqqWqj|r|d kr|tkr|d kr|r| j d j||2n | j dt| dkrEt| } n | d } | rv| dd |dkrvg} n||| g| D]}|d^qfVd}}}d}g} g} t} q|tkr'td ||fiid 6i}t|tr|j |}n|j |q|tkr|dkr|rb| j d j||2n | j d| r| j\}}| j |d|fqqqj|d kr|tkr|d kr|d8}qj|r|dkrd}qj|tkrj||krj|}qjqjWdS(sAExtract messages from Python source code. :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) :return: an iterator over ``(lineno, funcname, message, comments)`` tuples :rtype: ``iterator`` itencodings iso-8859-1tdeftclasst(iRGit)ts# coding=%s %st __builtins__t,N(RtRu(R&R]RRTRtreadlineRRXRRtdecodeRR^RR-RR\RtevalR[tstrtpop(RFR"R#R$RjR@tmessage_linenot call_stacktbufRkttranslator_commentstin_deftin_translator_commentst comment_tagRsttokensttoktvalueR tcommentt old_linenot old_comment((s:/usr/lib/python2.7/site-packages/babel/messages/extract.pyRSGs  "$           $  ccsddlm}m}d}}g}d} g} t} |jdd} d} d}xb||jj| D]E}|jdkr|j dkr|r|j }|d7}qn|dkrc|jdkrc|j d j }| r | dd |j dkr | j |j |fqrnx|D]5}|j |r'| j |j |j fPq'q'WnN|jd kr8g} |j d d !j }x |D]}|j |r|j}|r-|d j |d sD   (   hq  p