registerups: Reworked configuration parsing.
It looks more complicated, but contains error handling.
This commit is contained in:
parent
34fc66d20e
commit
098714d37d
|
@ -2,7 +2,6 @@
|
||||||
# IP addres or hostname of UPS SNMP card
|
# IP addres or hostname of UPS SNMP card
|
||||||
address = 192.168.0.105
|
address = 192.168.0.105
|
||||||
# TCP port
|
# TCP port
|
||||||
# default - 3052
|
|
||||||
port = 3052
|
port = 3052
|
||||||
# Secret phrase for software authentication. This value must be the
|
# Secret phrase for software authentication. This value must be the
|
||||||
# same as configured on the UPS web interface
|
# same as configured on the UPS web interface
|
||||||
|
|
|
@ -78,30 +78,52 @@ class cps_ups:
|
||||||
def help():
|
def help():
|
||||||
print('Usage: ' + sys.argv[0] + ' <path-to-configuration-file>')
|
print('Usage: ' + sys.argv[0] + ' <path-to-configuration-file>')
|
||||||
|
|
||||||
|
configuration_structure = {'UPS': ['address',
|
||||||
|
'secret_key',
|
||||||
|
'port'],
|
||||||
|
'HOST': ['ip_address',
|
||||||
|
'shutdown_time',
|
||||||
|
'delay_time',
|
||||||
|
'hostname',
|
||||||
|
'outlet_id',
|
||||||
|
'bank_id',
|
||||||
|
'isps',
|
||||||
|
'contact',
|
||||||
|
'location',
|
||||||
|
'bedt']}
|
||||||
try:
|
try:
|
||||||
configfile = sys.argv[1]
|
configfile = sys.argv[1]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
help()
|
help()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not os.path.isfile(configfile):
|
if not os.path.isfile(configfile):
|
||||||
print('Error: configuration file ' + configfile + ' doesn\'t exists')
|
print('Error: configuration file ' + configfile + ' doesn\'t exists')
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
configuration = {}
|
||||||
|
try:
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(configfile)
|
||||||
|
for section in configuration_structure:
|
||||||
|
configuration[section] = {}
|
||||||
|
for param in configuration_structure[section]:
|
||||||
|
configuration[section][param] = config.get(section, param)
|
||||||
|
except (configparser.NoOptionError,
|
||||||
|
configparser.MissingSectionHeaderError,
|
||||||
|
configparser.ParsingError) as err:
|
||||||
|
print('Configuration file parsing error:\n' + str(err))
|
||||||
|
sys.exit(3)
|
||||||
|
|
||||||
config = configparser.ConfigParser()
|
u = cps_ups(address = configuration['UPS']['address'],
|
||||||
config.read(configfile)
|
secret_key = configuration['UPS']['secret_key'],
|
||||||
|
port = int(configuration['UPS']['port']))
|
||||||
|
|
||||||
u = cps_ups(address = config.get('UPS', 'address'),
|
u.register(ip_address = configuration['HOST']['ip_address'],
|
||||||
secret_key = config.get('UPS','secret_key'),
|
shutdown_time = configuration['HOST']['shutdown_time'],
|
||||||
port = int(config.get('UPS', 'port', fallback='3052')))
|
delay_time = configuration['HOST']['delay_time'],
|
||||||
|
hostname = configuration['HOST']['hostname'],
|
||||||
u.register(ip_address = config.get('HOST','ip_address'),
|
outlet_id = configuration['HOST']['outlet_id'],
|
||||||
shutdown_time = config.get('HOST','shutdown_time'),
|
bank_id = configuration['HOST']['bank_id'],
|
||||||
delay_time = config.get('HOST','delay_time'),
|
isps = configuration['HOST']['isps'],
|
||||||
hostname = config.get('HOST','hostname'),
|
contact = configuration['HOST']['contact'],
|
||||||
outlet_id = config.get('HOST','outlet_id'),
|
location = configuration['HOST']['location'],
|
||||||
bank_id = config.get('HOST','bank_id'),
|
bedt = configuration['HOST']['bedt'])
|
||||||
isps = config.get('HOST','isps'),
|
|
||||||
contact = config.get('HOST','contact'),
|
|
||||||
location = config.get('HOST','location'),
|
|
||||||
bedt = config.get('HOST','bedt'))
|
|
||||||
|
|
Loading…
Reference in New Issue