#!/usr/bin/env python #-*- encoding: utf-8 -*- # # converter.py # Convert the erroneous po file. # # Copyright (c) 2008 Pierre "delroth" Bourdon # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . class POMessage(object): def __init__(self, text): self.build_from_text(text) def build_from_text(self, text): lines = text.split('\n') self.comments = [] self.msgid = "" self.msgstr = "" comments = True msgid = False msgstr = False for l in lines: if l.startswith('#') and comments: self.comments.append(l[1:].strip()) elif l.startswith('msgid ') and comments: comments, msgid = False, True self.msgid += '"'.join(l.split('"')[1:-1]) elif l.startswith('msgstr ') and msgid: msgid, msgstr = False, True self.msgstr += '"'.join(l.split('"')[1:-1]) elif l.strip().startswith('"'): t = l.split('"')[1] if msgid: self.msgid += t elif msgstr: self.msgstr += t return def msgstr_or_id_to_text(self, t): lines = [] first_line = True while len(t) > 0: if first_line: size = 64 else: size = 70 part = t[:size] if len(t) > size: part = ''.join(part.split(' ')[:-1]) + ' ' size = len(part) lines.append('"' + t[:size] + '"') t = t[size:] if first_line: first_line = False return ([""] if lines == [] else lines) def to_text(self): lines = [] for l in self.comments: lines.append('#' + l) msgid = self.msgstr_or_id_to_text(self.msgid) msgid[0] = "msgid " + msgid[0] msgstr = self.msgstr_or_id_to_text(self.msgstr) msgstr[0] = "msgstr " + msgstr[0] lines.extend(msgid) lines.extend(msgstr) return '\n'.join(lines) import re message = re.compile(r'#.+?msgid ".+?msgstr ".+?#', re.M | re.S) def main(argv): if len(argv) < 4: print "Usage: %s