-
-
Save elrondwong/86f7fdc34135c60958953526cbdc69d7 to your computer and use it in GitHub Desktop.
Simple config file validation in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DEFAULT] | |
; Operation mode | |
; This is a global value for all sections | |
mode = master | |
[server] | |
; Connection lifetime | |
timeout = 3600 | |
; Garbage collection mode | |
; Accepted values: none, aggressive, smart, auto | |
gc_mode = smart | |
; Notice there is no mode set under this section - it will be read from defaults | |
[client] | |
; Fallback procedure for clients | |
; Accepted values: none, polling, auto | |
; Invalid value as an example here | |
fallback = socket | |
; Overriding global value here | |
mode = slave |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from configparser import ConfigParser | |
class MyException(Exception): | |
pass | |
class MyConfig(ConfigParser): | |
def __init__(self, config_file): | |
super(MyConfig, self).__init__() | |
self.read(config_file) | |
self.validate_config() | |
def validate_config(self): | |
required_values = { | |
'server': { | |
'timeout': None, | |
'gc_mode': ('none', 'aggressive', 'smart', 'auto'), | |
'mode': ('master') | |
}, | |
'client': { | |
'fallback': ('none', 'polling', 'auto'), | |
'mode': ('master', 'slave') | |
} | |
} | |
""" | |
Notice the different mode validations for global mode setting: we can | |
enforce different value sets for different sections | |
""" | |
for section, keys in required_values.items(): | |
if section not in self: | |
raise MyException( | |
'Missing section %s in the config file' % section) | |
for key, values in keys.items(): | |
if key not in self[section] or self[section][key] == '': | |
raise MyException(( | |
'Missing value for %s under section %s in ' + | |
'the config file') % (key, section)) | |
if values: | |
if self[section][key] not in values: | |
raise MyException(( | |
'Invalid value for %s under section %s in ' + | |
'the config file') % (key, section)) | |
cfg = {} | |
try: | |
# The example config file has an invalid value so cfg will stay empty first | |
cfg = MyConfig('config.ini') | |
except MyException as e: | |
# Initially you'll see this due to the invalid value | |
print(e) | |
else: | |
# Once you fix the config file you'll see this | |
print(cfg['client']['fallback']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment