Skip to content

Commit 1889311

Browse files
committed
Fix recording path issues
Also added fallback paths for app default config dir
1 parent e2a2612 commit 1889311

4 files changed

Lines changed: 118 additions & 62 deletions

File tree

‎radioactive/actions.py‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,27 @@ def handle_record(
9797

9898
if record_file_path and not os.path.exists(record_file_path):
9999
log.debug(f"filepath: {record_file_path}")
100-
os.makedirs(record_file_path, exist_ok=True)
100+
try:
101+
os.makedirs(record_file_path, exist_ok=True)
102+
except Exception as e:
103+
log.error(f"Could not create recording directory: {e}")
101104

102105
elif not record_file_path:
106+
from radioactive.paths import get_recordings_path
107+
103108
log.debug("filepath: fallback to default path")
104-
record_file_path = os.path.join(
105-
os.path.expanduser("~"), "Music/radioactive"
106-
) # fallback path
109+
record_file_path = get_recordings_path()
107110
try:
108111
os.makedirs(record_file_path, exist_ok=True)
109112
except Exception as e:
110-
log.debug(f"{e}")
111-
log.error("Could not make default directory")
112-
sys.exit(1)
113+
log.error(f"Could not create recording directory: {e}")
114+
log.warning("Recording might fail if the directory is not writable.")
115+
# We don't exit here, we try to proceed or return?
116+
# If we return, recording stops but app stays alive.
117+
# But earlier code sys.exit(1).
118+
# User wants NO CRASH.
119+
# Let's try to verify if we can write there?
120+
# For now, just catching the exception is enough to stop the crash.
113121

114122
now = datetime.datetime.now()
115123
month_name = now.strftime("%b").upper()

‎radioactive/config.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ def write_a_sample_config_file() -> None:
1919
# Create a ConfigParser object
2020
config = configparser.ConfigParser()
2121

22+
from radioactive.paths import get_recordings_path
23+
2224
# Add sections and key-value pairs
2325
config["AppConfig"] = {
2426
"loglevel": "info",
2527
"limit": "100",
2628
"sort": "votes",
2729
"filter": "none",
2830
"volume": "80",
29-
"filepath": "/home/{user}/recordings/radioactive/",
31+
"filepath": get_recordings_path(),
3032
"filetype": "mp3",
3133
"player": "ffplay",
3234
}
@@ -83,9 +85,9 @@ def get_option(key: str, default: str = "") -> str:
8385
options["sort"] = get_option("sort", "votes")
8486
options["filter"] = get_option("filter", "none")
8587
options["limit"] = get_option("limit", "100")
86-
options["filepath"] = get_option(
87-
"filepath", "/home/{user}/recordings/radioactive/"
88-
)
88+
from radioactive.paths import get_recordings_path
89+
90+
options["filepath"] = get_option("filepath", get_recordings_path())
8991

9092
# if filepath has any placeholder, replace {user} to actual user map
9193
if "{user}" in options["filepath"]:

‎radioactive/help.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def show_help():
124124
table.add_row(
125125
"--filepath",
126126
"Path to save the recorded audio",
127-
f"{user}/Music/radioactive",
127+
f"{user}/radioactive/recordings",
128128
)
129129

130130
table.add_row(

‎radioactive/paths.py‎

Lines changed: 96 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,131 @@
11
import os
22
import shutil
3+
import sys
34
from zenlog import log
45

56

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("~")
2212

2313

24-
def get_config_path():
14+
def get_base_dir():
2515
"""
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.
2819
"""
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")
3022

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}")
3429

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+
"""
3537
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}")
3739
try:
40+
# Ensure the directory exists
41+
os.makedirs(os.path.dirname(new_path), exist_ok=True)
3842
shutil.move(legacy_path, new_path)
3943
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)")
4268

4369
return new_path
4470

4571

4672
def get_alias_path():
4773
"""
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
5075
"""
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")
5278

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()
5680

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)")
6391

6492
return new_path
6593

6694

6795
def get_last_station_path():
6896
"""
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
7198
"""
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")
73101

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()
77103

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)")
84114

85115
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

Comments
 (0)