Skip to content

Commit 1a1cef4

Browse files
committed
station encoding can now get a "Default" value, to leave the station's
encoding empty. This will make it use the default (config) encoding.
1 parent 249a5f2 commit 1a1cef4

File tree

5 files changed

+53
-15
lines changed

5 files changed

+53
-15
lines changed

pyradio/common.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,8 @@ class CsvReadWrite():
643643
''' A base class to read and write a PyRadio playlist '''
644644
_items = None
645645

646+
encoding_to_remove = None
647+
646648
def __init__(self, a_file=None):
647649
self._file = a_file
648650

@@ -722,6 +724,10 @@ def read(self, a_file=None):
722724
else:
723725
buffering = '0@128'
724726

727+
if self.encoding_to_remove is not None:
728+
if enc == self.encoding_to_remove:
729+
enc = ''
730+
725731
# Append the parsed values to the reading stations list
726732
station_info = [
727733
name, url, enc, {'image': icon} if icon else '',
@@ -760,6 +766,9 @@ def _format_playlist_row(self, a_row):
760766
# Extract the 'image' from the icon dictionary if present
761767
if len(this_row) > Station.icon and 'image' in this_row[Station.icon]:
762768
this_row[Station.icon] = this_row[Station.icon]['image']
769+
if self.encoding_to_remove is not None:
770+
if this_row[Station.encoding] == self.encoding_to_remove:
771+
this_row[Station.encoding] = ''
763772

764773
if len(this_row) > Station.buffering:
765774
if this_row[Station.buffering] == '0@128':

pyradio/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ def read_playlist_file(
718718
self._read_playlist_version = self._playlist_version = Station.url
719719
self._reading_stations = []
720720
csv_in = CsvReadWrite(a_file=stationFile)
721+
csv_in.encoding_to_remove = self.default_encoding
721722
ret = csv_in.read()
722723
if not ret:
723724
self._reading_stations = []
@@ -855,6 +856,7 @@ def save_playlist_file(self, stationFile=''):
855856
return 0
856857

857858
out_csv = CsvReadWrite(st_file)
859+
out_csv.encoding_to_remove = self.default_encoding
858860
ret = out_csv.write(items=self.stations)
859861
if ret == 0:
860862
self.dirty_playlist = False

pyradio/config_window.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,14 +2514,18 @@ class PyRadioSelectEncodings():
25142514
_invalid = []
25152515

25162516
def __init__(self, maxY, maxX, encoding, config_encoding,
2517-
global_functions=None):
2517+
global_functions=None, show_default=False):
25182518
self._parent_maxY = maxY
25192519
self._parent_maxX = maxX
25202520
self.encoding = encoding
2521+
logger.error('I got encoding = "{}"'.format(encoding))
25212522
self._orig_encoding = encoding
25222523
self._config_encoding = config_encoding
25232524
self._orig_encoding = encoding
25242525
self._encodings = get_encodings()
2526+
self._show_default = show_default
2527+
if show_default:
2528+
self._encodings += [['Default', '', 'Use the encoding set in the config']]
25252529
self._num_of_rows = int(len(self._encodings) / self._num_of_columns)
25262530
self.init_window()
25272531

@@ -2695,11 +2699,17 @@ def _resize(self, init=False):
26952699
self.refresh_selection()
26962700

26972701
def setEncoding(self, this_encoding, init=False):
2702+
logger.error(f'{this_encoding = }')
26982703
ret = self._is_encoding(this_encoding)
26992704
if ret == -1:
2700-
if logger.isEnabledFor(logging.ERROR):
2701-
logger.error('encoding "{}" not found, reverting to "utf-8"'.format(this_encoding))
2702-
self.encoding = 'utf-8'
2705+
if self._show_default:
2706+
if logger.isEnabledFor(logging.ERROR):
2707+
logger.error('encoding "{}" not found, reverting to "Default"'.format(this_encoding))
2708+
self.encoding = 'Default'
2709+
else:
2710+
if logger.isEnabledFor(logging.ERROR):
2711+
logger.error('encoding "{}" not found, reverting to "utf-8"'.format(this_encoding))
2712+
self.encoding = 'utf-8'
27032713
self.selection = self._is_encoding(self.encoding)
27042714
else:
27052715
self.selection = ret
@@ -2714,7 +2724,8 @@ def in_alias(a_list, a_string):
27142724
return True
27152725
return False
27162726
for i, an_encoding in enumerate(self._encodings):
2717-
if a_string == an_encoding[0] or in_alias(an_encoding[1], a_string):
2727+
if a_string == an_encoding[0] or \
2728+
in_alias(an_encoding[1], a_string):
27182729
return i
27192730
return -1
27202731

pyradio/edit.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,14 +750,15 @@ def _show_extra_fields(self):
750750
col[self._focus-sid] = 9
751751
except IndexError:
752752
pass
753+
disp_encoding = 'Default' if self._encoding == self._config_encoding else self._encoding
753754
logger.error(f'{self.item = }')
754755
logger.error(f'{self._volume = }')
755756
self._win.addstr(7, 2, 'Volume:', curses.color_pair(4))
756757
self._win.addstr(' ' + f'{self._volume:>3}' + ' ', curses.color_pair(col[0]))
757758
self._win.addstr(' ' + 'Buffering:', curses.color_pair(4))
758759
self._win.addstr(' ' + f'{self._buff:>2}' + ' ', curses.color_pair(col[1]))
759760
self._win.addstr(' ' + 'Encoding:', curses.color_pair(4))
760-
self._win.addstr(' ' + self._encoding + ' ', curses.color_pair(col[2]))
761+
self._win.addstr(' ' + disp_encoding + ' ', curses.color_pair(col[2]))
761762
# delete to end of line
762763
y, x = self._win.getyx()
763764
self._win.addstr((self.maxX - x -1) * ' ', curses.color_pair(5))
@@ -799,6 +800,8 @@ def _display_field_tip(self):
799800
self._win.addstr(12, x, 'Tip: ', curses.color_pair(4))
800801
self._win.addstr('Press ', curses.color_pair(5))
801802
self._win.addstr('Enter ', curses.color_pair(4))
803+
self._win.addstr('or ', curses.color_pair(5))
804+
self._win.addstr('Space ', curses.color_pair(4))
802805
self._win.addstr('to activate', curses.color_pair(5))
803806
elif self._focus == 7:
804807
''' Tip: Press \\p before pasting here '''
@@ -888,10 +891,14 @@ def _return_station(self):
888891
sp = buff.split('@')
889892
sp[0] = self._buff
890893
buff = '@'.join(sp)
894+
if self._encoding in ('Default', self._config_encoding):
895+
encoding = ''
896+
else:
897+
encoding = self._encoding
891898
self.new_station = [
892899
self._line_editor[0].string.strip(),
893900
self._line_editor[1].string.strip(),
894-
self._encoding,
901+
encoding,
895902
{'image': self._line_editor[2].string.strip()},
896903
self._profile,
897904
buff,

pyradio/radio.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ def __init__(self, pyradio_config,
679679
self.ws.DELETE_PLAYLIST_MODE: self._ask_to_delete_playlist,
680680
self.ws.KEYBOARD_CONFIG_MODE: self._redisplay_keyboard_config,
681681
self.ws.LOCALIZED_CONFIG_MODE: self._redisplay_localized_config,
682-
self.ws.EDIT_PROFILE_MODE: self._redisplay_profile_config,
682+
self.ws.EDIT_PROFILE_MODE: self._redisplay_profile_editor,
683683
}
684684

685685
self._help_keys = {
@@ -2625,7 +2625,8 @@ def playSelection(self, restart=False):
26252625
self._last_played_station_id = self.selection
26262626
with self._check_lock:
26272627
self._station_to_check_id = self.selection
2628-
logger.error('********* checking {} : {} ********\n'.format(self._station_to_check_id, self.stations[self._station_to_check_id]))
2628+
if logger.isEnabledFor(logging.INFO):
2629+
logger.info('\n*********\nstarting id = {}\n:{}\n********\n'.format(self._station_to_check_id, self.stations[self._station_to_check_id]))
26292630
# logger.error('setting playing to {}'.format(self.selection))
26302631
self.playing = self.selection
26312632
if not stream_url:
@@ -5653,7 +5654,7 @@ def _localized_init_config(self, parent=None):
56535654
)
56545655
self._keyboard_localized_win.show(parent=self.outerBodyWin)
56555656

5656-
def _redisplay_profile_config(self):
5657+
def _redisplay_profile_editor(self):
56575658
if self._station_profile_editor is None:
56585659
profiles = ['Default'] + self._cnf.profile_manager.all_profiles()
56595660
profile = self._station_editor.profile
@@ -8128,19 +8129,27 @@ def keypress(self, char):
81288129
''' display line editor help '''
81298130
self._show_line_editor_help()
81308131
elif ret == 3:
8132+
logger.error('\n\nHERE\n\n')
81318133
''' show encoding '''
8132-
if self._station_editor._encoding == '':
8133-
self._station_editor._encoding = self._cnf.default_encoding
8134+
if self._station_editor._encoding == '' or \
8135+
self._station_editor._encoding == self._cnf.default_encoding:
8136+
self._station_editor._encoding = 'Default'
81348137
self.ws.operation_mode = self.ws.EDIT_STATION_ENCODING_MODE
8135-
self._encoding_select_win = PyRadioSelectEncodings(self.outerBodyMaxY,
8136-
self.outerBodyMaxX, self._station_editor._encoding, self._cnf.default_encoding)
8138+
self._encoding_select_win = PyRadioSelectEncodings(
8139+
self.outerBodyMaxY,
8140+
self.outerBodyMaxX,
8141+
self._station_editor._encoding,
8142+
self._cnf.default_encoding,
8143+
show_default=True
8144+
)
8145+
logger.error('{}'.format(self._station_editor._encoding))
81378146
self._encoding_select_win.set_reduced_global_functions(self._global_functions)
81388147
self._encoding_select_win.init_window()
81398148
self._encoding_select_win.refresh_win()
81408149
self._encoding_select_win.setEncoding(self._station_editor._encoding)
81418150
elif ret == 5:
81428151
self.ws.operation_mode = self.ws.EDIT_PROFILE_MODE
8143-
self._redisplay_profile_config()
8152+
self._redisplay_profile_editor()
81448153
return
81458154

81468155
elif self.ws.operation_mode in (self.ws.RENAME_PLAYLIST_MODE, self.ws.CREATE_PLAYLIST_MODE):

0 commit comments

Comments
 (0)