Package translate :: Package convert :: Module po2moz
[hide private]
[frames] | no frames]

Source Code for Module translate.convert.po2moz

  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  """convert Gettext PO localization files to Mozilla .dtd and .properties files 
 23   
 24  see: http://translate.sourceforge.net/wiki/toolkit/po2moz for examples and 
 25  usage instructions 
 26  """ 
 27   
 28  import os.path 
 29   
 30  from translate.convert import po2dtd 
 31  from translate.convert import po2prop 
 32  from translate.convert import prop2mozfunny 
 33  from translate.storage import xpi 
 34  from translate.convert import convert 
 35   
 36   
37 -class MozConvertOptionParser(convert.ArchiveConvertOptionParser):
38
39 - def __init__(self, formats, usetemplates=False, usepots=False, 40 description=None):
41 convert.ArchiveConvertOptionParser.__init__(self, formats, usetemplates, usepots, 42 description=description, 43 archiveformats={"xpi": xpi.XpiFile})
44
45 - def initoutputarchive(self, options):
46 """creates an outputarchive if required""" 47 if options.output and self.isarchive(options.output, 'output'): 48 newlang = None 49 newregion = None 50 if options.locale is not None: 51 if options.locale.count("-") > 1: 52 raise ValueError("Invalid locale: %s - should be of the form xx-YY" % options.locale) 53 elif "-" in options.locale: 54 newlang, newregion = options.locale.split("-") 55 else: 56 newlang, newregion = options.locale, "" 57 if options.clonexpi is not None: 58 originalxpi = xpi.XpiFile(options.clonexpi, "r") 59 options.outputarchive = originalxpi.clone(options.output, "w", 60 newlang=newlang, 61 newregion=newregion) 62 elif self.isarchive(options.template, 'template'): 63 options.outputarchive = options.templatearchive.clone(options.output, "a", 64 newlang=newlang, 65 newregion=newregion) 66 else: 67 if os.path.exists(options.output): 68 options.outputarchive = xpi.XpiFile(options.output, "a", 69 locale=newlang, 70 region=newregion) 71 else: 72 # FIXME: this is unlikely to work because it has no jar files 73 options.outputarchive = xpi.XpiFile(options.output, "w", 74 locale=newlang, 75 region=newregion)
76
77 - def splitinputext(self, inputpath):
78 """splits a inputpath into name and extension""" 79 # TODO: not sure if this should be here, was in po2moz 80 d, n = os.path.dirname(inputpath), os.path.basename(inputpath) 81 s = n.find(".") 82 if s == -1: 83 return (inputpath, "") 84 root = os.path.join(d, n[:s]) 85 ext = n[s+1:] 86 return (root, ext)
87
88 - def recursiveprocess(self, options):
89 """recurse through directories and convert files""" 90 self.replacer.replacestring = options.locale 91 result = super(MozConvertOptionParser, self).recursiveprocess(options) 92 if self.isarchive(options.output, 'output'): 93 if options.progress in ('console', 'verbose'): 94 print "writing xpi file..." 95 options.outputarchive.close() 96 return result
97 98
99 -def main(argv=None):
100 # handle command line options 101 formats = {("dtd.po", "dtd"): ("dtd", po2dtd.convertdtd), 102 ("properties.po", "properties"): ("properties", 103 po2prop.convertmozillaprop), 104 ("it.po", "it"): ("it", prop2mozfunny.po2it), 105 ("ini.po", "ini"): ("ini", prop2mozfunny.po2ini), 106 ("inc.po", "inc"): ("inc", prop2mozfunny.po2inc), 107 # (None, "*"): ("*", convert.copytemplate), 108 ("*", "*"): ("*", convert.copyinput), 109 "*": ("*", convert.copyinput)} 110 # handle search and replace 111 replacer = convert.Replacer("${locale}", None) 112 for replaceformat in ("js", "rdf", "manifest"): 113 formats[(None, replaceformat)] = (replaceformat, 114 replacer.searchreplacetemplate) 115 formats[(replaceformat, replaceformat)] = (replaceformat, 116 replacer.searchreplaceinput) 117 formats[replaceformat] = (replaceformat, replacer.searchreplaceinput) 118 parser = MozConvertOptionParser(formats, usetemplates=True, description=__doc__) 119 parser.add_option("-l", "--locale", dest="locale", default=None, 120 help="set output locale (required as this sets the directory names)", 121 metavar="LOCALE") 122 parser.add_option("", "--clonexpi", dest="clonexpi", default=None, 123 help="clone xpi structure from the given xpi file") 124 parser.add_fuzzy_option() 125 parser.replacer = replacer 126 parser.run(argv)
127 128 129 if __name__ == '__main__': 130 main() 131