@@ -38,9 +38,16 @@ class PyRadioStations(object):
38
38
selected_playlist = - 1
39
39
number_of_stations = - 1
40
40
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
44
51
45
52
dirty_playlist = False
46
53
@@ -90,6 +97,14 @@ def __init__(self, stationFile=''):
90
97
self ._move_old_csv (self .stations_dir )
91
98
self ._check_stations_csv (self .stations_dir , self .root_path )
92
99
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
+
93
108
def _move_old_csv (self , usr ):
94
109
""" if a ~/.pyradio files exists, relocate it in user
95
110
config folder and rename it to stations.csv, or if
@@ -238,8 +253,8 @@ def read_playlist_file(self, stationFile=''):
238
253
# reason in cnf.playlist_recovery_result
239
254
return - 7
240
255
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
243
258
self ._reading_stations = []
244
259
with eval (self ._open_string [self ._open_string_id ]) as cfgfile :
245
260
try :
@@ -248,14 +263,19 @@ def read_playlist_file(self, stationFile=''):
248
263
continue
249
264
try :
250
265
name , url = [s .strip () for s in row ]
251
- self ._reading_stations .append ([name , url , '' ])
266
+ self ._reading_stations .append ([name , url , '' , '' ])
252
267
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
256
276
except :
257
277
self ._reading_stations = []
258
- self .new_format = prev_format
278
+ self ._playlist_version = prev_format
259
279
return - 1
260
280
261
281
self .stations = list (self ._reading_stations )
@@ -266,10 +286,7 @@ def read_playlist_file(self, stationFile=''):
266
286
self .number_of_stations = len (self .stations )
267
287
self .dirty_playlist = False
268
288
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 ))
273
290
self .jump_tag = - 1
274
291
return self .number_of_stations
275
292
@@ -321,15 +338,24 @@ def _playlist_format_changed(self):
321
338
Format type can change by editing encoding,
322
339
deleting a non-utf-8 station etc.
323
340
"""
324
- new_format = False
341
+ playlist_version = self . BASIC_PLAYLIST
325
342
for n in self .stations :
326
- if n [2 ] != '' :
327
- new_format = True
343
+ if n [3 ] != '' :
344
+ playlist_version = self . ONLINE_PLAYLIST
328
345
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
331
353
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
333
359
334
360
def save_playlist_file (self , stationFile = '' ):
335
361
""" Save a playlist
@@ -342,12 +368,13 @@ def save_playlist_file(self, stationFile=''):
342
368
"""
343
369
if self ._playlist_format_changed ():
344
370
self .dirty_playlist = True
345
- self .new_format = not self .new_format
346
371
347
372
if stationFile :
348
373
st_file = stationFile
349
374
else :
350
375
st_file = self .stations_file
376
+ if logger .isEnabledFor (logging .DEBUG ):
377
+ logger .debug ('Saving playlist: "{}"' .format (st_file ))
351
378
352
379
if not self .dirty_playlist :
353
380
if logger .isEnabledFor (logging .DEBUG ):
@@ -358,10 +385,12 @@ def save_playlist_file(self, stationFile=''):
358
385
359
386
tmp_stations = self .stations [:]
360
387
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 :
362
391
tmp_stations .append ([ '# Find lots more stations at http://www.iheart.com' , '' , '' ])
363
392
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' , '' , '' , '' ])
365
394
tmp_stations .reverse ()
366
395
try :
367
396
#with open(st_new_file, 'w') as cfgfile:
@@ -385,12 +414,15 @@ def save_playlist_file(self, stationFile=''):
385
414
return 0
386
415
387
416
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 :
391
421
return a_row
392
- else :
422
+ elif self . _playlist_version == self . ENCODING_PLAYLIST :
393
423
return a_row [:- 1 ]
424
+ else :
425
+ return a_row [:- 2 ]
394
426
395
427
def _get_playlist_elements (self , a_playlist ):
396
428
self .stations_file = path .abspath (a_playlist )
@@ -426,15 +458,19 @@ def append_station(self, params, stationFile=''):
426
458
-5: Error writing file
427
459
-6: Error renaming file
428
460
"""
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
434
465
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 ))
438
474
try :
439
475
#with open(st_file, 'a') as cfgfile:
440
476
""" Convert self._open_string to
@@ -446,11 +482,9 @@ def append_station(self, params, stationFile=''):
446
482
except :
447
483
return - 5
448
484
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 [:])
450
487
self .dirty_playlist = True
451
- st_file , ret = self ._get_playlist_abspath_from_data (stationFile )
452
- if ret < - 1 :
453
- return ret
454
488
ret = self .save_playlist_file (st_file )
455
489
if ret < 0 :
456
490
ret -= 4
@@ -491,7 +525,7 @@ def insert_station(self, station, target):
491
525
def move_station (self , source , target ):
492
526
""" Moves a station in the list from index source to index target
493
527
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))
495
529
if source == target or \
496
530
source < 0 or \
497
531
target < 0 or \
@@ -506,7 +540,7 @@ def move_station(self, source, target):
506
540
d = collections .deque (self .stations )
507
541
d .rotate (- source )
508
542
source_item = d .popleft ()
509
- logger .error ('DE source_item = "{}"' .format (source_item ))
543
+ # logger.error('DE source_item = "{}"'.format(source_item))
510
544
d .rotate (source )
511
545
d .rotate (- target )
512
546
d .appendleft (source_item )
0 commit comments