Skip to content

Commit

Permalink
fix: added categories.example.toml and fixed its loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Mar 23, 2023
1 parent 15c3838 commit 30ed1a5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
18 changes: 18 additions & 0 deletions categories.example.toml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
16 changes: 11 additions & 5 deletions src/quantifiedme/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions src/quantifiedme/derived/screentime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 30ed1a5

Please sign in to comment.