view commitwatcher/store.py @ 38:035368f84847

indent
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 04 Nov 2013 23:20:32 -0800
parents 3edb91cfd3c1
children b04538e7f9f3
line wrap: on
line source

"""
storage classes for commits and related data
"""

from abc import abstractmethod

__all__ = ['CommitStore', 'MemoryStore']

class CommitStore(object):
    """
    ABC for commits
    """

    def __init__(self, verbose=True):
        self.verbose = verbose

    @abstractmethod
    def __contains__(self, revision):
        """if a particular revision is already added"""

    @abstractmethod
    def store(self, commit):
        """store a commit"""

    def add(self, commit):
        """adds a commit to the store"""
        if commit.revision not in self:
            return
        self.store(commit)

    @abstractmethod
    def paths(self, *commits):
        """return all paths touched for a given commit"""

    def ancestry(self, path):
        sep = '/' # unix/url-style separators
        path = path.split('/')
        paths = []
        while path:
            paths.append(sep.join(path))
            path.pop()
        return paths

    def paths(self, *commits):
        """
        return paths touched by commits
        """

        paths = set()
        for commit in commits:
            for f in commit.files:
                paths.update(self.ancestry(f))
        return paths


class MemoryStore(CommitStore):
    """store in volatile memory"""
    # volatile!

    def __init__(self):
        self.path_to_commit = {}
        self._commits = []

    def store(self, commit):
        paths = self.paths(commit)
        for path in paths:
            self.path_to_commit.set_default(path, []).append(commit)
        raise NotImplementedError