Skip to content

Commit 2f8663c

Browse files
committed
theme file watch functionality implemented
1 parent 2b6ac48 commit 2f8663c

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

pyradio/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,10 @@ def theme(self, val):
14281428
self.opts['auto_update_theme'][1] = False
14291429
self.opts['dirty_config'][1] = True
14301430

1431+
@property
1432+
def theme_path(self):
1433+
return path.join(self.stations_dir, 'themes', self.opts['theme'][1] + '.pyradio-theme')
1434+
14311435
@property
14321436
def auto_update_theme(self):
14331437
return self.opts['auto_update_theme'][1]

pyradio/radio.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,11 +1075,27 @@ def _change_browser_ticks(self, lineNum, sep_col):
10751075
for n in ticks:
10761076
self.bodyWin.chgat(lineNum, n, 1, sep_col)
10771077

1078-
def _watch_theme(self):
1078+
def _watch_theme(self, theme_path=None):
1079+
''' watch a file for changes
1080+
1081+
Parameters
1082+
=========
1083+
path the path to the file
1084+
if it's None, the thread is terminated
1085+
'''
1086+
if self._watch_theme_thread:
1087+
self.stop_watch_theme_thread = True
1088+
self._watch_theme_thread.join()
1089+
self._watch_theme_thread = None
1090+
self.stop_watch_theme_thread = False
1091+
if theme_path is None:
1092+
return
1093+
else:
1094+
path = theme_path
10791095
self._watch_theme_thread = threading.Thread(
10801096
target=self._wait_for_theme_to_change,
10811097
# args=(self._cnf,
1082-
args=(path.join(self._cnf.stations_dir, 'themes', 'auto.pyradio-theme'),
1098+
args=(path,
10831099
self._watch_theme_lock,
10841100
lambda: self.stop_watch_theme_thread,
10851101
self._auto_update_theme))
@@ -1175,7 +1191,6 @@ def _wait_for_theme_to_change(self, file, a_lock, stop, func):
11751191
logger.debug('File watch thread stopped on: {}'.format(file))
11761192

11771193
def run(self):
1178-
# self._watch_theme()
11791194
self._register_signals_handlers()
11801195
if self.ws.operation_mode == self.ws.DEPENDENCY_ERROR:
11811196
self.log.write(msg="Dependency missing. Press any key to exit....", error_msg=True)
@@ -1252,6 +1267,9 @@ def run(self):
12521267

12531268
self._cnf.setup_mouse()
12541269

1270+
''' start theme file thread '''
1271+
if self._cnf.auto_update_theme:
1272+
self._watch_theme(self._cnf.theme_path)
12551273
while True:
12561274
try:
12571275
c = self.bodyWin.getch()
@@ -6185,15 +6203,23 @@ def keypress(self, char):
61856203
# logger.error('4 redraw')
61866204
curses.doupdate()
61876205
''' update config window '''
6188-
if self._config_win:
6189-
self._config_win._config_options['theme'][1] = self._theme_name
6206+
if self._theme_selector.theme_path(theme_id):
6207+
if self._config_win:
6208+
self._config_win._config_options['theme'][1] = self._theme_name
61906209
if self.ws.window_mode == self.ws.CONFIG_MODE:
61916210
save_theme = True
61926211
# make default
61936212
if save_theme:
61946213
self._cnf.theme = self._theme_name
61956214
if logger.isEnabledFor(logging.INFO):
61966215
logger.info('Setting default theme: {}'.format(self._theme_name))
6216+
if self._theme_selector.theme_is_watched:
6217+
self._cnf.opts['auto_update_theme'][1] = True
6218+
self._watch_theme(self._theme_selector.theme_path(theme_id))
6219+
else:
6220+
self._cnf.opts['auto_update_theme'][1] = False
6221+
self._watch_theme()
6222+
61976223
try:
61986224
self._watch_theme_lock.release()
61996225
except:

pyradio/themes.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def __init__(self, parent, config, theme,
433433
self._applied_color_pair = applied_color_pair
434434
self._normal_color_pair = normal_color_pair
435435
self._transparent = is_transparent
436-
self._is_watched = is_watched
436+
self._theme_is_watched = is_watched
437437
self._a_lock = a_lock
438438
a_lock.acquire()
439439
if log_file:
@@ -447,6 +447,12 @@ def __init__(self, parent, config, theme,
447447
logger.error('applied_theme_name = {}'.format(applied_theme_name))
448448
logger.error('config_theme_name = {}'.format(config_theme_name))
449449
logger.error('========== Theme Window End\n\n')
450+
for n in self._cnf.opts.keys():
451+
logger.error('{0}: {1}'.format(n, self._cnf.opts[n]))
452+
453+
@property
454+
def theme_is_watched(self):
455+
return self._theme_is_watched
450456

451457
def set_global_functions(self, global_functions):
452458
self._global_functions = {}
@@ -474,6 +480,12 @@ def show(self, touch_selection=True):
474480
self._themes.extend(themes_to_add)
475481
self._items = len(self._themes)
476482
self._get_titles_ids()
483+
self._first_theme_to_watch = 0
484+
for i, n in enumerate(self._themes):
485+
if 'User Themes' in n[0]:
486+
self._first_theme_to_watch = i
487+
if self._first_theme_to_watch == 0:
488+
self._first_theme_to_watch = len(self._themes)
477489

478490
for a_theme in self._themes:
479491
if len(a_theme[0]) > self._max_title_width:
@@ -694,7 +706,7 @@ def refresh(self):
694706
col = curses.color_pair(self._normal_color_pair)
695707
self._win.hline(i + 1, 1, ' ', self._max_title_width + 2, col)
696708
if an_item == self._config_theme:
697-
if self._is_watched == self._themes[an_item][0]:
709+
if self._theme_is_watched:
698710
token = '+'
699711
else:
700712
token = '*'
@@ -832,16 +844,17 @@ def keypress(self, char):
832844
if not self.changed_from_config:
833845
self._config_theme = self._selection
834846
self._config_theme_name = self._themes[self._selection][0]
835-
if char in (ord('c'), ord('C')):
836-
if self._themes[self._selection][1]:
837-
self._applied_theme_is_watched = self._config_theme_is_watched = self._themes[self._selection][0]
838-
else:
839-
self._applied_theme_is_watched = self._config_theme_is_watched = ''
840-
if char in (ord('s'), ord('C')):
841-
''' close window '''
842-
curses.ungetch('q')
847+
self._theme_is_watched = False
848+
if char == ord('c'):
849+
if self._selection > self._first_theme_to_watch and \
850+
self._themes[self._selection][1]:
851+
logger.error('first = {0}, selection = {1}'.format(self._first_theme_to_watch, self._selection))
852+
self._theme_is_watched = True
853+
else:
854+
self._theme_is_watched = False
843855
else:
844-
self.refresh()
856+
self._theme_is_watched = False
857+
self.refresh()
845858
return self._selection, True
846859
elif char in (curses.KEY_UP, ord('k')):
847860
self.jumpnr = ''

0 commit comments

Comments
 (0)