|
1 | 1 | import os |
2 | 2 | import shutil |
| 3 | +import sys |
3 | 4 | from zenlog import log |
4 | 5 |
|
5 | 6 |
|
6 | | -def _get_xdg_config_dir(): |
7 | | - """Return the XDG configuration directory for radio-active.""" |
8 | | - xdg_config_home = os.environ.get("XDG_CONFIG_HOME") |
9 | | - if xdg_config_home: |
10 | | - return os.path.join(xdg_config_home, "radio-active") |
11 | | - # Default to ~/.config/radio-active |
12 | | - return os.path.join(os.path.expanduser("~"), ".config", "radio-active") |
13 | | - |
14 | | - |
15 | | -def _get_xdg_data_dir(): |
16 | | - """Return the XDG data directory for radio-active.""" |
17 | | - xdg_data_home = os.environ.get("XDG_DATA_HOME") |
18 | | - if xdg_data_home: |
19 | | - return os.path.join(xdg_data_home, "radio-active") |
20 | | - # Default to ~/.local/share/radio-active |
21 | | - return os.path.join(os.path.expanduser("~"), ".local", "share", "radio-active") |
| 7 | +def get_user_home(): |
| 8 | + """ |
| 9 | + Get the user's home directory in a cross-platform way. |
| 10 | + """ |
| 11 | + return os.path.expanduser("~") |
22 | 12 |
|
23 | 13 |
|
24 | | -def get_config_path(): |
| 14 | +def get_base_dir(): |
25 | 15 | """ |
26 | | - Get the path to the configuration file. |
27 | | - Migrates from legacy path if it exists and new path does not. |
| 16 | + Return the base directory for radioactive files: ~/radioactive |
| 17 | + This acts as the central storage for config, data, and recordings |
| 18 | + as per user request. |
28 | 19 | """ |
29 | | - legacy_path = os.path.join(os.path.expanduser("~"), ".radio-active-configs.ini") |
| 20 | + home = get_user_home() |
| 21 | + base_dir = os.path.join(home, "radioactive") |
30 | 22 |
|
31 | | - config_dir = _get_xdg_config_dir() |
32 | | - os.makedirs(config_dir, exist_ok=True) |
33 | | - new_path = os.path.join(config_dir, "config.ini") |
| 23 | + try: |
| 24 | + os.makedirs(base_dir, exist_ok=True) |
| 25 | + except Exception as e: |
| 26 | + # If we can't create the base dir, we are in trouble, |
| 27 | + # but we log it and proceed (might crash later if not handled) |
| 28 | + log.error(f"Could not create base directory {base_dir}: {e}") |
34 | 29 |
|
| 30 | + return base_dir |
| 31 | + |
| 32 | + |
| 33 | +def _migrate_file(legacy_path, new_path, description): |
| 34 | + """ |
| 35 | + Migrate a file from legacy_path to new_path if it exists. |
| 36 | + """ |
35 | 37 | if os.path.exists(legacy_path) and not os.path.exists(new_path): |
36 | | - log.info(f"Migrating config file from {legacy_path} to {new_path}") |
| 38 | + log.info(f"Migrating {description} from {legacy_path} to {new_path}") |
37 | 39 | try: |
| 40 | + # Ensure the directory exists |
| 41 | + os.makedirs(os.path.dirname(new_path), exist_ok=True) |
38 | 42 | shutil.move(legacy_path, new_path) |
39 | 43 | except Exception as e: |
40 | | - log.warning(f"Could not migrate config file: {e}") |
41 | | - # If migration fails, we return new_path anyway, user might have to manually move or start fresh |
| 44 | + log.warning(f"Could not migrate {description}: {e}") |
| 45 | + |
| 46 | + |
| 47 | +def get_config_path(): |
| 48 | + """ |
| 49 | + Get the path to the configuration file: ~/radioactive/config.ini |
| 50 | + """ |
| 51 | + base_dir = get_base_dir() |
| 52 | + new_path = os.path.join(base_dir, "config.ini") |
| 53 | + |
| 54 | + home = get_user_home() |
| 55 | + |
| 56 | + # 1. ~/.radio-active-configs.ini |
| 57 | + legacy_dot_path = os.path.join(home, ".radio-active-configs.ini") |
| 58 | + _migrate_file(legacy_dot_path, new_path, "config file (dotfile)") |
| 59 | + |
| 60 | + # 2. XDG locations (from previous attempts) |
| 61 | + # ~/.config/radioactive/config.ini |
| 62 | + xdg_path_new = os.path.join(home, ".config", "radioactive", "config.ini") |
| 63 | + _migrate_file(xdg_path_new, new_path, "config file (xdg-new)") |
| 64 | + |
| 65 | + # ~/.config/radio-active/config.ini |
| 66 | + xdg_path_old = os.path.join(home, ".config", "radio-active", "config.ini") |
| 67 | + _migrate_file(xdg_path_old, new_path, "config file (xdg-old)") |
42 | 68 |
|
43 | 69 | return new_path |
44 | 70 |
|
45 | 71 |
|
46 | 72 | def get_alias_path(): |
47 | 73 | """ |
48 | | - Get the path to the alias (favorites) file. |
49 | | - Migrates from legacy path if it exists and new path does not. |
| 74 | + Get the path to the alias (favorites) file: ~/radioactive/alias_map |
50 | 75 | """ |
51 | | - legacy_path = os.path.join(os.path.expanduser("~"), ".radio-active-alias") |
| 76 | + base_dir = get_base_dir() |
| 77 | + new_path = os.path.join(base_dir, "alias_map") |
52 | 78 |
|
53 | | - config_dir = _get_xdg_config_dir() # Aliases are user config |
54 | | - os.makedirs(config_dir, exist_ok=True) |
55 | | - new_path = os.path.join(config_dir, "alias_map") |
| 79 | + home = get_user_home() |
56 | 80 |
|
57 | | - if os.path.exists(legacy_path) and not os.path.exists(new_path): |
58 | | - log.info(f"Migrating alias file from {legacy_path} to {new_path}") |
59 | | - try: |
60 | | - shutil.move(legacy_path, new_path) |
61 | | - except Exception as e: |
62 | | - log.warning(f"Could not migrate alias file: {e}") |
| 81 | + # 1. ~/.radio-active-alias |
| 82 | + legacy_dot_path = os.path.join(home, ".radio-active-alias") |
| 83 | + _migrate_file(legacy_dot_path, new_path, "alias file (dotfile)") |
| 84 | + |
| 85 | + # 2. XDG locations |
| 86 | + xdg_path_new = os.path.join(home, ".config", "radioactive", "alias_map") |
| 87 | + _migrate_file(xdg_path_new, new_path, "alias file (xdg-new)") |
| 88 | + |
| 89 | + xdg_path_old = os.path.join(home, ".config", "radio-active", "alias_map") |
| 90 | + _migrate_file(xdg_path_old, new_path, "alias file (xdg-old)") |
63 | 91 |
|
64 | 92 | return new_path |
65 | 93 |
|
66 | 94 |
|
67 | 95 | def get_last_station_path(): |
68 | 96 | """ |
69 | | - Get the path to the last played station file. |
70 | | - Migrates from legacy path if it exists and new path does not. |
| 97 | + Get the path to the last played station file: ~/radioactive/last_station |
71 | 98 | """ |
72 | | - legacy_path = os.path.join(os.path.expanduser("~"), ".radio-active-last-station") |
| 99 | + base_dir = get_base_dir() |
| 100 | + new_path = os.path.join(base_dir, "last_station") |
73 | 101 |
|
74 | | - data_dir = _get_xdg_data_dir() # Last station is state/data |
75 | | - os.makedirs(data_dir, exist_ok=True) |
76 | | - new_path = os.path.join(data_dir, "last-station") |
| 102 | + home = get_user_home() |
77 | 103 |
|
78 | | - if os.path.exists(legacy_path) and not os.path.exists(new_path): |
79 | | - log.info(f"Migrating last station file from {legacy_path} to {new_path}") |
80 | | - try: |
81 | | - shutil.move(legacy_path, new_path) |
82 | | - except Exception as e: |
83 | | - log.warning(f"Could not migrate last station file: {e}") |
| 104 | + # 1. ~/.radio-active-last_station |
| 105 | + legacy_dot_path = os.path.join(home, ".radio-active-last_station") |
| 106 | + _migrate_file(legacy_dot_path, new_path, "last station file (dotfile)") |
| 107 | + |
| 108 | + # 2. XDG locations (usually in local/share, but we check config too just in case) |
| 109 | + xdg_data_new = os.path.join(home, ".local", "share", "radioactive", "last_station") |
| 110 | + _migrate_file(xdg_data_new, new_path, "last station file (xdg-new)") |
| 111 | + |
| 112 | + xdg_data_old = os.path.join(home, ".local", "share", "radio-active", "last_station") |
| 113 | + _migrate_file(xdg_data_old, new_path, "last station file (xdg-old)") |
84 | 114 |
|
85 | 115 | return new_path |
| 116 | + |
| 117 | + |
| 118 | +def get_recordings_path(): |
| 119 | + """ |
| 120 | + Get the path for recordings: ~/radioactive/recordings |
| 121 | + """ |
| 122 | + base_dir = get_base_dir() |
| 123 | + recordings_path = os.path.join(base_dir, "recordings") |
| 124 | + |
| 125 | + try: |
| 126 | + os.makedirs(recordings_path, exist_ok=True) |
| 127 | + except Exception as e: |
| 128 | + log.error(f"Could not create recordings directory {recordings_path}: {e}") |
| 129 | + # Not exiting here, hoping the caller handles it or it works next time |
| 130 | + |
| 131 | + return recordings_path |
0 commit comments