pep8 fixes for subiquitycore/ui/container.py

This commit is contained in:
Ryan Harper 2018-05-22 11:33:55 -05:00
parent 2d6cca8281
commit 860edbc7b5
1 changed files with 52 additions and 34 deletions

View File

@ -14,26 +14,28 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# This is adapted from # This is adapted from
# https://github.com/pimutils/khal/commit/bd7c5f928a7670de9afae5657e66c6dc846688ac, which has this license: # https://github.com/pimutils/khal/commit/bd7c5f928a7670de9afae5657e66c6dc846688ac # noqa
#
# #
# Copyright (c) 2013-2015 Christian Geier et al. # Copyright (c) 2013-2015 Christian Geier et al.
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy of # Permission is hereby granted, free of charge, to any person obtaining a copy
# this software and associated documentation files (the "Software"), to deal in # of this software and associated documentation files (the "Software"), to
# the Software without restriction, including without limitation the rights to # deal in the Software without restriction, including without limitation the
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# the Software, and to permit persons to whom the Software is furnished to do so, # sell copies of the Software, and to permit persons to whom the Software is
# subject to the following conditions: # furnished to do so, subject to the following conditions:
# #
# The above copyright notice and this permission notice shall be included in all # The above copyright notice and this permission notice shall be included in
# copies or substantial portions of the Software. # all copies or substantial portions of the Software.
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER # AUTHORS OR OPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Extended versions of urwid containers. """Extended versions of urwid containers.
@ -107,7 +109,6 @@ def _has_other_selectable(widgets, cur_focus):
return False return False
for key, command in list(urwid.command_map._command.items()): for key, command in list(urwid.command_map._command.items()):
if command in ('next selectable', 'prev selectable', urwid.ACTIVATE): if command in ('next selectable', 'prev selectable', urwid.ACTIVATE):
urwid.command_map[key + ' no wrap'] = command urwid.command_map[key + ' no wrap'] = command
@ -149,7 +150,7 @@ class TabCyclingPile(urwid.Pile):
# reason we can't do this via simple subclassing is the need to # reason we can't do this via simple subclassing is the need to
# call _select_{first,last}_selectable on the newly focused # call _select_{first,last}_selectable on the newly focused
# element when the focus changes. # element when the focus changes.
def keypress(self, size, key ): def keypress(self, size, key):
"""Pass the keypress to the widget in focus. """Pass the keypress to the widget in focus.
Unhandled 'up' and 'down' keys may cause a focus change.""" Unhandled 'up' and 'down' keys may cause a focus change."""
if not self.contents: if not self.contents:
@ -157,7 +158,9 @@ class TabCyclingPile(urwid.Pile):
# start subiquity change: new code # start subiquity change: new code
downkey = key downkey = key
if not key.endswith(' no wrap') and self._command_map[key] in ('next selectable', 'prev selectable'): if not key.endswith(' no wrap') and (self._command_map[key] in
('next selectable',
'prev selectable')):
if _has_other_selectable(self._widgets(), self.focus_position): if _has_other_selectable(self._widgets(), self.focus_position):
downkey += ' no wrap' downkey += ' no wrap'
# end subiquity change # end subiquity change
@ -169,9 +172,12 @@ class TabCyclingPile(urwid.Pile):
i = self.focus_position i = self.focus_position
if self.selectable(): if self.selectable():
tsize = self.get_item_size(size, i, True, item_rows) tsize = self.get_item_size(size, i, True, item_rows)
# start subiquity change: pass downkey to focus, not key, do not return if command_map[upkey] is next/prev selectable # start subiquity change: pass downkey to focus, not key,
# do not return if command_map[upkey] is next/prev selectable
upkey = self.focus.keypress(tsize, downkey) upkey = self.focus.keypress(tsize, downkey)
if self._command_map[upkey] not in ('cursor up', 'cursor down', 'next selectable', 'prev selectable'): if self._command_map[upkey] not in ('cursor up', 'cursor down',
'next selectable',
'prev selectable'):
return upkey return upkey
# end subiquity change # end subiquity change
@ -187,7 +193,8 @@ class TabCyclingPile(urwid.Pile):
self._select_first_selectable() self._select_first_selectable()
return key return key
elif self._command_map[key] == 'prev selectable': elif self._command_map[key] == 'prev selectable':
for i, (w, o) in reversed(list(enumerate(self._contents[:self.focus_position]))): positions = self._contents[:self.focus_position]
for i, (w, o) in reversed(list(enumerate(positions))):
if w.selectable(): if w.selectable():
self.set_focus(i) self.set_focus(i)
_maybe_call(w, "_select_last_selectable") _maybe_call(w, "_select_last_selectable")
@ -197,9 +204,9 @@ class TabCyclingPile(urwid.Pile):
return key return key
# continued subiquity change: set 'meth' appropriately # continued subiquity change: set 'meth' appropriately
elif self._command_map[key] == 'cursor up': elif self._command_map[key] == 'cursor up':
candidates = list(range(i-1, -1, -1)) # count backwards to 0 candidates = list(range(i-1, -1, -1)) # count backwards to 0
meth = '_select_last_selectable' meth = '_select_last_selectable'
else: # self._command_map[key] == 'cursor down' else: # self._command_map[key] == 'cursor down'
candidates = list(range(i+1, len(self.contents))) candidates = list(range(i+1, len(self.contents)))
meth = '_select_first_selectable' meth = '_select_first_selectable'
# end subiquity change # end subiquity change
@ -222,7 +229,7 @@ class TabCyclingPile(urwid.Pile):
rows = item_rows[j] rows = item_rows[j]
if self._command_map[key] == 'cursor up': if self._command_map[key] == 'cursor up':
rowlist = list(range(rows-1, -1, -1)) rowlist = list(range(rows-1, -1, -1))
else: # self._command_map[key] == 'cursor down' else: # self._command_map[key] == 'cursor down'
rowlist = list(range(rows)) rowlist = list(range(rows))
for row in rowlist: for row in rowlist:
tsize = self.get_item_size(size, j, True, item_rows) tsize = self.get_item_size(size, j, True, item_rows)
@ -237,15 +244,16 @@ class TabCyclingPile(urwid.Pile):
class OneSelectableColumns(urwid.Columns): class OneSelectableColumns(urwid.Columns):
def __init__(self, widget_list, dividechars=0, focus_column=None, def __init__(self, widget_list, dividechars=0, focus_column=None,
min_width=1, box_columns=None): min_width=1, box_columns=None):
super().__init__(widget_list, dividechars, focus_column, min_width, box_columns) super().__init__(widget_list, dividechars, focus_column,
min_width, box_columns)
selectables = 0 selectables = 0
for w, o in self._contents: for w, o in self._contents:
if w.selectable(): if w.selectable():
selectables +=1 selectables += 1
if selectables > 1: if selectables > 1:
raise Exception("subiquity only supports one selectable in a Columns") raise Exception(
"subiquity only supports one selectable in a Columns")
class TabCyclingListBox(urwid.ListBox): class TabCyclingListBox(urwid.ListBox):
@ -288,7 +296,9 @@ class TabCyclingListBox(urwid.ListBox):
def keypress(self, size, key): def keypress(self, size, key):
downkey = key downkey = key
if not key.endswith(' no wrap') and self._command_map[key] in ('next selectable', 'prev selectable'): if not key.endswith(' no wrap') and (self._command_map[key] in
('next selectable',
'prev selectable')):
if _has_other_selectable(self.body, self.focus_position): if _has_other_selectable(self.body, self.focus_position):
downkey += ' no wrap' downkey += ' no wrap'
upkey = super().keypress(size, downkey) upkey = super().keypress(size, downkey)
@ -309,7 +319,8 @@ class TabCyclingListBox(urwid.ListBox):
self._select_first_selectable() self._select_first_selectable()
return key return key
elif self._command_map[key] == 'prev selectable': elif self._command_map[key] == 'prev selectable':
for i, w in reversed(list(enumerate(self.body[:self.focus_position]))): positions = self.body[:self.focus_position]
for i, w in reversed(list(enumerate(positions))):
if w.selectable(): if w.selectable():
self.set_focus(i) self.set_focus(i)
_maybe_call(w, "_select_last_selectable") _maybe_call(w, "_select_last_selectable")
@ -352,6 +363,7 @@ class FocusTrackingMixin:
class FocusTrackingPile(FocusTrackingMixin, TabCyclingPile): class FocusTrackingPile(FocusTrackingMixin, TabCyclingPile):
pass pass
class FocusTrackingColumns(FocusTrackingMixin, OneSelectableColumns): class FocusTrackingColumns(FocusTrackingMixin, OneSelectableColumns):
pass pass
@ -386,6 +398,7 @@ class FocusTrackingListBox(TabCyclingListBox):
Columns = FocusTrackingColumns Columns = FocusTrackingColumns
Pile = FocusTrackingPile Pile = FocusTrackingPile
class ScrollBarListBox(urwid.WidgetDecoration): class ScrollBarListBox(urwid.WidgetDecoration):
def __init__(self, lb): def __init__(self, lb):
@ -396,9 +409,11 @@ class ScrollBarListBox(urwid.WidgetDecoration):
f("\N{FULL BLOCK}", 'scrollbar_fg'), f("\N{FULL BLOCK}", 'scrollbar_fg'),
] ]
self.bar = Pile([ self.bar = Pile([
('weight', 1, f("\N{BOX DRAWINGS LIGHT VERTICAL}", 'scrollbar_bg')), ('weight', 1, f("\N{BOX DRAWINGS LIGHT VERTICAL}",
'scrollbar_bg')),
('weight', 1, self.boxes[0]), ('weight', 1, self.boxes[0]),
('weight', 1, f("\N{BOX DRAWINGS LIGHT VERTICAL}", 'scrollbar_bg')), ('weight', 1, f("\N{BOX DRAWINGS LIGHT VERTICAL}",
'scrollbar_bg')),
]) ])
super().__init__(lb) super().__init__(lb)
@ -418,7 +433,9 @@ class ScrollBarListBox(urwid.WidgetDecoration):
# case for all the listboxes we have in subiquity today. # case for all the listboxes we have in subiquity today.
maxcol, maxrow = size maxcol, maxrow = size
offset, inset = self.original_widget.get_focus_offset_inset((maxcol - 1, maxrow)) offset, inset = (
self.original_widget.get_focus_offset_inset((maxcol - 1,
maxrow)))
seen_focus = False seen_focus = False
height = height_before_focus = 0 height = height_before_focus = 0
@ -464,7 +481,8 @@ class ScrollBarListBox(urwid.WidgetDecoration):
(self.bar.contents[2][0], self.bar.options('weight', bottom)), (self.bar.contents[2][0], self.bar.options('weight', bottom)),
] ]
canvases = [ canvases = [
(self.original_widget.render((maxcol - 1, maxrow), focus), self.original_widget.focus_position, True, maxcol - 1), (self.original_widget.render((maxcol - 1, maxrow), focus),
self.original_widget.focus_position, True, maxcol - 1),
(self.bar.render((1, maxrow)), None, False, 1) (self.bar.render((1, maxrow)), None, False, 1)
] ]
return urwid.CanvasJoin(canvases) return urwid.CanvasJoin(canvases)