Skip to content

Commit a72f250

Browse files
committed
all file read/write will be utf-8 encoded (python2 not affected)
1 parent 2f32c35 commit a72f250

13 files changed

+208
-193
lines changed

devel/build_install_pyradio

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,27 @@ done
434434
# echo "***** installing for user..."
435435
do_dev
436436

437+
if [ "${TO_PYTHON}" == "2" ];
438+
then
439+
tar czf pyradio.tar.gz pyradio
440+
for a_file in pyradio/*.py
441+
do
442+
sed -i "/open/s/, encoding='utf-8'//" "${a_file}"
443+
done
444+
fi
445+
437446
if [ -e requirements.txt ];then
438447
python"${TO_PYTHON}" -m pip install -r requirements.txt . || pip_error
439448
else
440449
python"${TO_PYTHON}" -m pip install . || pip_error
441450
fi
442451

452+
if [ "${TO_PYTHON}" == "2" ];
453+
then
454+
tar xzf pyradio.tar.gz
455+
rm pyradio.tar.gz
456+
fi
457+
443458
do_undev
444459

445460
if [ $? -eq 0 ]

pyradio/browser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ def read_config(self):
14771477
lines = []
14781478
term_str = []
14791479
try:
1480-
with open(self.config_file, 'r') as cfgfile:
1480+
with open(self.config_file, 'r', encoding='utf-8') as cfgfile:
14811481
lines = [line.strip() for line in cfgfile if line.strip() and not line.startswith('#') ]
14821482

14831483
except:
@@ -1634,7 +1634,7 @@ def save_config(self,
16341634
txt += 'SEARCH_TERM = ' + asterisk + str(search_history[n]).replace('{u\'', '{\'').replace('u\'', '\'') + '\n'
16351635

16361636
try:
1637-
with open(self.config_file, 'w') as cfgfile:
1637+
with open(self.config_file, 'w', encoding='utf-8') as cfgfile:
16381638
cfgfile.write(txt)
16391639
except:
16401640
if logger.isEnabledFor(logging.ERROR):

pyradio/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def erase_curses_win(self, Y, X, beginY, beginX, char=' ', color=5):
6060
def is_rasberrypi():
6161
''' Try to detest rasberry pi '''
6262
try:
63-
with io.open('/sys/firmware/devicetree/base/model', 'r') as m:
63+
with io.open('/sys/firmware/devicetree/base/model', 'r', encoding='utf-8') as m:
6464
if 'raspberry pi' in m.read().lower():
6565
return True
6666
except Exception:

pyradio/config.py

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .common import is_rasberrypi
2121
from .player import pywhich
2222
HAS_REQUESTS = True
23+
2324
try:
2425
import requests
2526
except:
@@ -84,7 +85,6 @@ class PyRadioStations(object):
8485

8586
playlist_recovery_result = 0
8687

87-
_open_string = [ "open(stationFile, 'r')", "open(stationFile, 'r', encoding='utf-8')" ]
8888
_open_string_id = 0
8989

9090
jump_tag = -1
@@ -324,7 +324,7 @@ def save_last_playlist(self, sel):
324324
if llp[2]:
325325
# logger.error(f'llp = {llp} - saved = {self._last_opened_playlist_name}')
326326
try:
327-
with open(lp, 'w') as f:
327+
with open(lp, 'w', encoding='utf-8') as f:
328328
writter = csv.writer(f)
329329
writter.writerow(out_pl)
330330
except PermissionError:
@@ -475,7 +475,7 @@ def _get_playlist_abspath_from_data(self, stationFile=''):
475475

476476
def _package_stations(self):
477477
''' 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:
479479
for row in csv.reader(filter(lambda row: row[0]!='#', cfgfile), skipinitialspace=True):
480480
if not row:
481481
continue
@@ -511,7 +511,7 @@ def read_playlist_for_server(self, stationFile):
511511
out = []
512512
in_file = self._construct_playlist_path(stationFile)
513513
try:
514-
with open(in_file, 'r') as cfgfile:
514+
with open(in_file, 'r', encoding='utf-8') as cfgfile:
515515
for row in csv.reader(filter(lambda row: row[0]!='#', cfgfile), skipinitialspace=True):
516516
if not row:
517517
continue
@@ -571,7 +571,7 @@ def read_playlist_file(
571571
prev_format = self._playlist_version
572572
self._read_playlist_version = self._playlist_version = self.PLAYLIST_HAS_NAME_URL
573573
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:
575575
try:
576576
for row in csv.reader(filter(lambda row: row[0]!='#', cfgfile), skipinitialspace=True):
577577
if not row:
@@ -625,7 +625,7 @@ def read_playlist_file(
625625
remove(a_file)
626626
except:
627627
pass
628-
with open(ver_file, 'a') as f:
628+
with open(ver_file, 'a', encoding='utf-8') as f:
629629
pass
630630

631631
return self.number_of_stations
@@ -749,10 +749,7 @@ def save_playlist_file(self, stationFile=''):
749749
# tmp_stations.append([ '# Find lots more stations at http://www.iheart.com' , '', '', '' ])
750750
#tmp_stations.reverse()
751751
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:
756753
writter = csv.writer(cfgfile)
757754
for a_station in tmp_stations:
758755
writter.writerow(self._format_playlist_row(a_station))
@@ -887,9 +884,7 @@ def append_station(self, params, stationFile=''):
887884
logger.debug('Appending station to playlist: "{}"'.format(stationFile))
888885
try:
889886
#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:
893888
writter = csv.writer(cfgfile)
894889
writter.writerow(params)
895890
return 0
@@ -927,7 +922,7 @@ def paste_station_to_named_playlist(self, a_station, a_playlist):
927922
while w_str.endswith(','):
928923
w_str = w_str[:-1]
929924
try:
930-
with open(a_playlist, 'a') as f:
925+
with open(a_playlist, 'a', encoding='utf-8') as f:
931926
f.write('\n' + w_str)
932927
return 0
933928
except:
@@ -1096,7 +1091,7 @@ def append_to_register(self, register, station):
10961091
string_to_write = ','.join(a_station) + '\n'
10971092
with self._registers_lock:
10981093
try:
1099-
with open(reg_file, 'a') as f:
1094+
with open(reg_file, 'a', encoding='utf-8') as f:
11001095
f.write(string_to_write)
11011096
except:
11021097
if logger.isEnabledFor(logging.DEBUG):
@@ -1627,7 +1622,7 @@ def _read_notification_command(self):
16271622
for i, n in enumerate(ns):
16281623
if path.exists(n):
16291624
try:
1630-
with open(n, 'r') as f:
1625+
with open(n, 'r', encoding='utf-8') as f:
16311626
for line in f:
16321627
self._notification_command.append(line.replace('\n', '').strip())
16331628
except:
@@ -1772,7 +1767,7 @@ def _get_lock_file(self):
17721767
self.locked = True
17731768
else:
17741769
try:
1775-
with open(self._session_lock_file, 'w') as f:
1770+
with open(self._session_lock_file, 'w', encoding='utf-8') as f:
17761771
pass
17771772
except:
17781773
pass
@@ -1849,12 +1844,12 @@ def _check_config_file(self, usr):
18491844

18501845
def _convert_config_for_rasberrypi(self, package_config_file, user_config_file):
18511846
lines = []
1852-
with open(package_config_file, 'r') as f:
1847+
with open(package_config_file, 'r', encoding='utf-8') as f:
18531848
lines = [line.strip() for line in f]
18541849
for i in range(len(lines)):
18551850
if lines[i].startswith('player'):
18561851
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:
18581853
f.write('\n'.join(lines) + '\n')
18591854

18601855
def _validate_remote_control_server_ip(self, val):
@@ -1887,7 +1882,7 @@ def _validate_remote_control_server_ip(self, val):
18871882
def read_config(self):
18881883
lines = []
18891884
try:
1890-
with open(self.config_file, 'r') as cfgfile:
1885+
with open(self.config_file, 'r', encoding='utf-8') as cfgfile:
18911886
lines = [line.strip() for line in cfgfile if line.strip() and not line.startswith('#') ]
18921887

18931888
except:
@@ -2024,7 +2019,7 @@ def read_config(self):
20242019
''' read distro from package config file '''
20252020
package_config_file = path.join(path.dirname(__file__), 'config')
20262021
try:
2027-
with open(package_config_file, 'r') as pkg_config:
2022+
with open(package_config_file, 'r', encoding='utf-8') as pkg_config:
20282023
lines = [line.strip() for line in pkg_config if line.strip() and not line.startswith('#') ]
20292024
for line in lines:
20302025
sp = line.split('=')
@@ -2073,8 +2068,9 @@ def get_last_playlist(self):
20732068
'''
20742069
playlist = ''
20752070
lp = path.join(self.stations_dir, 'last_playlist')
2071+
print('lp = "{}"'.format(lp))
20762072
if path.exists(lp):
2077-
with open(lp, 'r') as f:
2073+
with open(lp, 'r', encoding='utf-8') as f:
20782074
for row in csv.reader(filter(lambda row: row[0]!='#', f), skipinitialspace=True):
20792075
if not row:
20802076
continue
@@ -2364,7 +2360,7 @@ def save_config(self, from_command_line=False):
23642360
calcf = self.bck_opts['calculated_color_factor']
23652361

23662362
try:
2367-
with open(self.config_file, 'w') as cfgfile:
2363+
with open(self.config_file, 'w', encoding='utf-8') as cfgfile:
23682364
cfgfile.write(txt.format(
23692365
self.opts['player'][1],
23702366
self.opts['open_last_playlist'][1],
@@ -2469,7 +2465,7 @@ def is_blacklisted_terminal(self):
24692465
user_terminal = []
24702466
if path.exists(term_file):
24712467
try:
2472-
with open(term_file, 'r') as term:
2468+
with open(term_file, 'r', encoding='utf-8') as term:
24732469
user_terminals = term.read().splitlines()
24742470
except:
24752471
pass
@@ -3146,8 +3142,7 @@ def download(self, a_theme=None, a_path=None, print_errors=None):
31463142
requests_response = requests.get(url, timeout=1)
31473143
requests_response.raise_for_status()
31483144
try:
3149-
with open(w_path, 'w') as f:
3150-
pass
3145+
with open(w_path, 'w', encoding='utf-8') as f:
31513146
f.write(requests_response.text)
31523147
written = True
31533148
except:
@@ -3315,7 +3310,7 @@ def download(self, a_theme=None, a_path=None, print_errors=None):
33153310
else:
33163311
w_theme = a_theme
33173312

3318-
with open(self._ln, 'r') as jfile:
3313+
with open(self._ln, 'r', encoding='utf-8') as jfile:
33193314
jdata = json.load(jfile)
33203315

33213316
lines = templates[self.theme_id].split('\n')
@@ -3325,7 +3320,7 @@ def download(self, a_theme=None, a_path=None, print_errors=None):
33253320

33263321
ret = True
33273322
try:
3328-
with open(w_path, 'w') as out_file:
3323+
with open(w_path, 'w', encoding='utf-8') as out_file:
33293324
for n in lines:
33303325
out_file.write(n + '\n')
33313326
except:
@@ -3602,7 +3597,7 @@ def download(self, a_theme=None, a_path=None, print_errors=None):
36023597
# for n in lines:
36033598
# logger.error(n)
36043599
try:
3605-
with open(w_path, 'w') as out_file:
3600+
with open(w_path, 'w', encoding='utf-8') as out_file:
36063601
for n in lines:
36073602
out_file.write(n + '\n')
36083603
except:
@@ -3633,7 +3628,7 @@ def _read_last_line_from_ln(self):
36333628
def _read_theme_sh(self, theme_name):
36343629
lines = {}
36353630
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:
36373632
for line in f:
36383633
if in_theme:
36393634
l = line.replace('\n', '').split(': ')

pyradio/config_window.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import glob
66
import csv
77
from os import path, sep, remove
8-
from sys import platform
8+
from sys import platform, version_info
99

1010
from .common import *
1111
from .window_stack import Window_Stack_Constants
@@ -18,7 +18,6 @@
1818
import locale
1919
locale.setlocale(locale.LC_ALL, '') # set your locale
2020

21-
2221
logger = logging.getLogger(__name__)
2322

2423
def set_global_functions(global_functions):
@@ -2552,7 +2551,7 @@ def _get_result(self):
25522551

25532552
stationFile = path.join(self._config_path, self._items[self._selected_playlist_id] + '.csv')
25542553
self._select_playlist_error = 0
2555-
with open(stationFile, 'r') as cfgfile:
2554+
with open(stationFile, 'r', encoding='utf-8') as cfgfile:
25562555
try:
25572556
for row in csv.reader(filter(lambda row: row[0] != '#', cfgfile), skipinitialspace=True):
25582557
if not row:
@@ -2809,7 +2808,7 @@ def _read_items(self):
28092808
self._items = []
28102809
stationFile = path.join(self._config_path, self._default_playlist + '.csv')
28112810
if path.exists(stationFile):
2812-
with open(stationFile, 'r') as cfgfile:
2811+
with open(stationFile, 'r', encoding='utf-8') as cfgfile:
28132812
try:
28142813
for row in csv.reader(filter(lambda row: row[0] != '#', cfgfile), skipinitialspace=True):
28152814
if not row:

pyradio/edit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
logger = logging.getLogger(__name__)
2121

22+
2223
class PyRadioSearch(SimpleCursesLineEdit):
2324

2425
_caption = 'Search'
@@ -1063,7 +1064,7 @@ def _act_on_file(self):
10631064
self.new_file_name = path.join(self._to_path, self._widgets[0].string + '.csv')
10641065
if self._create:
10651066
try:
1066-
with open(self.new_file_name, 'wt'):
1067+
with open(self.new_file_name, 'w', encoding='utf-8'):
10671068
pass
10681069
except:
10691070
return -2

pyradio/install.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
VERSION = ''
3333

3434
PY3 = sys.version[0] == '3'
35+
3536
# import logging
3637
# logger = logging.getLogger(__name__)
3738

@@ -340,10 +341,10 @@ def windows_put_devel_version():
340341
cur_dir = os.getcwd()
341342
copyfile(os.path.join(cur_dir, 'config.py'), os.path.join(cur_dir, 'config.py.dev'))
342343
try:
343-
with open(os.path.join(cur_dir, 'config.py'), 'r') as con:
344+
with open(os.path.join(cur_dir, 'config.py'), 'r', encoding='utf-8') as con:
344345
lines = con.read()
345346
lines = lines.replace("git_description = ''", "git_description = '" + long_descr + "'")
346-
with open(os.path.join(cur_dir, 'config.py'), 'w') as con:
347+
with open(os.path.join(cur_dir, 'config.py'), 'w', encoding='utf-8') as con:
347348
con.write(lines)
348349
except:
349350
print('Error: Cannot change downloaded files...\n Please close all running programs and try again.')
@@ -583,7 +584,7 @@ def update_or_uninstall_on_windows(self, mode='update', from_pyradio=False, firs
583584
self.ZIP_URL[0] = self.ZIP_URL[0] + VERSION + '.zip'
584585
self.ZIP_DIR[0] += VERSION
585586
try:
586-
with open(bat, "w") as b:
587+
with open(bat, "w", encoding='utf-8') as b:
587588
b.write('@ECHO OFF\n')
588589
b.write('CLS\n')
589590
b.write('python -m pip install --upgrade wheel 1>NUL 2>NUL\n')
@@ -726,10 +727,10 @@ def _change_git_discription_in_config_py(self):
726727
''' change git_discription in pyradio/config.py '''
727728
if self._github_long_description is not None:
728729
try:
729-
with open(os.path.join(self._install_dir, 'pyradio', 'config.py'), 'r') as con:
730+
with open(os.path.join(self._install_dir, 'pyradio', 'config.py'), 'r', encoding='utf-8') as con:
730731
lines = con.read()
731732
lines = lines.replace("git_description = ''", "git_description = '" + self._github_long_description + "'")
732-
with open(os.path.join(self._install_dir, 'pyradio', 'config.py'), 'w') as con:
733+
with open(os.path.join(self._install_dir, 'pyradio', 'config.py'), 'w', encoding='utf-8') as con:
733734
con.write(lines)
734735
except:
735736
print('Error: Cannot change downloaded files...\n Please close all running programs and try again.')
@@ -758,7 +759,7 @@ def _download_pyradio(self):
758759
except:
759760
print('Error: PyRadio source code ZIP file is corrupt...\n')
760761
sys.exit(1)
761-
with open(os.path.join(self._dir, self.ZIP_DIR[self._package], 'DEV'), 'w') as b:
762+
with open(os.path.join(self._dir, self.ZIP_DIR[self._package], 'DEV'), 'w', encoding='utf-8') as b:
762763
pass
763764
''' DEBUG on linux
764765
get new install.py, copy.py (any py)

0 commit comments

Comments
 (0)