Browse Source

cfg.py: improve missing file warning

The MMGen Project 3 years ago
parent
commit
d2d2ce2ef7
2 changed files with 9 additions and 8 deletions
  1. 7 6
      mmgen/cfg.py
  2. 2 2
      test/test_py_d/ts_cfg.py

+ 7 - 6
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)

+ 2 - 2
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)