diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2021-03-29 04:18:45 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2021-03-29 04:18:45 +0300 |
commit | 9157ca9b7301bf04f92cc8fff1d69b3639537117 (patch) | |
tree | 1769c6611a2ee61f6b2daafd1d4833d6ea11f462 /cgitize | |
parent | cgitize.repo: refactoring (diff) | |
download | cgitize-9157ca9b7301bf04f92cc8fff1d69b3639537117.tar.gz cgitize-9157ca9b7301bf04f92cc8fff1d69b3639537117.zip |
cgitize.repo: more refactoring
Diffstat (limited to 'cgitize')
-rw-r--r-- | cgitize/main.py | 13 | ||||
-rw-r--r-- | cgitize/repo.py | 185 |
2 files changed, 118 insertions, 80 deletions
diff --git a/cgitize/main.py b/cgitize/main.py index e78f33b..006f235 100644 --- a/cgitize/main.py +++ b/cgitize/main.py @@ -79,7 +79,7 @@ class Config: @property def via_ssh(self): - return self.impl.getboolean('DEFAULT', 'ssh', fallback=Repo.DEFAULT_VIA_SSH) + return self.impl.getboolean('DEFAULT', 'ssh', fallback=True) @property def github_username(self): @@ -97,14 +97,6 @@ class Config: def bitbucket_app_password(self): return self.impl.get('BITBUCKET', 'app_password', fallback=None) - def set_defaults(self): - Repo.DEFAULT_OWNER = self.default_owner - Repo.DEFAULT_VIA_SSH = self.via_ssh - GitHub.DEFAULT_USER = self.github_username - GitHub.ACCESS_TOKEN = self.github_access_token - Bitbucket.DEFAULT_USER = self.bitbucket_username - Bitbucket.APP_PASSWORD = self.bitbucket_app_password - @property def my_repos(self): path = self.impl.get('DEFAULT', 'my_repos', fallback=DEFAULT_MY_REPOS_PATH) @@ -121,13 +113,14 @@ def main(args=None): with setup_logging(): args = parse_args(args) config = Config.read(args.config) - config.set_defaults() my_repos = config.import_my_repos() cgit = CGit(config.clone_url) output = Output(config.output, cgit) success = True for repo in my_repos: if args.repos is None or repo.repo_id in args.repos: + repo.fill_defaults(config) + repo.validate() if not output.pull(repo): success = False if success: diff --git a/cgitize/repo.py b/cgitize/repo.py index 0d6341b..3a2cf1e 100644 --- a/cgitize/repo.py +++ b/cgitize/repo.py @@ -8,115 +8,160 @@ import os.path class Repo: - DEFAULT_OWNER = None - DEFAULT_VIA_SSH = True - @staticmethod def extract_repo_name(repo_id): return os.path.basename(repo_id) - def __init__(self, repo_id, clone_url, owner=None, desc=None, + def __init__(self, repo_id, clone_url=None, owner=None, desc=None, homepage=None): - self.repo_id = repo_id - self.repo_name = self.extract_repo_name(repo_id) - self.clone_url = clone_url - if owner is None: - owner = Repo.DEFAULT_OWNER - self.owner = owner - if desc is None: - if homepage is not None: - desc = homepage - elif clone_url is not None: - desc = clone_url - else: - desc = self.repo_name - self.desc = desc - self.homepage = homepage + self._repo_id = repo_id + self._repo_name = self.extract_repo_name(repo_id) + self._clone_url = clone_url + self._owner = owner + self._desc = desc + self._homepage = homepage + + def fill_defaults(self, config): + if self._owner is None: + self._owner = config.default_owner + + def validate(self): + if self.clone_url is None: + raise RuntimeError('upstream repository URL must be specified') + + @property + def repo_id(self): + return self._repo_id + + @property + def repo_name(self): + return self._repo_name + + @property + def clone_url(self): + return self._clone_url + + @property + def owner(self): + return self._owner + + @property + def desc(self): + if self._desc is not None: + return self._desc + if self.homepage: + return self.homepage + if self.clone_url: + return self.clone_url + return self.repo_name + + @property + def homepage(self): + return self._homepage class HostedRepo(Repo, abc.ABC): - def __init__(self, repo_id, clone_url=None, owner=None, desc=None, - homepage=None, user=None, via_ssh=None): - user = user or self.get_default_user() - if user is None: - 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: - 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, + def __init__(self, repo_id, owner=None, desc=None, homepage=None, + user=None, via_ssh=True): + super().__init__(repo_id, clone_url=None, owner=owner, desc=desc, homepage=homepage) + self._user = user + self._via_ssh = via_ssh - @abc.abstractmethod - def provider_name(self): - pass + def fill_defaults(self, config): + super().fill_defaults(config) + self._via_ssh = config.via_ssh - @abc.abstractmethod - def get_default_user(self): - pass + def validate(self): + super().validate() + if self.user is None: + raise RuntimeError(f'neither explicit or default {self.provider_name} username was specified') + @property + def user(self): + return self._user + + @property @abc.abstractmethod - def build_clone_url_ssh(self, user, name): + def provider_name(self): pass + @property @abc.abstractmethod - def build_clone_url_https(self, user, name): + def clone_url_ssh(self): pass + @property @abc.abstractmethod - def build_homepage_url(self, user, name): + def clone_url_https(self): pass + @property + def clone_url(self): + if self._via_ssh: + return self.clone_url_ssh + return self.clone_url_https + class GitHub(HostedRepo): - DEFAULT_USER = None - ACCESS_TOKEN = None + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._access_token = None + + def fill_defaults(self, config): + super().fill_defaults(config) + if self._user is None: + self._user = config.github_username + self._access_token = config.github_access_token + @property def provider_name(self): return 'GitHub' - def get_default_user(self): - return GitHub.DEFAULT_USER + @property + def homepage(self): + return f'https://github.com/{self.user}/{self.repo_name}' - def build_clone_url_ssh(self, user, name): - return f'ssh://git@github.com/{user}/{name}.git' + @property + def clone_url_ssh(self): + return f'ssh://git@github.com/{self.user}/{self.repo_name}.git' - def build_clone_url_https(self, user, name): - if GitHub.ACCESS_TOKEN is None: + @property + def clone_url_https(self): + if self._access_token is None: auth = '' else: - auth = f'{GitHub.ACCESS_TOKEN}@' - return f'https://{auth}github.com/{user}/{name}.git' - - def build_homepage_url(self, user, name): - return f'https://github.com/{user}/{name}' + auth = f'{self._access_token}@' + return f'https://{auth}github.com/{self.user}/{self.repo_name}.git' class Bitbucket(HostedRepo): - DEFAULT_USER = None - APP_PASSWORD = None + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._app_password = None + + def fill_defaults(self, config): + super().fill_defaults(config) + if self._user is None: + self._user = config.bitbucket_username + self._app_password = config.bitbucket_app_password + @property def provider_name(self): return 'Bitbucket' - def get_default_user(self): - return Bitbucket.DEFAULT_USER + @property + def homepage(self): + return f'https://bitbucket.org/{self.user}/{self.repo_name.lower()}' - def build_clone_url_ssh(self, user, name): - return f'ssh://git@bitbucket.org/{user}/{name}.git' + @property + def clone_url_ssh(self): + return f'ssh://git@bitbucket.org/{self.user}/{self.repo_name}.git' - def build_clone_url_https(self, user, name): - if Bitbucket.APP_PASSWORD is None: + @property + def clone_url_https(self): + if self._app_password is None: auth = '' else: - auth = f'{user}:{Bitbucket.APP_PASSWORD}@' - return f'https://{auth}bitbucket.org/{user}/{name}.git' - - def build_homepage_url(self, user, name): - return f'https://bitbucket.org/{user}/{name.lower()}' + auth = f'{self.user}:{self._app_password}@' + return f'https://{auth}bitbucket.org/{self.user}/{self.repo_name}.git' |