Skip to content

Commit

Permalink
Version 0.7.6
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
s-n-g committed Jun 1, 2019
1 parent c5219d3 commit 1fa9a60
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 54 deletions.
7 changes: 7 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2019-05-27 s-n-g
* 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)

2019-04-18 s-n-g
* Version 0.7.5
* Minimum python version supported changed. Now it's 2.7+/3.5+
Expand Down
3 changes: 2 additions & 1 deletion README.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ <h3 id="managing-playlists-within-pyradio">Managing playlists (within PyRadio)</
<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>
<p>Currently, the following actions are available:</p>
<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>
<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>
<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>
<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>
<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>
Expand All @@ -180,7 +181,7 @@ <h3 id="station-by-station-encoding-declaration">Station by station encoding dec
<p>Finally, we insert the new station to the playlist:</p>
<pre>Station1,Station1_URL,
Station2,Station2_URL,iso-8859-7</pre>
<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>
<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>
<h3 id="global-encoding-declaration">Global encoding declaration</h3>
<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>
<p>Setting this parameter to a different encoding, will permit <strong>PyRadio</strong> to successfully decode such stations.</p>
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ Currently, the following actions are available:

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***".

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).

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).

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.
Expand Down Expand Up @@ -237,7 +239,7 @@ Station2,Station2_URL,iso-8859-7
```

**Note:**
Using the ***-a*** command line option will save you all this trouble, as it will automatically take care of creating a valid ***CSV*** file.
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**.

### Global encoding declaration

Expand Down
7 changes: 5 additions & 2 deletions pyradio.1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.\" Copyright (C) 2011 Ben Dowling <http://www.coderholic.com/pyradio>
.\" This manual is freely distributable under the terms of the GPL.
.\"
.TH PYRADIO 1 "April 2019"
.TH PYRADIO 1 "June 2019"

.SH NAME
.PP
Expand Down Expand Up @@ -178,6 +178,8 @@ Currently, the following actions are available:

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".

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).

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).

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.
Expand Down Expand Up @@ -248,7 +250,8 @@ Finally, we insert the new station to the playlist:
Station2\fB,\fIStation2_URL\fB,\fIiso-8859-7

.IP \fBNote\fR
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.
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.


.PP
.B
Expand Down
2 changes: 1 addition & 1 deletion pyradio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
" pyradio -- Console radio player. "

version_info = (0, 7, 5)
version_info = (0, 7, 6)

__version__ = version = '.'.join(map(str, version_info))
__project__ = __name__
Expand Down
20 changes: 12 additions & 8 deletions pyradio/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
SELECT_ENCODING_MODE = 6
SELECT_PLAYLIST_MODE = 7
SELECT_STATION_MODE = 8
SELECT_STATION_ENCODING_MODE = 9
REMOVE_STATION_MODE = 50
SAVE_PLAYLIST_MODE = 51
ASK_TO_SAVE_PLAYLIST_MODE = 52
ASK_TO_SAVE_PLAYLIST_WHEN_OPENING_PLAYLIST_MODE = 52
ASK_TO_SAVE_PLAYLIST_WHEN_EXITING_MODE = 53
MAIN_HELP_MODE = 100
PLAYLIST_HELP_MODE = 101
CONFIG_HELP_MODE = 102
Expand All @@ -21,13 +23,15 @@
SELECT_ENCODING_HELP_MODE = 105
SELECT_PLAYLIST_HELP_MODE = 106
SELECT_STATION_HELP_MODE = 107
PLAYLIST_LOAD_ERROR_MODE = 200
PLAYLIST_RELOAD_ERROR_MODE = 201
PLAYLIST_RELOAD_CONFIRM_MODE = 202
PLAYLIST_DIRTY_RELOAD_CONFIRM_MODE = 203
PLAYLIST_SCAN_ERROR_MODE = 204
SAVE_PLAYLIST_ERROR_1_MODE = 205
SAVE_PLAYLIST_ERROR_2_MODE = 206
PLAYLIST_RECOVERY_ERROR_MODE = 200
PLAYLIST_NOT_FOUND_ERROR_MODE = 201
PLAYLIST_LOAD_ERROR_MODE = 202
PLAYLIST_RELOAD_ERROR_MODE = 203
PLAYLIST_RELOAD_CONFIRM_MODE = 204
PLAYLIST_DIRTY_RELOAD_CONFIRM_MODE = 205
PLAYLIST_SCAN_ERROR_MODE = 206
SAVE_PLAYLIST_ERROR_1_MODE = 207
SAVE_PLAYLIST_ERROR_2_MODE = 208
FOREIGN_PLAYLIST_ASK_MODE = 300
FOREIGN_PLAYLIST_MESSAGE_MODE = 301
FOREIGN_PLAYLIST_COPY_ERROR_MODE = 302
Expand Down
65 changes: 59 additions & 6 deletions pyradio/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import glob
import curses
import collections
from os import path, getenv, makedirs, remove
from os import path, getenv, makedirs, remove, rename
from time import ctime
from datetime import datetime
from shutil import copyfile, move
Expand Down Expand Up @@ -43,6 +43,8 @@ class PyRadioStations(object):

dirty_playlist = False

playlist_recovery_result = 0

def __init__(self, stationFile=''):

if sys.platform.startswith('win'):
Expand Down Expand Up @@ -149,6 +151,7 @@ def _get_playlist_abspath_from_data(self, stationFile=''):
-2 - playlist not found
-3 - negative number specified
-4 - number not found
-8 - file type not supported
"""
ret = -1
orig_input = stationFile
Expand All @@ -158,6 +161,8 @@ def _get_playlist_abspath_from_data(self, stationFile=''):
stationFile = path.abspath(stationFile)
else:
""" try to find it in config dir """
if path.exists(stationFile):
return '', -8
stationFile += '.csv'
stationFile = path.join(self.stations_dir, stationFile)
if path.exists(stationFile) and path.isfile(stationFile):
Expand Down Expand Up @@ -196,16 +201,24 @@ def read_playlist_file(self, stationFile=''):
x - number of stations or
-1 - playlist is malformed
-2 - playlist not found
-7 - playlist recovery failed
-8 - file not supported
"""
prev_file = self.stations_file
prev_format = self.new_format
self.new_format = False

ret = 0
stationFile, ret = self._get_playlist_abspath_from_data(stationFile)
if ret < 0:
# returns -2, -3, -4 or -8
return ret

self.playlist_recovery_result = self._recover_backed_up_playlist(stationFile)
if self.playlist_recovery_result > 0:
# playlist recovery failed
# reason in cnf.playlist_recovery_result
return -7
prev_file = self.stations_file
prev_format = self.new_format
self.new_format = False
self._reading_stations = []
with open(stationFile, 'r') as cfgfile:
try:
Expand All @@ -214,10 +227,10 @@ def read_playlist_file(self, stationFile=''):
continue
try:
name, url = [s.strip() for s in row]
self._reading_stations.append((name, url, ''))
self._reading_stations.append([name, url, ''])
except:
name, url, enc = [s.strip() for s in row]
self._reading_stations.append((name, url, enc))
self._reading_stations.append([name, url, enc])
self.new_format = True
except:
self._reading_stations = []
Expand All @@ -238,6 +251,46 @@ def read_playlist_file(self, stationFile=''):
logger.debug('Playlist is in old format')
return self.number_of_stations

def _recover_backed_up_playlist(self, stationFile):
""" If a playlist backup file exists (.txt file), try to
recover it (rename it to .csv)
Return:
-1: playlist recovered
0: no back up file found
1: remove (empty) csv file failed
2: rename txt to csv failed """
backup_stationFile = stationFile.replace('.csv', '.txt')
if path.isfile(backup_stationFile):
if logging:
if logger.isEnabledFor(logging.INFO):
logger.info('Trying to recover backed up playlist: "{}"'.format(backup_stationFile))
else:
print('Trying to recover backed up playlist:\n "{}"'.format(backup_stationFile))


if path.isfile(stationFile):
try:
remove(stationFile)
except:
# remove failed
if logger.isEnabledFor(logging.INFO):
logger.info('Playlist recovery failed: Cannot remove CSV file')
return 1
try:
rename(backup_stationFile, stationFile)
except:
# rename failed
if logger.isEnabledFor(logging.INFO):
logger.info('Playlist recovery failed: Cannot rename TXT file to CSV')
return 2
# playlist recovered
if logger.isEnabledFor(logging.INFO):
logger.info('Playlist recovery successful!!!')
return -1
# no playlist back up found
return 0

def _playlist_format_changed(self):
""" Check if we have new or old format
and report if format has changed
Expand Down
20 changes: 20 additions & 0 deletions pyradio/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,26 @@ def print_playlist_selection_error(a_selection, cnf, ret, exit_if_malformed=True
elif ret == -6:
print('Error: Failed to rename playlist')
sys.exit(1)
elif ret == -7:
print('Error: Playlist recovery failed!\n')
if cnf.playlist_recovery_result == 1:
msg = """Both a playlist file (CSV) and a playlist backup file (TXT)
exist for the selected playlist. In this case, PyRadio would
try to delete the CSV file, and then rename the TXT file to CSV.\n
Unfortunately, deleting the CSV file has failed, so you have to
manually address the issue."""
else:
msg = """A playlist backup file (TXT) has been found for the selected
playlist. In this case, PyRadio would try to rename this file
to CSV.\n
Unfortunately, renaming this file has failed, so you have to
manually address the issue."""
print(msg)
#open_conf_dir(cnf)
sys.exit(1)
elif ret == -8:
print('File type not supported')
sys.exit(1)

def open_conf_dir(cnf):
import subprocess
Expand Down
Loading

0 comments on commit 1fa9a60

Please sign in to comment.