Commits

Konstantine Rybnikov committed 2df0ba5

some work on module vcs_util is done

Comments (0)

Files changed (3)

fat_po_editor/vcs_util.py

+# -*- coding: utf-8 -*-
+
+import vcs
+
+from .util import StringBuilder
+
+def is_useful_changeset(changeset):
+    """Returns if changeset is useful for fat_po_edito"""
+    for file_ in changeset.affected_files:
+        if file_.endswith('.po'):
+            return True
+    return False
+
+class BoundChangesetInfo(object):
+    """Info about changeset that will be really useful for fat_po_editor"""
+
+    def __init__(self, changeset):
+        self.revision = changeset.short_id
+        self.author = changeset.author
+        self.affected_pofiles = list(filter(lambda x: x.endswith('.po'),
+                                            changeset.affected_files))
+
+    def __unicode__(self):
+        rv = StringBuilder()
+        rv += u"Revision no.: %s\n" % self.revision
+        rv += u"Author: %s\n" % self.author
+        rv += u"Affected pofiles: [%s]\n" % u", ".joinself.affected_pofiles
+        return unicode(rv)
+
+class RepoWithPofiles(object):
+    def __init__(self, path=None):
+        self.path = path
+        self.vcs_repo = vcs.get_repo(path)
+
+    def get_last_changesets_info(self, limit=10):
+        """Returns information needed to draw nice representation of last
+        affected pofiles and they're authors"""
+        MAX_ITERATED_CHANGESETS_NUM = 1000
+
+        revisions = self.vcs_repo.revisions[:MAX_ITERATED_CHANGESETS_NUM]
+        revision_to_changeset_map = dict(
+            map(lambda changeset: (changeset,
+                                   self.vcs_repo.get_changeset(changeset)),
+                reversed(revisions)))
+        useful_revisions = (
+            map(lambda x: is_useful_changeset(revision_to_changeset_map.get(x)),
+                revisions))
+        rv = [BoundChangesetInfo(self.vcs_repo.get_changeset(r))
+              for r in useful_revisions]
+        return rv
+# -*- coding: utf-8 -*-
+
+import os
+__testdata_path = os.path.join(os.path.dirname(__file__), 'testdata')
+
+def get_testdata_path():
+    return __testdata_path

tests/test_vcs_util.py

+# -*- coding: utf-8 -*-
+
+import os
+import unittest
+
+from fat_po_editor.vcs_util import RepoWithPofiles, BoundChangesetInfo
+from . import get_testdata_path
+
+class TestVcsUtilRepoWithPofiles(unittest.TestCase):
+    def setUp(self):
+        self.testdata_path = os.path.join(get_testdata_path(),
+                                          'repo1')
+        self.repo = RepoWithPofiles(self.testdata_path)
+
+    def test_get_last_changesets_info_simple(self):
+        info = self.repo.get_last_changesets_info()
+        self.assertEquals(len(info), 2)
+        self.assertEquals(unicode(info[0]),
+                          (u"Revision no.: "))
+