Skip to content

Commit 998bc55

Browse files
committed
- adding resource_opener config parameter
- fixing issue #236
1 parent a87d409 commit 998bc55

File tree

11 files changed

+73
-29
lines changed

11 files changed

+73
-29
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ A clear and concise description of what the bug is.
1313
**To Reproduce**
1414
Steps to reproduce the behavior:
1515
1. Press '....'
16-
3. Select '....'
16+
3. Select '....'
1717
4. See error
1818

1919
**Expected behavior**
2020
A clear and concise description of what you expected to happen.
2121

2222
**System (please complete the following information):**
2323
- OS: [e.g. Dabian, macOS, Windows]
24-
- **Python** version:
25-
- `pipx` version (if applicable):
24+
- **Python** version:
25+
- `pipx` version (if applicable):
2626
- `pipx` installation method: System dependent / Fully isolated
2727
- **PyRadio** Version:
28+
- Player used: [e.g mpv, mplayer, vlc]
2829

2930
**Additional context**
3031
Add any other context about the problem here.

pyradio/common.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import io
33
import csv
44
from sys import version as sys_version
5-
from os import rename, remove
5+
from os import rename, remove, access, X_OK
66
from os.path import exists, dirname, join
7+
from shutil import which
78
from copy import deepcopy
89
from rich import print
910
import logging
@@ -478,3 +479,17 @@ def update_stations_csv(self, print_messages=True):
478479
# for n in self._stations:
479480
# print(n)
480481

482+
def validate_resource_opener_path(a_file):
483+
# Check if the file exists
484+
if not exists(a_file):
485+
# If the file doesn't exist, try to find it using shutil.which
486+
full_path = which(a_file)
487+
if full_path is None:
488+
return None
489+
else:
490+
a_file = full_path
491+
# Check if the file is executable
492+
if not access(a_file, X_OK):
493+
return None
494+
# Return the validated path
495+
return a_file

pyradio/config

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ enable_mouse = False
4545
# Default value: default
4646
recording_dir = default
4747

48+
# Resource Opener
49+
# This is a Linux (et al) only parameter. It has no effect on Windows or MacOS.
50+
# A Resource Opener is a program used to open files passed to it as arguments.
51+
# PyRadio will use it to open either directories or HTML files.
52+
# Default value is "auto", in which case, PyRadio will try to use xdg-open,
53+
# gio, mimeopen, mimeo or handlr, in that order of detection. If none if found,
54+
# the requested file will simply not open.
55+
# To set a custom Opener, insert the absolute path to its executable, followed
56+
# by any parameter required, for example: "/usr/bin/gio open".
57+
resource_opener = auto
58+
4859
# Desktop notifications
4960
# If this option is enabled, a Desktop notification will be displayed using the
5061
# notification daemon / service. If enabled but no notification is displayed,

pyradio/config.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
from os import getuid
2626
from pyradio import version, stations_updated
2727

28+
from .common import validate_resource_opener_path
2829
from .browser import PyRadioStationsBrowser, probeBrowsers
2930
from .install import get_github_long_description
3031
from .common import is_rasberrypi
3132
from .player import pywhich
3233
from .server import IPsWithNumbers
3334
from .xdg import XdgDirs, XdgMigrate, CheckDir
34-
from .install import run_tool
35+
from .install import get_a_linux_resource_opener
3536
HAS_REQUESTS = True
3637

3738
try:
@@ -1285,6 +1286,7 @@ class PyRadioConfig(PyRadioStations):
12851286
opts['enable_notifications'] = ['Enable notifications: ', '-1']
12861287
opts['use_station_icon'] = [' Use station icon: ', True]
12871288
opts['recording_dir'] = ['Recordings dir: ', '']
1289+
opts['resource_opener'] = ['Resource Opener: ', 'auto']
12881290
opts['conn_title'] = ['Connection Options: ', '']
12891291
opts['connection_timeout'] = ['Connection timeout: ', '10']
12901292
opts['force_http'] = ['Force http connections: ', False]
@@ -1368,7 +1370,7 @@ class PyRadioConfig(PyRadioStations):
13681370

13691371
_fixed_recording_dir = None
13701372

1371-
_linux_run_tool = None
1373+
_linux_resource_opener = None
13721374

13731375
def __init__(self, user_config_dir=None, headless=False):
13741376
# keep old recording / new recording dir
@@ -1426,8 +1428,8 @@ def __init__(self, user_config_dir=None, headless=False):
14261428
player_instance = None
14271429

14281430
@property
1429-
def linux_run_tool(self):
1430-
return self._linux_run_tool
1431+
def linux_resource_opener(self):
1432+
return self._linux_resource_opener
14311433

