From 076f43b08213ffa8c5746e4d6f5b8bf129541e2b Mon Sep 17 00:00:00 2001 From: jpt Date: Wed, 30 Apr 2025 22:26:37 -0500 Subject: [PATCH] add xdg_base_dirs --- pyproject.toml | 1 + src/tt/config.py | 22 ++++++++++- src/tt/db.py | 6 ++- tt.toml | 99 ------------------------------------------------ uv.lock | 11 ++++++ 5 files changed, 37 insertions(+), 102 deletions(-) delete mode 100644 tt.toml diff --git a/pyproject.toml b/pyproject.toml index e9a565d..2ca753e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,7 @@ dependencies = [ "textual>=1.0.0", "tomlkit>=0.13.2", "typer>=0.15.1", + "xdg-base-dirs>=6.0.2", ] [project.scripts] diff --git a/src/tt/config.py b/src/tt/config.py index dd7d300..eed611a 100644 --- a/src/tt/config.py +++ b/src/tt/config.py @@ -1,11 +1,29 @@ import tomlkit +from xdg_base_dirs import xdg_config_home + +# This code implements a singleton-like pattern. +# +# The global variable _raw_config contains the cached config loaded +# from a TOML config file. +# +# _load_config populates the variable if it has not already been loaded. +# +# Functions that need the config, can use _load_config() which will +# always return a copy of the config regardless of it needed to be loaded +# or not. +# +# Eventually, this approach could also enable a live-reload function. +# +# A command could be introduced that would reset _raw_config to None, +# and the next read would reload the fresh data. _raw_config = None def _load_config(): global _raw_config if not _raw_config: - with open("tt.toml", "r") as f: + # ~/.config/tt/tt.toml + with open(xdg_config_home() / "tt/tt.toml", "r") as f: _raw_config = tomlkit.load(f) return _raw_config @@ -24,6 +42,6 @@ def get_view(name): raise ValueError(f"no such view! {name}") - +# Valid statuses & projects are read dynamically from the user's config. STATUSES = get_enum("status") PROJECTS = get_enum("projects") diff --git a/src/tt/db.py b/src/tt/db.py index 11bc73f..d0ac1ee 100644 --- a/src/tt/db.py +++ b/src/tt/db.py @@ -1,5 +1,6 @@ import json from datetime import date, timedelta, datetime +from xdg_base_dirs import xdg_data_home from peewee import ( BooleanField, CharField, @@ -9,8 +10,11 @@ from peewee import ( TextField, ) +# This module defines the core data types. +# + db = SqliteDatabase( - "tasks.db", + xdg_data_home() / "tt/tasks.db", pragmas={ "journal_mode": "wal", "synchronous": "normal", diff --git a/tt.toml b/tt.toml deleted file mode 100644 index 871bc21..0000000 --- a/tt.toml +++ /dev/null @@ -1,99 +0,0 @@ -[[enums]] -name = "status" -values = [ - { value = "zero", color = "#666666" }, - { value = "blocked", color = "#cc9900" }, - { value = "wip", color = "#33aa99" }, - { value = "done", color = "#009900" }, -] - -[[enums]] -name = "projects" -values = [ - { value = "SECT", color = "purple" }, - { value = "life", color = "#00cc00" }, - { value = "CAPP", color = "#cc0000" }, - { value = "ilikethis", color = "#cccc00" }, - { value = "krang", color = "#ff00ff"}, - { value = "artworld", color = "#0000cc"}, - { value = "TT", color = "#00ff00"}, -] - -[[enums]] -name = "recurrences" -values = [ - { value = "days-btwn" }, - { value = "monthly" }, -] - -[[views]] -name = "tasks" -sort = "due,status" - -[views.filters] -status = "wip,blocked,zero" - -[[views.columns]] -field_name = "id" -read_only = true - -[[views.columns]] -field_name = "text" -display_name = "Task" -default = "new task" -editor = true - -[[views.columns]] -field_name = "status" -field_type = "enum" -enum = "status" -default = "zero" - -[[views.columns]] -field_name = "type" -default = "" - -[[views.columns]] -field_name = "due" -field_type = "date" -default = "1999-01-01" - -[[views.columns]] -field_name = "due_week" -read_only = true -#field_type = "date" -#default = "1999-01-01" - -[[views.columns]] -field_name = "project" -display_name = "Project" -default = "" -field_type = "enum" -enum = "projects" - -[[views]] -name = "recurring" - -[[views.columns]] -field_name = "id" -read_only = true - -[[views.columns]] -field_name = "template" -default = "recur {val}" - -[[views.columns]] -field_name = "type" -default = "days-btwn" -field_type = "enum" -enum = "generators" - -[[views.columns]] -field_name = "val" -display_name = "Value" -default = "1" - -[[views.columns]] -field_name = "next_at" -display_name = "Next @" -read_only = true diff --git a/uv.lock b/uv.lock index 6f60ee5..26153c7 100644 --- a/uv.lock +++ b/uv.lock @@ -922,6 +922,7 @@ dependencies = [ { name = "textual" }, { name = "tomlkit" }, { name = "typer" }, + { name = "xdg-base-dirs" }, ] [package.dev-dependencies] @@ -939,6 +940,7 @@ requires-dist = [ { name = "textual", specifier = ">=1.0.0" }, { name = "tomlkit", specifier = ">=0.13.2" }, { name = "typer", specifier = ">=0.15.1" }, + { name = "xdg-base-dirs", specifier = ">=6.0.2" }, ] [package.metadata.requires-dev] @@ -981,6 +983,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/37/87/1f677586e8ac487e29672e4b17455758fce261de06a0d086167bb760361a/uc_micro_py-1.0.3-py3-none-any.whl", hash = "sha256:db1dffff340817673d7b466ec86114a9dc0e9d4d9b5ba229d9d60e5c12600cd5", size = 6229 }, ] +[[package]] +name = "xdg-base-dirs" +version = "6.0.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/bf/d0/bbe05a15347538aaf9fa5b51ac3b97075dfb834931fcb77d81fbdb69e8f6/xdg_base_dirs-6.0.2.tar.gz", hash = "sha256:950504e14d27cf3c9cb37744680a43bf0ac42efefc4ef4acf98dc736cab2bced", size = 4085 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/fc/03/030b47fd46b60fc87af548e57ff59c2ca84b2a1dadbe721bb0ce33896b2e/xdg_base_dirs-6.0.2-py3-none-any.whl", hash = "sha256:3c01d1b758ed4ace150ac960ac0bd13ce4542b9e2cdf01312dcda5012cfebabe", size = 4747 }, +] + [[package]] name = "yarl" version = "1.18.3"