def get_logger(name, custom_handlers=()): """ gets a uniformally configured logger for the project. adds a default handler that is defined by the configuration. :param name: the name of the logger :param custom_handlers: an iterable of handlers to add to the logger. all handlers are normat python logging ones. """ logger = logging.getLogger(name) if logger.hasHandlers() and not configuration.is_testing(): # when testing, hasHandlers is somewhy set but we don't get output return logger config = configuration.get_config() logger.setLevel(config[configuration.CONFIG_DEBUG_LEVEL]) # create file handler which logs even debug messages formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') default_handler = logging.FileHandler(config['log_file']) if config.get('log_file', None) else \ logging.StreamHandler(sys.stdout) for handler in itertools.chain(custom_handlers, (default_handler,)): handler.setLevel(config[configuration.CONFIG_DEBUG_LEVEL]) # create console handler with a higher log level handler.setFormatter(formatter) logger.addHandler(handler) return logger
def get_repo(cls, paths=()): paths = paths or configuration.get_config()[configuration.CONFIG_DISPATCHER_CONSUMER_DIR] or \ [Path(__file__).parent] rep = cls(paths) rep.module_filter(lambda x: hasattr(x, rep._attr_getter) and callable( getattr(x, rep._attr_getter))) rep.module_name_filter(lambda x: x.name.endswith(f'_{rep.type}.py')) return rep
def get_for(cls, user): """ opens the storage of a specific user :param user: :return: """ return cls( configuration.get_config()[configuration.CONFIG_USER_STORAGE_BASE], user)
def get_config(self): """ retrieves configuration from the server, if necessary""" config_url = self.url / configuration.get_config()[ configuration.CONFIG_SERVER_CONFIG_ENDPOINT] resp = config_url.get() if not resp.ok or resp.status_code != 200: raise Exception( f"Couldn't get server config: {resp.status_code}, {resp.text}") self._server_config = resp.json()
def start_service(name, detach, re_run): images = configuration.get_config()[ configuration.CONFIG_SERVICE_DOCKER_IMAGES] dock = docker.from_env() if name == 'all': if not detach: click.echo("Error: can't run all images without detaching") exit(-1) for i in images.values(): _run_image(i, dock, detach, re_run) else: _run_image(images[name], dock, detach=detach, re_run=re_run)
def get(cls, paths=()): """ returns a new repository. note that __init__ is 'private' and you have to use this. :param paths: :return: """ repository = cls( cls.__GetChecker(), paths=paths or configuration.get_config()[configuration.CONFIG_PARSER_DIR] or [cls._class_file()]) repository.specialize_props() return repository
def _run_server(host, port, publish, encoder=None, run_threaded=False): module_logger.debug("getting server...") server = get_server(publish, encoder) server_args = dict(host=host, port=port) if run_threaded: t = threading.Thread(name=configuration.get_config()[ configuration.CONFIG_SERVER_THREAD_NAME], target=server.run, kwargs=server_args) module_logger.info(f"starting {server} on {host}:{port} on thread...") t.start() else: module_logger.info(f"starting {server} on {host}:{port} ...") server.run(**server_args)
def run_server_with_url(host, port, publish_url, run_threaded=False, encoder=None): publisher = None with logging.log_exception(logger=module_logger, format="Could not find publisher"): publisher = dispatchers.repository.DispatcherRepository.get_repo( ).get_dispatcher( publish_url, configuration.get_config()[ configuration.CONFIG_SERVER_PUBLISH_TOPICS]) module_logger.info(f"got publisher {publisher}") with logging.log_exception(logger=module_logger, to_suppress=(Exception, )): _run_server(host, port, publisher)
def create(cls): repo = pathlib.Path(configuration.get_config()[configuration.CONFIG_RAW_MESSAGE_REPO]) repo.mkdir(parents=True, exist_ok=True) path = repo / str(f"snapshot_{datetime.now():%b-%d-%Y_%H:%M:%S_%f}") module_logger.info(f"creating {path}") return cls(path.open('wb'), path)
def get_configuration(): return configuration.get_config()[configuration.CONFIG_CLIENT_CONFIG]
def sessionserver(config_dict): config_path = configuration.get_config()[ configuration.CONFIG_SERVER_CONFIG_ENDPOINT] with HTTPServer() as httpserver: httpserver.expect_request(config_path).respond_with_json(config_dict) yield httpserver