From d2d2ce2ef7eb05c6865a6ca10664be32b5bfeb5e Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 3 Sep 2021 20:17:25 +0000 Subject: [PATCH] cfg.py: improve missing file warning --- mmgen/cfg.py | 13 +++++++------ test/test_py_d/ts_cfg.py | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/mmgen/cfg.py b/mmgen/cfg.py index 7877bdee..cc0c8739 100755 --- a/mmgen/cfg.py +++ b/mmgen/cfg.py @@ -43,17 +43,18 @@ class CfgFile(object): file_not_found_fs = 'WARNING: {} not found at {!r}' line_data = namedtuple('cfgfile_line',['name','value','lineno','chunk']) + class warn_missing_file(oneshot_warning): + color = 'yellow' # has no effect, as color not initialized yet + message = '{} not found at {!r}' + def __init__(self): self.fn = os.path.join(self.fn_dir,self.fn_base) - self.data = self.get_data() - - def get_data(self): try: - return open(self.fn).read().splitlines() + self.data = open(self.fn).read().splitlines() except: if self.warn_missing: - msg(self.file_not_found_fs.format(self.desc,self.fn)) - return '' + self.warn_missing_file( div=self.fn, fmt_args=(self.desc,self.fn) ) + self.data = '' def copy_data(self): assert self.write_ok, 'writing to file {!r} not allowed!'.format(self.fn) diff --git a/test/test_py_d/ts_cfg.py b/test/test_py_d/ts_cfg.py index cfcc57fa..ec52be5b 100755 --- a/test/test_py_d/ts_cfg.py +++ b/test/test_py_d/ts_cfg.py @@ -21,6 +21,7 @@ class TestSuiteCfg(TestSuiteBase): networks = ('btc',) tmpdir_nums = [40] base_passthru_opts = () + color = True cmd_group = ( ('nosysfile', (40,'init with missing system cfg sample file', [])), @@ -58,8 +59,7 @@ class TestSuiteCfg(TestSuiteBase): def nosysfile(self): t = self.spawn_test() errstr = CfgFile.file_not_found_fs.format(CfgFileSampleSys.desc,self.path('shared_data')+'/mmgen.cfg') - for i in (1,2,3,4,5): - t.expect(errstr) + t.expect(errstr) for k in ('usr','sys','sample'): t.expect('{} cfg file:\s+{}'.format(capfirst(k),self.path(k)),regex=True) assert not os.path.exists(self.path(k)), self.path(k)