Skip to content

Commit a548c89

Browse files
committed
- Implementing global functions for config window
- Implementing global functions for z, Z, E - Fixing a couple of bugs
1 parent 64a102f commit a548c89

File tree

4 files changed

+168
-73
lines changed

4 files changed

+168
-73
lines changed

pyradio/config_window.py

+70-16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121

2222
logger = logging.getLogger(__name__)
2323

24+
def set_global_functions(global_functions):
25+
ret = {}
26+
if global_functions is not None:
27+
ret = dict(global_functions)
28+
if 't' in ret.keys():
29+
del ret['t']
30+
del ret['T']
31+
return ret
2432

2533
class PyRadioConfigWindow(object):
2634
n_u = Window_Stack_Constants
@@ -85,13 +93,14 @@ def __init__(self, parent, config,
8593
toggle_transparency_function,
8694
show_theme_selector_function,
8795
save_parameters_function,
88-
reset_parameters_function
89-
):
96+
reset_parameters_function,
97+
global_functions=None):
9098
self.parent = parent
9199
self._cnf = config
92100
self._toggle_transparency_function = toggle_transparency_function
93101
self._show_theme_selector_function = show_theme_selector_function
94102
self._save_parameters_function = save_parameters_function
103+
self._global_functions = set_global_functions(global_functions)
95104
self._reset_parameters_function = reset_parameters_function
96105
self._saved_config_options = deepcopy(config.opts)
97106
self._config_options = deepcopy(config.opts)
@@ -354,7 +363,9 @@ def keypress(self, char):
354363
if self.too_small:
355364
return 1, []
356365
val = list(self._config_options.items())[self.selection]
357-
if val[0] == 'radiobrowser':
366+
if chr(char) in self._global_functions.keys():
367+
self._global_functions[chr(char)]()
368+
elif val[0] == 'radiobrowser':
358369
if char in (curses.KEY_RIGHT, ord('l'),
359370
ord(' '), curses.KEY_ENTER, ord('\n')):
360371
return 2, []
@@ -518,7 +529,8 @@ class PyRadioExtraParams(object):
518529

519530
def __init__(self,
520531
config,
521-
parent):
532+
parent,
533+
global_functions=None):
522534
''' setting editing to 0 so that help functions work '''
523535
self.editing = 0
524536
self._max_lines = 16
@@ -532,6 +544,7 @@ def __init__(self,
532544
self._title = ' Player Extra Parameters '
533545
self._too_small_str = 'Window too small'
534546
self._cnf.get_player_params_from_backup(param_type=1)
547+
self._global_functions = global_functions
535548
self._redisplay()
536549

537550
@property
@@ -547,7 +560,9 @@ def set_parrent(self, window):
547560
self._redisplay()
548561

549562
def _redisplay(self):
563+
logger.error('_redisplay()')
550564
pY, pX = self._parent.getmaxyx()
565+
logger.error('pY = {0}, pX = {1}'.format(pY, pX))
551566
if pY < self._max_lines + 2 or pX < 30:
552567
self._too_small = True
553568
self._win = curses.newwin(
@@ -559,6 +574,8 @@ def _redisplay(self):
559574
else:
560575
self._too_small = False
561576
self.maxX = pX - 2 if pX < 40 else 40
577+
logger.error('maxX = {}'.format(self.maxX))
578+
logger.error('max_lines = {}'.format(self._max_lines))
562579
self._win = curses.newwin(
563580
self._max_lines, self.maxX,
564581
int((pY - self._max_lines) / 2) + 2,
@@ -573,10 +590,12 @@ def _redisplay(self):
573590
self._cnf.PLAYER_NAME,
574591
self._win,
575592
lambda: True,
576-
from_config=False
593+
from_config=False,
594+
global_functions=self._global_functions
577595
)
578596

579597
def show(self):
598+
logger.error('show()')
580599
self._win.bkgdset(' ', curses.color_pair(3))
581600
self._win.erase()
582601
self._win.box()
@@ -612,13 +631,15 @@ class ExtraParametersEditor(object):
612631
def __init__(self,
613632
parent,
614633
config,
615-
string=''):
634+
string='',
635+
global_functions=None):
616636
self._parent = parent
617637
self._cnf = config
618638
self.edit_string = string
619639
self._caption = ' Parameter value '
620640
self._string = self._orig_string = string
621641

642+
self._global_functions = set_global_functions(global_functions)
622643
self.Y, self.X = self._parent.getbegyx()
623644
self.Y += 1
624645
self.X += 1
@@ -771,7 +792,10 @@ def keypress(self, char):
771792
2: Display line editor help
772793
'''
773794
ret = 1
774-
if char == ord('?') and self._focus > 0:
795+
if chr(char) in self._global_functions.keys():
796+
self._global_functions[chr(char)]()
797+
return 1
798+
elif char == ord('?') and self._focus > 0:
775799
return 2
776800
elif char in (curses.KEY_EXIT, 27, ord('q')) and \
777801
self._focus > 0:
@@ -864,9 +888,11 @@ def __init__(self,
864888
startY=1,
865889
startX=1,
866890
max_lines=11,
867-
from_config=True):
891+
from_config=True,
892+
global_functions=None):
868893
self._cnf = config
869894
self._orig_params = deepcopy(self._cnf.saved_params)
895+
self._global_functions = set_global_functions(global_functions)
870896
if logger.isEnabledFor(logging.DEBUG):
871897
logger.debug('original parameters = {}'.format(self._orig_params))
872898
self._orig_player = player
@@ -1138,7 +1164,10 @@ def keypress(self, char):
11381164
5 - add parameter
11391165
6 - line editor help
11401166
'''
1141-
if char in (
1167+
if chr(char) in self._global_functions.keys():
1168+
self._global_functions[chr(char)]()
1169+
return -1
1170+
elif char in (
11421171
curses.KEY_ENTER, ord('\n'),
11431172
ord('\r'), ord(' '), ord('l'),
11441173
curses.KEY_RIGHT, ord('s')):
@@ -1253,14 +1282,16 @@ class PyRadioSelectPlayer(object):
12531282
'''
12541283
mlength = 13
12551284

1256-
def __init__(self, config, parent, player):
1285+
def __init__(self, config, parent, player,
1286+
global_functions=None):
12571287
if logger.isEnabledFor(logging.DEBUG):
12581288
logger.debug('current players = {}'.format(player))
12591289
self._cnf = config
12601290
self._parent = parent
12611291
self._parent_maxY, self._parent_maxX = parent.getmaxyx()
12621292
self.player = player
12631293
self._orig_player = player
1294+
self._global_functions = set_global_functions(global_functions)
12641295
self.focus = True
12651296

12661297
''' Is editor active?
@@ -1432,7 +1463,9 @@ def keypress(self, char):
14321463
3 - Editor is visible
14331464
4 - Editor exited
14341465
'''
1435-
if self.editing == 0:
1466+
if chr(char) in self._global_functions.keys():
1467+
self._global_functions[chr(char)]()
1468+
elif self.editing == 0:
14361469
''' focus on players '''
14371470
if char in (9, ):
14381471
if self._players[self.selection][1]:
@@ -1598,7 +1631,8 @@ class PyRadioSelectEncodings(object):
15981631

15991632
_invalid = []
16001633

1601-
def __init__(self, maxY, maxX, encoding, config_encoding):
1634+
def __init__(self, maxY, maxX, encoding, config_encoding,
1635+
global_functions=None):
16021636
self._parent_maxY = maxY
16031637
self._parent_maxX = maxX
16041638
self.encoding = encoding
@@ -1609,6 +1643,12 @@ def __init__(self, maxY, maxX, encoding, config_encoding):
16091643
self._num_of_rows = int(len(self._encodings) / self._num_of_columns)
16101644
self.init_window()
16111645

1646+
def set_global_functions(self, global_functions):
1647+
self._global_functions = global_functions
1648+
1649+
def set_reduced_global_functions(self, global_functions):
1650+
self._global_functions = set_global_functions(global_functions)
1651+
16121652
def init_window(self, set_encoding=True):
16131653
self._win = None
16141654
self._win = curses.newwin(
@@ -1827,7 +1867,10 @@ def _col_row_to_selection(self, a_column, a_row):
18271867
def keypress(self, char):
18281868
''' Encoding key press
18291869
'''
1830-
if char in (ord('c'), ):
1870+
if chr(char) in self._global_functions.keys():
1871+
self._global_functions[chr(char)]()
1872+
1873+
elif char in (ord('c'), ):
18311874
self.encoding = self._config_encoding
18321875
self.setEncoding(self.encoding, init=True)
18331876

@@ -1951,7 +1994,8 @@ def __init__(self,
19511994
parent,
19521995
config_path,
19531996
default_playlist,
1954-
include_registers=False):
1997+
include_registers=False,
1998+
global_functions=None):
19551999
''' Select a playlist from a list
19562000
19572001
include_registers changes its behavior
@@ -1980,6 +2024,7 @@ def __init__(self,
19802024
if self._include_registers:
19812025
self._title = ' Paste: Select target '
19822026
self._playlist_in_editor = self._selected_playlist
2027+
self._global_functions = set_global_functions(global_functions)
19832028
self.init_window()
19842029

19852030
def __del__(self):
@@ -2221,7 +2266,10 @@ def keypress(self, char):
22212266
0, station path - selected station path (for paste window)
22222267
1, '' - Cancel
22232268
'''
2224-
if self._select_playlist_error == -1 or \
2269+
if chr(char) in self._global_functions.keys():
2270+
self._global_functions[chr(char)]()
2271+
2272+
elif self._select_playlist_error == -1 or \
22252273
self._select_playlist_error == 0:
22262274
self._error_win = None
22272275
self._select_playlist_error = -2
@@ -2350,12 +2398,14 @@ class PyRadioSelectStation(PyRadioSelectPlaylist):
23502398

23512399
_default_playlist = ''
23522400

2353-
def __init__(self, parent, config_path, default_playlist, default_station):
2401+
def __init__(self, parent, config_path, default_playlist, default_station,
2402+
global_functions=None):
23542403
self._default_playlist = default_playlist
23552404
self._orig_default_playlist = default_playlist
23562405
if logger.isEnabledFor(logging.INFO):
23572406
logger.info('displaying stations from: "{}"'.format(default_playlist))
23582407
PyRadioSelectPlaylist.__init__(self, parent, config_path, default_station)
2408+
self._global_functions = set_global_functions(global_functions)
23592409
self._title = ' Station Selection '
23602410
''' adding 2 to padding calculation
23612411
(i.e. no selection and random selection
@@ -2456,6 +2506,10 @@ def keypress(self, char):
24562506
self.setStation(self._orig_playlist)
24572507
return -1, ''
24582508

2509+
elif chr(char) in self._global_functions.keys():
2510+
self._global_functions[chr(char)]()
2511+
return -1, ''
2512+
24592513
return PyRadioSelectPlaylist.keypress(self, char)
24602514

24612515
# pymode:lint_ignore=W901

pyradio/edit.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1163,15 +1163,18 @@ class PyRadioConnectionType(object):
11631163
_note_text = ' Note '
11641164
_max_lines = 14
11651165

1166-
def __init__(self, parent, connection_type):
1166+
def __init__(self, parent, connection_type, global_functions=None):
11671167
self._parent = parent
1168+
self._global_functions = global_functions
1169+
if self._global_functions is None:
1170+
self._global_functions = {}
11681171
self.connection_type = connection_type
11691172

11701173
def show(self, parent=None):
11711174
if parent:
11721175
self._parent = parent
11731176
y, x = self._parent.getmaxyx()
1174-
new_y = int((y - self._max_lines) / 2) + 2
1177+
new_y = int((y - self._max_lines) / 2) + 1
11751178
new_x = int((x - len(self._text) - 9 - 4) / 2)
11761179
self.MaxX = len(self._text) + 9 + 4
11771180
self._win = None
@@ -1231,7 +1234,10 @@ def keypress(self, char):
12311234
0: go on
12321235
1: Ok
12331236
"""
1234-
if char in (curses.KEY_ENTER, ord('\n'), ord('\r'), ord('s')):
1237+
if chr(char) in self._global_functions.keys():
1238+
self._global_functions[chr(char)]()
1239+
1240+
elif char in (curses.KEY_ENTER, ord('\n'), ord('\r'), ord('s')):
12351241
return 1
12361242

12371243
elif char in (curses.KEY_EXIT, 27, ord('q'), ord('h'), curses.KEY_LEFT):

0 commit comments

Comments
 (0)