better error handling for config files

This commit is contained in:
petrucci4prez 2017-06-03 18:37:24 -04:00
parent b8f918dca1
commit 276eb2fb1a
2 changed files with 29 additions and 18 deletions

View File

@ -13,12 +13,31 @@ class _ReadOnlyFile():
''' '''
def __init__(self, path): def __init__(self, path):
self._path = path self._path = path
with open(self._path, 'r') as f: try:
self._dict = yaml.safe_load(f) self._load()
except FileNotFoundError:
logger.warn('File %s not found. Attempting to copy example', self._path)
defaultPath = self._path + '.default'
try:
shutil.copy(defaultPath, self._path)
except FileNotFoundError:
logger.error('Example file %s not found', defaultPath)
raise SystemExit
self._path = defaultPath
self._load()
except yaml.parser.ParserError as e:
logger.error(e)
raise SystemExit
def __getitem__(self, key): def __getitem__(self, key):
return self._dict[key] return self._dict[key]
def _load(self):
with open(self._path, 'r') as f:
self._dict = yaml.safe_load(f)
class _ReadWriteFile(_ReadOnlyFile): class _ReadWriteFile(_ReadOnlyFile):
''' '''
Same as above but adds write functionality. Intended for files that retain Same as above but adds write functionality. Intended for files that retain
@ -35,13 +54,5 @@ class _ReadWriteFile(_ReadOnlyFile):
with open(self._path, 'w') as f: with open(self._path, 'w') as f:
yaml.dump(self._dict, f, default_flow_style=False) yaml.dump(self._dict, f, default_flow_style=False)
def _openFile(cls, path): configFile = _ReadOnlyFile('config/pyledriver.yaml')
try: stateFile = _ReadWriteFile('config/state.yaml')
return cls(path)
except:
logger.warn('File %s not found. Copying example', path)
shutil.copy(path + '.default', path)
return cls(path)
configFile = _openFile(_ReadOnlyFile, 'config/pyledriver.yaml')
stateFile = _openFile(_ReadWriteFile, 'config/state.yaml')

View File

@ -12,7 +12,6 @@ Logger conventions
import logging, os import logging, os
from subprocess import run, PIPE, CalledProcessError from subprocess import run, PIPE, CalledProcessError
from logging.handlers import TimedRotatingFileHandler, SMTPHandler from logging.handlers import TimedRotatingFileHandler, SMTPHandler
from config import configFile
from auxilary import mkdirSafe from auxilary import mkdirSafe
def _formatConsole(gluster = False): def _formatConsole(gluster = False):
@ -27,7 +26,7 @@ class GlusterFSHandler(TimedRotatingFileHandler):
''' '''
Logic to mount timed rotating file within a gluster volume. Note that this Logic to mount timed rotating file within a gluster volume. Note that this
class will mount itself automatically. Note that the actual filepaths for class will mount itself automatically. Note that the actual filepaths for
logging are hardcoded here logging are hardcoded here
''' '''
def __init__(self, server, volume, mountpoint, options=None): def __init__(self, server, volume, mountpoint, options=None):
if not os.path.exists(mountpoint): if not os.path.exists(mountpoint):
@ -94,7 +93,8 @@ rootLogger.addHandler(console)
# 2) init the module level logger so we can log anything that happens as we build the other loggers # 2) init the module level logger so we can log anything that happens as we build the other loggers
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# 3) mount glusterfs, any errors here will go to console output # 3) init glusterfs, any errors here will go to console output
from config import configFile
glusterConf = configFile['gluster'] glusterConf = configFile['gluster']
if glusterConf['server'] != 'example.com': if glusterConf['server'] != 'example.com':
@ -105,10 +105,10 @@ else:
logger.error('Gluster not configured. Please update config/pyledriver.yaml') logger.error('Gluster not configured. Please update config/pyledriver.yaml')
raise SystemExit raise SystemExit
# 5) import gmail, this must come here as it uses loggers for some of its setup # 4) import gmail, this must come here as it uses loggers for some of its setup
from gmail import gmail, GmailHandler from gmail import gmail, GmailHandler
# 6) init gmail handler # 5) init gmail handler
gmail = GmailHandler(gmail['username'], gmail['passwd'], gmail['recipientList'], gmail = GmailHandler(gmail['username'], gmail['passwd'], gmail['recipientList'],
'harrison4hegemon - critical error') 'harrison4hegemon - critical error')
gmail.setLevel(logging.CRITICAL) gmail.setLevel(logging.CRITICAL)