Add static typing for vcs module

This commit is contained in:
Romain Failliot 2022-01-09 18:25:15 -05:00
parent 914ea66cc3
commit 0a2d3cc3e0
5 changed files with 17 additions and 9 deletions

View File

@ -19,6 +19,8 @@
import os import os
from typing import List
class FolderSet: class FolderSet:
'''Utility class to help support Git and Monotone. '''Utility class to help support Git and Monotone.
@ -27,18 +29,18 @@ class FolderSet:
"mtn automate inventory." "mtn automate inventory."
''' '''
def __init__(self, names): def __init__(self, names: List[str]) -> None:
self.folders = f = [] self.folders: List[str] = []
for name in names: for name in names:
name = os.path.abspath(name) name = os.path.abspath(name)
# ensure all names end with os.sep # ensure all names end with os.sep
if not name.endswith(os.sep): if not name.endswith(os.sep):
name += 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 # returns True if the given abspath is a file that should be included in
# the interesting file subset # the interesting file subset
def contains(self, abspath): def contains(self, abspath: str) -> bool:
if not abspath.endswith(os.sep): if not abspath.endswith(os.sep):
abspath += os.sep abspath += os.sep
for f in self.folders: for f in self.folders:

View File

@ -30,7 +30,7 @@ from diffuse.vcs.vcs_interface import VcsInterface
# Mercurial support # Mercurial support
class Hg(VcsInterface): class Hg(VcsInterface):
def __init__(self, root: str): def __init__(self, root: str):
VcsInterface.__init__(self, root) super().__init__(root)
self.working_rev: Optional[str] = None self.working_rev: Optional[str] = None
def _getPreviousRevision(self, prefs, rev): def _getPreviousRevision(self, prefs, rev):

View File

@ -33,7 +33,7 @@ from diffuse.vcs.vcs_interface import VcsInterface
# SVK support subclasses from this # SVK support subclasses from this
class Svn(VcsInterface): class Svn(VcsInterface):
def __init__(self, root: str): def __init__(self, root: str):
VcsInterface.__init__(self, root) super().__init__(root)
self.url: Optional[str] = None self.url: Optional[str] = None
@staticmethod @staticmethod
@ -61,7 +61,7 @@ class Svn(VcsInterface):
m = int(rev) m = int(rev)
return str(max(m > 1, 0)) return str(max(m > 1, 0))
def _getURL(self, prefs): def _getURL(self, prefs: Preferences) -> Optional[str]:
if self.url is None: if self.url is None:
vcs, prefix = self._getVcs(), self._getURLPrefix() vcs, prefix = self._getVcs(), self._getURLPrefix()
n = len(prefix) n = len(prefix)

View File

@ -17,12 +17,13 @@
# with this program; if not, write to the Free Software Foundation, Inc., # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from abc import ABCMeta, abstractmethod
from typing import List, Optional, Tuple from typing import List, Optional, Tuple
from diffuse.preferences import Preferences from diffuse.preferences import Preferences
class VcsInterface: class VcsInterface(metaclass=ABCMeta):
"""Interface for the VCSs.""" """Interface for the VCSs."""
PathRevisionPair = Tuple[Optional[str], Optional[str]] PathRevisionPair = Tuple[Optional[str], Optional[str]]
@ -32,15 +33,19 @@ class VcsInterface:
"""The object will initialized with the repository's root folder.""" """The object will initialized with the repository's root folder."""
self.root = root self.root = root
@abstractmethod
def getFileTemplate(self, prefs: Preferences, name: str) -> PathRevisionList: def getFileTemplate(self, prefs: Preferences, name: str) -> PathRevisionList:
"""Indicates which revisions to display for a file when none were explicitly """Indicates which revisions to display for a file when none were explicitly
requested.""" requested."""
@abstractmethod
def getCommitTemplate(self, prefs, rev, names): def getCommitTemplate(self, prefs, rev, names):
"""Indicates which file revisions to display for a commit.""" """Indicates which file revisions to display for a commit."""
@abstractmethod
def getFolderTemplate(self, prefs, names): def getFolderTemplate(self, prefs, names):
"""Indicates which file revisions to display for a set of folders.""" """Indicates which file revisions to display for a set of folders."""
@abstractmethod
def getRevision(self, prefs: Preferences, name: str, rev: str) -> bytes: def getRevision(self, prefs: Preferences, name: str, rev: str) -> bytes:
"""Returns the contents of the specified file revision""" """Returns the contents of the specified file revision"""

View File

@ -186,7 +186,8 @@ def _get_svk_repo(path: str, prefs: Preferences) -> Optional[VcsInterface]:
# find working copies by parsing the config file # find working copies by parsing the config file
with open(svkconfig, 'r', encoding='utf-8') as f: with open(svkconfig, 'r', encoding='utf-8') as f:
ss: List[str] = utils.readlines(f) ss: List[str] = utils.readlines(f)
projs, sep = [], os.sep projs: List[str] = []
sep = os.sep
# find the separator character # find the separator character
for s in ss: for s in ss:
if s.startswith(' sep: ') and len(s) > 7: if s.startswith(' sep: ') and len(s) > 7: