Skip to content

Commit d2a9e04

Browse files
authored
Merge pull request #26 from nsec/rich
Use `rich` library
2 parents e89e691 + 7104195 commit d2a9e04

File tree

17 files changed

+502
-141
lines changed

17 files changed

+502
-141
lines changed

.github/workflows/tests.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ jobs:
3636
run: |
3737
pip install -e .
3838
39+
- name: ruff
40+
run: |
41+
ruff format --check ./ctf
42+
ruff check ./ctf
43+
3944
- name: ctf init
4045
run: |
4146
ctf init test-ctf

ctf/__main__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/usr/bin/env python3
2+
import logging
23
import os
34

5+
import typer
46
from typer import Typer
7+
from typing_extensions import Annotated
58

69
from ctf import (
710
CTF_ROOT_DIRECTORY,
@@ -39,6 +42,17 @@
3942
app.add_typer(version_app)
4043

4144

45+
@app.callback()
46+
def global_options(
47+
verbose: Annotated[
48+
bool, typer.Option("--verbose", "-v", help="Enable DEBUG logging.")
49+
] = False,
50+
):
51+
if verbose:
52+
LOG.setLevel(logging.DEBUG)
53+
LOG.handlers[0].setLevel(logging.DEBUG)
54+
55+
4256
def main():
4357
app()
4458

ctf/flags.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
from enum import StrEnum, unique
66

7+
import rich
78
import typer
89
import yaml
910
from typing_extensions import Annotated
@@ -66,12 +67,12 @@ def flags(
6667
return
6768

6869
if format == OutputFormat.JSON:
69-
print(json.dumps(obj=flags, indent=2))
70+
rich.print(json.dumps(obj=flags, indent=2))
7071
elif format == OutputFormat.CSV:
7172
output = io.StringIO()
7273
writer = csv.DictWriter(f=output, fieldnames=flags[0].keys())
7374
writer.writeheader()
7475
writer.writerows(rowdicts=flags)
75-
print(output.getvalue())
76+
rich.print(output.getvalue())
7677
elif format == OutputFormat.YAML:
77-
print(yaml.safe_dump(data=flags))
78+
rich.print(yaml.safe_dump(data=flags))

ctf/list.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import os
22
from enum import StrEnum
33

4+
import rich
45
import typer
5-
from tabulate import tabulate
6+
from rich.table import Table
67
from typing_extensions import Annotated
78

89
from ctf import CTF_ROOT_DIRECTORY
9-
from ctf.logger import LOG
1010
from ctf.utils import parse_post_yamls, parse_track_yaml
1111

1212
app = typer.Typer()
@@ -52,19 +52,16 @@ def list_tracks(
5252
)
5353

5454
if format.value == "pretty":
55-
LOG.info(
56-
"\n"
57-
+ tabulate(
58-
parsed_tracks,
59-
headers=[
60-
"Internal track name",
61-
"Discourse Topic Name",
62-
"Dev",
63-
"Support",
64-
"QA",
65-
],
66-
tablefmt="fancy_grid",
67-
)
68-
)
55+
table = Table(title="Tracks")
56+
table.add_column("Internal track name", style="cyan")
57+
table.add_column("Discourse topic name", style="magenta")
58+
table.add_column("Dev")
59+
table.add_column("Support")
60+
table.add_column("QA")
61+
62+
for parsed_track in sorted(parsed_tracks, key=lambda x: x[0].lower()):
63+
table.add_row(*parsed_track)
64+
65+
rich.print(table)
6966
else:
7067
raise ValueError(f"Invalid format: {format.value}")

ctf/logger.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import logging
22

3-
import coloredlogs
3+
from rich.logging import RichHandler
44

55
LOG = logging.getLogger()
6-
LOG.setLevel(level=logging.DEBUG)
7-
coloredlogs.install(level="DEBUG", logger=LOG)
6+
LOG.setLevel(level=logging.INFO)
7+
FORMAT = "%(message)s"
8+
logging.basicConfig(
9+
level=logging.DEBUG,
10+
format=FORMAT,
11+
datefmt="[%X]",
12+
handlers=[RichHandler(level=logging.INFO)],
13+
)

ctf/new.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ def new(
3434
],
3535
template: Annotated[
3636
Template,
37-
typer.Option("--template", "-t", help="Template to use for the track."),
37+
typer.Option(
38+
"--template",
39+
"-t",
40+
help="Template to use for the track.",
41+
prompt="Template to use for the track.",
42+
),
3843
] = Template.APACHE_PHP,
3944
force: Annotated[
4045
bool,

ctf/services.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22

3+
import rich
34
import typer
45
from typing_extensions import Annotated
56

@@ -49,4 +50,4 @@ def services(
4950
check = service["check"]
5051
port = service["port"]
5152

52-
print(f"{track}/{instance}/{name} {contact} {address} {check} {port}")
53+
rich.print(f"{track}/{instance}/{name} {contact} {address} {check} {port}")

ctf/stats.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
from datetime import datetime
77

8+
import rich
89
import typer
910
from typing_extensions import Annotated
1011

@@ -151,7 +152,7 @@ def stats(
151152
sorted(stats["number_of_points_per_track"].items(), key=lambda item: item[1])
152153
)
153154

154-
print(json.dumps(stats, indent=2, ensure_ascii=False))
155+
rich.print(json.dumps(stats, indent=2, ensure_ascii=False))
155156
if generate_badges:
156157
if not _has_pybadges:
157158
LOG.critical(msg="Module pybadges was not found.")
@@ -291,7 +292,7 @@ def stats(
291292
# Execute your command here (replace with what you need)
292293
result = (
293294
subprocess.run(
294-
["python", "scripts/ctf.py", "stats"],
295+
["ctf", "stats"],
295296
check=False,
296297
capture_output=True,
297298
text=True,

ctf/templates/init/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ challenges/*/terraform/versions.tf
2121
!.vscode/settings.json
2222
!.vscode/extensions.json
2323

24+
.idea

ctf/templates/init/.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"editor.tabSize": 2
44
},
55
"yaml.schemas": {
6-
"scripts/schemas/track.yaml.json": "challenges/**/track.yaml",
7-
"scripts/schemas/post.json": "challenges/*/posts/*.yaml"
6+
"schemas/track.yaml.json": "challenges/*/track.yaml",
7+
"schemas/post.json": "challenges/*/posts/*.yaml"
88
}
99
}

0 commit comments

Comments
 (0)