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
    47 -    def handlefile(self, filename):
    48          try:
    49              store = factory.getobject(filename)
    50          except Exception, e:
    51              print >> sys.stderr, str(e)
    52              return
    53          # do something useful with the store and db
    54          try:
    55              self.tmdb.add_store(store, self.source_lang, self.target_lang, commit=False)
    56          except Exception, e:
    57              print e
    58          print "new file:", filename
    59
    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
    70 -    def handledir(self, dirname):
    71          path, name = os.path.split(dirname)
    72          if name in ["CVS", ".svn", "_darcs", ".git", ".hg", ".bzr"]:
    73              return
    74          entries = os.listdir(dirname)
    75          self.handlefiles(dirname, entries)
    76
    77 -def main():
    78      try:
    79          import psyco
    80          psyco.full()
    81      except Exception:
    82          pass
    83      parser = OptionParser()
    84      parser.add_option("-d", "--tmdb", dest="tmdbfile",
    85                        help="translation memory database file")
    86      parser.add_option("-s", "--import-source-lang", dest="source_lang",
    87                        help="source language of translation files")
    88      parser.add_option("-t", "--import-target-lang", dest="target_lang",
    89                        help="target language of translation files")
    90      (options, args) = parser.parse_args()
    91
    92      Builder(options.tmdbfile, options.source_lang, options.target_lang, args)
    93
    94  if __name__ == '__main__':
    95      main()
    96