Package translate :: Package tools :: Module podebug
[hide private]
[frames] | no frames]

Source Code for Module translate.tools.podebug

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  #  
  4  # Copyright 2004-2006 Zuza Software Foundation 
  5  #  
  6  # This file is part of translate. 
  7  # 
  8  # translate is free software; you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public License as published by 
 10  # the Free Software Foundation; either version 2 of the License, or 
 11  # (at your option) any later version. 
 12  #  
 13  # translate is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with translate; if not, write to the Free Software 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 21   
 22  """Insert debug messages into XLIFF and Gettex PO localization files 
 23   
 24  See: http://translate.sourceforge.net/wiki/toolkit/podebug for examples and 
 25  usage instructions 
 26  """ 
 27   
 28  from translate.storage import factory 
 29  import os 
 30  import re 
 31  import md5 
 32   
33 -class podebug:
34 - def __init__(self, format=None, rewritestyle=None, hash=None, ignoreoption=None):
35 if format is None: 36 self.format = "" 37 else: 38 self.format = format 39 self.rewritefunc = getattr(self, "rewrite_%s" % rewritestyle, None) 40 self.ignorefunc = getattr(self, "ignore_%s" % ignoreoption, None) 41 self.hash = hash
42
43 - def rewrite_xxx(self, string):
44 return "xxx%sxxx" % string
45
46 - def rewrite_en(self, string):
47 return string
48
49 - def rewrite_blank(self, string):
50 return ""
51
52 - def ignore_openoffice(self, locations):
53 for location in locations: 54 if location.startswith("Common.xcu#..Common.View.Localisation"): 55 return True 56 elif location.startswith("profile.lng#STR_DIR_MENU_NEW_"): 57 return True 58 elif location.startswith("profile.lng#STR_DIR_MENU_WIZARD_"): 59 return True 60 return False
61
62 - def ignore_mozilla(self, locations):
63 if len(locations) == 1 and locations[0].lower().endswith(".accesskey"): 64 return True 65 for location in locations: 66 if location.endswith(".height") or location.endswith(".width") or \ 67 location.endswith(".macWidth") or location.endswith(".unixWidth"): 68 return True 69 if location == "brandShortName" or location == "brandFullName" or location == "vendorShortName": 70 return True 71 if location.lower().endswith(".commandkey") or location.endswith(".key"): 72 return True 73 return False
74
75 - def convertunit(self, unit, prefix):
76 if self.ignorefunc: 77 if self.ignorefunc(unit.getlocations()): 78 return unit 79 if self.hash: 80 if unit.getlocations(): 81 hashable = unit.getlocations()[0] 82 else: 83 hashable = unit.source 84 prefix = md5.new(hashable).hexdigest()[:self.hash] + " " 85 if self.rewritefunc: 86 unit.target = self.rewritefunc(unit.source) 87 elif not unit.istranslated(): 88 unit.target = unit.source 89 if unit.hasplural(): 90 strings = unit.target.strings 91 for i, string in enumerate(strings): 92 strings[i] = prefix + string 93 unit.target = strings 94 else: 95 unit.target = prefix + unit.target 96 return unit
97
98 - def convertstore(self, store):
99 filename = self.shrinkfilename(store.filename) 100 prefix = self.format 101 for formatstr in re.findall("%[0-9c]*[sfFbBd]", self.format): 102 if formatstr.endswith("s"): 103 formatted = self.shrinkfilename(store.filename) 104 elif formatstr.endswith("f"): 105 formatted = store.filename 106 formatted = os.path.splitext(formatted)[0] 107 elif formatstr.endswith("F"): 108 formatted = store.filename 109 elif formatstr.endswith("b"): 110 formatted = os.path.basename(store.filename) 111 formatted = os.path.splitext(formatted)[0] 112 elif formatstr.endswith("B"): 113 formatted = os.path.basename(store.filename) 114 elif formatstr.endswith("d"): 115 formatted = os.path.dirname(store.filename) 116 else: 117 continue 118 formatoptions = formatstr[1:-1] 119 if formatoptions: 120 if "c" in formatoptions and formatted: 121 formatted = formatted[0] + filter(lambda x: x.lower() not in "aeiou", formatted[1:]) 122 length = filter(str.isdigit, formatoptions) 123 if length: 124 formatted = formatted[:int(length)] 125 prefix = prefix.replace(formatstr, formatted) 126 for unit in store.units: 127 if unit.isheader() or unit.isblank(): 128 continue 129 unit = self.convertunit(unit, prefix) 130 return store
131
132 - def shrinkfilename(self, filename):
133 if filename.startswith("." + os.sep): 134 filename = filename.replace("." + os.sep, "", 1) 135 dirname = os.path.dirname(filename) 136 dirparts = dirname.split(os.sep) 137 if not dirparts: 138 dirshrunk = "" 139 else: 140 dirshrunk = dirparts[0][:4] + "-" 141 if len(dirparts) > 1: 142 dirshrunk += "".join([dirpart[0] for dirpart in dirparts[1:]]) + "-" 143 baseshrunk = os.path.basename(filename)[:4] 144 if "." in baseshrunk: 145 baseshrunk = baseshrunk[:baseshrunk.find(".")] 146 return dirshrunk + baseshrunk
147
148 -def convertpo(inputfile, outputfile, templatefile, format=None, rewritestyle=None, hash=None, ignoreoption=None):
149 """reads in inputfile using po, changes to have debug strings, writes to outputfile""" 150 # note that templatefile is not used, but it is required by the converter... 151 inputstore = factory.getobject(inputfile) 152 if inputstore.isempty(): 153 return 0 154 convertor = podebug(format=format, rewritestyle=rewritestyle, hash=hash, ignoreoption=ignoreoption) 155 outputstore = convertor.convertstore(inputstore) 156 outputfile.write(str(outputstore)) 157 return 1
158
159 -def main():
160 from translate.convert import convert 161 formats = {"po":("po", convertpo), "xlf":("xlf", convertpo)} 162 parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__) 163 # TODO: add documentation on format strings... 164 parser.add_option("-f", "--format", dest="format", default="[%s] ", help="specify format string") 165 rewritestylelist = ["xxx", "en", "blank"] 166 parser.add_option("", "--rewrite", dest="rewritestyle", 167 type="choice", choices=rewritestylelist, metavar="STYLE", help="the translation rewrite style: %s" % ", ".join(rewritestylelist)) 168 ignoreoptionlist = ["openoffice", "mozilla"] 169 parser.add_option("", "--ignore", dest="ignoreoption", 170 type="choice", choices=ignoreoptionlist, metavar="APPLICATION", help="apply tagging ignore rules for the given application: %s" % ", ".join(ignoreoptionlist)) 171 parser.add_option("", "--hash", dest="hash", metavar="LENGTH", type="int", help="add an md5 hash to translations") 172 parser.passthrough.append("format") 173 parser.passthrough.append("rewritestyle") 174 parser.passthrough.append("ignoreoption") 175 parser.passthrough.append("hash") 176 parser.run()
177 178 179 if __name__ == '__main__': 180 main() 181