Browse Source

Extract constant for config file section

.. and improve config error reporting
Sebastian Pipping 9 years ago
parent
commit
692284f41d
1 changed files with 9 additions and 7 deletions
  1. 9
    7
      client-barcode/freitagslib/settings.py

+ 9
- 7
client-barcode/freitagslib/settings.py View File

@@ -9,6 +9,8 @@ import sys
9 9
 import ConfigParser
10 10
 
11 11
 
12
+_SECTION = 'client-barcode'
13
+
12 14
 _KEYS = (
13 15
 	# Key, type, default
14 16
 	('CLIENT_DEBUG', bool, False),  # Should go first to enable debugging as early as possible
@@ -32,28 +34,28 @@ class Settings:
32 34
 
33 35
 		cp = ConfigParser.ConfigParser(_DEFAULTS)
34 36
 		if not cp.read(filename):
35
-			print('FATAL: Config file %s could not be parsed.' % (filename), file=sys.stderr)
37
+			print('FATAL: Config file "%s" could not be parsed.' % (filename), file=sys.stderr)
36 38
 			return False
37 39
 
38 40
 		valid = True
39 41
 		for key, _type, dummy in _KEYS + additional_keys:
40 42
 			try:
41 43
 				if _type is bool:
42
-					value = cp.getboolean('client-barcode', key)
44
+					value = cp.getboolean(_SECTION, key)
43 45
 				else:
44
-					value = cp.get('client-barcode', key)
46
+					value = cp.get(_SECTION, key)
45 47
 			except ValueError:
46
-				print('ERROR: Key %s in config file %s must be of type %s.' \
48
+				print('ERROR: Key "%s" in config file "%s" must be of type "%s".' \
47 49
 						% (key, filename, _type.__name__), file=sys.stderr)
48 50
 				valid = False
49 51
 			except ConfigParser.NoOptionError:
50
-				print('ERROR: Config file %s misses required key %s.' \
51
-						% (filename, key), file=sys.stderr)
52
+				print('ERROR: Config file "%s" misses required key "%s.%s".' \
53
+						% (filename, _SECTION, key), file=sys.stderr)
52 54
 				valid = False
53 55
 			else:
54 56
 				setattr(self, key, value)
55 57
 				if self.CLIENT_DEBUG:
56
-					print('DEBUG: CONFIG: %s=%s("%s")' % (key, type(value).__name__, value))
58
+					print('DEBUG: CONFIG: %s.%s=%s("%s")' % (_SECTION, key, type(value).__name__, value))
57 59
 
58 60
 		return valid
59 61
 

Loading…
Cancel
Save