Skip to content

Commit 81e071f

Browse files
committed
Adding editor validation and error reporting
1 parent f03096b commit 81e071f

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

pyradio/edit.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
from time import sleep
55
import logging
66
from sys import version_info
7+
try:
8+
# python 3
9+
from urllib.parse import urlparse
10+
except ImportError:
11+
from urlparse import urlparse
712
from .simple_curses_widgets import SimpleCursesLineEdit
813
from .log import Log
914

@@ -403,12 +408,33 @@ def _update_focus(self):
403408
self._line_editor[1].focused = True
404409

405410
def _return_station(self):
406-
if self._encoding == 'utf-8':
407-
self._encoding = ''
408-
self.new_station = [ self._line_editor[0].string, self._line_editor[1].string, self._encoding ]
411+
ret = self._validate()
412+
if ret == 1:
413+
if self._encoding == 'utf-8':
414+
self._encoding = ''
415+
self.new_station = [ self._line_editor[0].string.strip(), self._line_editor[1].string.strip(), self._encoding ]
416+
return ret
417+
418+
def _validate(self):
419+
if not self._line_editor[0].string.strip():
420+
return -2
421+
url = urlparse(self._line_editor[1].string.strip())
422+
if not (url.scheme and url.netloc):
423+
return -3
424+
if url.scheme not in ('http', 'https'):
425+
return -3
426+
if url.netloc != 'localhost':
427+
dot = url.netloc.find('.')
428+
if dot == -1:
429+
return -3
430+
elif dot > len(url.netloc) - 3:
431+
return -3
432+
return 1
409433

