Improve error handling with config file

This commit is contained in:
2020-05-19 00:06:33 +02:00
parent 89eb4bf97b
commit 11a5809f6a
2 changed files with 36 additions and 11 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
config.json
.project .project
.pydevproject .pydevproject
PDMameUpdate.json

View File

@ -33,12 +33,12 @@ Notes
a cookies.txt file in the .config/transmission directory a cookies.txt file in the .config/transmission directory
See: https://forum.transmissionbt.com/viewtopic.php?t=7468 See: https://forum.transmissionbt.com/viewtopic.php?t=7468
/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/!\ Provided with no warranty whatsoever. /!\ !!! Provided with no warranty whatsoever. !!!
/!\ Make sure you understand what the script /!\ !!! Make sure you understand what the script !!!
/!\ does and adapt it to your context /!\ !!! does and adapt it to your context !!!
/!\ Use with caution. /!\ !!! Use with caution. !!!
/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
''' '''
import argparse import argparse
import feedparser import feedparser
@ -59,17 +59,41 @@ from urllib.parse import quote
def open_config_file(): def open_config_file():
"""Reads configuration from PDMameUpdate.json file""" """Reads configuration from PDMameUpdate.json file"""
for loc in os.environ.get("PDMAMEUPDATE_CONF"), "/etc", os.path.expanduser("~"), os.curdir: locations = (
os.environ.get("PDMAMEUPDATE_CONF"),
"/etc",
os.path.expanduser("~"),
os.curdir
)
for loc in locations:
if loc is not None: if loc is not None:
logging.info("Searching for config file in '%s'", loc)
config_path = loc + '/PDMameUpdate.json' config_path = loc + '/PDMameUpdate.json'
if pathlib.Path(config_path).is_file(): if pathlib.Path(config_path).is_file():
config_file_loc = config_path config_file_loc = config_path
break break
if 'config_file_loc' not in locals():
logging.error("Config file not found")
raise FileNotFoundError("Config file not found")
logging.info('Opening config file: '+config_file_loc) logging.info('Opening config file: '+config_file_loc)
config_file = None; config_file = None
with open(config_file_loc, 'r') as config_file: with open(config_file_loc, 'r') as config_file:
config = json.load(config_file) config = json.load(config_file)
parameters = [
"mame-directory",
"pleasuredome-password",
"pleasuredome-user",
"torrent-directory",
"transmission-password",
"transmission-port",
"transmission-user"
]
for key in parameters:
if key not in config:
logging.error('Missing key in config file: %s', key)
raise ValueError('Invalid config file.')
return config return config
@ -251,6 +275,7 @@ def update_torrents():
t = requests.get(torrent['remote-link'], verify=False) t = requests.get(torrent['remote-link'], verify=False)
open(new_torrent, 'wb').write(t.content) open(new_torrent, 'wb').write(t.content)
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig( logging.basicConfig(
level=logging.WARNING, level=logging.WARNING,