Commits

Konstantine Rybnikov committed 4d9fbde

implement diffutil.compare_two_pofiles

Comments (0)

Files changed (2)

fat_po_editor/diffutil.py

         }
         self.operation_types = self.operation_to_storage_map.keys()
     
-    def entry_append(self, operation_type, entry):
+    def append_entry(self, operation_type, entry):
         """
         ``operation_type``
             one of ['add', 'rm', 'modify']
     ``pofile1``, ``pofile2``
         :class:`~polib.POFile` instances.
     """
+
+    rv = Diff()
     
-    pass
+    pofile1_msgid_to_poentry_map = dict(map(lambda entry: (entry.msgid, entry), pofile1))
+    pofile2_msgid_to_poentry_map = dict(map(lambda entry: (entry.msgid, entry), pofile2))
+    
+    for item in pofile2:
+        if item.msgid not in pofile1_msgid_to_poentry_map:
+            rv.append_entry('add', item)
+        else:
+            old_item = pofile1_msgid_to_poentry_map[item.msgid]
+            if item != old_item:
+                rv.append_entry('modify', [old_item, item])
+    
+    for item in pofile1:
+        if item.msgid not in pofile2_msgid_to_poentry_map:
+            rv.append_entry('rm', item)
+
+    return rv

tests/test_diffutil.py

         diff = Diff()
         entry = POEntry(msgid=u"First msgid",
                         msgstr=u"First msgstr")
-        diff.entry_append('add', entry)
+        diff.append_entry('add', entry)
 
         self.assertEquals(
             diff.get_entries('add'),
         diff = Diff()
         entry = POEntry(msgid=u"First msgid",
                         msgstr=u"First msgstr")
-        diff.entry_append('add', entry)
+        diff.append_entry('add', entry)
 
         self.assertEquals(
             unicode(diff),
         diff = Diff()
         entry = POEntry(msgid=u"First msgid",
                         msgstr=u"First msgstr")
-        diff.entry_append('add', entry)
+        diff.append_entry('add', entry)
         entry = POEntry(msgid=u"Second msgid",
                         msgstr=u"Second msgstr")
-        diff.entry_append('rm', entry)
+        diff.append_entry('rm', entry)
         entry = [POEntry(msgid=u"Third msgid",
                          msgstr=u"Third msgstr"),
                  POEntry(msgid=u"Third msgid",
                          msgstr=u"Third msgstr")]
-        diff.entry_append('modify', entry)
+        diff.append_entry('modify', entry)
         
         self.assertEquals(
             unicode(diff),
         diff = Diff()
         entry = POEntry(msgid=u"F" * 81,
                         msgstr=u"First msgstr")
-        diff.entry_append('add', entry)
+        diff.append_entry('add', entry)
 
         self.assertEquals(
             unicode(diff),
         diff = Diff()
         entry = POEntry(msgid=u"First \nmsgid",
                         msgstr=u"First msgstr")
-        diff.entry_append('add', entry)
+        diff.append_entry('add', entry)
 
         self.assertEquals(
             unicode(diff),
         diff = Diff()
         entry = POEntry(msgid=u"First msgid",
                         msgstr=u"First msgstr")
-        diff.entry_append('add', entry)
+        diff.append_entry('add', entry)
         entry = POEntry(msgid=u"Second msgid",
                         msgstr=u"Second msgstr")
-        diff.entry_append('rm', entry)
+        diff.append_entry('rm', entry)
         entry = POEntry(msgid=u"Third msgid",
                         msgstr=u"Third msgstr")
-        diff.entry_append('modify', [entry, entry])
+        diff.append_entry('modify', [entry, entry])
 
         self.assertEquals(
             diff.full_represent(),
         diff1 = Diff()
         entry = POEntry(msgid=u"First msgid",
                         msgstr=u"First msgstr")
-        diff1.entry_append('add', entry)
+        diff1.append_entry('add', entry)
         diff2 = Diff()
         entry = POEntry(msgid=u"First msgid",
                         msgstr=u"First msgstr")
-        diff2.entry_append('add', entry)
+        diff2.append_entry('add', entry)
         
         self.assertEquals(diff1 == diff2, True)
 
         diff1 = Diff()
         entry = POEntry(msgid=u"First msgid",
                         msgstr=u"First msgstr")
-        diff1.entry_append('add', entry)
+        diff1.append_entry('add', entry)
         diff2 = Diff()
         entry = POEntry(msgid=u"Second msgid",
                         msgstr=u"Second msgstr")
-        diff2.entry_append('add', entry)
+        diff2.append_entry('add', entry)
         
         self.assertEquals(diff1 == diff2, False)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.