Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scm: add abstract factory #4

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/outpost/barbican/scm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#
# SPDX-License-Identifier: Apache-2.0

from .git import Git
from enum import auto, unique
import collections.abc
from .scm import ScmBaseClass
from ..utils import StrEnum

from typing import TYPE_CHECKING

Expand All @@ -12,10 +14,34 @@

__all__ = ["Git"]

SCM_FACTORY_DICT = {
"git": Git,
# TODO tarball, etc.
}

@unique
class ScmMethodEnum(StrEnum):
Git = auto()


class ScmMethodFactoryMap(collections.abc.Mapping[ScmMethodEnum, collections.abc.Callable]):
def __init__(self) -> None:
self._key_type = ScmMethodEnum

def __len__(self):
return len(self._key_type)

def __iter__(self):
yield from [k.value for k in list(self._key_type)]

def __getitem__(self, key):
method = self._key_type(key)

from importlib import import_module
import sys

return getattr(
import_module("." + method.value, sys.modules[__name__].__name__), method.name
)


SCM_FACTORY_DICT = ScmMethodFactoryMap()


def scm_create(package: "Package") -> ScmBaseClass:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from git import Repo
from git.exc import GitCommandError

from outpost.barbican.scm import Git
from outpost.barbican.scm.git import Git


class GitTestBase:
Expand Down
Loading