14321434
@property
14331435
def xdg_compliant(self):
@@ -1832,7 +1834,7 @@ def open_a_dir(self, a_dir):
18321834
elif system().lower() == 'darwin':
18331835
Popen([which('open'), a_dir])
18341836
else:
1835-
xdg_open_path = self._linux_run_tool if self._linux_run_tool else run_tool()
1837+
xdg_open_path = self._linux_resource_opener if self._linux_resource_opener else get_a_linux_resource_opener()
18361838
if xdg_open_path:
18371839
try:
18381840
Popen(
@@ -1850,7 +1852,7 @@ def open_config_dir(self, recording=0):
18501852
elif system().lower() == 'darwin':
18511853
Popen([which('open'), a_dir])
18521854
else:
1853-
xdg_open_path = self._linux_run_tool if self._linux_run_tool else run_tool()
1855+
xdg_open_path = self._linux_resource_opener if self._linux_resource_opener else get_a_linux_resource_opener()
18541856
if xdg_open_path:
18551857
try:
18561858
Popen(
@@ -2052,6 +2054,7 @@ def _read_config(self, distro_config=False):
20522054
self._make_sure_dirs_exist()
20532055
self._first_read = False
20542056
return
2057+
self._linux_resource_opener = None
20552058
lines = []
20562059
try:
20572060
with open(file_to_read, 'r', encoding='utf-8') as cfgfile:
@@ -2224,6 +2227,14 @@ def _read_config(self, distro_config=False):
22242227
not platform.startswith('win') and \
22252228
sp[1].lower() == 'true':
22262229
self.xdg_compliant = True
2230+
elif sp[0] == 'resource_opener' and \
2231+
not platform.startswith('win'):
2232+
if self.opts['resource_opener'][1] != 'auto':
2233+
tmp = self.opts['resource_opener'][1].split(' ')
2234+
prog = validate_resource_opener_path(tmp[0])
2235+
if prog is not None:
2236+
tmp[0] = prog
2237+
self._linux_resource_opener = ' '.join(tmp)
22272238

22282239
# logger.error('\n\nself.params{}\n\n'.format(self.params))
22292240
''' read distro from package config file '''

pyradio/config_window.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def set_global_functions(global_functions):
2828
del ret[ord('t')]
2929
return ret
3030

31+
3132
class PyRadioConfigWindow(object):
3233
n_u = Window_Stack_Constants
3334

@@ -68,6 +69,10 @@ class PyRadioConfigWindow(object):
6869
_help_text.append(['If this options is enabled, a Desktop Notification will be displayed using the notification daemon / service.', '|', 'If enabled but no notification is displayed, please refer to', 'https://github.com/coderholic/pyradio/desktop-notification.md', '|', 'Valid values are:', ' -1: disabled ', ' 0: enabled (no repetition) ', ' x: repeat every x seconds ', '|', 'Default value: -1'])
6970
_help_text.append(['Notice: Not applicable on Windows!', '|', 'Online Radio Directory Services (like RadioBrowser) will usually provide an icon for the stations they advertise.', '|', 'PyRadio can use this icon (provided that one exists and is of JPG or PNG format) while displaying Desktop Notifications.', '|', 'Setting this option to True, will enable the behavior above.', '|', 'If this option is False, the default icon will be used.', '|', 'Default value: True'])
7071
_help_text.append([ 'This is the folder where recorded files will be saved', '|', 'Tip: When you open the window "h" will display HTML help about this parameter (not in Line Editor).', '|', 'Default value: "pyradio-recordings" in home dir' ])
72+
_help_text.append(['This is a Linux (et al) only parameter. It has no effect on Windows or MacOS.', '|',
73+
'A Resource Opener is a program used to open files passed to it as arguments. PyRadio will use it to open either directories or HTML files.', '|',
74+
'Default value is "auto", in which case, PyRadio will try to use xdg-open, gio, mimeopen, mimeo or handlr, in that order of detection. If none if found, the requested file will simply not open.'
75+
])
7176
_help_text.append(None)
7277
_help_text.append(['PyRadio will wait for this number of seconds to get a station/server message indicating that playback has actually started.', '|',
7378
'If this does not happen within this number of seconds after the connection is initiated, PyRadio will consider the station unreachable, and display the "Failed to connect to: station" message.', '|', 'Press "h"/Left or "l"/Right to change value.',
@@ -519,6 +524,7 @@ def _load_default_values(self):
519524
self._config_options['enable_notifications'][1] = '-1'
520525
self._config_options['use_station_icon'][1] = 'True'
521526
self._config_options['recording_dir'][1] = path.join(path.expanduser('~'), 'pyradio-recordings')
527+
opts['resource_opener'] = ['Resource Opener: ', 'auto']
522528
self._config_options['connection_timeout'][1] = '10'
523529
self._config_options['theme_title'][1] = ''
524530
''' Transparency '''

pyradio/html_help.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from sys import platform
55
from os import path
66
from shutil import which
7-
from .install import run_tool
7+
from .install import get_a_linux_resource_opener
88

99
logger = logging.getLogger(__name__)
1010

@@ -30,14 +30,14 @@ def __init__(self):
3030
self._path = a_path
3131
break
3232

33-
def open_file(self, linux_run_tool=None, browser=False):
33+
def open_file(self, linux_resource_opener=None, browser=False):
3434
a_file = self._files[1] if browser else self._files[0]
35-
self._open_file(a_file, linux_run_tool=linux_run_tool)
35+
self._open_file(a_file, linux_resource_opener=linux_resource_opener)
3636

37-
def open_filename(self,a_file, linux_run_tool=None):
38-
self._open_file(a_file, linux_run_tool=linux_run_tool)
37+
def open_filename(self,a_file, linux_resource_opener=None):
38+
self._open_file(a_file, linux_resource_opener=linux_resource_opener)
3939

40-
def _open_file(self, a_file, linux_run_tool=None):
40+
def _open_file(self, a_file, linux_resource_opener=None):
4141
if logger.isEnabledFor(logging.DEBUG):
4242
logger.debug('HtmlHelp: opening "{}"'.format(path.join(self._path, a_file)))
4343
this_platform = platform.lower()
@@ -48,10 +48,10 @@ def _open_file(self, a_file, linux_run_tool=None):
4848
if this_platform.startswith('darwin'):
4949
cmd = [which('open'), path.join(self._path, a_file)]
5050
else:
51-
if linux_run_tool is None:
52-
tool = run_tool()
51+
if linux_resource_opener is None:
52+
tool = get_a_linux_resource_opener()
5353
else:
54-
tool = linux_run_tool
54+
tool = linux_resource_opener
5555
if tool is None:
5656
if logger.isEnabledFor(logging.INFO):
5757
logger.info('HtmlHelp: Cannot find a run tool for Linux!')

pyradio/install.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def WindowExists(title):
419419
else:
420420
return True
421421

422-
def run_tool():
422+
def get_a_linux_resource_opener():
423423
''' return an resource open tool from a list
424424
the list comes from "Resource openers"
425425
https://wiki.archlinux.org/title/default_applications
@@ -455,7 +455,7 @@ def open_cache_dir():
455455
elif platform.system().lower() == 'darwin':
456456
subprocess.Popen([which('open'), c.cache_dir])
457457
else:
458-
prog = run_tool()
458+
prog = get_a_linux_resource_opener()
459459
if prog:
460460
try:
461461
subprocess.Popen(

pyradio/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from .log import Log
2323
from .common import StationsChanges
2424
from .schedule import PyRadioScheduleList
25-
from .install import run_tool
25+
from .install import get_a_linux_resource_opener
2626
import locale
2727
locale.setlocale(locale.LC_ALL, "")
2828

@@ -1049,7 +1049,7 @@ def open_conf_dir(cnf, msg=None, a_dir=None):
10491049
elif platform.system().lower() == 'darwin':
10501050
subprocess.Popen([shutil.which('open'), op_dir])
10511051
else:
1052-
prog = cnf.linux_run_tool if cnf.linux_run_tool else run_tool()
1052+
prog = cnf.linux_resource_opener if cnf.linux_resource_opener else get_a_linux_resource_opener()
10531053
if prog:
10541054
try:
10551055
subprocess.Popen(

pyradio/messages_system.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,16 +1119,16 @@ def set_text(self, parent, *args):
11191119
When a new directory is specified, |PyRadio| will try to |move| the
11201120
existing directory to the new location.
11211121
1122-
If the new directory
1122+
If the new directory...
11231123
1. |does not exist|
11241124
___|PyRadio| will move the original directory to the new location
11251125
___and optionally rename it.
1126-
2. |already exists and it is empty|
1126+
2. |already exists and is empty|
11271127
___|PyRadio| will |delete| the new directory and |move| the original
11281128
___directory to the new location and optionally rename it.
1129-
3. |PyRadio| will |move| the original directory |inside| the new
1129+
3. |already exists and is not empty|
1130+
___|PyRadio| will |move| the original directory |inside| the new
11301131
___directory and optionally rename it.
1131-
11321132
'''
11331133
),
11341134

pyradio/player.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def get_info_string(self, a_station, max_width, win_width):
678678
# logger.debug('\n\n\n')
679679

680680
if 'Codec:' not in a_list[-1]:
681-
a_list[n] = '|' + a_list[n]
681+
a_list.append(' Codec:')
682682

683683
# if a_list[1].startswith('_'):
684684
# a_list[1] = '|' + a_list[1]

pyradio/radio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6133,7 +6133,7 @@ def keypress(self, char):
61336133
self._update_status_bar_right(status_suffix='')
61346134
html = HtmlHelp()
61356135
html.open_file(
6136-
linux_run_tool=self._cnf.linux_run_tool,
6136+
linux_resource_opener=self._cnf.linux_resource_opener,
61376137
browser=self._cnf.browsing_station_service
61386138
)
61396139

0 commit comments

Comments
 (0)