Package translate :: Package tools :: Module porestructure
[hide private]
[frames] | no frames]

Source Code for Module translate.tools.porestructure

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright 2005, 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  """Restructure Gettxt PO files produced by poconflicts into the original 
 23  directory tree for merging using pomerge 
 24   
 25  See: http://translate.sourceforge.net/wiki/toolkit/porestructure for examples and 
 26  usage instructions 
 27  """ 
 28   
 29  import os 
 30  import sys 
 31   
 32  from translate.storage import po 
 33  from translate.misc import optrecurse 
 34   
 35   
36 -class SplitOptionParser(optrecurse.RecursiveOptionParser):
37 """a specialized Option Parser for posplit""" 38
39 - def parse_args(self, args=None, values=None):
40 """parses the command line options, handling implicit input/output args""" 41 (options, args) = optrecurse.RecursiveOptionParser.parse_args(self, args, values) 42 if not options.output: 43 self.error("Output file is rquired") 44 return (options, args)
45
46 - def set_usage(self, usage=None):
47 """sets the usage string - if usage not given, uses getusagestring for each option""" 48 if usage is None: 49 self.usage = "%prog " + " ".join([self.getusagestring(option) for option in self.option_list]) + \ 50 "\n input directory is searched for PO files with (poconflicts) comments, all entries are written to files in a directory structure for pomerge" 51 else: 52 super(SplitOptionParser, self).set_usage(usage)
53
54 - def recursiveprocess(self, options):
55 """recurse through directories and process files""" 56 if not self.isrecursive(options.output, 'output'): 57 try: 58 self.warning("Output directory does not exist. Attempting to create") 59 #TODO: maybe we should only allow it to be created, otherwise we mess up an existing tree... 60 os.mkdir(options.output) 61 except: 62 self.error(optrecurse.optparse.OptionValueError("Output directory does not exist, attempt to create failed")) 63 if self.isrecursive(options.input, 'input') and getattr(options, "allowrecursiveinput", True): 64 if isinstance(options.input, list): 65 inputfiles = self.recurseinputfilelist(options) 66 else: 67 inputfiles = self.recurseinputfiles(options) 68 else: 69 if options.input: 70 inputfiles = [os.path.basename(options.input)] 71 options.input = os.path.dirname(options.input) 72 else: 73 inputfiles = [options.input] 74 self.textmap = {} 75 self.initprogressbar(inputfiles, options) 76 for inputpath in inputfiles: 77 fullinputpath = self.getfullinputpath(options, inputpath) 78 try: 79 success = self.processfile(options, fullinputpath) 80 except Exception, error: 81 if isinstance(error, KeyboardInterrupt): 82 raise self.warning("Error processing: input %s" % (fullinputpath), options, sys.exc_info()) 83 success = False 84 self.reportprogress(inputpath, success) 85 del self.progressbar
86
87 - def processfile(self, options, fullinputpath):
88 """process an individual file""" 89 inputfile = self.openinputfile(options, fullinputpath) 90 inputpofile = po.pofile(inputfile) 91 for pounit in inputpofile.units: 92 if not (pounit.isheader() or pounit.hasplural()): #XXX 93 if pounit.hasmarkedcomment("poconflicts"): 94 for comment in pounit.othercomments: 95 if comment.find("# (poconflicts)") == 0: 96 pounit.othercomments.remove(comment) 97 break 98 #TODO: refactor writing out 99 outputpath = comment[comment.find(")") + 2:].strip() 100 self.checkoutputsubdir(options, os.path.dirname(outputpath)) 101 fulloutputpath = os.path.join(options.output, outputpath) 102 if os.path.isfile(fulloutputpath): 103 outputfile = open(fulloutputpath, 'r') 104 outputpofile = po.pofile(outputfile) 105 else: 106 outputpofile = po.pofile() 107 outputpofile.units.append(pounit) #TODO:perhaps check to see if it's already there... 108 outputfile = open(fulloutputpath, 'w') 109 outputfile.write(str(outputpofile))
110 111
112 -def main():
113 #outputfile extentions will actually be determined by the comments in the po files 114 pooutput = ("po", None) 115 formats = {(None, None): pooutput, ("po", "po"): pooutput, "po": pooutput} 116 parser = SplitOptionParser(formats, description=__doc__) 117 parser.set_usage() 118 parser.run()
119 120 121 if __name__ == '__main__': 122 main() 123