1- #!/usr/bin/python
2- # -*- coding: utf-8 -*-
1+ from __future__ import annotations
32
43import click
4+ from attrs import define , field
55
66from shellfoundry .utilities .config .config_context import ConfigContext
77from shellfoundry .utilities .config .config_file_creation import ConfigFileCreation
1515DEFAULTS_CHAR = "*"
1616
1717
18- class ConfigCommandExecutor ( object ):
19- def __init__ ( self , global_cfg , cfg_creation = None ) :
20- self . global_cfg = global_cfg
21- self . cfg_creation = cfg_creation or ConfigFileCreation ( )
18+ @ define
19+ class ConfigCommandExecutor :
20+ global_cfg : bool
21+ cfg_creation : ConfigFileCreation = field ( factory = ConfigFileCreation )
2222
23- def config (self , kv = (None , None ), key_to_remove = None ):
23+ def config (
24+ self ,
25+ kv : tuple [str | None , str | None ] = (None , None ),
26+ key_to_remove : str = None ,
27+ ) -> None :
2428 config_file_path = self ._get_config_file_path (self .global_cfg )
25- if self . _should_remove_key ( key_to_remove ):
29+ if key_to_remove is not None : # remove key
2630 context = ConfigContext (config_file_path )
2731 ConfigRecord (key_to_remove ).delete (context )
28- elif self ._should_append_key (kv ):
29- field , name = kv
30- if not name :
31- raise click .BadArgumentUsage (
32- "Field '{}' can not be empty" .format (field )
33- )
32+ elif None not in kv : # append key
33+ config_key , config_value = kv
34+ if not config_value :
35+ raise click .BadArgumentUsage (f"Field '{ config_key } ' can not be empty" )
3436 else :
3537 self .cfg_creation .create (config_file_path )
3638 context = ConfigContext (config_file_path )
3739 ConfigRecord (* kv ).save (context )
3840 else :
3941 self ._echo_config (config_file_path )
4042
41- def _should_append_key (self , kv ):
42- return None not in kv
43-
44- def _should_remove_key (self , key_to_remove ):
45- return key_to_remove is not None
46-
47- def _echo_config (self , config_file_path ):
48-
43+ def _echo_config (self , config_file_path : str ) -> None :
44+ """Print current configuration."""
4945 config_data = Configuration .readall (
5046 config_file_path , mark_defaults = DEFAULTS_CHAR
5147 )
5248 table = self ._format_config_as_table (config_data , DEFAULTS_CHAR )
5349 click .echo (table )
5450 click .echo ("" )
5551 click .echo (
56- "* Value marked with '{}' is actually the default value and has not been override by the user." .format ( # noqa: E501
57- DEFAULTS_CHAR
58- )
52+ f"* Value marked with '{ DEFAULTS_CHAR } ' "
53+ f"is actually the default value and has not been override by the user."
5954 )
6055
61- def _format_config_as_table (self , config_data , defaults_char ):
56+ @staticmethod
57+ def _format_config_as_table (
58+ config_data : dict [str , dict ], defaults_char : str
59+ ) -> str :
60+ """Format configuration in readable table view."""
61+ import terminaltables
62+
6263 from shellfoundry .utilities .modifiers .configuration .password_modification import ( # noqa: E501
6364 PasswordModification ,
6465 )
@@ -72,15 +73,14 @@ def _format_config_as_table(self, config_data, defaults_char):
7273 if key in PasswordModification .HANDLING_KEYS :
7374 value = "[encrypted]"
7475 table_data .append ([key , value , default_val ])
75- import terminaltables
7676
7777 table = terminaltables .AsciiTable (table_data )
7878 table .outer_border = False
7979 table .inner_column_border = False
8080 return table .table
8181
8282 @staticmethod
83- def _get_config_file_path (is_global_flag ) :
83+ def _get_config_file_path (is_global_flag : bool ) -> str :
8484 if is_global_flag :
8585 cfg_provider = GlobalConfigProvider ()
8686 return cfg_provider .get_config_path ()
0 commit comments