1616from rich .text import Text
1717
1818from SoftLayer .CLI import environment
19- from SoftLayer import utils
2019
2120
2221class 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]" :
0 commit comments