Compare commits

..

2 Commits
2.5.2 ... 2.5.3

Author SHA1 Message Date
98f2f19d5b Release 2.5.3 2023-01-02 03:24:11 +02:00
5c4d3eeffd allow setting context with existing
In case of running command from web interface, it will raise exception
because context has been copied with subprocesses
2023-01-02 03:21:15 +02:00
6 changed files with 29 additions and 15 deletions

View File

@ -1,7 +1,7 @@
# Maintainer: Evgeniy Alekseev # Maintainer: Evgeniy Alekseev
pkgname='ahriman' pkgname='ahriman'
pkgver=2.5.2 pkgver=2.5.3
pkgrel=1 pkgrel=1
pkgdesc="ArcH linux ReposItory MANager" pkgdesc="ArcH linux ReposItory MANager"
arch=('any') arch=('any')

View File

@ -58,22 +58,25 @@ class _Context:
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], value: T) -> None: def set(self, key: ContextKey[T], value: T, strict: bool = True) -> None:
""" """
set value for the specified key set value for the specified key
Args: Args:
key(ContextKey[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
strict(bool, optional): check if key already exists (Default value = True)
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 key.key in self._content: has_key = key.key in self._content
if strict and has_key:
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}")
if not has_key:
self._content[key.key] = value self._content[key.key] = value
def __iter__(self) -> Iterator[str]: def __iter__(self) -> Iterator[str]:

View File

@ -83,12 +83,12 @@ class Repository(Executor, UpdateHandler):
""" """
ctx = context.get() ctx = context.get()
ctx.set(ContextKey("database", SQLite), self.database) ctx.set(ContextKey("database", SQLite), self.database, strict=False)
ctx.set(ContextKey("configuration", Configuration), self.configuration) ctx.set(ContextKey("configuration", Configuration), self.configuration, strict=False)
ctx.set(ContextKey("pacman", Pacman), self.pacman) ctx.set(ContextKey("pacman", Pacman), self.pacman, strict=False)
ctx.set(ContextKey("sign", GPG), self.sign) ctx.set(ContextKey("sign", GPG), self.sign, strict=False)
ctx.set(ContextKey("repository", type(self)), self) ctx.set(ContextKey("repository", type(self)), self, strict=False)
context.set(ctx) context.set(ctx)

View File

@ -17,4 +17,4 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
__version__ = "2.5.2" __version__ = "2.5.3"

View File

@ -32,11 +32,11 @@ def test_set_context(configuration: Configuration, database: SQLite, mocker: Moc
instance = Repository.load("x86_64", configuration, database, report=False, unsafe=False) instance = Repository.load("x86_64", configuration, database, report=False, unsafe=False)
set_mock.assert_has_calls([ set_mock.assert_has_calls([
MockCall(ContextKey("database", SQLite), instance.database), MockCall(ContextKey("database", SQLite), instance.database, strict=False),
MockCall(ContextKey("configuration", Configuration), instance.configuration), MockCall(ContextKey("configuration", Configuration), instance.configuration, strict=False),
MockCall(ContextKey("pacman", Pacman), instance.pacman), MockCall(ContextKey("pacman", Pacman), instance.pacman, strict=False),
MockCall(ContextKey("sign", GPG), instance.sign), MockCall(ContextKey("sign", GPG), instance.sign, strict=False),
MockCall(ContextKey("repository", Repository), instance), MockCall(ContextKey("repository", Repository), instance, strict=False),
]) ])

View File

@ -57,6 +57,17 @@ def test_set_value_exception() -> None:
ctx.set(ContextKey("key", str), 42) ctx.set(ContextKey("key", str), 42)
def test_set_value_exists() -> None:
"""
must skip key set in case if key already exists and strict check is disabled
"""
key, value = ContextKey("key", int), 42
ctx = _Context()
ctx.set(key, value)
ctx.set(key, value, strict=False)
def test_contains() -> None: def test_contains() -> None:
""" """
must correctly check if element is in list must correctly check if element is in list