20
20
from .common import is_rasberrypi
21
21
from .player import pywhich
22
22
HAS_REQUESTS = True
23
+
23
24
try :
24
25
import requests
25
26
except :
@@ -84,7 +85,6 @@ class PyRadioStations(object):
84
85
85
86
playlist_recovery_result = 0
86
87
87
- _open_string = [ "open(stationFile, 'r')" , "open(stationFile, 'r', encoding='utf-8')" ]
88
88
_open_string_id = 0
89
89
90
90
jump_tag = - 1
@@ -324,7 +324,7 @@ def save_last_playlist(self, sel):
324
324
if llp [2 ]:
325
325
# logger.error(f'llp = {llp} - saved = {self._last_opened_playlist_name}')
326
326
try :
327
- with open (lp , 'w' ) as f :
327
+ with open (lp , 'w' , encoding = 'utf-8' ) as f :
328
328
writter = csv .writer (f )
329
329
writter .writerow (out_pl )
330
330
except PermissionError :
@@ -475,7 +475,7 @@ def _get_playlist_abspath_from_data(self, stationFile=''):
475
475
476
476
def _package_stations (self ):
477
477
''' read package stations.csv file '''
478
- with open (self .root_path , 'r' ) as cfgfile :
478
+ with open (self .root_path , 'r' , encoding = 'utf-8' ) as cfgfile :
479
479
for row in csv .reader (filter (lambda row : row [0 ]!= '#' , cfgfile ), skipinitialspace = True ):
480
480
if not row :
481
481
continue
@@ -511,7 +511,7 @@ def read_playlist_for_server(self, stationFile):
511
511
out = []
512
512
in_file = self ._construct_playlist_path (stationFile )
513
513
try :
514
- with open (in_file , 'r' ) as cfgfile :
514
+ with open (in_file , 'r' , encoding = 'utf-8' ) as cfgfile :
515
515
for row in csv .reader (filter (lambda row : row [0 ]!= '#' , cfgfile ), skipinitialspace = True ):
516
516
if not row :
517
517
continue
@@ -571,7 +571,7 @@ def read_playlist_file(
571
571
prev_format = self ._playlist_version
572
572
self ._read_playlist_version = self ._playlist_version = self .PLAYLIST_HAS_NAME_URL
573
573
self ._reading_stations = []
574
- with eval ( self . _open_string [ self . _open_string_id ] ) as cfgfile :
574
+ with open ( stationFile , 'r' , encoding = 'utf-8' ) as cfgfile :
575
575
try :
576
576
for row in csv .reader (filter (lambda row : row [0 ]!= '#' , cfgfile ), skipinitialspace = True ):
577
577
if not row :
@@ -625,7 +625,7 @@ def read_playlist_file(
625
625
remove (a_file )
626
626
except :
627
627
pass
628
- with open (ver_file , 'a' ) as f :
628
+ with open (ver_file , 'a' , encoding = 'utf-8' ) as f :
629
629
pass
630
630
631
631
return self .number_of_stations
@@ -749,10 +749,7 @@ def save_playlist_file(self, stationFile=''):
749
749
# tmp_stations.append([ '# Find lots more stations at http://www.iheart.com' , '', '', '' ])
750
750
#tmp_stations.reverse()
751
751
try :
752
- #with open(st_new_file, 'w') as cfgfile:
753
- ''' Convert self._open_string to
754
- open(st_new_file, 'w') '''
755
- with eval (self ._open_string [self ._open_string_id ].replace ("'r'" , "'w'" ).replace ('stationFile' ,'st_new_file' )) as cfgfile :
752
+ with open (st_new_file , 'w' , encoding = 'utf-8' ) as cfgfile :
756
753
writter = csv .writer (cfgfile )
757
754
for a_station in tmp_stations :
758
755
writter .writerow (self ._format_playlist_row (a_station ))
@@ -887,9 +884,7 @@ def append_station(self, params, stationFile=''):
887
884
logger .debug ('Appending station to playlist: "{}"' .format (stationFile ))
888
885
try :
889
886
#with open(st_file, 'a') as cfgfile:
890
- ''' Convert self._open_string to
891
- with open(st_file, 'a') '''
892
- with eval (self ._open_string [self ._open_string_id ].replace ("'r'" , "'a'" ).replace ('stationFile' ,'st_file' )) as cfgfile :
887
+ with open (st_file , 'a' , encoding = 'utf-8' ) as cfgfile :
893
888
writter = csv .writer (cfgfile )
894
889
writter .writerow (params )
895
890
return 0
@@ -927,7 +922,7 @@ def paste_station_to_named_playlist(self, a_station, a_playlist):
927
922
while w_str .endswith (',' ):
928
923
w_str = w_str [:- 1 ]
929
924
try :
930
- with open (a_playlist , 'a' ) as f :
925
+ with open (a_playlist , 'a' , encoding = 'utf-8' ) as f :
931
926
f .write ('\n ' + w_str )
932
927
return 0
933
928
except :
@@ -1096,7 +1091,7 @@ def append_to_register(self, register, station):
1096
1091
string_to_write = ',' .join (a_station ) + '\n '
1097
1092
with self ._registers_lock :
1098
1093
try :
1099
- with open (reg_file , 'a' ) as f :
1094
+ with open (reg_file , 'a' , encoding = 'utf-8' ) as f :
1100
1095
f .write (string_to_write )
1101
1096
except :
1102
1097
if logger .isEnabledFor (logging .DEBUG ):
@@ -1627,7 +1622,7 @@ def _read_notification_command(self):
1627
1622
for i , n in enumerate (ns ):
1628
1623
if path .exists (n ):
1629
1624
try :
1630
- with open (n , 'r' ) as f :
1625
+ with open (n , 'r' , encoding = 'utf-8' ) as f :
1631
1626
for line in f :
1632
1627
self ._notification_command .append (line .replace ('\n ' , '' ).strip ())
1633
1628
except :
@@ -1772,7 +1767,7 @@ def _get_lock_file(self):
1772
1767
self .locked = True
1773
1768
else :
1774
1769
try :
1775
- with open (self ._session_lock_file , 'w' ) as f :
1770
+ with open (self ._session_lock_file , 'w' , encoding = 'utf-8' ) as f :
1776
1771
pass
1777
1772
except :
1778
1773
pass
@@ -1849,12 +1844,12 @@ def _check_config_file(self, usr):
1849
1844
1850
1845
def _convert_config_for_rasberrypi (self , package_config_file , user_config_file ):
1851
1846
lines = []
1852
- with open (package_config_file , 'r' ) as f :
1847
+ with open (package_config_file , 'r' , encoding = 'utf-8' ) as f :
1853
1848
lines = [line .strip () for line in f ]
1854
1849
for i in range (len (lines )):
1855
1850
if lines [i ].startswith ('player' ):
1856
1851
lines [i ] = 'player = mplayer,vlc,mpv'
1857
- with open (user_config_file , 'w' ) as f :
1852
+ with open (user_config_file , 'w' , encoding = 'utf-8' ) as f :
1858
1853
f .write ('\n ' .join (lines ) + '\n ' )
1859
1854
1860
1855
def _validate_remote_control_server_ip (self , val ):
@@ -1887,7 +1882,7 @@ def _validate_remote_control_server_ip(self, val):
1887
1882
def read_config (self ):
1888
1883
lines = []
1889
1884
try :
1890
- with open (self .config_file , 'r' ) as cfgfile :
1885
+ with open (self .config_file , 'r' , encoding = 'utf-8' ) as cfgfile :
1891
1886
lines = [line .strip () for line in cfgfile if line .strip () and not line .startswith ('#' ) ]
1892
1887
1893
1888
except :
@@ -2024,7 +2019,7 @@ def read_config(self):
2024
2019
''' read distro from package config file '''
2025
2020
package_config_file = path .join (path .dirname (__file__ ), 'config' )
2026
2021
try :
2027
- with open (package_config_file , 'r' ) as pkg_config :
2022
+ with open (package_config_file , 'r' , encoding = 'utf-8' ) as pkg_config :
2028
2023
lines = [line .strip () for line in pkg_config if line .strip () and not line .startswith ('#' ) ]
2029
2024
for line in lines :
2030
2025
sp = line .split ('=' )
@@ -2073,8 +2068,9 @@ def get_last_playlist(self):
2073
2068
'''
2074
2069
playlist = ''
2075
2070
lp = path .join (self .stations_dir , 'last_playlist' )
2071
+ print ('lp = "{}"' .format (lp ))
2076
2072
if path .exists (lp ):
2077
- with open (lp , 'r' ) as f :
2073
+ with open (lp , 'r' , encoding = 'utf-8' ) as f :
2078
2074
for row in csv .reader (filter (lambda row : row [0 ]!= '#' , f ), skipinitialspace = True ):
2079
2075
if not row :
2080
2076
continue
@@ -2364,7 +2360,7 @@ def save_config(self, from_command_line=False):
2364
2360
calcf = self .bck_opts ['calculated_color_factor' ]
2365
2361
2366
2362
try :
2367
- with open (self .config_file , 'w' ) as cfgfile :
2363
+ with open (self .config_file , 'w' , encoding = 'utf-8' ) as cfgfile :
2368
2364
cfgfile .write (txt .format (
2369
2365
self .opts ['player' ][1 ],
2370
2366
self .opts ['open_last_playlist' ][1 ],
@@ -2469,7 +2465,7 @@ def is_blacklisted_terminal(self):
2469
2465
user_terminal = []
2470
2466
if path .exists (term_file ):
2471
2467
try :
2472
- with open (term_file , 'r' ) as term :
2468
+ with open (term_file , 'r' , encoding = 'utf-8' ) as term :
2473
2469
user_terminals = term .read ().splitlines ()
2474
2470
except :
2475
2471
pass
@@ -3146,8 +3142,7 @@ def download(self, a_theme=None, a_path=None, print_errors=None):
3146
3142
requests_response = requests .get (url , timeout = 1 )
3147
3143
requests_response .raise_for_status ()
3148
3144
try :
3149
- with open (w_path , 'w' ) as f :
3150
- pass
3145
+ with open (w_path , 'w' , encoding = 'utf-8' ) as f :
3151
3146
f .write (requests_response .text )
3152
3147
written = True
3153
3148
except :
@@ -3315,7 +3310,7 @@ def download(self, a_theme=None, a_path=None, print_errors=None):
3315
3310
else :
3316
3311
w_theme = a_theme
3317
3312
3318
- with open (self ._ln , 'r' ) as jfile :
3313
+ with open (self ._ln , 'r' , encoding = 'utf-8' ) as jfile :
3319
3314
jdata = json .load (jfile )
3320
3315
3321
3316
lines = templates [self .theme_id ].split ('\n ' )
@@ -3325,7 +3320,7 @@ def download(self, a_theme=None, a_path=None, print_errors=None):
3325
3320
3326
3321
ret = True
3327
3322
try :
3328
- with open (w_path , 'w' ) as out_file :
3323
+ with open (w_path , 'w' , encoding = 'utf-8' ) as out_file :
3329
3324
for n in lines :
3330
3325
out_file .write (n + '\n ' )
3331
3326
except :
@@ -3602,7 +3597,7 @@ def download(self, a_theme=None, a_path=None, print_errors=None):
3602
3597
# for n in lines:
3603
3598
# logger.error(n)
3604
3599
try :
3605
- with open (w_path , 'w' ) as out_file :
3600
+ with open (w_path , 'w' , encoding = 'utf-8' ) as out_file :
3606
3601
for n in lines :
3607
3602
out_file .write (n + '\n ' )
3608
3603
except :
@@ -3633,7 +3628,7 @@ def _read_last_line_from_ln(self):
3633
3628
def _read_theme_sh (self , theme_name ):
3634
3629
lines = {}
3635
3630
in_theme = False
3636
- with open (self ._theme_sh_executable , 'r' ) as f :
3631
+ with open (self ._theme_sh_executable , 'r' , encoding = 'utf-8' ) as f :
3637
3632
for line in f :
3638
3633
if in_theme :
3639
3634
l = line .replace ('\n ' , '' ).split (': ' )
0 commit comments