Convert update-copyright.py to a more modular framework.
[update-copyright.git] / update_copyright / vcs / __init__.py
1 # Copyright
2
3 """Backends for version control systems."""
4
5 from . import utils as _utils
6
7
8 class VCSBackend (object):
9     name = None
10
11     def __init__(self, author_hacks=None, year_hacks=None, aliases=None):
12         if author_hacks is None:
13             author_hacks = {}
14         self._author_hacks = author_hacks
15         if year_hacks is None:
16             year_hacks = {}
17         self._year_hacks = year_hacks
18         if aliases is None:
19             aliases = {}
20         self._aliases = aliases
21
22     def _years(self, filename=None):
23         raise NotImplementedError()
24
25     def original_year(self, filename=None):
26         years = self._years(filename=filename)
27         if filename is None:
28             years.update(self._year_hacks.values())
29         elif _utils.splitpath(filename) in self._year_hacks:
30             years.update(year_hacks[_utils.splitpath(filename)])
31         years = sorted(years)
32         return years[0]
33
34     def _authors(self, filename=None):
35         raise NotImplementedError()
36
37     def authors(self, filename=None, with_emails=True):
38         authors = self._authors(filename=filename)
39         if filename is None:
40             for path,authors in self._author_hacks.items():
41                 authors.update(authors)
42         elif _utils.splitpath(filename) in self._author_hacks:
43             authors.update(self._author_hacks[_utils.splitpath(filename)])
44         return _utils.replace_aliases(
45             authors, with_email=False, aliases=self._aliases)
46
47     def is_versioned(self, filename=None):
48         raise NotImplementedError()