allow setting context with existing

In case of running command from web interface, it will raise exception
because context has been copied with subprocesses
This commit is contained in:
2023-01-02 03:21:15 +02:00
parent 64cc8fd6b0
commit 11732a8609
4 changed files with 27 additions and 13 deletions

View File

@ -58,23 +58,26 @@ class _Context:
raise ValueError(f"Value {value} is not an instance of {key.return_type}")
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
Args:
key(ContextKey[T]): context key name
value(T): context value associated with the specified key
strict(bool, optional): check if key already exists (Default value = True)
Raises:
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
"""
if key.key in self._content:
has_key = key.key in self._content
if strict and has_key:
raise KeyError(key.key)
if not isinstance(value, key.return_type):
raise ValueError(f"Value {value} is not an instance of {key.return_type}")
self._content[key.key] = value
if not has_key:
self._content[key.key] = value
def __iter__(self) -> Iterator[str]:
"""

View File

@ -83,12 +83,12 @@ class Repository(Executor, UpdateHandler):
"""
ctx = context.get()
ctx.set(ContextKey("database", SQLite), self.database)
ctx.set(ContextKey("configuration", Configuration), self.configuration)
ctx.set(ContextKey("pacman", Pacman), self.pacman)
ctx.set(ContextKey("sign", GPG), self.sign)
ctx.set(ContextKey("database", SQLite), self.database, strict=False)
ctx.set(ContextKey("configuration", Configuration), self.configuration, strict=False)
ctx.set(ContextKey("pacman", Pacman), self.pacman, strict=False)
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)