Skip to content

Commit afb60e7

Browse files
committed
fix:colored python3.8 compatibility
1 parent 5a73990 commit afb60e7

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ requires-python = ">= 3.8"
2828
dependencies = [
2929
"britive>=3.0.0",
3030
"click>=8.1.7",
31+
"colored",
3132
"cryptography",
3233
"jmespath",
3334
"merge-args",

src/pybritive/britive_cli.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import yaml
1616
from britive import exceptions
1717
from britive.britive import Britive
18-
from colored import Fore, Style
1918
from jwt.exceptions import PyJWTError
2019
from tabulate import tabulate
2120

@@ -26,6 +25,11 @@
2625
from .helpers.credentials import EncryptedFileCredentialManager, FileCredentialManager
2726
from .helpers.split import profile_split
2827

28+
try:
29+
from colored import Fore, Style
30+
except ImportError: # colored in < python3.9
31+
from colored import fore, style
32+
2933
default_table_format = 'fancy_grid'
3034
debug_enabled = os.getenv('PYBRITIVE_DEBUG')
3135
default_browser = os.getenv('PYBRITIVE_BROWSER')
@@ -171,14 +175,21 @@ def _display_banner(self):
171175
return
172176

173177
# if we get here then we need to at least grab the banner and see if it has changed
174-
banner = self.b.banner()
175-
banner_changed = Cache().save_banner(tenant=self.tenant_name, banner=banner)
176-
msg_type = banner.get('messageType', 'UNKNOWN')
177-
color = {'caution': Style.BOLD + Fore.red, 'warning': Style.BOLD + Fore.yellow}.get(
178-
msg_type.lower(), Style.BOLD + Fore.blue
179-
)
180-
if banner and banner_changed:
181-
self.print(f'{color}*** {msg_type}: {banner.get("message", "<no message>")} ***{Style.reset}')
178+
if banner := self.b.banner():
179+
banner_changed = Cache().save_banner(tenant=self.tenant_name, banner=banner)
180+
msg_type = banner.get('messageType', 'UNKNOWN')
181+
try:
182+
color = {'caution': Style.BOLD + Fore.red, 'warning': Style.BOLD + Fore.yellow}.get(
183+
msg_type.lower(), Style.BOLD + Fore.blue
184+
)
185+
style_reset = Style.reset
186+
except NameError: # colored in < python3.9
187+
color = {'caution': style.BOLD + fore.RED, 'warning': style.BOLD + fore.YELLOW}.get(
188+
msg_type.lower(), style.BOLD + fore.BLUE
189+
)
190+
style_reset = style.RESET
191+
if banner_changed:
192+
self.print(f'{color}*** {msg_type}: {banner.get("message", "<no message>")} ***{style_reset}')
182193

183194
def _update_sdk_user_agent(self):
184195
# update the user agent to include the pybritive cli version

0 commit comments

Comments
 (0)