Package translate :: Package tools :: Module build_tmdb

[hide private]
[frames] | no frames]

# Source Code for Module translate.tools.build_tmdb

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2008 Zuza Software Foundation
      5  #
      6  # This file is part of the Translate Toolkit.
      7  #
      8  # This program 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  # This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
     20
     21  """Import units from translations files into tmdb."""
     22
     23  import os
     24  from optparse import OptionParser
     25  import sys
     26
     27  from translate.storage import factory
     28  from translate.storage import tmdb
     29
     30
     31 -class Builder:
     32
     33 -    def __init__(self, tmdbfile, source_lang, target_lang, filenames):
     34          self.tmdb = tmdb.TMDB(tmdbfile)
     35          self.source_lang = source_lang
     36          self.target_lang = target_lang
     37
     38          for filename in filenames:
     39              if not os.path.exists(filename):
     40                  print >> sys.stderr, "cannot process %s: does not exist" % filename
     41                  continue
     42              elif os.path.isdir(filename):
     43                  self.handledir(filename)
     44              else:
     45                  self.handlefile(filename)
     46          self.tmdb.connection.commit()
     47
     48 -    def handlefile(self, filename):
     49          try:
     50              store = factory.getobject(filename)
     51          except Exception, e:
     52              print >> sys.stderr, str(e)
     53              return
     54          # do something useful with the store and db
     55          try:
     56              self.tmdb.add_store(store, self.source_lang, self.target_lang, commit=False)
     57          except Exception, e:
     58              print e
     59          print "File added:", filename
     60
     61 -    def handlefiles(self, dirname, filenames):
     62          for filename in filenames:
     63              pathname = os.path.join(dirname, filename)
     64              if os.path.isdir(pathname):
     65                  self.handledir(pathname)
     66              else:
     67                  self.handlefile(pathname)
     68
     69 -    def handledir(self, dirname):
     70          path, name = os.path.split(dirname)
     71          if name in ["CVS", ".svn", "_darcs", ".git", ".hg", ".bzr"]:
     72              return
     73          entries = os.listdir(dirname)
     74          self.handlefiles(dirname, entries)
     75
     76
     77 -def main():
     78      try:
     79          import psyco
     80          psyco.full()
     81      except Exception:
     82          pass
     83      parser = OptionParser(usage="%prog [options] <input files>")
     84      parser.add_option(
     85          "-d", "--tmdb", dest="tmdb_file", default="tm.db",
     86          help="translation memory database file (default: tm.db)")
     87      parser.add_option(
     88          "-s", "--import-source-lang", dest="source_lang", default="en",
     89          help="source language of translation files (default: en)")
     90      parser.add_option(
     91          "-t", "--import-target-lang", dest="target_lang",
     92          help="target language of translation files")
     93      (options, args) = parser.parse_args()
     94
     95      if not options.target_lang:
     96          parser.error('No target language specified.')
     97
     98      if len(args) < 1:
     99          parser.error('No input file(s) specified.')
    100
    101      Builder(options.tmdb_file, options.source_lang, options.target_lang, args)
    102
    103  if __name__ == '__main__':
    104      main()
    105