Config: set opts_data['sets'] options after env and cfgfile

This commit is contained in:
The MMGen Project 2025-05-26 09:39:11 +00:00
commit abdb00f49f
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 49 additions and 14 deletions

View file

@ -535,6 +535,10 @@ class Config(Lockable):
# Step 6: set auto typeset opts from user-supplied data or cfgfile data, in that order:
self._set_auto_typeset_opts(self._cfgfile_opts.auto_typeset)
# Step 7: set opts_data['sets'] opts:
if opts_data and 'sets' in opts_data:
self._set_opts_data_sets_opts(opts_data)
if self.regtest or self.bob or self.alice or self.carol or gc.prog_name == f'{gc.proj_id}-regtest':
self.network = 'regtest'
self.regtest_user = 'bob' if self.bob else 'alice' if self.alice else 'carol' if self.carol else None
@ -740,6 +744,19 @@ class Config(Lockable):
elif key in cfgfile_auto_typeset_opts:
do_set(key, cfgfile_auto_typeset_opts[key], ref_type)
def _set_opts_data_sets_opts(self, opts_data):
for a_opt, a_val, b_opt, b_val in opts_data['sets']:
if (usr_a_val := getattr(self, a_opt, None)) not in (None, False):
if a_val == bool or usr_a_val == a_val:
if ((usr_b_val := getattr(self, b_opt, None)) in (None, False)) or usr_b_val == b_val:
setattr(self, b_opt, b_val)
else:
die(1, 'Option --{}={} conflicts with option --{}={}\n'.format(
b_opt.replace('_', '-'),
usr_b_val,
a_opt.replace('_', '-'),
usr_a_val))
def _die_on_incompatible_opts(self):
for group in self._incompatible_opts:
bad = [k for k in self.__dict__ if k in group and getattr(self, k) is not None]

View file

@ -135,19 +135,6 @@ def process_uopts(cfg, opts_data, opts, need_proto):
uargs = []
uopts = dict(get_uopts())
if 'sets' in opts_data:
for a_opt, a_val, b_opt, b_val in opts_data['sets']:
if a_opt in uopts:
u_val = uopts[a_opt]
if (u_val and a_val == bool) or u_val == a_val:
if b_opt in uopts and uopts[b_opt] != b_val:
die(1,
'Option conflict:'
+ '\n --{}={}, with'.format(b_opt.replace('_', '-'), uopts[b_opt])
+ '\n --{}={}\n'.format(a_opt.replace('_', '-'), uopts[a_opt]))
else:
uopts[b_opt] = b_val
return uopts, uargs
cmd_opts_v1_pat = re.compile(r'^-([a-zA-Z0-9-]), --([a-zA-Z0-9-]{2,64})(=| )(.+)')

View file

@ -27,6 +27,8 @@ class CmdTestCfgFile(CmdTestBase):
cmd_group = (
('sysfile', (40, 'init with system cfg sample file in place', [])),
('opts_data_sets1', (40, 'opts_data["sets"] opt set in environment', [])),
('opts_data_sets2', (40, 'opts_data["sets"] opt set in cfg_file', [])),
('no_metadata_sample', (40, 'init with unversioned cfg sample file', [])),
('altered_sample', (40, 'init with user-modified cfg sample file', [])),
('old_sample', (40, 'init with old v2 cfg sample file', [])),
@ -102,6 +104,20 @@ class CmdTestCfgFile(CmdTestBase):
self.check_replaced_sample()
return t
def opts_data_sets1(self): # no_license (in env) sets grokify
self.write_to_cfgfile('usr', ['scroll true'])
t = self.spawn_test(args=['print_cfg', 'no_license', 'foobleize', 'grokify', 'scroll'])
t.expect('foobleize: None')
t.expect('grokify: True')
return t
def opts_data_sets2(self): # autosign (in cfg file) sets foobleize
self.write_to_cfgfile('usr', ['autosign true'])
t = self.spawn_test(args=['print_cfg', 'no_license', 'autosign', 'foobleize', 'grokify'])
t.expect('foobleize: True')
t.expect('grokify: True')
return t
def no_metadata_sample(self):
self.copy_sys_sample()
S = self.read_from_cfgfile('sys')

View file

@ -5,7 +5,22 @@ import os
from mmgen.cfg import Config
from mmgen.util import msg
cfg = Config(process_opts=True)
opts_data = {
'sets': [
('no_license', True, 'grokify', True),
('autosign', True, 'foobleize', True)
],
'text': {
'desc': 'Cfg file test',
'usage':'[args] [opts]',
'options': """
-a, --autosign Turn on autosigning
-F, --foobleize Foobleize the input
-G, --grokify Grokify the output
"""},
}
cfg = Config(opts_data=opts_data)
cmd_args = cfg._args
op, args = (cmd_args[0], cmd_args[1:]) if cmd_args else (None, None)