From e3993cf374a2ced6dce68d3d7acbd34b1d69e730 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 27 Mar 2021 12:07:01 +0300 Subject: dedupe the code a bit --- cgit/repos/repo.py | 85 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/cgit/repos/repo.py b/cgit/repos/repo.py index 7d0c4a5..4b3072c 100644 --- a/cgit/repos/repo.py +++ b/cgit/repos/repo.py @@ -3,6 +3,7 @@ # For details, see https://github.com/egor-tensin/cgit-repos. # Distributed under the MIT License. +import abc import os.path @@ -33,15 +34,12 @@ class Repo: self.homepage = homepage -class GithubRepo(Repo): - DEFAULT_USER = None - +class HostedRepo(Repo, abc.ABC): def __init__(self, repo_id, clone_url=None, owner=None, desc=None, - homepage=None, user=DEFAULT_USER, via_ssh=None): + homepage=None, user=None, via_ssh=None): + user = user or self.get_default_user() if user is None: - if GithubRepo.DEFAULT_USER is None: - raise RuntimeError('neither explicit or default GitHub username was provided') - user = GithubRepo.DEFAULT_USER + raise RuntimeError(f'neither explicit or default {self.provider_name()} username was provided') name = Repo.extract_repo_name(repo_id) if clone_url is None: if via_ssh is None: @@ -55,49 +53,60 @@ class GithubRepo(Repo): super().__init__(repo_id, clone_url, owner=owner, desc=desc, homepage=homepage) - @staticmethod - def build_clone_url_ssh(user, name): + @abc.abstractmethod + def provider_name(self): + pass + + @abc.abstractmethod + def get_default_user(self): + pass + + @abc.abstractmethod + def build_clone_url_ssh(self, user, name): + pass + + @abc.abstractmethod + def build_clone_url_https(self, user, name): + pass + + @abc.abstractmethod + def build_homepage_url(self, user, name): + pass + + +class GithubRepo(HostedRepo): + DEFAULT_USER = None + + def provider_name(self): + return 'GitHub' + + def get_default_user(self): + return GithubRepo.DEFAULT_USER + + def build_clone_url_ssh(self, user, name): return f'ssh://git@github.com/{user}/{name}.git' - @staticmethod - def build_clone_url_https(user, name): + def build_clone_url_https(self, user, name): return f'https://github.com/{user}/{name}.git' - @staticmethod - def build_homepage_url(user, name): + def build_homepage_url(self, user, name): return f'https://github.com/{user}/{name}' -class BitbucketRepo(Repo): +class BitbucketRepo(HostedRepo): DEFAULT_USER = None - def __init__(self, repo_id, clone_url=None, owner=None, desc=None, - homepage=None, user=DEFAULT_USER, via_ssh=None): - if user is None: - if BitbucketRepo.DEFAULT_USER is None: - raise RuntimeError('neither explicit or default Bitbucket username was provided') - user = BitbucketRepo.DEFAULT_USER - name = Repo.extract_repo_name(repo_id) - if clone_url is None: - if via_ssh is None: - via_ssh = Repo.DEFAULT_VIA_SSH - if via_ssh: - clone_url = self.build_clone_url_ssh(user, name) - else: - clone_url = self.build_clone_url_https(user, name) - if homepage is None: - homepage = self.build_homepage_url(user, name) - super().__init__(repo_id, clone_url, owner=owner, desc=desc, - homepage=homepage) + def provider_name(self): + return 'Bitbucket' - @staticmethod - def build_clone_url_ssh(user, name): + def get_default_user(self): + return BitbucketRepo.DEFAULT_USER + + def build_clone_url_ssh(self, user, name): return f'ssh://git@bitbucket.org/{user}/{name}.git' - @staticmethod - def build_clone_url_https(user, name): + def build_clone_url_https(self, user, name): return f'https://bitbucket.org/{user}/{name}.git' - @staticmethod - def build_homepage_url(user, name): + def build_homepage_url(self, user, name): return f'https://bitbucket.org/{user}/{name.lower()}' -- cgit v1.2.3