Use appropriate app_dirs for cache and log files

This commit is contained in:
Maximilian Keßler 2023-07-04 22:02:25 +02:00
parent a93601c997
commit 17195a17ad
Signed by: max
GPG key ID: BCC5A619923C0BA5
4 changed files with 19 additions and 6 deletions

View file

@ -1,5 +1,6 @@
# constants.py # constants.py
APP_NAME = 'hanabi-suite'
# some values shared by all (default) hanabi instances # some values shared by all (default) hanabi instances
HAND_SIZES = {2: 5, 3: 5, 4: 4, 5: 4, 6: 3} HAND_SIZES = {2: 5, 3: 5, 4: 4, 5: 4, 6: 3}

View file

@ -1,10 +1,13 @@
import json 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 # 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): def get(url, refresh=False):

View file

@ -1,5 +1,10 @@
import logging import logging
import os
import verboselogs import verboselogs
import platformdirs
from hanabi import constants
class LoggerManager: class LoggerManager:
@ -28,15 +33,18 @@ class LoggerManager:
self.console_handler.setLevel(console_level) self.console_handler.setLevel(console_level)
self.console_handler.setFormatter(self.nothing_formatter) 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.setFormatter(self.file_formatter)
self.debug_file_handler.setLevel(logging.DEBUG) 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.setFormatter(self.file_formatter)
self.verbose_file_handler.setLevel(verboselogs.VERBOSE) 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.setFormatter(self.info_file_formatter)
self.info_file_handler.setLevel(logging.INFO) self.info_file_handler.setLevel(logging.INFO)

View file

@ -8,3 +8,4 @@ alive_progress
argparse argparse
verboselogs verboselogs
pebble pebble
platformdirs