def save(self, backup=True): try: if backup: copyfile(self.conf_file, str(self.conf_file.resolve()) + '.orig') with open(self.conf_file, 'w') as file: yaml.dump(self.config, file) return True except: logger().warning('Error saving config file') return False
def connection(name=None, c: dict = None): conn: Connector if c and c.get('backend'): backend_module = None try: backend_module = available_backends[c['backend']] params = Config().get('connections.' + name) conn = Connector(name, backend_module, params=params) app.STARTED_CONNECTIONS.append(conn) conn.start() except KeyError: logger().warning(c['backend'] + ' backend is not supported.') return None
def main(): try: parser = argparse.ArgumentParser() parser.add_argument('--version', '-V', action='store_true', default=False, help='print version') parser.add_argument('--debug', '-d', action='store_true', default=False, help='debug mode') parser.add_argument('--conf', '-c', action='store', default=None, help='config file') arguments = parser.parse_args() initialize(arguments.debug) application(arguments) except BaseException as e: if e.__class__.__name__ != 'SystemExit': logger().error(str(e.__class__.__name__) + ': ' + str(e)) elif e.code != 0: logger().warning(str(e.__class__.__name__) + ': ' + str(e))
def application(args): if args.version: print(__appname__ + ' ' + __version__) print(__description__) sys.exit(0) if args.conf: conf = os.path.abspath(args.conf) else: conf = None logger().debug('loading configuration from ' + str(conf)) Config(conf) logger().info('successfully loaded configuration') signal.signal(signal.SIGINT, global_signal_handler) with ThreadPoolExecutor(max_workers=os.cpu_count()) as pool: connections = Config().get('connections') task = [ pool.submit(connection, name=c, c=connections[c]) for c in connections ] for future in as_completed(task): future.result() raise NotImplementedError('not implemented')
def log(): return logger('backend')
def global_signal_handler(_, __): logger().info('Gracefully stopping all connections...') for conn in STARTED_CONNECTIONS: conn.stop() sys.exit(0)
def log(): return logger('plugin')
def stop(self): try: self.backend.__stop__(self.name, self.params) except BaseException as e: logger().warning(e)