Exceptions typing in bastd ui

This commit is contained in:
Roman Trapeznikov 2020-05-21 19:20:29 +03:00
parent 9143415308
commit 976cf18247
No known key found for this signature in database
GPG Key ID: 827DD41DACE1E018
21 changed files with 45 additions and 49 deletions

View File

@ -627,8 +627,8 @@ class AccountSettingsWindow(ba.Window):
elif account_type == 'Game Circle':
account_type_name = ba.Lstr(resource='gameCircleText')
else:
raise Exception("unknown account type: '" + str(account_type) +
"'")
raise ValueError("unknown account type: '" + str(account_type) +
"'")
self._game_service_button = btn = ba.buttonwidget(
parent=self._subcontainer,
position=((self._sub_width - button_width) * 0.5, v),
@ -1105,7 +1105,7 @@ class AccountSettingsWindow(ba.Window):
elif sel == self._scrollwidget:
sel_name = 'Scroll'
else:
raise Exception('unrecognized selection')
raise ValueError('unrecognized selection')
ba.app.window_states[self.__class__.__name__] = sel_name
except Exception:
ba.print_exception('exception saving state for', self.__class__)

View File

@ -50,7 +50,7 @@ class ColorPicker(popup.PopupWindow):
c_raw = get_player_colors()
if len(c_raw) != 16:
raise Exception('expected 16 player colors')
raise ValueError('expected 16 player colors')
self.colors = [c_raw[0:4], c_raw[4:8], c_raw[8:12], c_raw[12:16]]
if scale is None:
@ -189,7 +189,7 @@ class ColorPickerExact(popup.PopupWindow):
from ba.internal import get_player_colors
c_raw = get_player_colors()
if len(c_raw) != 16:
raise Exception('expected 16 player colors')
raise ValueError('expected 16 player colors')
self.colors = [c_raw[0:4], c_raw[4:8], c_raw[8:12], c_raw[12:16]]
if scale is None:

View File

@ -1555,7 +1555,7 @@ class CoopBrowserWindow(ba.Window):
elif sel == self._scrollwidget:
sel_name = 'Scroll'
else:
raise Exception('unrecognized selection')
raise ValueError('unrecognized selection')
ba.app.window_states[self.__class__.__name__] = {
'sel_name': sel_name
}

View File

@ -807,11 +807,8 @@ class GatherWindow(ba.Window):
color=(1, 0, 0))
ba.playsound(ba.getsound('error'))
return
try:
port = int(cast(str, ba.textwidget(query=port_textwidget)))
if port > 65535 or port < 0:
raise Exception()
except Exception:
port = int(cast(str, ba.textwidget(query=port_textwidget)))
if port > 65535 or port < 0:
ba.screenmessage(
ba.Lstr(resource='internal.invalidPortErrorText'),
color=(1, 0, 0))
@ -1958,7 +1955,7 @@ class GatherWindow(ba.Window):
elif sel == self._tab_container:
sel_name = 'TabContainer'
else:
raise Exception('unrecognized selection: ' + str(sel))
raise ValueError(f'unrecognized selection: \'{sel}\'')
ba.app.window_states[self.__class__.__name__] = {
'sel_name': sel_name,
'tab': self._current_tab,

View File

@ -49,7 +49,7 @@ class MainMenuWindow(ba.Window):
super().__init__(root_widget=ba.containerwidget(
transition=transition,
toolbar_visibility='menu_minimal_no_back' if self.
_in_game else 'menu_minimal_no_back'))
_in_game else 'menu_minimal_no_back'))
self._is_kiosk = ba.app.kiosk_mode
self._tdelay = 0.0
@ -95,7 +95,7 @@ class MainMenuWindow(ba.Window):
_ba.get_local_active_input_devices_count()
if (((app.on_tv or app.platform == 'mac')
and ba.app.config.get('launchCount', 0) <= 1)
or force_test):
or force_test):
def _check_show_bs_remote_window() -> None:
try:
@ -131,7 +131,7 @@ class MainMenuWindow(ba.Window):
store_char_tex = self._get_store_char_tex()
account_state_num = _ba.get_account_state_num()
if (account_state_num != self._account_state_num
or store_char_tex != self._store_char_tex):
or store_char_tex != self._store_char_tex):
self._store_char_tex = store_char_tex
self._account_state_num = account_state_num
account_state = self._account_state = (_ba.get_account_state())
@ -294,8 +294,8 @@ class MainMenuWindow(ba.Window):
size=(self._button_width, self._button_height),
scale=scale,
label=ba.Lstr(resource=self._r +
('.quitText' if 'Mac' in
ba.app.user_agent_string else '.exitGameText')),
('.quitText' if 'Mac' in
ba.app.user_agent_string else '.exitGameText')),
on_activate_call=self._quit,
transition_delay=self._tdelay)
@ -320,8 +320,7 @@ class MainMenuWindow(ba.Window):
# If we're not in-game, have no quit button, and this is android,
# we want back presses to quit our activity.
if (not self._in_game and not self._have_quit_button
and ba.app.platform == 'android'):
and ba.app.platform == 'android'):
def _do_quit() -> None:
confirm.QuitWindow(swish=True, back=True)
@ -671,10 +670,10 @@ class MainMenuWindow(ba.Window):
custom_menu_entries = session.get_custom_menu_entries()
for cme in custom_menu_entries:
if (not isinstance(cme, dict) or 'label' not in cme
or not isinstance(cme['label'], (str, ba.Lstr))
or 'call' not in cme or not callable(cme['call'])):
raise Exception('invalid custom menu entry: ' +
str(cme))
or not isinstance(cme['label'], (str, ba.Lstr))
or 'call' not in cme or not callable(cme['call'])):
raise ValueError('invalid custom menu entry: ' +
str(cme))
except Exception:
custom_menu_entries = []
ba.print_exception('exception getting custom menu entries for',
@ -757,7 +756,7 @@ class MainMenuWindow(ba.Window):
autoselect=self._use_autoselect)
# Add a 'leave' button if the menu-owner has a player.
if ((self._input_player or self._connected_to_remote_player)
and not self._is_kiosk):
and not self._is_kiosk):
h, v, scale = positions[self._p_index]
self._p_index += 1
btn = ba.buttonwidget(parent=self._root_widget,
@ -770,7 +769,7 @@ class MainMenuWindow(ba.Window):
autoselect=self._use_autoselect)
if (player_name != '' and player_name[0] != '<'
and player_name[-1] != '>'):
and player_name[-1] != '>'):
txt = ba.Lstr(resource=self._r + '.justPlayerText',
subs=[('${NAME}', player_name)])
else:
@ -866,7 +865,7 @@ class MainMenuWindow(ba.Window):
# Select cancel by default; this occasionally gets called by accident
# in a fit of button mashing and this will help reduce damage.
confirm.ConfirmWindow(ba.Lstr(resource=self._r +
'.leavePartyConfirmText'),
'.leavePartyConfirmText'),
self._leave_party,
cancel_is_selected=True)