410434
def keypress(self, char):
411435
""" Returns:
436+
-3: url is invalid
437+
-2: Station name is empty
412438
-1: Cancel (new_station = None)
413439
0: go on
414440
1: Ok (new_station holds data)
@@ -438,8 +464,7 @@ def keypress(self, char):
438464
return 3
439465
elif self._focus == 3:
440466
# ok
441-
self._return_station()
442-
ret = 1
467+
return self._return_station()
443468
elif self._focus == 4:
444469
# cancel
445470
self.new_station = None
@@ -448,8 +473,7 @@ def keypress(self, char):
448473
self.new_station = None
449474
ret = -1
450475
elif char == ord('s') and self._focus > 1:
451-
self._return_station
452-
ret = -1
476+
return self._return_station()
453477
elif char == ord('q') and self._focus > 1:
454478
self.new_station = None
455479
ret = -1

pyradio/radio.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ def __init__(self, pyradio_config, play=False, req_player='', theme=''):
188188
self.ws.ADD_STATION_MODE: self._show_station_editor,
189189
self.ws.EDIT_STATION_MODE: self._show_station_editor,
190190
self.ws.LINE_EDITOR_HELP: self._show_line_editor_help,
191+
self.ws.EDIT_STATION_NAME_ERROR: self._print_editor_name_error,
192+
self.ws.EDIT_STATION_URL_ERROR: self._print_editor_url_error,
191193
}
192194

193195
""" list of help functions """
@@ -1479,6 +1481,28 @@ def _print_save_playlist_error_2(self):
14791481
prompt = ' Press any key ',
14801482
is_message=True)
14811483

1484+
def _print_editor_name_error(self):
1485+
txt = '''Station Name is empty!
1486+
1487+
Please provide a Station Name.
1488+
'''
1489+
self._show_help(txt,
1490+
mode_to_set = self.ws.EDIT_STATION_NAME_ERROR,
1491+
caption = ' Error ',
1492+
prompt = ' Press any key ',
1493+
is_message=True)
1494+
1495+
def _print_editor_url_error(self):
1496+
txt = '''Station URL is invalid!
1497+
1498+
Please provide a valid Station URL.
1499+
'''
1500+
self._show_help(txt,
1501+
mode_to_set = self.ws.EDIT_STATION_URL_ERROR,
1502+
caption = ' Error ',
1503+
prompt = ' Press any key ',
1504+
is_message=True)
1505+
14821506
def _print_ask_to_create_theme(self):
14831507
txt ='''You have requested to edit a |read-only| theme,
14841508
which is not possible. Do you want to create a
@@ -2002,7 +2026,8 @@ def keypress(self, char):
20022026
self.refreshBody()
20032027
return
20042028

2005-
elif char in (ord('t'), ):
2029+
elif char in (ord('t'), ) and \
2030+
self.ws.operation_mode not in (self.ws.EDIT_STATION_MODE, self.ws.ADD_STATION_MODE):
20062031
# only open it on main modes
20072032
if self.ws.window_mode != self.ws.THEME_MODE and \
20082033
self.ws.operation_mode <= self.ws.SEARCH_PLAYLIST_MODE and \
@@ -2198,7 +2223,11 @@ def keypress(self, char):
21982223
self.volume_functions[chr(char)]()
21992224
return
22002225
ret = self._station_editor.keypress(char)
2201-
if ret == -1:
2226+
if ret == -3:
2227+
self._print_editor_url_error()
2228+
elif ret == -2:
2229+
self._print_editor_name_error()
2230+
elif ret == -1:
22022231
# Cancel
22032232
self.ws.close_window()
22042233
self._station_editor = None
@@ -2674,11 +2703,11 @@ def keypress(self, char):
26742703
self.helpWin = None
26752704
if self.ws.operation_mode == self.ws.MAIN_HELP_MODE:
26762705
self.ws.close_window()
2677-
self.refreshBody()
2706+
#self.refreshBody()
26782707
self._show_main_help_page_2()
26792708
else:
26802709
self.ws.close_window()
2681-
self.refreshBody()
2710+
#self.refreshBody()
26822711
self._show_main_help()
26832712
return
26842713
if logger.isEnabledFor(logging.DEBUG):
@@ -2824,6 +2853,7 @@ def keypress(self, char):
28242853
return
28252854

28262855
if self.ws.operation_mode == self.ws.NORMAL_MODE:
2856+
logger.error('DE here')
28272857
if char in ( ord('a'), ord('A') ):
28282858
self._station_editor = PyRadioEditor(self.stations, self.selection, self.bodyWin)
28292859
if char == ord('A'):

pyradio/window_stack.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class Window_Stack_Constants(object):
5959
FOREIGN_PLAYLIST_MESSAGE_MODE = 301
6060
FOREIGN_PLAYLIST_COPY_ERROR_MODE = 302
6161
CONFIG_SAVE_ERROR_MODE = 303
62+
EDIT_STATION_NAME_ERROR = 304
63+
EDIT_STATION_URL_ERROR = 305
6264
THEME_MODE = 400
6365
UPDATE_NOTIFICATION_MODE = 1000
6466
SESSION_LOCKED_MODE = 1001
@@ -119,6 +121,8 @@ class Window_Stack_Constants(object):
119121
ADD_STATION_MODE: 'ADD_STATION_MODE',
120122
EDIT_STATION_MODE: 'EDIT_STATION_MODE',
121123
LINE_EDITOR_HELP: 'LINE_EDITOR_HELP',
124+
EDIT_STATION_NAME_ERROR: 'EDIT_STATION_NAME_ERROR',
125+
EDIT_STATION_URL_ERROR: 'EDIT_STATION_URL_ERROR',
122126
}
123127

124128
''' When PASSIVE_WINDOWS target is one of them,
@@ -151,6 +155,8 @@ class Window_Stack_Constants(object):
151155
THEME_HELP_MODE,
152156
SEARCH_HELP_MODE,
153157
LINE_EDITOR_HELP,
158+
EDIT_STATION_NAME_ERROR,
159+
EDIT_STATION_URL_ERROR,
154160
)
155161

156162
def __init__(self):

0 commit comments

Comments
 (0)