Skip to content

Commit 1f6a712

Browse files
committed
refactor from code review
* pyproject.toml ordering * create a helper class for Text * refactor to use Text.*() helpers
1 parent dff03d2 commit 1f6a712

3 files changed

Lines changed: 32 additions & 26 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name = "drupal-advisory-database"
33
requires-python = ">= 3.13"
44

55
[tool.poetry.dependencies]
6+
colorama = "^0.4.6"
67
jsonschema = "^4.23.0"
78
markdownify = "^1.1.0"
89
mypy = "^1.15.0"
@@ -12,7 +13,6 @@ ruff = "~0.12.0"
1213
semver = "^3.0.4"
1314
types-jsonschema = "^4.23.0.20241208"
1415
types-requests = "^2.32.0.20250328"
15-
colorama = "^0.4.6"
1616
types-colorama = "^0.4.15.20240311"
1717

1818
[tool.poetry]

scripts/generate_osv_advisories.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616

1717
import requests
1818
import semver
19-
from colorama import Fore, Style
20-
from colorama import init as colorama_init
2119
from markdownify import markdownify
2220

2321
from typings import drupal, osv
22+
from helpers.text import Text
2423
from user_agent import user_agent
2524

26-
colorama_init()
27-
2825

2926
def fetch_drupal_node(nid: str) -> drupal.Node:
3027
"""
@@ -354,11 +351,11 @@ def patch_advisory(sa_id: str, sa_advisory: drupal.Advisory) -> bool:
354351

355352
if before == sa_advisory['field_affected_versions']:
356353
sa_advisory['field_affected_versions'] = after
357-
print(' \\- ' + success_string('patched affected versions'))
354+
print(' \\- ' + Text.success('patched affected versions'))
358355
return True
359356
print(
360357
' \\- '
361-
+ warning_string(
358+
+ Text.warning(
362359
f'skipped patching as affected version is now "{sa_advisory["field_affected_versions"]}"'
363360
)
364361
)
@@ -382,14 +379,14 @@ def build_osv_advisory(
382379
# we expect that the downloader has excluded PSA type entries, but
383380
# we still guard against them here just in case one slips through
384381
if sa_advisory['field_is_psa'] == '1':
385-
print(' \\-' + error_string('skipping as it is a psa? (this should not happen)'))
382+
print(' \\-' + Text.error('skipping as it is a psa? (this should not happen)'))
386383
return None
387384

388385
# there's not really much we can do if there isn't an affected version
389386
# todo: since build_affected_ranges throws if this isn't present, it might
390387
# make more sense to use that, with a custom exception class
391388
if sa_advisory['field_affected_versions'] is None:
392-
print(' \\- ' + notice_string('skipping as we do not have any affected versions'))
389+
print(' \\- ' + Text.notice('skipping as we do not have any affected versions'))
393390
return None
394391

395392
osv_advisory: osv.Vulnerability = {
@@ -469,7 +466,7 @@ def generate_osv_advisories() -> None:
469466
if is_existing_advisory_ahead(name, sa_id, osv_advisory['modified']):
470467
print(
471468
' \\- '
472-
+ error_string(
469+
+ Text.error(
473470
'error: current modified date is ahead of the proposed modified date (is your cache up to date?)'
474471
)
475472
)
@@ -480,21 +477,5 @@ def generate_osv_advisories() -> None:
480477
f.write('\n')
481478

482479

483-
def notice_string(message: str) -> str:
484-
return f'{Fore.YELLOW}{Style.DIM}{message}{Style.RESET_ALL}'
485-
486-
487-
def success_string(message: str) -> str:
488-
return f'{Fore.GREEN}{message}{Style.RESET_ALL}'
489-
490-
491-
def warning_string(message: str) -> str:
492-
return f'{Fore.YELLOW}{message}{Style.RESET_ALL}'
493-
494-
495-
def error_string(message: str) -> str:
496-
return f'{Fore.RED}{message}{Style.RESET_ALL}'
497-
498-
499480
if __name__ == '__main__':
500481
generate_osv_advisories()

scripts/helpers/text.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from colorama import Fore, Style
2+
from colorama import just_fix_windows_console
3+
4+
just_fix_windows_console()
5+
6+
7+
class Text:
8+
@staticmethod
9+
def notice(message: str) -> str:
10+
return f'{Fore.YELLOW}{Style.DIM}{message}{Style.RESET_ALL}'
11+
12+
13+
@staticmethod
14+
def success(message: str) -> str:
15+
return f'{Fore.GREEN}{message}{Style.RESET_ALL}'
16+
17+
18+
@staticmethod
19+
def warning(message: str) -> str:
20+
return f'{Fore.YELLOW}{message}{Style.RESET_ALL}'
21+
22+
23+
@staticmethod
24+
def error(message: str) -> str:
25+
return f'{Fore.RED}{message}{Style.RESET_ALL}'

0 commit comments

Comments
 (0)