def test_invalid_config_no_specs(self): config_file = base_path + '/static/config_invalid.yml' cf = Reader() config_data = cf.parse(config_file) self.assertIsInstance(config_data, dict) self.assertIn('debug', config_data) self.assertEqual('abc', config_data['debug'])
def test_valid_config(self): config_file = base_path + '/static/config_valid.yml' specs_file = base_path + '/static/specs.yml' cf = Reader() config_data = cf.parse(config_file, specs_file) self.assertIsInstance(config_data, dict) self.assertIn('debug', config_data) self.assertTrue(config_data['debug']) self.assertIn('logger', config_data) self.assertEqual('syslog', config_data['logger'])
def test_empty_config(self): config_file = base_path + '/static/config_empty.yml' specs_file = base_path + '/static/specs.yml' default_config_file = base_path + '/static/default.yml' cf = Reader() config_data = cf.parse(config_file, specs_file, default_config_file) self.assertIsInstance(config_data, dict) self.assertIn('debug', config_data) self.assertFalse(config_data['debug']) self.assertIn('logger', config_data) self.assertEqual('monolog', config_data['logger'])
def test_invalid_config(self): config_file = base_path + '/static/config_invalid.yml' specs_file = base_path + '/static/specs.yml' with self.assertRaisesRegex(ValueError, "'abc' is not of type 'boolean'"): Reader().parse(config_file, specs_file)
def test_valid_config_bad_default(self): config_file = base_path + '/static/config_empty.yml' specs_file = base_path + '/static/specs.yml' with self.assertRaisesRegex(IOError, 'Your default .+ does not exist'): Reader().parse(config_file=config_file, specs=specs_file, default_file='/does/not/exists')
def test_default(self): if os.path.isfile(utils.get_venv_basedir() + '/config/app.yml'): return with self.assertRaisesRegex( IOError, 'Missing config file: "(.+)config/app\.yml" does not exist'): Reader().parse()
def _read_config(self): from impulsare_config import Reader specs_file = self._base_path + '/static/specs.yml' default_file = self._base_path + '/static/default.yml' self.config = Reader().parse(self.config_file, specs_file, default_file)
def __init__(self, config_file: str, listener: str): """Init the Queue from config parameters""" base_path = os.path.abspath(os.path.dirname(__file__)) config_specs = base_path + '/static/specs.yml' config_default = base_path + '/static/default.yml' config = ConfigReader().parse(config_file, config_specs, config_default) config_listener = config.get('distributer') redis = Redis(config_listener['host'], config_listener['port']) if listener not in config: raise KeyError('You must have a key {} in your config with a sub-key queue'.format(listener)) self._logger = Logger('distributer', config_file) self._logger.log.debug('Distributer QueueManager called') self._logger.log.debug('Connect to queue {}'.format(config[listener]['queue'])) self._queue = RedisQueue(config[listener]['queue'], connection=redis)
def __init__(self, app: str, config_file: str = None): """Write logs to a file, with the app's name""" base_path = os.path.abspath(os.path.dirname(__file__)) config_specs = base_path + '/static/specs.yml' config_default = base_path + '/static/default.yml' self.config = Reader().parse(config_file, config_specs, config_default).get('logger') self.app = app self.level = getattr(logging, self.config.get('level')) self.log = logging.getLogger(app) self.log.setLevel(self.level) if self.log.hasHandlers(): return self._add_handlers()
def test_exists(self): cf = Reader() self.assertIs(type(cf), Reader)
def test_overriden(self): with self.assertRaisesRegex( IOError, 'Missing config file: "/does/not/exist" does not exist'): Reader().parse('/does/not/exist')
class Logger(): """Config Reader. Reads, validate and add default values to YAML""" def __init__(self, app: str, config_file: str = None): """Write logs to a file, with the app's name""" base_path = os.path.abspath(os.path.dirname(__file__)) config_specs = base_path + '/static/specs.yml' config_default = base_path + '/static/default.yml' self.config = Reader().parse(config_file, config_specs, config_default).get('logger') self.app = app self.level = getattr(logging, self.config.get('level')) self.log = logging.getLogger(app) self.log.setLevel(self.level) if self.log.hasHandlers(): return self._add_handlers() def _add_handlers(self): if self.config.get('handlers')['file'] is True: self.filename = self._get_filename() self.log.addHandler(self._get_file_handler()) if self.config.get('handlers')['console'] is True: self.log.addHandler(self._get_stream_handler()) def _get_filename(self): directory = self.config.get('directory') if directory is None: return utils.get_venv_basedir() + '/logs/{}.log'.format(self.app) return '{}/{}.log'.format(directory, self.app) def _get_file_handler(self): max_size = int(self.config.get('max_size') * 1024 * 1024) rotate = int(self.config.get('rotate')) try: file_handler = RotatingFileHandler(self.filename, 'a', max_size, rotate) file_handler.setLevel(self.level) file_handler.setFormatter( logging.Formatter(self.config.get('formatter'))) except Exception: raise IOError("Can't write to '{}'".format(self.filename)) return file_handler def _get_stream_handler(self): from click import style log_format = style('[LOGGER] ', fg='green') log_format += style('(' + self.app.upper() + ') ', fg='yellow') log_format += '%(levelname)s - %(message)s' stream_handler = logging.StreamHandler() stream_handler.setLevel(self.level) stream_handler.setFormatter(logging.Formatter(log_format)) return stream_handler