1212
1313class ConfigCommand (CLICommand ):
1414 """Display and manipulate configuration values."""
15- usage = "pybm config get <option>\n " \
16- " or: pybm config set <option> <value>\n " \
17- " or: pybm config list\n " \
18- " or: pybm config describe <option>\n "
15+
16+ usage = (
17+ "pybm config get <option>\n "
18+ " or: pybm config set <option> <value>\n "
19+ " or: pybm config list\n "
20+ " or: pybm config describe <option>\n "
21+ )
1922
2023 def __init__ (self ):
2124 super ().__init__ (name = "config" )
2225
2326 def add_arguments (self , subcommand : str = None ):
2427 # special version action and version kwarg
2528 if subcommand == "get" :
26- self .parser .add_argument ("option" ,
27- type = str ,
28- metavar = "<option>" ,
29- help = "Config option to display. For a "
30- "comprehensive list of options, "
31- "run `pybm config list`." )
29+ self .parser .add_argument (
30+ "option" ,
31+ type = str ,
32+ metavar = "<option>" ,
33+ help = "Config option to display. For a "
34+ "comprehensive list of options, "
35+ "run `pybm config list`." ,
36+ )
3237 elif subcommand == "set" :
33- self .parser .add_argument ("option" ,
34- type = str ,
35- metavar = "<option>" ,
36- help = "Config option to set. For a "
37- "comprehensive list of options, "
38- "run `pybm config list`." )
38+ self .parser .add_argument (
39+ "option" ,
40+ type = str ,
41+ metavar = "<option>" ,
42+ help = "Config option to set. For a "
43+ "comprehensive list of options, "
44+ "run `pybm config list`." ,
45+ )
3946 # TODO: Revise nargs value for this option
40- self .parser .add_argument ("value" ,
41- metavar = "<value>" ,
42- help = "Config option to display. For a "
43- "comprehensive list of options, "
44- "run `pybm config list`." )
47+ self .parser .add_argument (
48+ "value" ,
49+ metavar = "<value>" ,
50+ help = "Config option to display. For a "
51+ "comprehensive list of options, "
52+ "run `pybm config list`." ,
53+ )
4554 elif subcommand == "describe" :
46- self .parser .add_argument ("option" ,
47- type = str ,
48- metavar = "<option>" ,
49- help = "Config option to describe. For a "
50- "comprehensive list of options, "
51- "run `pybm config list`." )
55+ self .parser .add_argument (
56+ "option" ,
57+ type = str ,
58+ metavar = "<option>" ,
59+ help = "Config option to describe. For a "
60+ "comprehensive list of options, "
61+ "run `pybm config list`." ,
62+ )
5263
5364 @contextlib .contextmanager
54- def context (self , op : str , attr : str , value : Optional [str ],
55- verbose : bool = False ):
65+ def context (self , op : str , attr : str , value : Optional [str ], verbose : bool = False ):
5666 # TODO: This is BS
5767 is_group = "." not in attr
5868 expr = "value" + "s" * is_group + f" { value !r} " * (value is not None )
5969 try :
6070 if verbose :
6171 opt_type = "group" if is_group else "option"
6272 op = op .capitalize ()
63- print (f"{ op } ting { expr } for config { opt_type } { attr !r} "
64- f"....." , end = "" )
73+ print (f"{ op } ting { expr } for config { opt_type } { attr !r} ....." , end = "" )
6574 yield
6675 if verbose :
6776 print ("done." )
@@ -72,58 +81,73 @@ def context(self, op: str, attr: str, value: Optional[str],
7281
7382 def get (self , options : argparse .Namespace , verbose : bool ) -> int :
7483 attr : str = options .option
84+
7585 with self .context ("get" , attr , None , verbose ):
7686 value = PybmConfig .load (".pybm/config.yaml" ).get_value (attr )
87+
7788 if is_dataclass (value ):
7889 for k , v in asdict (value ).items ():
7990 print (f"{ k } = { v } " )
8091 else :
8192 print (f"{ attr } = { value } " )
93+
8294 return SUCCESS
8395
8496 def set (self , options : argparse .Namespace , verbose : bool ) -> int :
8597 attr , value = options .option , options .value
8698 path = ".pybm/config.yaml"
99+
87100 with self .context ("set" , attr , value , verbose ):
88101 PybmConfig .load (path ).set_value (attr , value ).save (path )
102+
89103 return SUCCESS
90104
91105 @staticmethod
92106 def list (options : argparse .Namespace , verbose : bool ) -> int :
93107 del verbose # unused
94108 config = PybmConfig .load (".pybm/config.yaml" )
109+
95110 for name in get_all_names (config ):
96111 group = config .get_value (name )
97- print (f"Config values for group \" { name } \" :" )
112+ print (f"Config values for group { name !r} :" )
113+
98114 for k , v in asdict (group ).items ():
99115 val = v if v != "" else "(empty string)"
100116 print (f"{ k } : { val } " )
117+
101118 print ("" )
102119 return SUCCESS
103120
104121 @staticmethod
105122 def describe (options : argparse .Namespace , verbose : bool ) -> int :
106123 del verbose # unused
107124 attr = options .option
125+
108126 if "__" in attr :
109- raise PybmError ("Only unprivileged configuration attributes can "
110- "be described via `pybm config describe`." )
127+ raise PybmError (
128+ "Only unprivileged configuration attributes can "
129+ "be described via `pybm config describe`."
130+ )
131+
111132 config = PybmConfig .load (".pybm/config.yaml" )
112133 config .describe (attr )
134+
113135 return SUCCESS
114136
115137 def run (self , args : List [str ]):
116138 subcommand_handlers = {
117139 "set" : self .set ,
118140 "get" : self .get ,
119141 "list" : self .list ,
120- "describe" : self .describe
142+ "describe" : self .describe ,
121143 }
122144
123145 if not args or args [0 ] not in subcommand_handlers :
124146 self .parser .print_help ()
125147 return ERROR
148+
126149 subcommand , * args = args
150+
127151 self .add_arguments (subcommand = subcommand )
128152
129153 # double hyphen prevents `config set` values to be mistaken for
0 commit comments