|
| 1 | +""" |
| 2 | +PyWorkout user configuration. |
| 3 | +
|
| 4 | +A user can override exercises, rep counts, and (most usefully) video paths |
| 5 | +without editing source. Config lives at ``~/.pyworkout/config.json`` and is |
| 6 | +deep-merged over the built-in defaults in ``data.py`` -- so overriding just one |
| 7 | +group's video path leaves every other group untouched. |
| 8 | +
|
| 9 | +Missing config -> defaults, silently. Malformed JSON -> defaults, with a single |
| 10 | +warning line (this is a hobby tool; never crash on a bad config). |
| 11 | +""" |
| 12 | + |
| 13 | +import copy |
| 14 | +import json |
| 15 | +import os |
| 16 | + |
| 17 | +try: |
| 18 | + from data import WORKOUTS, GROUP_ORDER |
| 19 | +except ImportError: # installed as part of the package |
| 20 | + from .data import WORKOUTS, GROUP_ORDER |
| 21 | + |
| 22 | +CONFIG_DIR_NAME = ".pyworkout" |
| 23 | +CONFIG_FILE_NAME = "config.json" |
| 24 | + |
| 25 | + |
| 26 | +def config_dir(): |
| 27 | + """Return the directory holding PyWorkout config and history.""" |
| 28 | + return os.path.join(os.path.expanduser("~"), CONFIG_DIR_NAME) |
| 29 | + |
| 30 | + |
| 31 | +def default_config_path(): |
| 32 | + """Return the default path to ``config.json``.""" |
| 33 | + return os.path.join(config_dir(), CONFIG_FILE_NAME) |
| 34 | + |
| 35 | + |
| 36 | +def _deep_merge(base, override): |
| 37 | + """Return a deep copy of ``base`` with ``override`` recursively merged in.""" |
| 38 | + result = copy.deepcopy(base) |
| 39 | + for key, value in override.items(): |
| 40 | + if ( |
| 41 | + key in result |
| 42 | + and isinstance(result[key], dict) |
| 43 | + and isinstance(value, dict) |
| 44 | + ): |
| 45 | + result[key] = _deep_merge(result[key], value) |
| 46 | + else: |
| 47 | + result[key] = copy.deepcopy(value) |
| 48 | + return result |
| 49 | + |
| 50 | + |
| 51 | +def default_workouts(): |
| 52 | + """Return a deep copy of the built-in workout data.""" |
| 53 | + return copy.deepcopy(WORKOUTS) |
| 54 | + |
| 55 | + |
| 56 | +def load_config(path=None): |
| 57 | + """ |
| 58 | + Load the merged workout config. |
| 59 | +
|
| 60 | + Reads JSON from ``path`` (defaults to ``default_config_path()``) and |
| 61 | + deep-merges it over the built-in defaults. Returns the defaults unchanged |
| 62 | + when the file is absent or unreadable. |
| 63 | + """ |
| 64 | + if path is None: |
| 65 | + path = default_config_path() |
| 66 | + |
| 67 | + if not os.path.exists(path): |
| 68 | + return default_workouts() |
| 69 | + |
| 70 | + try: |
| 71 | + with open(path, encoding="UTF-8") as handle: |
| 72 | + user = json.load(handle) |
| 73 | + except (json.JSONDecodeError, OSError) as err: |
| 74 | + print(f"Warning: could not read config at {path} ({err}). Using defaults.") |
| 75 | + return default_workouts() |
| 76 | + |
| 77 | + if not isinstance(user, dict): |
| 78 | + print(f"Warning: config at {path} is not a JSON object. Using defaults.") |
| 79 | + return default_workouts() |
| 80 | + |
| 81 | + return _deep_merge(WORKOUTS, user) |
| 82 | + |
| 83 | + |
| 84 | +def write_default_config(path=None): |
| 85 | + """ |
| 86 | + Write the built-in defaults to ``path`` as a starting point users can edit. |
| 87 | +
|
| 88 | + Creates the config directory if needed. Returns the path written. |
| 89 | + """ |
| 90 | + if path is None: |
| 91 | + path = default_config_path() |
| 92 | + |
| 93 | + os.makedirs(os.path.dirname(path), exist_ok=True) |
| 94 | + with open(path, "w", encoding="UTF-8") as handle: |
| 95 | + json.dump(default_workouts(), handle, indent=2) |
| 96 | + return path |
| 97 | + |
| 98 | + |
| 99 | +def group_order(workouts=None): |
| 100 | + """ |
| 101 | + Return group keys in display order. |
| 102 | +
|
| 103 | + Built-in groups keep their canonical order; any extra groups a user added |
| 104 | + via config are appended in sorted order. |
| 105 | + """ |
| 106 | + if workouts is None: |
| 107 | + return list(GROUP_ORDER) |
| 108 | + ordered = [g for g in GROUP_ORDER if g in workouts] |
| 109 | + extra = sorted(k for k in workouts if k not in GROUP_ORDER) |
| 110 | + return ordered + extra |
0 commit comments