Compare commits

..

1 Commits

Author SHA1 Message Date
f6da56ae20 implement local reporter mode 2024-05-10 13:54:11 +03:00
12 changed files with 31 additions and 65 deletions

View File

@ -38,12 +38,12 @@ class _Context:
""" """
self._content: dict[str, Any] = {} self._content: dict[str, Any] = {}
def get(self, key: ContextKey[T] | type[T]) -> T: def get(self, key: ContextKey[T]) -> T:
""" """
get value for the specified key get value for the specified key
Args: Args:
key(ContextKey[T] | type[T]): context key name key(ContextKey[T]): context key name
Returns: Returns:
T: value associated with the key T: value associated with the key
@ -52,37 +52,29 @@ class _Context:
KeyError: in case if the specified context variable was not found KeyError: in case if the specified context variable was not found
ValueError: in case if type of value is not an instance of specified return type ValueError: in case if type of value is not an instance of specified return type
""" """
if not isinstance(key, ContextKey):
key = ContextKey.from_type(key)
if key.key not in self._content: if key.key not in self._content:
raise KeyError(key.key) raise KeyError(key.key)
value = self._content[key.key] value = self._content[key.key]
if not isinstance(value, key.return_type): if not isinstance(value, key.return_type):
raise ValueError(f"Value {value} is not an instance of {key.return_type}") raise ValueError(f"Value {value} is not an instance of {key.return_type}")
return value return value
def set(self, key: ContextKey[T] | type[T], value: T) -> None: def set(self, key: ContextKey[T], value: T) -> None:
""" """
set value for the specified key set value for the specified key
Args: Args:
key(ContextKey[T] | type[T]): context key name key(ContextKey[T]): context key name
value(T): context value associated with the specified key value(T): context value associated with the specified key
Raises: Raises:
KeyError: in case if the specified context variable already exists KeyError: in case if the specified context variable already exists
ValueError: in case if type of value is not an instance of specified return type ValueError: in case if type of value is not an instance of specified return type
""" """
if not isinstance(key, ContextKey):
key = ContextKey.from_type(key)
if key.key in self._content: if key.key in self._content:
raise KeyError(key.key) raise KeyError(key.key)
if not isinstance(value, key.return_type): if not isinstance(value, key.return_type):
raise ValueError(f"Value {value} is not an instance of {key.return_type}") raise ValueError(f"Value {value} is not an instance of {key.return_type}")
self._content[key.key] = value self._content[key.key] = value
def __iter__(self) -> Iterator[str]: def __iter__(self) -> Iterator[str]:

View File

@ -22,6 +22,7 @@ from ahriman.core.configuration import Configuration
from ahriman.core.gitremote.remote_push import RemotePush from ahriman.core.gitremote.remote_push import RemotePush
from ahriman.core.status.client import Client from ahriman.core.status.client import Client
from ahriman.core.triggers import Trigger from ahriman.core.triggers import Trigger
from ahriman.models.context_key import ContextKey
from ahriman.models.package import Package from ahriman.models.package import Package
from ahriman.models.repository_id import RepositoryId from ahriman.models.repository_id import RepositoryId
from ahriman.models.result import Result from ahriman.models.result import Result
@ -110,7 +111,7 @@ class RemotePushTrigger(Trigger):
GitRemoteError: if database is not set in context GitRemoteError: if database is not set in context
""" """
ctx = context.get() ctx = context.get()
reporter = ctx.get(Client) reporter = ctx.get(ContextKey("reporter", Client))
for target in self.targets: for target in self.targets:
section, _ = self.configuration.gettype( section, _ = self.configuration.gettype(

View File

@ -27,6 +27,7 @@ from ahriman.core.repository.executor import Executor
from ahriman.core.repository.update_handler import UpdateHandler from ahriman.core.repository.update_handler import UpdateHandler
from ahriman.core.sign.gpg import GPG from ahriman.core.sign.gpg import GPG
from ahriman.core.status.client import Client from ahriman.core.status.client import Client
from ahriman.models.context_key import ContextKey
from ahriman.models.pacman_synchronization import PacmanSynchronization from ahriman.models.pacman_synchronization import PacmanSynchronization
from ahriman.models.repository_id import RepositoryId from ahriman.models.repository_id import RepositoryId
@ -89,12 +90,12 @@ class Repository(Executor, UpdateHandler):
# directly without loader # directly without loader
ctx = _Context() ctx = _Context()
ctx.set(SQLite, self.database) ctx.set(ContextKey("database", SQLite), self.database)
ctx.set(Configuration, self.configuration) ctx.set(ContextKey("configuration", Configuration), self.configuration)
ctx.set(Pacman, self.pacman) ctx.set(ContextKey("pacman", Pacman), self.pacman)
ctx.set(GPG, self.sign) ctx.set(ContextKey("reporter", Client), self.reporter)
ctx.set(Client, self.reporter) ctx.set(ContextKey("sign", GPG), self.sign)
ctx.set(type(self), self) ctx.set(ContextKey("repository", type(self)), self)
context.set(ctx) context.set(ctx)

View File

@ -24,6 +24,7 @@ from ahriman.core.sign.gpg import GPG
from ahriman.core.support.package_creator import PackageCreator from ahriman.core.support.package_creator import PackageCreator
from ahriman.core.support.pkgbuild.keyring_generator import KeyringGenerator from ahriman.core.support.pkgbuild.keyring_generator import KeyringGenerator
from ahriman.core.triggers import Trigger from ahriman.core.triggers import Trigger
from ahriman.models.context_key import ContextKey
from ahriman.models.repository_id import RepositoryId from ahriman.models.repository_id import RepositoryId
@ -133,8 +134,8 @@ class KeyringTrigger(Trigger):
trigger action which will be called at the start of the application trigger action which will be called at the start of the application
""" """
ctx = context.get() ctx = context.get()
sign = ctx.get(GPG) sign = ctx.get(ContextKey("sign", GPG))
database = ctx.get(SQLite) database = ctx.get(ContextKey("database", SQLite))
for target in self.targets: for target in self.targets:
generator = KeyringGenerator(database, sign, self.repository_id, self.configuration, target) generator = KeyringGenerator(database, sign, self.repository_id, self.configuration, target)

View File

@ -25,6 +25,7 @@ from ahriman.core.build_tools.sources import Sources
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.core.status.client import Client from ahriman.core.status.client import Client
from ahriman.core.support.pkgbuild.pkgbuild_generator import PkgbuildGenerator from ahriman.core.support.pkgbuild.pkgbuild_generator import PkgbuildGenerator
from ahriman.models.context_key import ContextKey
from ahriman.models.package import Package from ahriman.models.package import Package
@ -71,7 +72,7 @@ class PackageCreator:
path(Path): path to directory with package files path(Path): path to directory with package files
""" """
ctx = context.get() ctx = context.get()
reporter = ctx.get(Client) reporter: Client = ctx.get(ContextKey("reporter", Client))
_, repository_id = self.configuration.check_loaded() _, repository_id = self.configuration.check_loaded()
package = Package.from_build(path, repository_id.architecture, None) package = Package.from_build(path, repository_id.architecture, None)

View File

@ -18,7 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from dataclasses import dataclass from dataclasses import dataclass
from typing import Generic, Self, TypeVar from typing import Generic, TypeVar
T = TypeVar("T") T = TypeVar("T")
@ -35,16 +35,3 @@ class ContextKey(Generic[T]):
""" """
key: str key: str
return_type: type[T] return_type: type[T]
@classmethod
def from_type(cls, return_type: type[T]) -> Self:
"""
construct key from type
Args:
return_type(type[T]): return type used for the specified context key
Returns:
Self: context key with autogenerated
"""
return cls(return_type.__name__, return_type)

View File

@ -3,6 +3,7 @@ from pytest_mock import MockerFixture
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.core.database import SQLite from ahriman.core.database import SQLite
from ahriman.core.gitremote import RemotePushTrigger from ahriman.core.gitremote import RemotePushTrigger
from ahriman.models.context_key import ContextKey
from ahriman.models.package import Package from ahriman.models.package import Package
from ahriman.models.result import Result from ahriman.models.result import Result
@ -29,5 +30,5 @@ def test_on_result(configuration: Configuration, result: Result, package_ahriman
trigger = RemotePushTrigger(repository_id, configuration) trigger = RemotePushTrigger(repository_id, configuration)
trigger.on_result(result, [package_ahriman]) trigger.on_result(result, [package_ahriman])
database_mock.assert_called_once_with(SQLite) database_mock.assert_called_once_with(ContextKey("database", SQLite))
run_mock.assert_called_once_with(result) run_mock.assert_called_once_with(result)

View File

@ -6,6 +6,7 @@ from ahriman.core.configuration import Configuration
from ahriman.core.database import SQLite from ahriman.core.database import SQLite
from ahriman.core.repository import Repository from ahriman.core.repository import Repository
from ahriman.core.sign.gpg import GPG from ahriman.core.sign.gpg import GPG
from ahriman.models.context_key import ContextKey
def test_load(configuration: Configuration, database: SQLite, mocker: MockerFixture) -> None: def test_load(configuration: Configuration, database: SQLite, mocker: MockerFixture) -> None:
@ -28,9 +29,9 @@ def test_set_context(configuration: Configuration, database: SQLite, mocker: Moc
instance = Repository.load(repository_id, configuration, database, report=False) instance = Repository.load(repository_id, configuration, database, report=False)
set_mock.assert_has_calls([ set_mock.assert_has_calls([
MockCall(SQLite, instance.database), MockCall(ContextKey("database", SQLite), instance.database),
MockCall(Configuration, instance.configuration), MockCall(ContextKey("configuration", Configuration), instance.configuration),
MockCall(Pacman, instance.pacman), MockCall(ContextKey("pacman", Pacman), instance.pacman),
MockCall(GPG, instance.sign), MockCall(ContextKey("sign", GPG), instance.sign),
MockCall(Repository, instance), MockCall(ContextKey("repository", Repository), instance),
]) ])

View File

@ -5,6 +5,7 @@ from ahriman.core.configuration import Configuration
from ahriman.core.database import SQLite from ahriman.core.database import SQLite
from ahriman.core.sign.gpg import GPG from ahriman.core.sign.gpg import GPG
from ahriman.core.support import KeyringTrigger from ahriman.core.support import KeyringTrigger
from ahriman.models.context_key import ContextKey
def test_configuration_sections(configuration: Configuration) -> None: def test_configuration_sections(configuration: Configuration) -> None:
@ -28,5 +29,5 @@ def test_on_start(configuration: Configuration, mocker: MockerFixture) -> None:
trigger = KeyringTrigger(repository_id, configuration) trigger = KeyringTrigger(repository_id, configuration)
trigger.on_start() trigger.on_start()
context_mock.assert_has_calls([MockCall(GPG), MockCall(SQLite)]) context_mock.assert_has_calls([MockCall(ContextKey("sign", GPG)), MockCall(ContextKey("database", SQLite))])
run_mock.assert_called_once_with() run_mock.assert_called_once_with()

View File

@ -4,6 +4,7 @@ from pytest_mock import MockerFixture
from ahriman.core.database import SQLite from ahriman.core.database import SQLite
from ahriman.core.support.package_creator import PackageCreator from ahriman.core.support.package_creator import PackageCreator
from ahriman.models.context_key import ContextKey
from ahriman.models.package import Package from ahriman.models.package import Package
from ahriman.models.package_description import PackageDescription from ahriman.models.package_description import PackageDescription
from ahriman.models.package_source import PackageSource from ahriman.models.package_source import PackageSource
@ -37,5 +38,5 @@ def test_run(package_creator: PackageCreator, database: SQLite, mocker: MockerFi
init_mock.assert_called_once_with(local_path) init_mock.assert_called_once_with(local_path)
package_mock.assert_called_once_with(local_path, "x86_64", None) package_mock.assert_called_once_with(local_path, "x86_64", None)
database_mock.assert_called_once_with(SQLite) database_mock.assert_called_once_with(ContextKey("database", SQLite))
insert_mock.assert_called_once_with(package, pytest.helpers.anyvar(int)) insert_mock.assert_called_once_with(package, pytest.helpers.anyvar(int))

View File

@ -15,18 +15,6 @@ def test_get_set() -> None:
assert ctx.get(key) == value assert ctx.get(key) == value
def test_get_set_type() -> None:
"""
must set and get variable by type
"""
key, value = int, 42
ctx = _Context()
ctx.set(key, value)
assert ctx.get(key) == value
assert ctx.get(ContextKey.from_type(int)) == value
def test_get_key_exception() -> None: def test_get_key_exception() -> None:
""" """
must raise KeyError in case if key was not found must raise KeyError in case if key was not found

View File

@ -1,9 +0,0 @@
from ahriman.models.context_key import ContextKey
def test_from_type() -> None:
"""
must construct key from type
"""
assert ContextKey.from_type(int) == ContextKey("int", int)
assert ContextKey.from_type(ContextKey) == ContextKey("ContextKey", ContextKey)