Source

fat_po_editor / fat_po_editor / vcs_util.py

Full commit
# -*- 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