#!/usr/bin/python
# 
# Copyright 2002, 2003 Zuza Software Foundation
# 
# This file is part of translate.
#
# translate is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# translate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with translate; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""script that converts a set of .dtd and .properties files to a set of .po files"""

import os.path
from translate.convert import dtd2po
from translate.convert import prop2po
from translate.storage import xpi
from translate import __version__
from translate.convert import convert

class MozConvertOptionParser(convert.ConvertOptionParser):
  def __init__(self, formats, usetemplates=False, usepots=False):
    convert.ConvertOptionParser.__init__(self, formats, usetemplates, usepots)

  def isrecursive(self, fileoption):
    """checks if fileoption is a recursive file"""
    if self.isxpi(fileoption): return True
    return super(MozConvertOptionParser, self).isrecursive(fileoption)

  def isxpi(self, fileoption):
    """returns whether the file option is an xpi file"""
    return isinstance(fileoption, (str, unicode)) and fileoption.endswith(os.extsep + "xpi") and os.path.isfile(fileoption)

  def recurseinputfiles(self, options):
    """recurse through xpi file / directories and return files to be converted"""
    if options.template and self.isxpi(options.template):
      options.templatexpi = xpi.XpiFile(options.template, includenonloc=True)
    if self.isxpi(options.input):
      options.inputxpi = xpi.XpiFile(options.input, includenonloc=True)
      return self.recursexpifiles(options)
    else:
      return super(MozConvertOptionParser, self).recurseinputfiles(options)

  def recursexpifiles(self, options):
    """recurse through xpi files and convert files"""
    inputfiles = []
    for inputpath in options.inputxpi.iterextractnames():
      top, name = os.path.split(inputpath)
      if not self.isvalidinputname(options, name):
        continue
      inputfiles.append(inputpath)
    return inputfiles

  def openinputfile(self, options, fullinputpath):
    """opens the input file"""
    if self.isxpi(options.input):
      jarfilename, filename = options.inputxpi.ostojarpath(fullinputpath)
      return options.inputxpi.openinputstream(jarfilename, filename)
    else:
      return super(MozConvertOptionParser, self).openinputfile(options, fullinputpath)

  def getfullinputpath(self, options, inputpath):
    """gets the absolute path to an input file"""
    if self.isxpi(options.input):
      return inputpath
    else:
      return os.path.join(options.input, inputpath)

  def opentemplatefile(self, options, fulltemplatepath):
    """opens the template file (if required)"""
    if fulltemplatepath is not None:
      if self.isxpi(options.template):
        # TODO: deal with different names in input/template xpis
        jarfilename, filename = options.templatexpi.ostojarpath(fulltemplatepath)
        if options.templatexpi.jarfileexists(jarfilename, filename):
          return options.templatexpi.openinputstream(jarfilename, filename)
        else:
          self.warning("missing template file %s" % fulltemplatepath)
    return super(MozConvertOptionParser, self).opentemplatefile(options, fulltemplatepath)

  def getfulltemplatepath(self, options, templatepath):
    """gets the absolute path to a template file"""
    if templatepath is not None and self.usetemplates and options.template:
      if self.isxpi(options.template):
        return templatepath
      else:
        return os.path.join(options.template, templatepath)
    else:
      return None

  def templateexists(self, options, templatepath):
    """returns whether the given template exists..."""
    if templatepath is not None:
      if self.isxpi(options.template):
        # TODO: deal with different names in input/template xpis
        try:
          jarfilename, filename = options.templatexpi.ostojarpath(templatepath)
        except IndexError:
          return False
        return options.templatexpi.jarfileexists(jarfilename, filename)
    return super(MozConvertOptionParser, self).templateexists(options, templatepath)

if __name__ == '__main__':
  formats = {("dtd", "dtd"): ("dtd.po", dtd2po.convertdtd),
             ("properties", "properties"): ("properties.po", prop2po.convertprop),
             "dtd": ("dtd.po", dtd2po.convertdtd),
             "properties": ("properties.po", prop2po.convertprop),
             (None, "*"): ("*", convert.copytemplate),
             ("*", "*"): ("*", convert.copyinput),
             "*": ("*", convert.copyinput)}
  parser = MozConvertOptionParser(formats, usetemplates=True, usepots=True)
  parser.convertparameters.append("pot")
  parser.runconversion()

