From 30ed1a534c7c0c46e83d330f14a0509e3ebcfb82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Thu, 23 Mar 2023 11:41:36 +0100 Subject: [PATCH] fix: added categories.example.toml and fixed its loading --- categories.example.toml | 18 ++++++++++++++++++ config.example.toml | 2 +- src/quantifiedme/config.py | 16 +++++++++++----- src/quantifiedme/derived/screentime.py | 15 ++++++++------- 4 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 categories.example.toml diff --git a/categories.example.toml b/categories.example.toml new file mode 100644 index 0000000..60bd52c --- /dev/null +++ b/categories.example.toml @@ -0,0 +1,18 @@ +[categories] + + [categories.Work] + $re = "Google (Sheets|Slides|Forms)" + + [categories.Work.Programming] + $re = "[Pp]rogramming" + ActivityWatch = "[Aa]ctivity[Ww]atch|aw-.*" + QuantifiedMe = "[Qq]uantified[Mm]e" + + [categories.Media] + YouTube = "YouTube|youtube.com" + Music = "Spotify" + + [categories.Media.'Social Media'] + Twitter = "Twitter|twitter.com" + Reddit = "Reddit|reddit.com" + Facebook = "Facebook|facebook.com" diff --git a/config.example.toml b/config.example.toml index feb6489..659640f 100644 --- a/config.example.toml +++ b/config.example.toml @@ -11,7 +11,7 @@ date_offset_hours = 5 habitbull = "~/Downloads/HabitBullData.csv" location = "~/location" oura = "~/Downloads/oura_2020-02-27T09-07-47.json" -categories = "~/categories.toml" # categories.toml from aw-research +categories = "categories.example.toml" # categories.toml from aw-research [data.smartertime_buckets] example-hostname = '~/data/smartertime/smartertime_export_example-hostname_2020-01-01_bb7f26aa.awbucket.json' diff --git a/src/quantifiedme/config.py b/src/quantifiedme/config.py index 2dd9ed7..050e717 100644 --- a/src/quantifiedme/config.py +++ b/src/quantifiedme/config.py @@ -13,15 +13,21 @@ rootdir = srcdir.parent -def _get_config_path(): +def _get_config_path(use_example=False) -> Path: + if use_example: + return Path(rootdir) / "config.example.toml" return Path(appdirs.user_config_dir("quantifiedme")) / "config.toml" -def load_config() -> MutableMapping[str, Any]: - filepath = _get_config_path() - if not filepath.exists(): +def load_config(use_example=False) -> MutableMapping[str, Any]: + # fallback default + filepath = _get_config_path(use_example=True) + + if (config_path := _get_config_path(use_example)).exists(): + filepath = config_path + else: logger.warning("No config found, falling back to example config") - filepath = Path(rootdir) / "config.example.toml" + with open(filepath) as f: logger.info("Loading config from %s", filepath) return toml.load(f) diff --git a/src/quantifiedme/derived/screentime.py b/src/quantifiedme/derived/screentime.py index 422d7b9..33a7808 100644 --- a/src/quantifiedme/derived/screentime.py +++ b/src/quantifiedme/derived/screentime.py @@ -17,7 +17,7 @@ from ..load.activitywatch import load_events as load_events_activitywatch from ..load.smartertime import load_events as load_events_smartertime -from ..config import load_config +from ..config import load_config, _get_config_path from ..cache import cache_dir from ..load.activitywatch_fake import create_fake_events @@ -131,12 +131,13 @@ def _join_events( def classify(events: list[Event], personal: bool) -> list[Event]: # Now load the classes from within the notebook, or from a CSV file. - use_example = not personal - if use_example: - logger.info("Using example categories") - categories_path = Path(__file__).parent / "categories.example.toml" - else: - categories_path = Path(load_config()["data"]["categories"]).expanduser() + config = load_config(use_example=not personal) + categories_path = Path(config["data"]["categories"]).expanduser() + # if categories_path is relative, it's relative to the config file + if not categories_path.is_absolute(): + categories_path = ( + _get_config_path(use_example=not personal).parent / categories_path + ) aw_research.classify._init_classes(filename=str(categories_path)) events = aw_research.classify.classify(events)