Attachment 'check_i18n.py'

Download

   1 #! /usr/bin/env python
   2 
   3 """
   4 Searches in the MoinMoin sources for calls of get_text() or _() and tries to
   5 extract the parameter. Then it checks the language modules if those parameters
   6 are in the dictionary.
   7 
   8 usage:
   9 
  10   check_i18n.py [lang1 lang2]
  11 
  12 If no parameter is given check_i18n.py checks all languages. This script
  13 assumes that it is called from MoinMoin/scripts. Please adjust the path if
  14 needed below.
  15 """
  16 
  17 import sys, re, os.path
  18 
  19 MoinMoin_dir = ".."
  20 sys.path.append(os.path.join(MoinMoin_dir, ".."))
  21 
  22 from MoinMoin import i18n
  23 from MoinMoin.util import pysupport
  24 
  25 one_line_re = r"(_|get_text) *\( *(('[^']*')|(\"[^\"]*\")) *\)"
  26 multi_line_re = r'(_|get_text)\s*\(\s*"""((\s|.)*?)"""\s*\)'
  27 
  28 def search(file_name):
  29     text_dict = {}
  30     f = open(file_name, 'r')
  31     text = f.read()
  32     f.close()
  33     for match in re.finditer(one_line_re, text):
  34         found = match.group(2)[1:-1]
  35         if text_dict.has_key(found):
  36             text_dict[found].append(file_name)
  37         else:
  38             text_dict[found] = [file_name]
  39     for match in re.finditer(multi_line_re, text):
  40         found = match.group(2)
  41         if text_dict.has_key(found):
  42             text_dict[found].append(file_name)
  43         else:
  44             text_dict[found] = [file_name]
  45     return text_dict
  46 
  47 def visitor(text_dict, dirname, names):
  48     for file_name in names:
  49         if file_name[-3:] == ".py":
  50             tmp_dict = search(os.path.join(dirname, file_name))
  51             for text, files in tmp_dict.items():
  52                 if text_dict.has_key(text):
  53                     text_dict[text].extend(files)
  54                 else:
  55                     text_dict[text] = files
  56             
  57 def process_files(path):
  58     text_dict = {}
  59     os.path.walk('..', visitor, text_dict)
  60     return text_dict
  61 
  62 
  63 def check_language(text_dict, lang):
  64     language = pysupport.importName("MoinMoin.i18n." + lang, "text")
  65     if not language:
  66         print "Language %s not found!" % lang
  67         return
  68 
  69     print "\n\nCHECKING LANGUAGE : ", lang
  70     print "========================\n"
  71     for text in text_dict:
  72         if not language.has_key(text):
  73             print "%r\nnot found. It's used in" % text,
  74             for file in text_dict[text]:
  75                 print file,
  76             print "\n"
  77     #for text in language:
  78     #    if not text_dict.has_key(text):
  79     #        print "%r\n defined but not found in source\n" % text
  80 
  81 def main():
  82     text = {}
  83     text_dict = process_files(MoinMoin_dir)
  84     if len(sys.argv) > 1:
  85         for lang in sys.argv[1:]:
  86             check_language(text_dict, lang)            
  87     else:
  88         for lang in i18n.languages:
  89             check_language(text_dict, lang)
  90 
  91     #for text in text_dict:
  92     #    print text
  93     #    print
  94 
  95 main()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2003-12-07 18:15:54, 2.7 KB) [[attachment:check_i18n.py]]
  • [get | view] (2003-12-07 18:15:54, 6.0 KB) [[attachment:findtext.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.