Skip to content

Commit 1fa9a60

Browse files
committed
Version 0.7.6
- Added "e" option to change station's encoding. - Implemented playlist backup and recovery, to address saving errors. - Inform user when playlist not found and of playlist recovery result. - Parameter -s - Check if file is supported (ends with .csv)
1 parent c5219d3 commit 1fa9a60

File tree

10 files changed

+324
-54
lines changed

10 files changed

+324
-54
lines changed

Changelog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2019-05-27 s-n-g
2+
* Version 0.7.6
3+
* Added "e" option to change station's encoding.
4+
* Implemented playlist backup and recovery, to address saving errors.
5+
* Inform user when playlist not found and of playlist recovery result.
6+
* Parameter -s - Check if file is supported (ends with .csv)
7+
18
2019-04-18 s-n-g
29
* Version 0.7.5
310
* Minimum python version supported changed. Now it's 2.7+/3.5+

README.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ <h3 id="managing-playlists-within-pyradio">Managing playlists (within PyRadio)</
154154
<p>Once <strong>PyRadio</strong> has been loaded, one can perform a series of actions on the current playlist and set of playlists saved in its configuration directory.</p>
155155
<p>Currently, the following actions are available:</p>
156156
<p>One thing you may want to do is remove a station from a playlist, e.g. when found that it not longer works. You can do that by pressing “<strong><em>DEL</em></strong>” or “<strong><em>x</em></strong>”.</p>
157+
<p>Pressing “<strong><em>e</em></strong>” will enable you to change the encoding of the selected station. If the station is currently playing, playback will be restarted so that the encoding’s change takes effect (hopefully correctly displaying the station/song title).</p>
157158
<p>Then, when this is done, you can either save the modified playlist, by pressing “<strong><em>s</em></strong>”, or reload the playlist from disk, by pressing “<strong><em>R</em></strong>”. A modified playlist will automatically be saved when <strong>PyRadio</strong> exits (or Ctrl-C is pressed).</p>
158159
<p>Finally, opening another playlist is also possible. Just press “<strong><em>o</em></strong>” and you will be presented with a list of saved playlists to choose from. These playlists must be saved beforehand in <strong>PyRadio</strong>’s configuration directory.</p>
159160
<p>While executing any of the previous actions, you may get confirmation messages (when opening a playlist while the current one is modified but not saved, for example) or error messages (when an action fails). Just follow the on screen information, keeping in mind that a capital letter as an answer will save this answer in <strong>PyRadio</strong>’s configuration file for future reference.</p>
@@ -180,7 +181,7 @@ <h3 id="station-by-station-encoding-declaration">Station by station encoding dec
180181
<p>Finally, we insert the new station to the playlist:</p>
181182
<pre>Station1,Station1_URL,
182183
Station2,Station2_URL,iso-8859-7</pre>
183-
<p style="margin: 1.5em 4em 0 4em; text-indent: -2.5em;"><strong>Note:</strong> Using the <strong><em>-a</em></strong> command line option will save you all this trouble, as it will automatically take care of creating a valid <strong><em>CSV</em></strong> file.</p>
184+
<p style="margin: 1.5em 4em 0 4em; text-indent: -2.5em;"><strong>Note:</strong> Using the <strong><em>-a</em></strong> command line option will save you all this trouble, as it will automatically take care of creating a valid <strong><em>CSV</em></strong> file. Alternatively, you can change the selected station’s encoding by pressing “<strong><em>e</em></strong>” while in <strong>PyRadio</strong>.</p>
184185
<h3 id="global-encoding-declaration">Global encoding declaration</h3>
185186
<p><strong>PyRadio</strong>’s configuration file contains the parameter <strong><em>default_encoding</em></strong>, which by default is set to <strong>utf-8</strong>.</p>
186187
<p>Setting this parameter to a different encoding, will permit <strong>PyRadio</strong> to successfully decode such stations.</p>

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ Currently, the following actions are available:
177177

178178
One thing you may want to do is remove a station from a playlist, e.g. when found that it not longer works. You can do that by pressing "***DEL***" or "***x***".
179179

180+
Pressing "***e***" will enable you to change the encoding of the selected station. If the station is currently playing, playback will be restarted so that the encoding's change takes effect (hopefully correctly displaying the station/song title).
181+
180182
Then, when this is done, you can either save the modified playlist, by pressing "***s***", or reload the playlist from disk, by pressing "***R***". A modified playlist will automatically be saved when **PyRadio** exits (or Ctrl-C is pressed).
181183

182184
Finally, opening another playlist is also possible. Just press "***o***" and you will be presented with a list of saved playlists to choose from. These playlists must be saved beforehand in **PyRadio**'s configuration directory.
@@ -237,7 +239,7 @@ Station2,Station2_URL,iso-8859-7
237239
```
238240

239241
**Note:**
240-
Using the ***-a*** command line option will save you all this trouble, as it will automatically take care of creating a valid ***CSV*** file.
242+
Using the ***-a*** command line option will save you all this trouble, as it will automatically take care of creating a valid ***CSV*** file. Alternatively, you can change the selected station's encoding by pressing "***e***" while in **PyRadio**.
241243

242244
### Global encoding declaration
243245

pyradio.1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.\" Copyright (C) 2011 Ben Dowling <http://www.coderholic.com/pyradio>
22
.\" This manual is freely distributable under the terms of the GPL.
33
.\"
4-
.TH PYRADIO 1 "April 2019"
4+
.TH PYRADIO 1 "June 2019"
55

66
.SH NAME
77
.PP
@@ -178,6 +178,8 @@ Currently, the following actions are available:
178178

179179
One thing you may want to do is remove a station from a playlist, e.g. when found that it not longer works. You can do that by pressing "\fIDEL\fR" or "\fIx\fR".
180180

181+
Pressing "\fIe\fR" will enable you to change the encoding of the selected station. If the station is currently playing, playback will be restarted so that the encoding's change takes effect (hopefully correctly displaying the station/song title).
182+
181183
Then, when this is done, you can either save the modified playlist, by pressing "\fIs\fR", or reload the playlist from disk, by pressing "\fIR\fR". A modified playlist will \fIautomatically\fR be saved when \fBpyradio\fR exits (or Ctrl-C is pressed).
182184

183185
Finally, opening another playlist is also possible. Just press "\fIo\fR" and you will be presented with a list of saved playists to choose from. These playlists must be saved beforehand in \fBpyradio\fR's configuration directory.
@@ -248,7 +250,8 @@ Finally, we insert the new station to the playlist:
248250
Station2\fB,\fIStation2_URL\fB,\fIiso-8859-7
249251

250252
.IP \fBNote\fR
251-
Using the \fB-a\fR command line option will save you all this trouble, as it will automatically take care of creating a valid \fBCSV\fR file.
253+
Using the \fB-a\fR command line option will save you all this trouble, as it will automatically take care of creating a valid \fBCSV\fR file. Alternatively, you can change the selected station's encoding by pressing "\fIe\fR" while in \fBpyradio\fR.
254+
252255

253256
.PP
254257
.B

pyradio/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" pyradio -- Console radio player. "
22

3-
version_info = (0, 7, 5)
3+
version_info = (0, 7, 6)
44

55
__version__ = version = '.'.join(map(str, version_info))
66
__project__ = __name__

pyradio/common.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
SELECT_ENCODING_MODE = 6
1111
SELECT_PLAYLIST_MODE = 7
1212
SELECT_STATION_MODE = 8
13+
SELECT_STATION_ENCODING_MODE = 9
1314
REMOVE_STATION_MODE = 50
1415
SAVE_PLAYLIST_MODE = 51
15-
ASK_TO_SAVE_PLAYLIST_MODE = 52
16+
ASK_TO_SAVE_PLAYLIST_WHEN_OPENING_PLAYLIST_MODE = 52
17+
ASK_TO_SAVE_PLAYLIST_WHEN_EXITING_MODE = 53
1618
MAIN_HELP_MODE = 100
1719
PLAYLIST_HELP_MODE = 101
1820
CONFIG_HELP_MODE = 102
@@ -21,13 +23,15 @@
2123
SELECT_ENCODING_HELP_MODE = 105
2224
SELECT_PLAYLIST_HELP_MODE = 106
2325
SELECT_STATION_HELP_MODE = 107
24-
PLAYLIST_LOAD_ERROR_MODE = 200
25-
PLAYLIST_RELOAD_ERROR_MODE = 201
26-
PLAYLIST_RELOAD_CONFIRM_MODE = 202
27-
PLAYLIST_DIRTY_RELOAD_CONFIRM_MODE = 203
28-
PLAYLIST_SCAN_ERROR_MODE = 204
29-
SAVE_PLAYLIST_ERROR_1_MODE = 205
30-
SAVE_PLAYLIST_ERROR_2_MODE = 206
26+
PLAYLIST_RECOVERY_ERROR_MODE = 200
27+
PLAYLIST_NOT_FOUND_ERROR_MODE = 201
28+
PLAYLIST_LOAD_ERROR_MODE = 202
29+
PLAYLIST_RELOAD_ERROR_MODE = 203
30+
PLAYLIST_RELOAD_CONFIRM_MODE = 204
31+
PLAYLIST_DIRTY_RELOAD_CONFIRM_MODE = 205
32+
PLAYLIST_SCAN_ERROR_MODE = 206
33+
SAVE_PLAYLIST_ERROR_1_MODE = 207
34+
SAVE_PLAYLIST_ERROR_2_MODE = 208
3135
FOREIGN_PLAYLIST_ASK_MODE = 300
3236
FOREIGN_PLAYLIST_MESSAGE_MODE = 301
3337
FOREIGN_PLAYLIST_COPY_ERROR_MODE = 302

pyradio/config.py

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import glob
66
import curses
77
import collections
8-
from os import path, getenv, makedirs, remove
8+
from os import path, getenv, makedirs, remove, rename
99
from time import ctime
1010
from datetime import datetime
1111
from shutil import copyfile, move
@@ -43,6 +43,8 @@ class PyRadioStations(object):
4343

4444
dirty_playlist = False
4545

46+
playlist_recovery_result = 0
47+
4648
def __init__(self, stationFile=''):
4749

4850
if sys.platform.startswith('win'):
@@ -149,6 +151,7 @@ def _get_playlist_abspath_from_data(self, stationFile=''):
149151
-2 - playlist not found
150152
-3 - negative number specified
151153
-4 - number not found
154+
-8 - file type not supported
152155
"""
153156
ret = -1
154157
orig_input = stationFile
@@ -158,6 +161,8 @@ def _get_playlist_abspath_from_data(self, stationFile=''):
158161
stationFile = path.abspath(stationFile)
159162
else:
160163
""" try to find it in config dir """
164+
if path.exists(stationFile):
165+
return '', -8
161166
stationFile += '.csv'
162167
stationFile = path.join(self.stations_dir, stationFile)
163168
if path.exists(stationFile) and path.isfile(stationFile):
@@ -196,16 +201,24 @@ def read_playlist_file(self, stationFile=''):
196201
x - number of stations or
197202
-1 - playlist is malformed
198203
-2 - playlist not found
204+
-7 - playlist recovery failed
205+
-8 - file not supported
199206
"""
200-
prev_file = self.stations_file
201-
prev_format = self.new_format
202-
self.new_format = False
203207

204208
ret = 0
205209
stationFile, ret = self._get_playlist_abspath_from_data(stationFile)
206210
if ret < 0:
211+
# returns -2, -3, -4 or -8
207212
return ret
208213

214+
self.playlist_recovery_result = self._recover_backed_up_playlist(stationFile)
215+
if self.playlist_recovery_result > 0:
216+
# playlist recovery failed
217+
# reason in cnf.playlist_recovery_result
218+
return -7
219+
prev_file = self.stations_file
220+
prev_format = self.new_format
221+
self.new_format = False
209222
self._reading_stations = []
210223
with open(stationFile, 'r') as cfgfile:
211224
try:
@@ -214,10 +227,10 @@ def read_playlist_file(self, stationFile=''):
214227
continue
215228
try:
216229
name, url = [s.strip() for s in row]
217-
self._reading_stations.append((name, url, ''))
230+
self._reading_stations.append([name, url, ''])
218231
except:
219232
name, url, enc = [s.strip() for s in row]
220-
self._reading_stations.append((name, url, enc))
233+
self._reading_stations.append([name, url, enc])
221234
self.new_format = True
222235
except:
223236
self._reading_stations = []
@@ -238,6 +251,46 @@ def read_playlist_file(self, stationFile=''):
238251
logger.debug('Playlist is in old format')
239252
return self.number_of_stations
240253

254+
def _recover_backed_up_playlist(self, stationFile):
255+
""" If a playlist backup file exists (.txt file), try to
256+
recover it (rename it to .csv)
257+
258+
Return:
259+
-1: playlist recovered
260+
0: no back up file found
261+
1: remove (empty) csv file failed
262+
2: rename txt to csv failed """
263+
backup_stationFile = stationFile.replace('.csv', '.txt')
264+
if path.isfile(backup_stationFile):
265+
if logging:
266+
if logger.isEnabledFor(logging.INFO):
267+
logger.info('Trying to recover backed up playlist: "{}"'.format(backup_stationFile))
268+
else:
269+
print('Trying to recover backed up playlist:\n "{}"'.format(backup_stationFile))
270+
271+
272+
if path.isfile(stationFile):
273+
try:
274+
remove(stationFile)
275+
except:
276+
# remove failed
277+
if logger.isEnabledFor(logging.INFO):
278+
logger.info('Playlist recovery failed: Cannot remove CSV file')
279+
return 1
280+
try:
281+
rename(backup_stationFile, stationFile)
282+
except:
283+
# rename failed
284+
if logger.isEnabledFor(logging.INFO):
285+
logger.info('Playlist recovery failed: Cannot rename TXT file to CSV')
286+
return 2
287+
# playlist recovered
288+
if logger.isEnabledFor(logging.INFO):
289+
logger.info('Playlist recovery successful!!!')
290+
return -1
291+
# no playlist back up found
292+
return 0
293+
241294
def _playlist_format_changed(self):
242295
""" Check if we have new or old format
243296
and report if format has changed

pyradio/main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,26 @@ def print_playlist_selection_error(a_selection, cnf, ret, exit_if_malformed=True
198198
elif ret == -6:
199199
print('Error: Failed to rename playlist')
200200
sys.exit(1)
201+
elif ret == -7:
202+
print('Error: Playlist recovery failed!\n')
203+
if cnf.playlist_recovery_result == 1:
204+
msg = """Both a playlist file (CSV) and a playlist backup file (TXT)
205+
exist for the selected playlist. In this case, PyRadio would
206+
try to delete the CSV file, and then rename the TXT file to CSV.\n
207+
Unfortunately, deleting the CSV file has failed, so you have to
208+
manually address the issue."""
209+
else:
210+
msg = """A playlist backup file (TXT) has been found for the selected
211+
playlist. In this case, PyRadio would try to rename this file
212+
to CSV.\n
213+
Unfortunately, renaming this file has failed, so you have to
214+
manually address the issue."""
215+
print(msg)
216+
#open_conf_dir(cnf)
217+
sys.exit(1)
218+
elif ret == -8:
219+
print('File type not supported')
220+
sys.exit(1)
201221

202222
def open_conf_dir(cnf):
203223
import subprocess

0 commit comments

Comments
 (0)