Commits

Robert Lehmann  committed 34351cc

Close #630: Revamp data structures used for message catalogs.

  • Participants
  • Parent commits aac8234

Comments (0)

Files changed (1)

File sphinx/builders/gettext.py

 """[1:]
 
 
+class Catalog(object):
+    def __init__(self):
+        self.messages = []  # retain order
+        self.metadata = {}  # msgid -> file, line, uid
+    def add(self, msg, origin):
+        if msg not in self.metadata:  # faster lookup in hash
+            self.messages.append(msg)
+            self.metadata[msg] = []
+        self.metadata[msg].append((origin.source, origin.line, origin.uid))
+
+
 class I18nBuilder(Builder):
     """
     General i18n builder.
 
     def init(self):
         Builder.init(self)
-        self.catalogs = defaultdict(dict)
+        self.catalogs = defaultdict(Catalog)
 
     def get_target_uri(self, docname, typ=None):
         return ''
                 continue # built-in message
             if isinstance(node, nodes.literal_block):
                 continue
-            if not msg in catalog:
-                catalog[msg] = []
-            catalog[msg].append((node.source, node.line, node.uid))
+            catalog.add(msg, node)
 
 
 class MessageCatalogBuilder(I18nBuilder):
             # XXX should supply tz
             ctime = datetime.now().strftime('%Y-%m-%d %H:%M%z'),
         )
-        for section, messages in self.status_iterator(
+        for section, catalog in self.status_iterator(
                 self.catalogs.iteritems(), "writing message catalogs... ",
                 lambda (section, _):darkgreen(section), len(self.catalogs)):
 
             pofile = open(pofn, 'w', encoding='utf-8')
             try:
                 pofile.write(POHEADER % data)
-                for message, positions in messages.iteritems():
+                for message in catalog.messages:
+                    positions = catalog.metadata[message]
                     if positions:
                         # generate "#: file1:line1 file2:line2 ..."
                         pofile.write(u"#: %s\n" % ", ".join("%s:%s" %