Skip to content

Commit d7238d5

Browse files
committed
Solved comments
1 parent 9062729 commit d7238d5

5 files changed

Lines changed: 60 additions & 78 deletions

File tree

SoftLayer/CLI/command.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from rich.text import Text
1717

1818
from SoftLayer.CLI import environment
19-
from SoftLayer import utils
2019

2120

2221
class OptionHighlighter(RegexHighlighter):
@@ -25,7 +24,6 @@ class OptionHighlighter(RegexHighlighter):
2524
r"(?P<switch>^\-\w)", # single options like -v
2625
r"(?P<option>\-\-[\w\-]+)", # long options like --verbose
2726
r"(?P<default_option>\[[^\]]+\])", # anything between [], usually default options
28-
2927
]
3028

3129

@@ -36,42 +34,40 @@ def __init__(self, *path, **attrs):
3634
click.MultiCommand.__init__(self, **attrs)
3735
self.path = path
3836
self.highlighter = OptionHighlighter()
39-
self.console = utils.console_color_themes(theme=None)
37+
self.env = None
38+
self.console = None
4039

41-
def updated_console_whit_theme(self, theme):
42-
"""Update the console depending on the theme"""
43-
# print(theme)
44-
self.console = utils.console_color_themes(theme)
40+
def ensure_env(self, ctx):
41+
"""ensures self.env is set"""
42+
if self.env is None:
43+
self.env = ctx.ensure_object(environment.Environment)
44+
self.env.load()
45+
if self.console is None:
46+
self.console = self.env.console
4547

4648
def list_commands(self, ctx):
4749
"""List all sub-commands."""
48-
env = ctx.ensure_object(environment.Environment)
49-
env.load()
50-
51-
return sorted(env.list_commands(*self.path))
50+
self.ensure_env(ctx)
51+
return sorted(self.env.list_commands(*self.path))
5252

5353
def get_command(self, ctx, cmd_name):
5454
"""Get command for click."""
55-
env = ctx.ensure_object(environment.Environment)
56-
env.load()
57-
self.updated_console_whit_theme(env.theme)
55+
self.ensure_env(ctx)
5856
# Do alias lookup (only available for root commands)
5957
if len(self.path) == 0:
60-
cmd_name = env.resolve_alias(cmd_name)
58+
cmd_name = self.env.resolve_alias(cmd_name)
6159

6260
new_path = list(self.path)
6361
new_path.append(cmd_name)
64-
module = env.get_command(*new_path)
62+
module = self.env.get_command(*new_path)
6563
if isinstance(module, types.ModuleType):
6664
return CommandLoader(*new_path, help=module.__doc__ or '')
6765
else:
6866
return module
6967

7068
def format_usage(self, ctx: click.Context, formatter: click.formatting.HelpFormatter) -> None:
7169
"""Formats and colorizes the usage information."""
72-
env = ctx.ensure_object(environment.Environment)
73-
env.load()
74-
self.updated_console_whit_theme(env.theme)
70+
self.ensure_env(ctx)
7571
pieces = self.collect_usage_pieces(ctx)
7672
for index, piece in enumerate(pieces):
7773
if piece == "[OPTIONS]":
@@ -157,17 +153,20 @@ class SLCommand(click.Command):
157153
def __init__(self, **attrs):
158154
click.Command.__init__(self, **attrs)
159155
self.highlighter = OptionHighlighter()
160-
self.console = utils.console_color_themes(theme=None)
156+
self.env = None
157+
self.console = None
161158

162-
def updated_console_whit_theme(self, theme):
163-
"""Update the console depending on the theme"""
164-
self.console = utils.console_color_themes(theme)
159+
def ensure_env(self, ctx):
160+
"""ensures self.env is set"""
161+
if self.env is None:
162+
self.env = ctx.ensure_object(environment.Environment)
163+
self.env.load()
164+
if self.console is None:
165+
self.console = self.env.console
165166

166167
def format_usage(self, ctx: click.Context, formatter: click.formatting.HelpFormatter) -> None:
167168
"""Formats and colorizes the usage information."""
168-
env = ctx.ensure_object(environment.Environment)
169-
env.load()
170-
self.updated_console_whit_theme(env.theme)
169+
self.ensure_env(ctx)
171170
pieces = self.collect_usage_pieces(ctx)
172171
for index, piece in enumerate(pieces):
173172
if piece == "[OPTIONS]":

SoftLayer/CLI/config/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def cli(env, auth):
9393
timeout = int(float(env.input('Timeout', default=defaults['timeout'] or 0)))
9494

9595
# Ask theme for console
96-
theme = env.input('Theme [dark/light]', default=defaults['theme'])
96+
theme = env.input('Theme (dark/light)', default=defaults['theme'] or 'dark')
9797

9898
path = '~/.softlayer'
9999
if env.config_file:

SoftLayer/CLI/environment.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import SoftLayer
2020
from SoftLayer.CLI import formatting
2121
from SoftLayer.CLI import routes
22+
from SoftLayer import utils
2223

