123 lines
4.4 KiB
Python
123 lines
4.4 KiB
Python
"""Where things live. ROUTINE_HOME overrides the data+config dir (tests and the dev data set).
|
|
|
|
The routine (schedule.json) and the history (routine.db) live in the data dir unless Settings →
|
|
Data points either somewhere else ("schedule_dir" / "db_dir" in the config: a folder, "" = the
|
|
default). With ROUTINE_HOME set, the defaults and the config file are all under it, so a test
|
|
never touches the real ones (a location set in that test config still applies).
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from routine import system
|
|
|
|
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
SHARED = os.path.join(REPO, "shared")
|
|
SCHEDULE = os.path.join(SHARED, "schedule.json") # the template: a first run starts from it
|
|
ANIMATIONS = os.path.join(SHARED, "animations.json")
|
|
SOUNDS = os.path.join(SHARED, "sounds")
|
|
APK = os.path.join(REPO, "android", "apk", "routine.apk")
|
|
|
|
_home = os.environ.get("ROUTINE_HOME")
|
|
DATA = system.data_dir() # Linux ~/.local/share/routine, macOS ~/Library/Application Support/Routine
|
|
CONFIG_DIR = system.config_dir() # Linux ~/.config/routine, macOS the same folder as DATA
|
|
CONFIG = os.path.join(CONFIG_DIR, "config.json")
|
|
LOGS = system.log_dir() # errors.log: Linux ~/.local/state/routine, macOS ~/Library/Logs/Routine
|
|
SCHEDULE_NAME, DB_NAME = "schedule.json", "routine.db"
|
|
DB = os.path.join(DATA, DB_NAME) # the default places (schedule_path / db_path
|
|
SCHEDULE_LIVE = os.path.join(DATA, SCHEDULE_NAME) # give the ones in use)
|
|
|
|
DEFAULTS = {
|
|
"sync_url": "", # the sync server (server/routine_server.py), e.g. http://host:8070
|
|
"sync_token": "",
|
|
"schedule_dir": "", # where schedule.json is kept ("" = DATA)
|
|
"db_dir": "", # where routine.db is kept ("" = DATA)
|
|
"mode": "system", # system | light | dark
|
|
"sounds": True,
|
|
"tooltips": True,
|
|
"hide_menubar": True,
|
|
"filter": "all", # all | incomplete
|
|
"window": [458, 1050], # width, height (tall and narrow)
|
|
"streak_anims": True, # a tick that reaches a streak level plays a streak animation instead
|
|
"cheat_day": True, # streaks may hold one forgiven miss ...
|
|
"cheat_after": 7, # ... earned by this many done days in a row
|
|
"emboss_history": True, # the History window draws its dots as little buttons
|
|
}
|
|
|
|
# Config keys that were renamed: read once from an older config and written back under the new name.
|
|
RENAMED = {} # old key: new key (none at the moment)
|
|
|
|
|
|
def _folder(cfg, key):
|
|
d = (cfg.get(key) or "").strip()
|
|
return os.path.abspath(os.path.expanduser(d)) if d else DATA
|
|
|
|
|
|
def schedule_dir(cfg):
|
|
return _folder(cfg, "schedule_dir")
|
|
|
|
|
|
def db_dir(cfg):
|
|
return _folder(cfg, "db_dir")
|
|
|
|
|
|
def schedule_path(cfg):
|
|
"""The user's routine, as edited in Settings → Routine."""
|
|
return os.path.join(schedule_dir(cfg), SCHEDULE_NAME)
|
|
|
|
|
|
def db_path(cfg):
|
|
return os.path.join(db_dir(cfg), DB_NAME)
|
|
|
|
|
|
def cheat_after(cfg):
|
|
"""The cheat-day setting as model's cheat_after: N, or 0 when cheat days are off."""
|
|
if not cfg.get("cheat_day", True):
|
|
return 0
|
|
try:
|
|
return max(1, int(cfg.get("cheat_after", 7)))
|
|
except (TypeError, ValueError):
|
|
return 7
|
|
|
|
|
|
def migrate_config(saved):
|
|
"""Renamed keys in a config as read from disk, in place: the old value moves to the new key
|
|
(unless the new one is already set). Returns the old key names it moved (never the values)."""
|
|
moved = []
|
|
for old, new in RENAMED.items():
|
|
if old in saved:
|
|
v = saved.pop(old)
|
|
if new not in saved:
|
|
saved[new] = v
|
|
moved.append(old)
|
|
return moved
|
|
|
|
|
|
def load_config():
|
|
cfg = dict(DEFAULTS)
|
|
try:
|
|
with open(CONFIG) as f:
|
|
saved = json.load(f)
|
|
except (OSError, ValueError):
|
|
saved = {}
|
|
if not isinstance(saved, dict):
|
|
saved = {}
|
|
moved = migrate_config(saved)
|
|
cfg.update(saved)
|
|
if moved:
|
|
try:
|
|
save_config(cfg)
|
|
print("routine: config keys renamed (%d)" % len(moved), file=sys.stderr)
|
|
except OSError:
|
|
pass
|
|
return cfg
|
|
|
|
|
|
def save_config(cfg):
|
|
os.makedirs(CONFIG_DIR, exist_ok=True)
|
|
tmp = CONFIG + ".tmp"
|
|
with open(tmp, "w") as f:
|
|
json.dump(cfg, f, indent=2)
|
|
os.chmod(tmp, 0o600) # holds the sync token
|
|
os.replace(tmp, CONFIG)
|