Skip to content

Commit 0cbe1c7

Browse files
author
João Silva
committed
!51 bug(tables): [#102] Probely CLI - get commands table's header shows up funny Closes #102
1 parent 69ba28d commit 0cbe1c7

10 files changed

Lines changed: 38 additions & 54 deletions

File tree

probely/cli/commands/findings/get.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from probely.cli.commands.findings.schemas import FindingsApiFiltersSchema
44
from probely.cli.common import prepare_filters_for_api
5-
from probely.cli.enums import EntityTypeEnum, OutputEnum
5+
from probely.cli.enums import OutputEnum
66
from probely.cli.renderers import OutputRenderer
7+
from probely.cli.tables.finding_table import FindingTable
78
from probely.exceptions import ProbelyCLIValidation
89
from probely.sdk.findings import list_findings, retrieve_findings
910

@@ -23,6 +24,6 @@ def findings_get_command_handler(args: argparse.Namespace):
2324
records=findings_generator,
2425
output_type=output_type,
2526
console=args.console,
26-
entity_type=EntityTypeEnum.FINDING,
27+
table_cls=FindingTable,
2728
)
2829
renderer.render()

probely/cli/commands/scans/get.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from probely.cli.commands.scans.schemas import ScanApiFiltersSchema
44
from probely.cli.common import prepare_filters_for_api
5-
from probely.cli.enums import EntityTypeEnum, OutputEnum
5+
from probely.cli.enums import OutputEnum
66
from probely.cli.renderers import OutputRenderer
7+
from probely.cli.tables.scan_table import ScanTable
78
from probely.exceptions import ProbelyCLIValidation
89
from probely.sdk.scans import list_scans, retrieve_scans
910

@@ -25,6 +26,6 @@ def scans_get_command_handler(args: argparse.Namespace):
2526
records=scans_generator,
2627
output_type=output_type,
2728
console=args.console,
28-
entity_type=EntityTypeEnum.SCAN,
29+
table_cls=ScanTable,
2930
)
3031
renderer.render()

probely/cli/commands/targets/get.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from probely.cli.commands.targets.schemas import TargetApiFiltersSchema
22
from probely.cli.common import prepare_filters_for_api
3-
from probely.cli.enums import EntityTypeEnum, OutputEnum
3+
from probely.cli.enums import OutputEnum
44

55
from probely.cli.renderers import OutputRenderer
6+
from probely.cli.tables.targets_table import TargetTable
67
from probely.exceptions import ProbelyCLIValidation
78
from probely.sdk.targets import list_targets, retrieve_targets
89

@@ -28,6 +29,6 @@ def targets_get_command_handler(args):
2829
records=targets_generator,
2930
output_type=output_type,
3031
console=args.console,
31-
entity_type=EntityTypeEnum.TARGET,
32+
table_cls=TargetTable,
3233
)
3334
renderer.render()

probely/cli/enums.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
from enum import Enum
2-
31
from probely.sdk.enums import ProbelyCLIEnum
42

53

6-
class EntityTypeEnum(Enum):
7-
SCAN = "scan"
8-
FINDING = "finding"
9-
TARGET = "target"
10-
11-
124
class OutputEnum(ProbelyCLIEnum):
135
YAML = "yaml"
146
JSON = "json"

probely/cli/renderers.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
import yaml
1414
from dateutil import parser
1515
from rich.console import Console
16-
from rich.live import Live
1716

18-
from probely.cli.enums import EntityTypeEnum, OutputEnum
17+
from probely.cli.enums import OutputEnum
18+
from probely.cli.tables.base_table import BaseOutputTable
1919
from probely.sdk.enums import ProbelyCLIEnum
2020

2121
UNKNOWN_VALUE_REP = "UNKNOWN"
@@ -32,12 +32,12 @@ def __init__(
3232
records: Generator[dict, None, None],
3333
output_type: Optional[OutputEnum],
3434
console: Console,
35-
entity_type: EntityTypeEnum,
35+
table_cls: Type[BaseOutputTable],
3636
):
3737
self.records = records
3838
self.output_type = output_type
3939
self.console = console
40-
self.entity_type = entity_type
40+
self.table_cls = table_cls
4141

