Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Standardize progress bar accross griffon
Browse files Browse the repository at this point in the history
this also fixes non working no progress bar option
  • Loading branch information
JakubFrejlach committed Nov 22, 2023
1 parent 54531d5 commit 9f741c9
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 268 deletions.
51 changes: 45 additions & 6 deletions griffon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import logging
import os
from configparser import ConfigParser
from contextlib import contextmanager
from functools import partial, wraps
from typing import Optional

import component_registry_bindings
import osidb_bindings
from osidb_bindings.bindings.python_client.models import Affect, Flaw, Tracker
from pkg_resources import resource_filename # type: ignore
from pkg_resources import resource_filename
from rich.console import RenderableType # type: ignore
from rich.logging import RichHandler
from rich.style import StyleType

from griffon.output import console

Expand Down Expand Up @@ -319,6 +323,44 @@ def get_fields(model, prefix=""):
return fields


@contextmanager
def console_status(no_progress_bar):
"""updatable console status progress bar"""

class DisabledStatusObject:
"""
Dummy disabled status object for graceful handle of
no progress bar option
"""

def __getattr__(self, attr):
def dummy_method(*args, **kwargs):
pass # Do nothing when any method is called

return dummy_method

class StatusObject:
"""
Status object for default Griffon status handling
"""

def __init__(self, status):
self.status = status

def update(self, status, *args, **kwargs):
self.status.update(
status=f"[magenta b]griffoning:[/magenta b] [bold]{status}[/bold]", *args, **kwargs
)

if no_progress_bar:
yield DisabledStatusObject()
else:
with console.status(
f"[magenta b]griffoning[/magenta b]", spinner="line"
) as operation_status:
yield StatusObject(operation_status)


def progress_bar(
func=None,
):
Expand All @@ -329,11 +371,8 @@ def progress_bar(
@wraps(func)
def wrapper(*args, **kwargs):
obj: dict = args[0].obj
if obj.get("NO_PROGRESS_BAR"):
func(*args, **kwargs)
else:
with console.status("griffoning", spinner="line"):
func(*args, **kwargs)
with console_status(obj.get("NO_PROGRESS_BAR")) as operation_status:
func(*args, operation_status=operation_status, **kwargs)

return wrapper

Expand Down
2 changes: 1 addition & 1 deletion griffon/commands/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
@catch_exception(handle=(HTTPError))
@click.pass_context
@progress_bar
def generate_affects_for_component_process(ctx, purl, cve_id):
def generate_affects_for_component_process(ctx, purl, cve_id, operation_status):
"""Generate affects for specific component."""
if not purl and not cve_id:
click.echo(ctx.get_help())
Expand Down
Loading

0 comments on commit 9f741c9

Please sign in to comment.