From 17195a17ada6fa5bd1abaa85c279d3813b6ee50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Tue, 4 Jul 2023 22:02:25 +0200 Subject: [PATCH] Use appropriate app_dirs for cache and log files --- hanabi/constants.py | 1 + hanabi/live/site_api.py | 9 ++++++--- hanabi/logger_setup.py | 14 +++++++++++--- requirements.txt | 1 + 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/hanabi/constants.py b/hanabi/constants.py index 34a5139..0588bcf 100644 --- a/hanabi/constants.py +++ b/hanabi/constants.py @@ -1,5 +1,6 @@ # constants.py +APP_NAME = 'hanabi-suite' # some values shared by all (default) hanabi instances HAND_SIZES = {2: 5, 3: 5, 4: 4, 5: 4, 6: 3} diff --git a/hanabi/live/site_api.py b/hanabi/live/site_api.py index 93b6d80..fbd4d90 100644 --- a/hanabi/live/site_api.py +++ b/hanabi/live/site_api.py @@ -1,10 +1,13 @@ import json -import requests_cache -from hanabi import logger +import requests_cache +import platformdirs + +from hanabi import logger +from hanabi import constants # Cache all requests to site to reduce traffic and latency -session = requests_cache.CachedSession('hanab.live') +session = requests_cache.CachedSession(platformdirs.user_cache_dir(constants.APP_NAME) + '/hanab.live') def get(url, refresh=False): diff --git a/hanabi/logger_setup.py b/hanabi/logger_setup.py index a3569a6..84b3974 100644 --- a/hanabi/logger_setup.py +++ b/hanabi/logger_setup.py @@ -1,5 +1,10 @@ import logging +import os + import verboselogs +import platformdirs + +from hanabi import constants class LoggerManager: @@ -28,15 +33,18 @@ class LoggerManager: self.console_handler.setLevel(console_level) self.console_handler.setFormatter(self.nothing_formatter) - self.debug_file_handler = logging.FileHandler("debug_log.txt") + log_dir = platformdirs.user_log_dir(constants.APP_NAME) + os.makedirs(log_dir, exist_ok=True) + + self.debug_file_handler = logging.FileHandler(log_dir + "/debug_log.txt") self.debug_file_handler.setFormatter(self.file_formatter) self.debug_file_handler.setLevel(logging.DEBUG) - self.verbose_file_handler = logging.FileHandler("verbose_log.txt") + self.verbose_file_handler = logging.FileHandler(log_dir + "/verbose_log.txt") self.verbose_file_handler.setFormatter(self.file_formatter) self.verbose_file_handler.setLevel(verboselogs.VERBOSE) - self.info_file_handler = logging.FileHandler("log.txt") + self.info_file_handler = logging.FileHandler(log_dir + "/log.txt") self.info_file_handler.setFormatter(self.info_file_formatter) self.info_file_handler.setLevel(logging.INFO) diff --git a/requirements.txt b/requirements.txt index 480456b..6bd5085 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ alive_progress argparse verboselogs pebble +platformdirs