Skip to content
Permalink
Browse files

Enable proper logging in utils

This commit removes the circular dependency between log_config and utils
by specifying the player_names in the player_monitors module, and using
appdirs module for DATA_DIR and CFG_DIR. Now the log_config is set in
the utils module itself, so logging to file works properly.
  • Loading branch information...
iamkroot committed Oct 31, 2019
1 parent 461c704 commit 65e0eba35f6248fdee99384888eacabf5be54b63
@@ -11,5 +11,6 @@ win10toast = {version = "*", sys_platform = "== 'win32'"}
notify2 = {version = "*", sys_platform = "!= 'win32'"}
dbus-python = {version = "*", sys_platform = "== 'linux'"}
toml = "*"
appdirs = "*"

[dev-packages]

Some generated files are not rendered by default. Learn more.

@@ -0,0 +1,8 @@
import appdirs
from pathlib import Path

NAME = "trakt-scrobbler"
DATA_DIR = Path(appdirs.user_data_dir(NAME, roaming=True))
CFG_DIR = Path(appdirs.user_config_dir(NAME, roaming=True))
DATA_DIR.mkdir(exist_ok=True, parents=True)
CFG_DIR.mkdir(exist_ok=True, parents=True)
@@ -1,6 +1,6 @@
import logging
import logging.config
from utils import DATA_DIR, config
from player_monitors import player_names
from app_dirs import DATA_DIR


class StoppedPlayersFilter(logging.Filter):
@@ -10,7 +10,7 @@ def __init__(self):
self.log_count = set()

def filter(self, record: logging.LogRecord):
if record.threadName in config['players']['monitored']:
if record.threadName in player_names:
if 'Unable to connect' in record.msg:
val = record.thread not in self.log_count
self.log_count.add(record.thread)
@@ -1,6 +1,5 @@
import inspect
import logging
import logging.config
import sys
import threading

@@ -12,9 +11,6 @@
from scrobbler import Scrobbler
from trakt_interface import get_access_token
from utils import config
from log_config import LOGGING_CONF

logging.config.dictConfig(LOGGING_CONF)
logger = logging.getLogger('trakt_scrobbler')


@@ -0,0 +1 @@
player_names = ["mpcbe", "mpchc", "mpv", "vlc"]
@@ -1,8 +1,9 @@
import logging
from threading import Thread, Timer
from app_dirs import DATA_DIR
from notifier import notify
import trakt_interface as trakt
from utils import DATA_DIR, read_json, write_json
from utils import read_json, write_json

logger = logging.getLogger('trakt_scrobbler')
WATCHED_CACHE_PATH = DATA_DIR / 'watched_cache.json'
@@ -4,8 +4,9 @@
import webbrowser
import trakt_key_holder
from datetime import datetime as dt
from app_dirs import DATA_DIR
from notifier import notify
from utils import DATA_DIR, safe_request, read_json, write_json
from utils import safe_request, read_json, write_json

logger = logging.getLogger('trakt_scrobbler')

@@ -1,39 +1,16 @@
import json
import logging
import logging.config
import sys
import toml
import requests
from pathlib import Path
from urllib.parse import urlparse, unquote

from app_dirs import CFG_DIR
from log_config import LOGGING_CONF
logging.config.dictConfig(LOGGING_CONF)
logger = logging.getLogger('trakt_scrobbler')


def get_dirs():
NAME = 'trakt-scrobbler'
DATA_DIR = None

def dir_path(path: str) -> Path:
return Path(path).expanduser() / NAME

if sys.platform == 'win32':
DATA_DIR = dir_path('~/AppData/Roaming')
CFG_DIR = DATA_DIR
elif sys.platform == 'linux':
DATA_DIR = dir_path('~/.local/share')
CFG_DIR = dir_path('~/.config')
elif sys.platform == 'darwin':
DATA_DIR = dir_path('~/Library/Application Support')
CFG_DIR = dir_path('~/Library/Preferences')
else:
logger.error('Unknown OS')
exit(1)

DATA_DIR.mkdir(parents=True, exist_ok=True)
CFG_DIR.mkdir(parents=True, exist_ok=True)
return DATA_DIR, CFG_DIR


def read_config(config_path: Path):
try:
return toml.load(config_path)
@@ -45,7 +22,6 @@ def read_config(config_path: Path):
exit(1)


DATA_DIR, CFG_DIR = get_dirs()
config = read_config(CFG_DIR / 'config.toml')


0 comments on commit 65e0eba

Please sign in to comment.
You can’t perform that action at this time.