2324
# pylint: disable=too-many-instance-attributes, invalid-name
2425

@@ -38,12 +39,12 @@ def __init__(self):
3839
self.vars = {}
3940

4041
self.client = None
41-
self.console = Console()
42+
self.theme = self.set_env_theme()
43+
self.console = utils.console_color_themes(self.theme)
4244
self.err_console = Console(stderr=True)
4345
self.format = 'table'
4446
self.skip_confirmations = False
4547
self.config_file = None
46-
self.theme = None
4748

4849
self._modules_loaded = False
4950

@@ -216,14 +217,20 @@ def set_env_theme(self, config_file=None):
216217
"""Get theme to color console and set in env"""
217218
theme = os.environ.get('SL_THEME')
218219
if theme:
219-
self.theme = theme
220+
return theme
220221
else:
221-
config = configparser.RawConfigParser({
222-
'theme': '',
223-
})
224-
config.read(config_file)
222+
config_files = ['/etc/softlayer.conf', '~/.softlayer']
223+
path_os = os.getenv('HOME')
224+
if path_os:
225+
config_files.append(path_os + '\\AppData\\Roaming\\softlayer')
226+
if config_file:
227+
config_files.append(config_file)
228+
config = configparser.RawConfigParser({'theme': 'dark'})
229+
config.read(config_files)
225230
if config.has_section('softlayer'):
226231
self.theme = config.get('softlayer', 'theme')
232+
return config.get('softlayer', 'theme')
233+
return 'dark'
227234

228235

229236
class ModuleLoader(object):

SoftLayer/utils.py

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -465,41 +465,7 @@ def decode_stacked(document, pos=0, decoder=JSONDecoder()):
465465

466466
def console_color_themes(theme):
467467
"""Colors in https://rich.readthedocs.io/en/stable/appendix/colors.html?highlight=light_pink1#standard-colors"""
468-
# Default theme
469-
if not theme:
470-
return Console(theme=Theme(
471-
{
472-
"options": "bold cyan", # OPTIONS
473-
"command": "orange3", # COMMAND
474-
"args": "bold cyan", # ARGS
475-
"path": "bold red", # command path
476-
"name_sub_command": "orange3", # sub command name
477-
"sub_command": "orange3", # sub command list
478-
# Help table colors options
479-
"option": "bold cyan",
480-
"switch": "bold green",
481-
"default_option": "light_coral",
482-
"option_keyword": "bold cyan",
483-
"args_keyword": "bold green",
484-
})
485-
)
486-
if theme == 'dark':
487-
return Console(theme=Theme(
488-
{
489-
"options": "bold cyan", # OPTIONS
490-
"command": "orange3", # COMMAND
491-
"args": "bold cyan", # ARGS
492-
"path": "bold red", # command path
493-
"name_sub_command": "orange3", # sub command name
494-
"sub_command": "orange3", # sub command list
495-
# Help table colors options
496-
"option": "bold cyan",
497-
"switch": "bold green",
498-
"default_option": "light_pink1",
499-
"option_keyword": "bold cyan",
500-
"args_keyword": "bold green",
501-
})
502-
)
468+
503469
if theme == 'light':
504470
return Console(theme=Theme(
505471
{
@@ -517,18 +483,28 @@ def console_color_themes(theme):
517483
"args_keyword": "bold green4",
518484
})
519485
)
520-
return None
486+
return Console(theme=Theme(
487+
{
488+
"options": "bold cyan", # OPTIONS
489+
"command": "orange3", # COMMAND
490+
"args": "bold cyan", # ARGS
491+
"path": "bold red", # command path
492+
"name_sub_command": "orange3", # sub command name
493+
"sub_command": "orange3", # sub command list
494+
# Help table colors options
495+
"option": "bold cyan",
496+
"switch": "bold green",
497+
"default_option": "light_pink1",
498+
"option_keyword": "bold cyan",
499+
"args_keyword": "bold green",
500+
})
501+
)
521502

522503

523504
def table_color_theme(theme):
524505
"""Define result table colors"""
525-
if not theme:
526-
return {'header': 'bright_cyan',
527-
'id_columns': 'pale_violet_red1'}
528-
if theme == 'dark':
529-
return {'header': 'bright_cyan',
530-
'id_columns': 'pale_violet_red1'}
531506
if theme == 'light':
532507
return {'header': 'dark_cyan',
533508
'id_columns': 'light_coral'}
534-
return None
509+
return {'header': 'bright_cyan',
510+
'id_columns': 'pale_violet_red1'}

tests/CLI/modules/config_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_show(self):
3636
'API Key': 'api-key',
3737
'Endpoint URL': 'http://endpoint-url',
3838
'Timeout': 'not set',
39-
'Theme': 'not set'})
39+
'Theme': 'dark'})
4040

4141

4242
class TestHelpSetup(testing.TestCase):

0 commit comments

Comments
 (0)