From 276eb2fb1a294499377dc5795674363236aa5d6a Mon Sep 17 00:00:00 2001 From: petrucci4prez Date: Sat, 3 Jun 2017 18:37:24 -0400 Subject: [PATCH] better error handling for config files --- config.py | 37 ++++++++++++++++++++++++------------- sharedLogging.py | 10 +++++----- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/config.py b/config.py index f51077f..5f77ac2 100644 --- a/config.py +++ b/config.py @@ -13,11 +13,30 @@ class _ReadOnlyFile(): ''' def __init__(self, path): self._path = path - with open(self._path, 'r') as f: - self._dict = yaml.safe_load(f) - + try: + 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): return self._dict[key] + + def _load(self): + with open(self._path, 'r') as f: + self._dict = yaml.safe_load(f) class _ReadWriteFile(_ReadOnlyFile): ''' @@ -35,13 +54,5 @@ class _ReadWriteFile(_ReadOnlyFile): with open(self._path, 'w') as f: yaml.dump(self._dict, f, default_flow_style=False) -def _openFile(cls, path): - try: - 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') +configFile = _ReadOnlyFile('config/pyledriver.yaml') +stateFile = _ReadWriteFile('config/state.yaml') diff --git a/sharedLogging.py b/sharedLogging.py index 9fb563a..e090489 100644 --- a/sharedLogging.py +++ b/sharedLogging.py @@ -12,7 +12,6 @@ Logger conventions import logging, os from subprocess import run, PIPE, CalledProcessError from logging.handlers import TimedRotatingFileHandler, SMTPHandler -from config import configFile from auxilary import mkdirSafe def _formatConsole(gluster = False): @@ -27,7 +26,7 @@ class GlusterFSHandler(TimedRotatingFileHandler): ''' Logic to mount timed rotating file within a gluster volume. Note that this 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): 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 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'] if glusterConf['server'] != 'example.com': @@ -105,10 +105,10 @@ else: logger.error('Gluster not configured. Please update config/pyledriver.yaml') 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 -# 6) init gmail handler +# 5) init gmail handler gmail = GmailHandler(gmail['username'], gmail['passwd'], gmail['recipientList'], 'harrison4hegemon - critical error') gmail.setLevel(logging.CRITICAL)