Skip to content

Commit

Permalink
refacto: make profiles handler more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Nov 28, 2023
1 parent 829d79f commit a4c1549
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
8 changes: 5 additions & 3 deletions qgis_deployment_toolbelt/profiles/local_git_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
from pathlib import Path

# project
from qgis_deployment_toolbelt.profiles.git_handler_base import GitHandlerBase
from qgis_deployment_toolbelt.profiles.profiles_handler_base import (
RemoteProfilesHandlerBase,
)
from qgis_deployment_toolbelt.utils.check_path import check_path, check_var_can_be_path

# 3rd party
Expand All @@ -35,13 +37,13 @@
# #############################################################################
# ########## Classes ###############
# ##################################
class LocalGitHandler(GitHandlerBase):
class LocalGitHandler(RemoteProfilesHandlerBase):
"""Handle local git repository."""

def __init__(
self,
source_repository_path_or_uri: str | Path,
source_repository_type: str = "local",
source_repository_type: str = "git_local",
branch_to_use: str | None = None,
) -> None:
"""Constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
# #############################################################################
# ########## Classes ###############
# ##################################
class GitHandlerBase:
class RemoteProfilesHandlerBase:
"""Common git repository handler using dulwich.
It's designed to handle thoses cases:
Expand All @@ -53,20 +53,24 @@ class GitHandlerBase:

SOURCE_REPOSITORY_ACTIVE_BRANCH: str | None = None
SOURCE_REPOSITORY_PATH_OR_URL: Path | str | None = None
SOURCE_REPOSITORY_TYPE: Literal["local", "remote"] | None = None
SOURCE_REPOSITORY_TYPE: Literal[
"git_local", "git_remote", "http", "local", "remote"
] | None = None

DESTINATION_PATH: Path | None = None
DESTINATION_BRANCH_TO_USE: str | None = None

def __init__(
self,
source_repository_type: Literal["local", "remote"],
source_repository_type: Literal[
"git_local", "git_remote", "http", "local", "remote"
],
branch_to_use: str | None = None,
) -> None:
"""Object instanciation.
Args:
source_repository_type (Literal["local", "remote"]): type of source
source_repository_type (Literal["git_local", "git_remote", "local", "remote"]): type of source
repository
branch_to_use (str | None, optional): branch to clone or checkout. If None,
the source active branch will be used. Defaults to None.
Expand All @@ -85,15 +89,15 @@ def url_parsed(self, remote_git_url: str) -> GitUrlParsed:
def is_valid_git_repository(
self,
source_repository_path_or_url: Path | str | None = None,
force_type: Literal["local", "remote"] | None = None,
force_type: Literal["git_local", "git_remote", "local", "remote"] | None = None,
raise_error: bool = True,
) -> bool:
"""Determine if the given path or URL is a valid repository or not.
Args:
source_repository_path_or_url (Path | str | None, optional): _description_.
Defaults to None.
force_type (Literal["local", "remote"], optional): force git repository
force_type (Literal["git_local","local", "remote"], optional): force git repository
type to check. If None, it uses the SOURCE_REPOSITORY_TYPE attribute.
Defaults None.
raise_error (bool, optional): if True, it raises an exception. Defaults
Expand Down Expand Up @@ -124,12 +128,18 @@ def is_valid_git_repository(
else:
repository_type = force_type

# check according to the repository type
if repository_type == "remote" and not self._is_url_git_repository(
# check according to the repository types
if repository_type in (
"git_remote",
"remote",
) and not self._is_url_git_repository(
remote_git_url=source_repository_path_or_url
):
valid_source = False
elif repository_type == "local" and not self._is_local_path_git_repository(
elif repository_type in (
"git_local",
"local",
) and not self._is_local_path_git_repository(
local_path=source_repository_path_or_url
):
valid_source = False
Expand Down Expand Up @@ -387,7 +397,7 @@ def clone_or_pull(self, to_local_destination_path: Path, attempt: int = 1) -> Re
) and not self.is_valid_git_repository(
source_repository_path_or_url=to_local_destination_path,
raise_error=False,
force_type="local",
force_type="git_local",
):
try:
logger.debug("Start cloning operations...")
Expand All @@ -414,7 +424,7 @@ def clone_or_pull(self, to_local_destination_path: Path, attempt: int = 1) -> Re
elif to_local_destination_path.exists() and self.is_valid_git_repository(
source_repository_path_or_url=to_local_destination_path,
raise_error=False,
force_type="local",
force_type="git_local",
):
# FETCH
logger.debug("Start fetching operations...")
Expand Down Expand Up @@ -488,7 +498,7 @@ def _clone(self, local_path: Path) -> Repo:
f"Cloning repository {self.SOURCE_REPOSITORY_PATH_OR_URL} ({branch=}) to {local_path}"
)

if self.SOURCE_REPOSITORY_TYPE == "local":
if self.SOURCE_REPOSITORY_TYPE in ("git_local", "local"):
with porcelain.open_repo_closing(
path_or_repo=self.SOURCE_REPOSITORY_PATH_OR_URL
) as repo_obj:
Expand Down
9 changes: 4 additions & 5 deletions qgis_deployment_toolbelt/profiles/remote_git_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
import logging

# project
from qgis_deployment_toolbelt.profiles.git_handler_base import GitHandlerBase

# 3rd party

from qgis_deployment_toolbelt.profiles.profiles_handler_base import (
RemoteProfilesHandlerBase,
)

# #############################################################################
# ########## Globals ###############
Expand All @@ -33,7 +32,7 @@
# #############################################################################
# ########## Classes ###############
# ##################################
class RemoteGitHandler(GitHandlerBase):
class RemoteGitHandler(RemoteProfilesHandlerBase):
"""Handle remote git repository."""

def __init__(
Expand Down

0 comments on commit a4c1549

Please sign in to comment.