1
0
mirror of https://github.com/Adam-Ant/drone-trigger synced 2024-12-20 07:24:34 +00:00

Replace ConfigParser with ConfigObj

This allows us to persist comments when we write to the config file, and
it being an actual list helps with some things we might want to do in
the future
This commit is contained in:
Adam Dodman 2017-07-16 22:41:13 +01:00
parent 2f140230a2
commit 2ae4b02cd2

25
main.py
View File

@ -1,21 +1,20 @@
import configparser
import os import os
import argparse
import configparser
import time import time
import argparse
from configobj import ConfigObj
import requests import requests
from json import loads as jload from json import loads as jload
defaultconfig = ''' defaultconfig = '''
# WARNING: COMMENTS IN THIS FILE WILL BE ERASED WHEN PROGRAM IS RUN!
[Connection] [Connection]
# Specify the URL to the drone server, including port and protocol # Specify the URL to the drone server, including port and protocol
host = https://drone.example.org host = https://drone.example.org
auth_key = eyJEXAMPLE.AUTH.KEY auth_key = eyJEXAMPLE.AUTH.KEY
# Specified in seconds (Default: 300) # Specified in seconds (Default: 300)
# sleep_time = 300 #sleep_time = 300
#[ExampleGitHubBuild] #[ExampleGitHubBuild]
# Example shown uses githubs api to find and compare on the sha of the latest commit # Example shown uses githubs api to find and compare on the sha of the latest commit
@ -26,7 +25,7 @@ auth_key = eyJEXAMPLE.AUTH.KEY
#url = https://api.github.com/repos/Kaylee/ShinyRepo/git/refs/heads/master #url = https://api.github.com/repos/Kaylee/ShinyRepo/git/refs/heads/master
# JSON Tree needed to resolve the value. # JSON Tree needed to resolve the value.
# structure = object.sha #structure = object.sha
#[ExampleGitHubRelease] #[ExampleGitHubRelease]
@ -82,23 +81,22 @@ if __name__ == '__main__':
c.close() c.close()
exit(78) # 78 is the exit code for invalid config exit(78) # 78 is the exit code for invalid config
config = configparser.ConfigParser() config = ConfigObj(filepath + '/dronetrigger.cfg')
config.read(filepath + '/dronetrigger.cfg')
if not ('Connection' in config): if not ('Connection' in config):
print('Error: Connection block not found, please check your config') print('Error: Connection block not found, please check your config')
exit(78) exit(78)
if not (config['Connection'].get('host', False)) or not (config['Connection'].get('auth_key', False)): if not (config['Connection'].get('host', False)) or not (config['Connection'].get('auth_key', False)):
print('Error: Missing connection details. please check your config') print('Error: Missing connection details. please check your config')
exit(78) exit(78)
if (len(config.sections()) < 2): if (len(config) < 2):
print('Error: Please configure some monitoring blocks!') print('Error: Please configure some monitoring blocks!')
exit(78) exit(78)
# These can be assumed since we have verified # These can be assumed since we have verified
drone_host = config['Connection']['host'] drone_host = config['Connection']['host']
drone_auth_key = config['Connection']['auth_key'] drone_auth_key = config['Connection']['auth_key']
sleep_time = config['Connection'].getint('sleep_time', 300) sleep_time = int(config['Connection'].get('sleep_time', 300))
for service in config.sections(): for service in config:
if service == 'Connection': if service == 'Connection':
continue continue
if not (config[service].get('url', False)) or not (config[service].get('structure', False)) or not(config[service].get('drone_repo'), False): if not (config[service].get('url', False)) or not (config[service].get('structure', False)) or not(config[service].get('drone_repo'), False):
@ -113,11 +111,10 @@ if __name__ == '__main__':
if not (config[service].get('current_value', False)): if not (config[service].get('current_value', False)):
print('Writing Initial value for ' + service + ': ' + curr_value) print('Writing Initial value for ' + service + ': ' + curr_value)
config[service]['current_value'] = curr_value config[service]['current_value'] = curr_value
with open(filepath + '/dronetrigger.cfg', 'w') as configfile: config.write()
config.write(configfile)
while(True): while(True):
for service in config.sections(): for service in config:
if service == 'Connection': if service == 'Connection':
continue continue
try: try: