translate.tools.build_tmdb

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