edouard@3108: import time edouard@3108: edouard@3108: locpfx = '#:svghmi.svg:' edouard@3108: edouard@3108: pot_header = '''\ edouard@3108: # SOME DESCRIPTIVE TITLE. edouard@3108: # Copyright (C) YEAR ORGANIZATION edouard@3108: # FIRST AUTHOR , YEAR. edouard@3108: # edouard@3108: msgid "" edouard@3108: msgstr "" edouard@3108: "Project-Id-Version: PACKAGE VERSION\\n" edouard@3108: "POT-Creation-Date: %(time)s\\n" edouard@3108: "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n" edouard@3108: "Last-Translator: FULL NAME \\n" edouard@3108: "Language-Team: LANGUAGE \\n" edouard@3108: "MIME-Version: 1.0\\n" edouard@3108: "Content-Type: text/plain; charset=CHARSET\\n" edouard@3108: "Content-Transfer-Encoding: ENCODING\\n" edouard@3108: "Generated-By: SVGHMI 1.0\\n" edouard@3108: edouard@3108: ''' edouard@3108: edouard@3108: class POTWriter: edouard@3108: def __init__(self): edouard@3108: self.__messages = {} edouard@3108: edouard@3108: def ImportMessages(self, msgs): edouard@3108: for msg in msgs: edouard@3108: self.addentry("\n".join([line.text for line in msg]), msg.get("label"), msg.get("id")) edouard@3108: edouard@3108: def addentry(self, msg, label, svgid): edouard@3108: entry = (label, svgid) edouard@3108: self.__messages.setdefault(msg, set()).add(entry) edouard@3108: edouard@3108: def write(self, fp): edouard@3108: timestamp = time.strftime('%Y-%m-%d %H:%M+%Z') edouard@3108: print >> fp, pot_header % {'time': timestamp} edouard@3108: reverse = {} edouard@3108: for k, v in self.__messages.items(): edouard@3108: keys = list(v) edouard@3108: keys.sort() edouard@3108: reverse.setdefault(tuple(keys), []).append((k, v)) edouard@3108: rkeys = reverse.keys() edouard@3108: rkeys.sort() edouard@3108: for rkey in rkeys: edouard@3108: rentries = reverse[rkey] edouard@3108: rentries.sort() edouard@3108: for k, v in rentries: edouard@3108: v = v.keys() edouard@3108: v.sort() edouard@3108: locline = locpfx edouard@3108: for label, svgid in v: edouard@3108: d = {'label': label, 'svgid': svgid} edouard@3108: s = _(' %(label)s:%(svgid)d') % d edouard@3108: if len(locline) + len(s) <= 78: edouard@3108: locline = locline + s edouard@3108: else: edouard@3108: print >> fp, locline edouard@3108: locline = locpfx + s edouard@3108: if len(locline) > len(locpfx): edouard@3108: print >> fp, locline edouard@3108: print >> fp, 'msgid', normalize(k) edouard@3108: print >> fp, 'msgstr ""\n' edouard@3108: edouard@3108: edouard@3108: class POReader: edouard@3108: def __init__(self): edouard@3108: self.__messages = {} edouard@3108: edouard@3108: def add(msgid, msgstr, fuzzy): edouard@3108: "Add a non-fuzzy translation to the dictionary." edouard@3108: if not fuzzy and msgstr: edouard@3108: self.__messages[msgid] = msgstr edouard@3108: edouard@3108: def read(self, fp): edouard@3108: ID = 1 edouard@3108: STR = 2 edouard@3108: edouard@3108: lines = fp.readlines() edouard@3108: section = None edouard@3108: fuzzy = 0 edouard@3108: edouard@3108: # Parse the catalog edouard@3108: lno = 0 edouard@3108: for l in lines: edouard@3108: lno += 1 edouard@3108: # If we get a comment line after a msgstr, this is a new entry edouard@3108: if l[0] == '#' and section == STR: edouard@3108: self.add(msgid, msgstr, fuzzy) edouard@3108: section = None edouard@3108: fuzzy = 0 edouard@3108: # Record a fuzzy mark edouard@3108: if l[:2] == '#,' and 'fuzzy' in l: edouard@3108: fuzzy = 1 edouard@3108: # Skip comments edouard@3108: if l[0] == '#': edouard@3108: continue edouard@3108: # Now we are in a msgid section, output previous section edouard@3108: if l.startswith('msgid') and not l.startswith('msgid_plural'): edouard@3108: if section == STR: edouard@3108: self.add(msgid, msgstr, fuzzy) edouard@3108: section = ID edouard@3108: l = l[5:] edouard@3108: msgid = msgstr = '' edouard@3108: is_plural = False edouard@3108: # This is a message with plural forms edouard@3108: elif l.startswith('msgid_plural'): edouard@3108: if section != ID: edouard@3108: print >> sys.stderr, 'msgid_plural not preceded by msgid on %s:%d' %\ edouard@3108: (infile, lno) edouard@3108: sys.exit(1) edouard@3108: l = l[12:] edouard@3108: msgid += '\0' # separator of singular and plural edouard@3108: is_plural = True edouard@3108: # Now we are in a msgstr section edouard@3108: elif l.startswith('msgstr'): edouard@3108: section = STR edouard@3108: if l.startswith('msgstr['): edouard@3108: if not is_plural: edouard@3108: print >> sys.stderr, 'plural without msgid_plural on %s:%d' %\ edouard@3108: (infile, lno) edouard@3108: sys.exit(1) edouard@3108: l = l.split(']', 1)[1] edouard@3108: if msgstr: edouard@3108: msgstr += '\0' # Separator of the various plural forms edouard@3108: else: edouard@3108: if is_plural: edouard@3108: print >> sys.stderr, 'indexed msgstr required for plural on %s:%d' %\ edouard@3108: (infile, lno) edouard@3108: sys.exit(1) edouard@3108: l = l[6:] edouard@3108: # Skip empty lines edouard@3108: l = l.strip() edouard@3108: if not l: edouard@3108: continue edouard@3108: l = ast.literal_eval(l) edouard@3108: if section == ID: edouard@3108: msgid += l edouard@3108: elif section == STR: edouard@3108: msgstr += l edouard@3108: else: edouard@3108: print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \ edouard@3108: 'before:' edouard@3108: print >> sys.stderr, l edouard@3108: sys.exit(1) edouard@3108: # Add last entry edouard@3108: if section == STR: edouard@3108: self.add(msgid, msgstr, fuzzy) edouard@3108: edouard@3108: