Skip to content

Commit 3277d14

Browse files
committed
- FIX: Playlists flagged as changed when adding a station
- Preparing for online stations browser
1 parent 44be7ab commit 3277d14

File tree

5 files changed

+85
-49
lines changed

5 files changed

+85
-49
lines changed

Changelog

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2019-10-29 s-n-g
2+
* FIX: Playlists flagged as changed when adding a station
3+
* Handling CJK presentation on station and playlist view
4+
* Preparing for online stations browser
5+
16
2019-10-23 s-n-g
27
* Handling CJK presentation on station and playlist window
38

pyradio/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" pyradio -- Console radio player. "
22

3-
version_info = (0, 8, 2)
3+
version_info = (0, 8, 3)
44

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

pyradio/config.py

+75-41
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ class PyRadioStations(object):
3838
selected_playlist = -1
3939
number_of_stations = -1
4040

41-
""" new_format: True: 3 columns (name,URL,encoding)
42-
new_format: False: 2 columns (name,URL) """
43-
new_format = False
41+
""" playlist_version:
42+
2: 4 columns (name,URL,encoding,online browser)
43+
1: 3 columns (name,URL,encoding)
44+
0: 2 columns (name,URL)
45+
"""
46+
BASIC_PLAYLIST = 0
47+
ENCODING_PLAYLIST = 1
48+
ONLINE_PLAYLIST = 2
49+
_playlist_version = BASIC_PLAYLIST
50+
_read_playlist_version = BASIC_PLAYLIST
4451

4552
dirty_playlist = False
4653

@@ -90,6 +97,14 @@ def __init__(self, stationFile=''):
9097
self._move_old_csv(self.stations_dir)
9198
self._check_stations_csv(self.stations_dir, self.root_path)
9299

100+
@property
101+
def playlist_version(self):
102+
return self._playlist_version
103+
104+
@playlist_version.setter
105+
def playlist_version(self, value):
106+
raise ValueError('parameter is read only')
107+
93108
def _move_old_csv(self, usr):
94109
""" if a ~/.pyradio files exists, relocate it in user
95110
config folder and rename it to stations.csv, or if
@@ -238,8 +253,8 @@ def read_playlist_file(self, stationFile=''):
238253
# reason in cnf.playlist_recovery_result
239254
return -7
240255
prev_file = self.stations_file
241-
prev_format = self.new_format
242-
self.new_format = False
256+
prev_format = self._playlist_version
257+
self._read_playlist_version = self._playlist_version = self.BASIC_PLAYLIST
243258
self._reading_stations = []
244259
with eval(self._open_string[self._open_string_id]) as cfgfile:
245260
try:
@@ -248,14 +263,19 @@ def read_playlist_file(self, stationFile=''):
248263
continue
249264
try:
250265
name, url = [s.strip() for s in row]
251-
self._reading_stations.append([name, url, ''])
266+
self._reading_stations.append([name, url, '', ''])
252267
except:
253-
name, url, enc = [s.strip() for s in row]
254-
self._reading_stations.append([name, url, enc])
255-
self.new_format = True
268+
try:
269+
name, url, enc = [s.strip() for s in row]
270+
self._reading_stations.append([name, url, enc, ''])
271+
self._read_playlist_version = self._playlist_version = self.ENCODING_PLAYLIST
272+
except:
273+
name, url, enc, onl = [s.strip() for s in row]
274+
self._reading_stations.append([name, url, enc, onl])
275+
self._read_playlist_version = self._playlist_version = self.ONLINE_PLAYLIST
256276
except:
257277
self._reading_stations = []
258-
self.new_format = prev_format
278+
self._playlist_version = prev_format
259279
return -1
260280

261281
self.stations = list(self._reading_stations)
@@ -266,10 +286,7 @@ def read_playlist_file(self, stationFile=''):
266286
self.number_of_stations = len(self.stations)
267287
self.dirty_playlist = False
268288
if logger.isEnabledFor(logging.DEBUG):
269-
if self.new_format:
270-
logger.debug('Playlist is in new format')
271-
else:
272-
logger.debug('Playlist is in old format')
289+
logger.debug('read_playlist_file: Playlist version: {}'.format(self._playlist_version))
273290
self.jump_tag = -1
274291
return self.number_of_stations
275292

@@ -321,15 +338,24 @@ def _playlist_format_changed(self):
321338
Format type can change by editing encoding,
322339
deleting a non-utf-8 station etc.
323340
"""
324-
new_format = False
341+
playlist_version = self.BASIC_PLAYLIST
325342
for n in self.stations:
326-
if n[2] != '':
327-
new_format = True
343+
if n[3] != '':
344+
playlist_version = self.ONLINE_PLAYLIST
328345
break
329-
if self.new_format == new_format:
330-
return False
346+
if playlist_version == self.BASIC_PLAYLIST:
347+
for n in self.stations:
348+
if n[2] != '':
349+
playlist_version = self.ENCODING_PLAYLIST
350+
break
351+
if self._playlist_version == playlist_version:
352+
ret = False
331353
else:
332-
return True
354+
self._playlist_version = playlist_version
355+
ret = True
356+
if logger.isEnabledFor(logging.DEBUG):
357+
logger.debug('_playlist_format_changed: Playlist version: {}'.format(self._playlist_version))
358+
return ret
333359

334360
def save_playlist_file(self, stationFile=''):
335361
""" Save a playlist
@@ -342,12 +368,13 @@ def save_playlist_file(self, stationFile=''):
342368
"""
343369
if self._playlist_format_changed():
344370
self.dirty_playlist = True
345-
self.new_format = not self.new_format
346371

347372
if stationFile:
348373
st_file = stationFile
349374
else:
350375
st_file = self.stations_file
376+
if logger.isEnabledFor(logging.DEBUG):
377+
logger.debug('Saving playlist: "{}"'.format(st_file))
351378

352379
if not self.dirty_playlist:
353380
if logger.isEnabledFor(logging.DEBUG):
@@ -358,10 +385,12 @@ def save_playlist_file(self, stationFile=''):
358385

359386
tmp_stations = self.stations[:]
360387
tmp_stations.reverse()
361-
if self.new_format:
388+
if self._playlist_version == self.BASIC_PLAYLIST:
389+
tmp_stations.append([ '# Find lots more stations at http://www.iheart.com' , '' ])
390+
elif self._playlist_version == self.ENCODING_PLAYLIST:
362391
tmp_stations.append([ '# Find lots more stations at http://www.iheart.com' , '', '' ])
363392
else:
364-
tmp_stations.append([ '# Find lots more stations at http://www.iheart.com' , '' ])
393+
tmp_stations.append([ '# Find lots more stations at http://www.iheart.com' , '', '', '' ])
365394
tmp_stations.reverse()
366395
try:
367396
#with open(st_new_file, 'w') as cfgfile:
@@ -385,12 +414,15 @@ def save_playlist_file(self, stationFile=''):
385414
return 0
386415

387416
def _format_playlist_row(self, a_row):
388-
""" Return a 2-column if in old format, or
389-
a 3-column row if in new format """
390-
if self.new_format:
417+
""" Return a 2-column if in old format,
418+
a 3-column row if has encoding, or
419+
a 4 column row if has online browser flag too """
420+
if self._playlist_version == self.ONLINE_PLAYLIST:
391421
return a_row
392-
else:
422+
elif self._playlist_version == self.ENCODING_PLAYLIST:
393423
return a_row[:-1]
424+
else:
425+
return a_row[:-2]
394426

395427
def _get_playlist_elements(self, a_playlist):
396428
self.stations_file = path.abspath(a_playlist)
@@ -426,15 +458,19 @@ def append_station(self, params, stationFile=''):
426458
-5: Error writing file
427459
-6: Error renaming file
428460
"""
429-
if self.new_format:
430-
if stationFile:
431-
st_file = stationFile
432-
else:
433-
st_file = self.stations_file
461+
if stationFile:
462+
st_file = stationFile
463+
else:
464+
st_file = self.stations_file
434465

435-
st_file, ret = self._get_playlist_abspath_from_data(st_file)
436-
if ret < -1:
437-
return ret
466+
st_file, ret = self._get_playlist_abspath_from_data(st_file)
467+
if ret < -1:
468+
return ret
469+
param_len = len(params) - 2
470+
self._playlist_format_changed()
471+
if param_len == self._playlist_version:
472+
if logger.isEnabledFor(logging.DEBUG):
473+
logger.debug('Appending station to playlist: "{}"'.format(stationFile))
438474
try:
439475
#with open(st_file, 'a') as cfgfile:
440476
""" Convert self._open_string to
@@ -446,11 +482,9 @@ def append_station(self, params, stationFile=''):
446482
except:
447483
return -5
448484
else:
449-
self.stations.append([ params[0], params[1], params[2] ])
485+
#self.stations.append([ params[0], params[1], params[2] ])
486+
self.stations.append(params[:])
450487
self.dirty_playlist = True
451-
st_file, ret = self._get_playlist_abspath_from_data(stationFile)
452-
if ret < -1:
453-
return ret
454488
ret = self.save_playlist_file(st_file)
455489
if ret < 0:
456490
ret -= 4
@@ -491,7 +525,7 @@ def insert_station(self, station, target):
491525
def move_station(self, source, target):
492526
""" Moves a station in the list from index source to index target
493527
It is moved ABOVE old target (old target becomes old target + 1)"""
494-
logger.error('DE source = {0}, target = {1}'.format(source, target))
528+
#logger.error('DE source = {0}, target = {1}'.format(source, target))
495529
if source == target or \
496530
source < 0 or \
497531
target < 0 or \
@@ -506,7 +540,7 @@ def move_station(self, source, target):
506540
d = collections.deque(self.stations)
507541
d.rotate(-source)
508542
source_item = d.popleft()
509-
logger.error('DE source_item = "{}"'.format(source_item))
543+
#logger.error('DE source_item = "{}"'.format(source_item))
510544
d.rotate(source)
511545
d.rotate(-target)
512546
d.appendleft(source_item)

pyradio/edit.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def _return_station(self):
415415
if ret == 1:
416416
if self._encoding == 'utf-8':
417417
self._encoding = ''
418-
self.new_station = [ self._line_editor[0].string.strip(), self._line_editor[1].string.strip(), self._encoding ]
418+
self.new_station = [ self._line_editor[0].string.strip(), self._line_editor[1].string.strip(), self._encoding, '']
419419
return ret
420420

421421
def _validate(self):

pyradio/radio.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1727,11 +1727,9 @@ def _open_playlist(self):
17271727
self.selection, self.startPos, self.playing, self.stations = self.selections[self.ws.operation_mode]
17281728
self.number_of_items, self.playing = self.readPlaylists()
17291729
self.stations = self._cnf.playlists
1730-
if self.number_of_items == 0:
1731-
return
1732-
else:
1730+
if self.number_of_items > 0:
17331731
self.refreshBody()
1734-
return
1732+
return
17351733

17361734
def _get_station_id(self, find):
17371735
for i, a_station in enumerate(self.stations):
@@ -2332,13 +2330,13 @@ def keypress(self, char):
23322330
self._cnf.dirty_playlist = True
23332331
self.stations[self.selection] = self._station_editor.new_station
23342332
else:
2333+
self._cnf.dirty_playlist = True
23352334
if self._station_editor.append:
23362335
self.stations.append(self._station_editor.new_station)
23372336
self.number_of_items = len(self.stations)
23382337
self.selection = self.number_of_items - 1
23392338
self.startPos = self.number_of_items - self.bodyMaxY + 2
23402339
else:
2341-
pass
23422340
ret, self.number_of_items = self._cnf.insert_station(self._station_editor.new_station, self.selection + 1)
23432341
self.stations = self._cnf.stations
23442342
self.selection += 1
@@ -2999,7 +2997,6 @@ def keypress(self, char):
29992997
if self._cnf.dirty_playlist:
30002998
if self._cnf.auto_save_playlist:
30012999
# save playlist and open playlist
3002-
pass
30033000
ret = self.saveCurrentPlaylist()
30043001
if ret == 0:
30053002
self._open_playlist()

0 commit comments

Comments
 (0)