Update exception handling to use `except X as Y` syntax.
[update-copyright.git] / update_copyright / vcs / __init__.py
1 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of update-copyright.
4 #
5 # update-copyright is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # update-copyright is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # update-copyright.  If not, see <http://www.gnu.org/licenses/>.
17
18 """Backends for version control systems."""
19
20 import os.path as _os_path
21
22 from . import utils as _utils
23
24
25 class VCSBackend (object):
26     name = None
27
28     def __init__(self, root='.', author_hacks=None, year_hacks=None,
29                  aliases=None):
30         self._root = root
31         if author_hacks is None:
32             author_hacks = {}
33         self._author_hacks = author_hacks
34         if year_hacks is None:
35             year_hacks = {}
36         self._year_hacks = year_hacks
37         if aliases is None:
38             aliases = {}
39         self._aliases = aliases
40
41     def _years(self, filename=None):
42         raise NotImplementedError()
43
44     def original_year(self, filename=None):
45         years = self._years(filename=filename)
46         if filename is None:
47             years.update(self._year_hacks.values())
48         else:
49             filename = _os_path.relpath(filename, self._root)
50             splitpath = _utils.splitpath(filename)
51             if splitpath in self._year_hacks:
52                 years.add(self._year_hacks[splitpath])
53         years = sorted(years)
54         return years[0]
55
56     def _authors(self, filename=None):
57         raise NotImplementedError()
58
59     def authors(self, filename=None, with_emails=True):
60         authors = self._authors(filename=filename)
61         if filename is None:
62             for path,_authors in self._author_hacks.items():
63                 authors.update(_authors)
64         else:
65             filename = _os_path.relpath(filename, self._root)
66             splitpath = _utils.splitpath(filename)
67             if splitpath in self._author_hacks:
68                 authors.update(self._author_hacks[splitpath])
69         return _utils.replace_aliases(
70             authors, with_email=with_emails, aliases=self._aliases)
71
72     def is_versioned(self, filename=None):
73         raise NotImplementedError()