modinfo in the translate toolkit is now a 2-tuple containing the mtime and
[pootle.git] / conflict2suggest.py
blob68b9d0f7f40ff5527293329307afc935453df3c0
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2005, 2006 Zuza Software Foundation
5 #
6 # This file is part of Pootle.
8 # Pootle 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.
13 # Pootle 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.
18 # You should have received a copy of the GNU General Public License
19 # along with Pootle; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 """processes conflicts from msgmerge and turns them into Pootle suggestions"""
24 from Pootle import pootlefile
25 from Pootle import projects
26 import os
28 conflictmarker = "#-#-#-#-#"
30 def processfile(filename):
31 dummyproject = projects.DummyProject(os.path.dirname(filename), None)
32 pofile = pootlefile.pootlefile(dummyproject, os.path.basename(filename))
33 pofile.readpofile()
34 conflictitems = []
35 for item in pofile.statistics.getstats()["total"]:
36 poentry = pofile.units[item]
37 if poentry.hasplural():
38 targets = poentry.target.strings
39 else:
40 targets = [poentry.target]
41 for target in targets:
42 if conflictmarker in target:
43 conflictitems.append((item, targets))
44 break
45 for item, targets in conflictitems:
46 replacetargets = []
47 for target in targets:
48 if conflictmarker not in target:
49 replacetargets.append(target)
50 continue
51 lines = target.split("\n")
52 parts = []
53 marker, part = "", ""
54 for line in lines:
55 if line.startswith(conflictmarker) and line.endswith(conflictmarker):
56 if marker or part:
57 parts.append((marker, part))
58 marker = line[len(conflictmarker):-len(conflictmarker)]
59 part = ""
60 else:
61 part += line
62 if marker or part:
63 parts.append((marker, part))
64 for marker, part in parts:
65 pofile.addsuggestion(item, part, marker.strip())
66 replacetargets.append("")
68 newvalues = {"target": replacetargets}
69 pofile.updateunit(item, newvalues, None, None)
71 def processdir(dirname):
72 for filename in os.listdir(dirname):
73 pathname = os.path.join(dirname, filename)
74 if os.path.isdir(pathname):
75 processdir(pathname)
76 elif filename.endswith(os.extsep + "po"):
77 processfile(pathname)
79 if __name__ == "__main__":
80 import sys
81 for filename in sys.argv[1:]:
82 if os.path.isdir(filename):
83 processdir(filename)
84 elif os.path.isfile(filename):
85 processfile(filename)
86 else:
87 print >>sys.stderr, "cannot process", filename