Skip to content

Commit 4182cd9

Browse files
committed
backup / resote players config files on Windows
1 parent 3b9fc5a commit 4182cd9

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

pyradio/player.py

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import collections
1313
import json
1414
import socket
15+
from shutil import copyfile as shutil_copy_file
1516

1617
import locale
1718
locale.setlocale(locale.LC_ALL, "")
@@ -348,6 +349,7 @@ def _get_all_config_files(self):
348349
else:
349350
config_files = []
350351
self.all_config_files['mplayer'] = config_files[:]
352+
self._restore_win_player_config_file()
351353

352354
@property
353355
def profile_name(self):
@@ -607,8 +609,36 @@ def _do_save_volume(self, config_string):
607609
return ret_strings[2].format(str(self.volume))
608610
self.volume = -1
609611
self.PROFILE_FROM_USER = True
612+
self.bck_win_player_config_file(config_file)
610613
return ret_string
611614

615+
def bck_win_player_config_file(self, config_file=None):
616+
if platform.startswith('win'):
617+
''' backup player config '''
618+
if config_file is None:
619+
cnf_file = self.config_files[0]
620+
else:
621+
cnf_file = config_file
622+
if os.path.exists(cnf_file):
623+
bck_file = os.path.join(os.getenv('APPDATA'), "pyradio", self.PLAYER_NAME + "-active.conf")
624+
try:
625+
shutil_copy_file(cnf_file, bck_file)
626+
except:
627+
pass
628+
629+
def _restore_win_player_config_file(self):
630+
if platform.startswith('win'):
631+
''' restore player config '''
632+
for k in ('mplayer', 'mpv'):
633+
cnf_file = self.all_config_files[k][0]
634+
if os.path.exists(cnf_file):
635+
bck_file = os.path.join(os.getenv('APPDATA'), "pyradio", k + "-active.conf")
636+
if os.path.exists(bck_file):
637+
try:
638+
shutil_copy_file(bck_file, cnf_file)
639+
except:
640+
pass
641+
612642
def _stop_delay_thread(self):
613643
if self.delay_thread is not None:
614644
try:
@@ -1549,16 +1579,28 @@ def play(self,
15491579

15501580
def _sendCommand(self, command):
15511581
''' send keystroke command to player '''
1552-
for a_process in (self.process, self.monitor_process):
1553-
if a_process is not None:
1554-
if logger.isEnabledFor(logging.DEBUG):
1555-
logger.debug('Sending Command: {}'.format(command).strip())
1556-
try:
1557-
a_process.stdin.write(command.encode('utf-8', 'replace'))
1558-
a_process.stdin.flush()
1559-
except:
1560-
if logger.isEnabledFor(logging.ERROR):
1561-
logger.error('Error while sending Command: {}'.format(command).strip(), exc_info=True)
1582+
for a_process in (self.monitor_process, self.process):
1583+
self._command_to_player(a_process, command)
1584+
return
1585+
if command in ('q', '/', '*'):
1586+
for a_process in (self.monitor_process, self.process):
1587+
self._command_to_player(a_process, command)
1588+
elif self.recording == self.NO_RECORDING or \
1589+
self.recording == self.RECORD_WITH_SILENCE:
1590+
self._command_to_player(self.process, command)
1591+
elif self.recording == self.RECORD_AND_LISTEN:
1592+
self._command_to_player(self.monitor_process, command)
1593+
1594+
def _command_to_player(self, a_process, command):
1595+
if a_process is not None:
1596+
if logger.isEnabledFor(logging.DEBUG):
1597+
logger.debug('Sending Command: {}'.format(command).strip())
1598+
try:
1599+
a_process.stdin.write(command.encode('utf-8', 'replace'))
1600+
a_process.stdin.flush()
1601+
except:
1602+
if logger.isEnabledFor(logging.ERROR):
1603+
logger.error('Error while sending Command: {}'.format(command).strip(), exc_info=True)
15621604

15631605
def close_from_windows(self):
15641606
''' kill player instance when window console is closed '''

0 commit comments

Comments
 (0)