View File

@ -538,7 +538,7 @@ class PlayWindow(ba.Window):
elif sel == self._back_button:
sel_name = 'Back'
else:
raise Exception('unrecognized selected widget')
raise ValueError(f'unrecognized selection {sel}')
ba.app.window_states[self.__class__.__name__] = sel_name
except Exception:
ba.print_exception('error saving state for', self.__class__)

View File

@ -58,7 +58,7 @@ class PlaylistTypeVars:
fallback_resource='freeForAllText')
self.sessiontype = ba.FreeForAllSession
else:
raise Exception('playlist type vars undefined for session type: ' +
raise TypeError('playlist type vars undefined for session type: ' +
str(sessiontype))
self.default_list_name = ba.Lstr(resource='defaultGameListNameText',
subs=[('${PLAYMODE}', play_mode_name)

View File

@ -62,7 +62,7 @@ class PlaylistBrowserWindow(ba.Window):
ba.app.main_window = 'Free-for-All Game Select'
ba.set_analytics_screen('FreeForAll Window')
else:
raise Exception(f'invalid sessiontype: {sessiontype}')
raise TypeError(f'invalid sessiontype: {sessiontype}')
self._pvars = playlist.PlaylistTypeVars(sessiontype)
self._sessiontype = sessiontype

View File

@ -264,19 +264,19 @@ class PlaylistEditGameWindow(ba.Window):
if 'choices' in setting:
for choice in setting['choices']:
if len(choice) != 2:
raise Exception(
raise ValueError(
"Expected 2-member tuples for 'choices'; got: " +
repr(choice))
if not isinstance(choice[0], str):
raise Exception(
raise TypeError(
'First value for choice tuple must be a str; got: '
+ repr(choice))
if not isinstance(choice[1], value_type):
raise Exception(
raise TypeError(
'Choice type does not match default value; choice:'
+ repr(choice) + '; setting:' + repr(setting))
if value_type not in (int, float):
raise Exception(
raise TypeError(
'Choice type setting must have int or float default; '
'got: ' + repr(setting))
@ -509,5 +509,5 @@ class PlaylistEditGameWindow(ba.Window):
elif setting_type == int:
ba.textwidget(edit=ctrl, text=str(int(val)))
else:
raise Exception('invalid vartype: ' + str(setting_type))
raise TypeError('invalid vartype: ' + str(setting_type))
self._settings[setting_name] = val

View File

@ -93,7 +93,7 @@ class PlayOptionsWindow(popup.PopupWindow):
elif self._sessiontype is ba.DualTeamSession:
plst = get_default_teams_playlist()
else:
raise Exception('unrecognized session-type: ' +
raise TypeError('unrecognized session-type: ' +
str(self._sessiontype))
else:
try:

View File

@ -153,7 +153,7 @@ class PopupMenuWindow(PopupWindow):
self._choices_disabled = list(choices_disabled)
self._done_building = False
if not choices:
raise Exception('Must pass at least one choice')
raise TypeError('Must pass at least one choice')
self._width = width
self._scale = scale
if len(choices) > 8:
@ -302,7 +302,7 @@ class PopupMenu:
current_choice = None
self._choices = list(choices)
if not choices:
raise Exception('no choices given')
raise TypeError('no choices given')
self._choices_display = list(choices_display)
self._choices_disabled = list(choices_disabled)
self._width = width
@ -313,7 +313,7 @@ class PopupMenu:
self._position = position
self._parent = parent
if not choices:
raise Exception('Must pass at least one choice')
raise TypeError('Must pass at least one choice')
self._parent = parent
self._button_size = button_size

View File

@ -546,7 +546,7 @@ class EditProfileWindow(ba.Window):
elif picker_type == 'highlight':
initial_color = self._highlight
else:
raise Exception('invalid picker_type: ' + picker_type)
raise ValueError('invalid picker_type: ' + picker_type)
colorpicker.ColorPicker(
parent=self._root_widget,
position=origin,

View File

@ -44,7 +44,7 @@ class PurchaseWindow(ba.Window):
header_text = ba.Lstr(resource='unlockThisText',
fallback_resource='unlockThisInTheStoreText')
if len(items) != 1:
raise Exception('expected exactly 1 item')
raise ValueError('expected exactly 1 item')
self._items = list(items)
self._width = 580
self._height = 520

View File

@ -596,11 +596,11 @@ class AdvancedSettingsWindow(ba.Window):
elif sel == self._language_inform_checkbox:
sel_name = 'LangInform'
else:
raise Exception('unrecognized selection')
raise ValueError(f'unrecognized selection \'{sel}\'')
elif sel == self._back_button:
sel_name = 'Back'
else:
raise Exception('unrecognized selection')
raise ValueError(f'unrecognized selection \'{sel}\'')
ba.app.window_states[self.__class__.__name__] = {
'sel_name': sel_name
}

View File

@ -252,7 +252,7 @@ class AllSettingsWindow(ba.Window):
elif sel == self._back_button:
sel_name = 'Back'
else:
raise Exception('unrecognized selection')
raise ValueError(f'unrecognized selection \'{sel}\'')
ba.app.window_states[self.__class__.__name__] = {
'sel_name': sel_name
}

View File

@ -266,7 +266,7 @@ class AudioSettingsWindow(ba.Window):
elif sel == self._vr_head_relative_audio_button:
sel_name = 'VRHeadRelative'
else:
raise Exception('unrecognized selected widget')
raise ValueError(f'unrecognized selection \'{sel}\'')
ba.app.window_states[self.__class__.__name__] = sel_name
except Exception:
ba.print_exception('error saving state for', self.__class__)

View File

@ -167,7 +167,7 @@ class TestingWindow(ba.Window):
for entry in self._entries:
if entry['name'] == name:
return entry
raise Exception(f'Entry not found: {name}')
raise ba.NotFoundError(f'Entry not found: {name}')
def _on_reset_press(self) -> None:
for entry in self._entries:

View File

@ -485,7 +485,7 @@ class SoundtrackBrowserWindow(ba.Window):
elif sel == self._back_button:
sel_name = 'Back'
else:
raise Exception('unrecognized selection')
raise ValueError(f'unrecognized selection \'{sel}\'')
ba.app.window_states[self.__class__.__name__] = sel_name
except Exception:
ba.print_exception('error saving state for', self.__class__)

View File

@ -1002,7 +1002,7 @@ class StoreBrowserWindow(ba.Window):
sel_name = 'Tab:' + list(self._tab_buttons.keys())[list(
self._tab_buttons.values()).index(sel)]
else:
raise Exception('unrecognized selection')
raise ValueError(f'unrecognized selection \'{sel}\'')
ba.app.window_states[self.__class__.__name__] = {
'sel_name': sel_name,
'tab': self._current_tab

View File

@ -69,7 +69,7 @@ class TournamentEntryWindow(popup.PopupWindow):
self._purchase_price_name = 'price.tournament_entry_1'
else:
if self._fee != 0:
raise Exception('invalid fee: ' + str(self._fee))
raise ValueError('invalid fee: ' + str(self._fee))
self._purchase_name = 'tournament_entry_0'
self._purchase_price_name = 'price.tournament_entry_0'

View File

@ -482,7 +482,7 @@ class WatchWindow(ba.Window):
elif sel == self._tab_container:
sel_name = 'TabContainer'
else:
raise Exception('unrecognized selection')
raise ValueError(f'unrecognized selection {sel}')
ba.app.window_states[self.__class__.__name__] = {
'sel_name': sel_name,
'tab': self._current_tab