Skip to content

Commit

Permalink
models.history:HistoryEntry - command_history can take ctx as both cl…
Browse files Browse the repository at this point in the history
…ick.Context and str
  • Loading branch information
MatteoCampinoti94 committed Jun 18, 2024
1 parent 233f805 commit 652e605
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions acacore/models/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HistoryEntry(ACABase):
@classmethod
def command_history(
cls,
ctx: Context,
ctx: Context | str,
operation: str,
uuid: UUID4 | None = None,
data: object | None = None,
Expand All @@ -43,16 +43,25 @@ def command_history(
:return: A `HistoryEntry` instance representing the command history entry.
"""
command: str
current: Context = ctx
command_parts: list[str] = []
while current:
command_parts.insert(0, current.command.name)
current = current.parent
command = ".".join(command_parts)

if isinstance(ctx, Context):
current: Context = ctx
command_parts: list[str] = [current.command.name]
while current.parent is not None:
current = current.parent
command_parts.insert(0, current.command.name)
command = ".".join(command_parts)
else:
command = ctx

operation = f"{command.strip(':.')}:{operation.strip(':')}"

if add_params_to_data and isinstance(data, dict):
if add_params_to_data and not isinstance(ctx, Context):
raise TypeError(f"add_params_to_data is not compatible with ctx of type {type(ctx)}")

if add_params_to_data and data is None:
data = {"acacore": __version__, "params": ctx.params}
elif add_params_to_data and isinstance(data, dict):
data |= {"acacore": __version__, "params": ctx.params}
elif add_params_to_data and isinstance(data, list):
data.append({"acacore": __version__, "params": ctx.params})
Expand Down

0 comments on commit 652e605

Please sign in to comment.