From 0a2d3cc3e05a53c2bbe7820ce8353c9618054209 Mon Sep 17 00:00:00 2001 From: Romain Failliot Date: Sun, 9 Jan 2022 18:25:15 -0500 Subject: [PATCH] Add static typing for vcs module --- src/diffuse/vcs/folder_set.py | 10 ++++++---- src/diffuse/vcs/hg.py | 2 +- src/diffuse/vcs/svn.py | 4 ++-- src/diffuse/vcs/vcs_interface.py | 7 ++++++- src/diffuse/vcs/vcs_registry.py | 3 ++- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/diffuse/vcs/folder_set.py b/src/diffuse/vcs/folder_set.py index df36555..aba091e 100644 --- a/src/diffuse/vcs/folder_set.py +++ b/src/diffuse/vcs/folder_set.py @@ -19,6 +19,8 @@ import os +from typing import List + class FolderSet: '''Utility class to help support Git and Monotone. @@ -27,18 +29,18 @@ class FolderSet: "mtn automate inventory." ''' - def __init__(self, names): - self.folders = f = [] + def __init__(self, names: List[str]) -> None: + self.folders: List[str] = [] for name in names: name = os.path.abspath(name) # ensure all names end with os.sep if not name.endswith(os.sep): name += os.sep - f.append(name) + self.folders.append(name) # returns True if the given abspath is a file that should be included in # the interesting file subset - def contains(self, abspath): + def contains(self, abspath: str) -> bool: if not abspath.endswith(os.sep): abspath += os.sep for f in self.folders: diff --git a/src/diffuse/vcs/hg.py b/src/diffuse/vcs/hg.py index 7cdb11f..29b2e89 100644 --- a/src/diffuse/vcs/hg.py +++ b/src/diffuse/vcs/hg.py @@ -30,7 +30,7 @@ from diffuse.vcs.vcs_interface import VcsInterface # Mercurial support class Hg(VcsInterface): def __init__(self, root: str): - VcsInterface.__init__(self, root) + super().__init__(root) self.working_rev: Optional[str] = None def _getPreviousRevision(self, prefs, rev): diff --git a/src/diffuse/vcs/svn.py b/src/diffuse/vcs/svn.py index cafd837..8d54af9 100644 --- a/src/diffuse/vcs/svn.py +++ b/src/diffuse/vcs/svn.py @@ -33,7 +33,7 @@ from diffuse.vcs.vcs_interface import VcsInterface # SVK support subclasses from this class Svn(VcsInterface): def __init__(self, root: str): - VcsInterface.__init__(self, root) + super().__init__(root) self.url: Optional[str] = None @staticmethod @@ -61,7 +61,7 @@ class Svn(VcsInterface): m = int(rev) return str(max(m > 1, 0)) - def _getURL(self, prefs): + def _getURL(self, prefs: Preferences) -> Optional[str]: if self.url is None: vcs, prefix = self._getVcs(), self._getURLPrefix() n = len(prefix) diff --git a/src/diffuse/vcs/vcs_interface.py b/src/diffuse/vcs/vcs_interface.py index 4ba9504..088c74b 100644 --- a/src/diffuse/vcs/vcs_interface.py +++ b/src/diffuse/vcs/vcs_interface.py @@ -17,12 +17,13 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +from abc import ABCMeta, abstractmethod from typing import List, Optional, Tuple from diffuse.preferences import Preferences -class VcsInterface: +class VcsInterface(metaclass=ABCMeta): """Interface for the VCSs.""" PathRevisionPair = Tuple[Optional[str], Optional[str]] @@ -32,15 +33,19 @@ class VcsInterface: """The object will initialized with the repository's root folder.""" self.root = root + @abstractmethod def getFileTemplate(self, prefs: Preferences, name: str) -> PathRevisionList: """Indicates which revisions to display for a file when none were explicitly requested.""" + @abstractmethod def getCommitTemplate(self, prefs, rev, names): """Indicates which file revisions to display for a commit.""" + @abstractmethod def getFolderTemplate(self, prefs, names): """Indicates which file revisions to display for a set of folders.""" + @abstractmethod def getRevision(self, prefs: Preferences, name: str, rev: str) -> bytes: """Returns the contents of the specified file revision""" diff --git a/src/diffuse/vcs/vcs_registry.py b/src/diffuse/vcs/vcs_registry.py index 3b935c2..84bedca 100644 --- a/src/diffuse/vcs/vcs_registry.py +++ b/src/diffuse/vcs/vcs_registry.py @@ -186,7 +186,8 @@ def _get_svk_repo(path: str, prefs: Preferences) -> Optional[VcsInterface]: # find working copies by parsing the config file with open(svkconfig, 'r', encoding='utf-8') as f: ss: List[str] = utils.readlines(f) - projs, sep = [], os.sep + projs: List[str] = [] + sep = os.sep # find the separator character for s in ss: if s.startswith(' sep: ') and len(s) > 7: