1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
37 """a specialized Option Parser for posplit"""
38
45
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
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
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
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()):
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
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)
108 outputfile = open(fulloutputpath, 'w')
109 outputfile.write(str(outputpofile))
110
111
113
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