4242
def render(self) -> None:
4343
if self.output_type == OutputEnum.JSON:
@@ -62,15 +62,13 @@ def _render_yaml(self) -> None:
6262
self.console.print(yaml.dump([record], indent=2, width=sys.maxsize))
6363

6464
def _render_table(self) -> None:
65-
from probely.cli.tables.table_factory import ( # Avoid circular import
66-
TableFactory,
67-
)
68-
69-
table_cls = TableFactory.get_table_class(self.entity_type)
70-
table = table_cls().create_table(show_header=True)
71-
with Live(table, console=self.console):
72-
for record in self.records:
73-
table_cls().add_row(table, record)
65+
table = self.table_cls.create_table(show_header=True)
66+
self.console.print(table)
67+
68+
for record in self.records:
69+
table = self.table_cls.create_table(show_header=False)
70+
self.table_cls.add_row(table, record)
71+
self.console.print(table)
7472

7573

7674
def get_printable_enum_value(enum: Type[ProbelyCLIEnum], api_enum_value: str) -> str:

probely/cli/tables/base_table.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44

55

66
class BaseOutputTable(ABC):
7+
@classmethod
78
@abstractmethod
8-
def create_table(self, show_header: bool) -> Table:
9+
def create_table(cls, show_header: bool) -> Table:
910
"""
1011
Initializes and returns a Rich Table with predefined columns.
1112
"""
1213
pass
1314

15+
@classmethod
1416
@abstractmethod
15-
def add_row(self, table: Table, record: dict) -> None:
17+
def add_row(cls, table: Table, record: dict) -> None:
1618
"""
1719
Adds a single row to the provided Rich Table based on the record data.
1820
"""

probely/cli/tables/finding_table.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Dict
2+
23
from rich.table import Table
34

45
from probely.cli.renderers import (
@@ -13,7 +14,8 @@
1314

1415

1516
class FindingTable(BaseOutputTable):
16-
def create_table(self, show_header: bool = False) -> Table:
17+
@classmethod
18+
def create_table(cls, show_header: bool = False) -> Table:
1719
table = Table(show_header=show_header, box=None)
1820

1921
table.add_column("ID", width=18)
@@ -26,7 +28,8 @@ def create_table(self, show_header: bool = False) -> Table:
2628

2729
return table
2830

29-
def add_row(self, table: Table, finding: Dict) -> None:
31+
@classmethod
32+
def add_row(cls, table: Table, finding: Dict) -> None:
3033
target = finding.get("target", {})
3134

3235
table.add_row(

probely/cli/tables/scan_table.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66

77
class ScanTable(BaseOutputTable):
8-
def create_table(self, show_header: bool = False) -> Table:
8+
@classmethod
9+
def create_table(cls, show_header: bool = False) -> Table:
910
table = Table(show_header=show_header, box=None)
1011

1112
table.add_column("ID", width=12)
@@ -20,7 +21,8 @@ def create_table(self, show_header: bool = False) -> Table:
2021

2122
return table
2223

23-
def add_row(self, table: Table, scan: dict) -> None:
24+
@classmethod
25+
def add_row(cls, table: Table, scan: dict) -> None:
2426
target = scan.get("target", {})
2527
site = target.get("site", {})
2628

probely/cli/tables/table_factory.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

probely/cli/tables/targets_table.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Dict
22

33
from rich.table import Table
4+
45
from probely.cli.renderers import (
56
get_printable_enum_value,
67
get_printable_labels,
@@ -11,7 +12,8 @@
1112

1213

1314
class TargetTable(BaseOutputTable):
14-
def create_table(self, show_header: bool = False) -> Table:
15+
@classmethod
16+
def create_table(cls, show_header: bool = False) -> Table:
1517
table = Table(show_header=show_header, box=None)
1618

1719
table.add_column("ID", width=12)
@@ -23,7 +25,8 @@ def create_table(self, show_header: bool = False) -> Table:
2325

2426
return table
2527

26-
def add_row(self, table: Table, target: Dict) -> None:
28+
@classmethod
29+
def add_row(cls, table: Table, target: Dict) -> None:
2730
site = target.get("site")
2831

2932
table.add_row(

0 commit comments

Comments
 (0)