Back to index

moin  1.9.0~rc2
Public Member Functions | Private Attributes
MoinMoin.i18n.tools.check_i18n.TextFinder Class Reference

List of all members.

Public Member Functions

def __init__
def setFilename
def visitModule
def walk
def parseNode
def addText
def addBadCall
def dictionary
def bad
def found

Private Attributes

 _name
 _dictionary
 _found
 _bad
 _filename
 _visited
 _lineno

Detailed Description

Walk through AST tree and collect text from gettext calls

Find all calls to gettext function in the source tree and collect
the texts in a dict. Use compiler to create an abstract syntax tree
from each source file, then find the nodes for gettext function
call, and get the text from the call.

Localized texts are used usually translated during runtime by
gettext functions and apear in the source as
_('text...'). TextFinder class finds calls to the '_' function in
any namespace, or your prefered gettext function.

Note that TextFinder will only retrieve text from function calls
with a constant argument like _('text'). Calls like _('text' % locals()),
_('text 1' + 'text 2') are marked as bad call in the report, and the
text is not retrieved into the dictionary.

Note also that texts in source can appear several times in the same
file or different files, but they will only apear once in the
dictionary that this tool creates.

The dictionary value for each text is a dictionary of filenames each
containing a list of (best guess) lines numbers containning the text.

Definition at line 40 of file check_i18n.py.


Constructor & Destructor Documentation

def MoinMoin.i18n.tools.check_i18n.TextFinder.__init__ (   self,
  name = '_' 
)
Init with the gettext function name or '_'

Definition at line 66 of file check_i18n.py.

00066 
00067     def __init__(self, name='_'):
00068         """ Init with the gettext function name or '_'"""
00069         self._name = name       # getText function name
00070         self._dictionary = {}   # Unique texts in the found texts
00071         self._found = 0         # All good calls including duplicates
00072         self._bad = 0           # Bad calls: _('%s' % var) or _('a' + 'b')


Member Function Documentation

Called when a bad call like _('a' + 'b') is found

Definition at line 153 of file check_i18n.py.

00153 
00154     def addBadCall(self, node):
00155         """Called when a bad call like _('a' + 'b') is found"""
00156         self._bad = self._bad + 1
00157         print
00158         print "<!> Warning: non-constant _ call:"
00159         print " `%s`" % str(node)
00160         print " `%s`:%s" % (self._filename, self._lineno)

Here is the caller graph for this function:

Add text to dictionary and count found texts.

Note that number of texts in dictionary could be different from
the number of texts found, because some texts appear several
times in the code.

Each text value is a dictionary of filenames that contain the
text and each filename value is the list of line numbers with
the text. Missing line numbers are recorded as 'NA'.

self._lineno is the last line number we checked. It may be the line
number of the text, or near it.

Definition at line 125 of file check_i18n.py.

00125 
00126     def addText(self, text):
00127         """ Add text to dictionary and count found texts.
00128 
00129         Note that number of texts in dictionary could be different from
00130         the number of texts found, because some texts appear several
00131         times in the code.
00132 
00133         Each text value is a dictionary of filenames that contain the
00134         text and each filename value is the list of line numbers with
00135         the text. Missing line numbers are recorded as 'NA'.
00136 
00137         self._lineno is the last line number we checked. It may be the line
00138         number of the text, or near it.
00139         """
00140 
00141         self._found = self._found + 1
00142 
00143         # Create key for this text if needed
00144         if text not in self._dictionary:
00145             self._dictionary[text] = {}
00146 
00147         # Create key for this filename if needed
00148         textInfo = self._dictionary[text]
00149         if self._filename not in textInfo:
00150             textInfo[self._filename] = [self._lineno]
00151         else:
00152             textInfo[self._filename].append(self._lineno)

Here is the caller graph for this function:

Definition at line 166 of file check_i18n.py.

00166 
00167     def bad(self):
00168         return self._bad

Definition at line 163 of file check_i18n.py.

00163 
00164     def dictionary(self):
00165         return self._dictionary

Definition at line 169 of file check_i18n.py.

00169 
00170     def found(self):
00171         return self._found
00172 

Parse function call nodes and collect text 

Definition at line 102 of file check_i18n.py.

00102 
00103     def parseNode(self, node):
00104         """ Parse function call nodes and collect text """
00105 
00106         # Get the current line number. Since not all nodes have a line number
00107         # we save the last line number - it should be close to the gettext call
00108         if node.lineno is not None:
00109             self._lineno = node.lineno
00110 
00111         if node.__class__ == CallFunc and node.args:
00112             child = node.node
00113             klass = child.__class__
00114             if (# Standard call _('text')
00115                 (klass == Name and child.name == self._name) or
00116                 # A call to an object attribute: object._('text')
00117                 (klass == Getattr and child.attrname == self._name)):
00118                 if node.args[0].__class__ == Const:
00119                     # Good call with a constant _('text')
00120                     self.addText(node.args[0].value)
00121                 else:
00122                     self.addBadCall(node)
00123                 return 1
00124         return 0

Here is the call graph for this function:

Here is the caller graph for this function:

Remember the filename we are parsing

Definition at line 73 of file check_i18n.py.

00073 
00074     def setFilename(self, filename):
00075         """Remember the filename we are parsing"""
00076         self._filename = filename

Start the search from the top node of a module

This is the entry point into the search. When compiler.walk is
called it calls this method with the module node.

This is the place to initialize module specific data.

Definition at line 77 of file check_i18n.py.

00077 
00078     def visitModule(self, node):
00079         """ Start the search from the top node of a module
00080 
00081         This is the entry point into the search. When compiler.walk is
00082         called it calls this method with the module node.
00083 
00084         This is the place to initialize module specific data.
00085         """
00086         self._visited = {}  # init node cache - we will visit each node once
00087         self._lineno = 'NA' # init line number
00088 
00089         # Start walking in the module node
00090         self.walk(node)

Walk through all nodes 

Definition at line 91 of file check_i18n.py.

00091 
00092     def walk(self, node):
00093         """ Walk through all nodes """
00094         if node in self._visited:
00095             # We visited this node already
00096             return
00097 
00098         self._visited[node] = 1
00099         if not self.parseNode(node):
00100             for child in node.getChildNodes():
00101                 self.walk(child)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 71 of file check_i18n.py.

Definition at line 69 of file check_i18n.py.

Definition at line 75 of file check_i18n.py.

Definition at line 70 of file check_i18n.py.

Definition at line 86 of file check_i18n.py.

Definition at line 68 of file check_i18n.py.

Definition at line 85 of file check_i18n.py.


The documentation for this class was generated from the following file: