Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_assemble_rows_long_text():
rows = [[get_random_text_widget(10),
get_random_text_widget(300)] for _ in range(5)]
assembled_rows = assemble_rows(rows, ignore_columns=[1])
lb = WidgetBase(SimpleListWalker(assembled_rows))
canvas = lb.render((80, 20), focus=False)
text = [bytes().join([t for at, cs, t in ln]) for ln in canvas.content()]
logging.info("%r", text)
assert len(text) == 20
first_col, second_col = text[0].split(b" ", 1)
assert first_col == rows[0][0].text.encode("utf-8")
assert rows[0][1].text.encode("utf-8").startswith(second_col)
lower_bound = 5
upper_bound = 10
list_count = 50
def loop_skeleton(greater_f, less_f, l, l_b, u_b, l_c):
# we could do while True, but then we need to clean the threads
for _ in range(100):
if len(l) > l_c:
for _ in range(random.randint(l_b, u_b)):
greater_f()
else:
for _ in range(random.randint(l_b, u_b)):
less_f()
body_widgets = [get_random_text_widget() for _ in range(100)]
body = WidgetBase(urwid.SimpleFocusListWalker(body_widgets))
frame = UI(MockUI(), body)
def add_and_remove_random():
widgets = []
def less_f():
if random.randint(1, 2) % 2:
w = get_random_text_widget()
frame.notify_widget(w)
widgets.append(w)
else:
widgets.append(frame.notify_message(get_random_text()))
def greater_f():
w = random.choice(widgets)
frame.remove_widget(w)
# regex = re.compile(regex_pattern, re.UNICODE)
# for match in regex.finditer(text):
# groups = match.groups()
# t = groups[-1]
# color_specs = groups[:-1]
# urwid_spec = translate_asci_sequence(color_specs)
# if urwid_spec:
# item = (urwid.AttrSpec(urwid_spec, "main_list_dg"), t)
# else:
# item = t
# item = urwid.AttrMap(urwid.Text(t, align="left", wrap="any"), "main_list_dg", "main_list_white")
# response.append(item)
# return response
class ScrollableListBox(WidgetBase):
def __init__(self, ui, text, focus_bottom=True):
self.walker = urwid.SimpleFocusListWalker([])
super().__init__(ui, self.walker)
self.set_text(text)
if focus_bottom:
try:
self.set_focus(len(self.walker) - 2)
except IndexError:
pass
def set_text(self, text):
self.walker.clear()
text = _ensure_unicode(text)
# logger.debug(repr(text))
text = strip_from_ansi_esc_sequences(text)
list_of_texts = text.split("\n")
except IndexError:
pass
def set_text(self, text):
self.walker.clear()
text = _ensure_unicode(text)
# logger.debug(repr(text))
text = strip_from_ansi_esc_sequences(text)
list_of_texts = text.split("\n")
self.walker[:] = [
urwid.AttrMap(urwid.Text(t.rstrip(), align="left", wrap="any"), "main_list_dg", "main_list_white")
for t in list_of_texts
]
class AsyncScrollableListBox(WidgetBase):
def __init__(self, generator, ui, static_data=None):
self.log_texts = []
if static_data:
static_data = _ensure_unicode(static_data).split("\n")
for d in static_data:
log_entry = d.rstrip()
if log_entry:
self.log_texts.append(urwid.Text(("main_list_dg", log_entry),
align="left", wrap="any"))
walker = urwid.SimpleFocusListWalker(self.log_texts)
super(AsyncScrollableListBox, self).__init__(ui, walker)
walker.set_focus(len(walker) - 1)
def fetch_logs():
line_w = urwid.AttrMap(
urwid.Text("", align="left", wrap="any"), "main_list_dg", "main_list_white"
logger = logging.getLogger(__name__)
class TagWidget(SelectableText):
"""
so we can easily access image and tag
"""
def __init__(self, docker_image, tag):
self.docker_image = docker_image
self.tag = tag
super().__init__(str(self.tag))
class ImageInfoWidget(WidgetBase, View):
"""
display info about image
"""
def __init__(self, ui, docker_image):
self.walker = urwid.SimpleFocusListWalker([])
super().__init__(ui, self.walker)
self.docker_image = docker_image
def refresh(self):
self.docker_image.refresh()
self.walker.clear()
self._basic_data()
self._containers()
self._image_names()
self._layers()
import urwid
from sen.tui.views.base import View
from sen.tui.widgets.list.util import SingleTextRow
from sen.tui.widgets.table import assemble_rows
from sen.tui.constants import MAIN_LIST_FOCUS
from sen.tui.widgets.util import SelectableText, get_map
from sen.tui.widgets.list.base import WidgetBase
class HelpBufferView(WidgetBase, View):
def __init__(self, ui, buffer, global_keybinds):
"""
:param ui:
:param buffer: Buffer instance, display help about this buffer
:param global_keybinds: dict
"""
self.ui = ui
self.buffer = buffer
self.walker = urwid.SimpleFocusListWalker([])
self.global_keybinds = global_keybinds
super().__init__(ui, self.walker)
def refresh(self):
template = [
SingleTextRow("Buffer: " + self.buffer.display_name,
class ProcessTree(urwidtrees.TreeBox):
def __init__(self, data):
tree = ProcessTreeBackend(data)
# We hide the usual arrow tip and use a customized collapse-icon.
t = urwidtrees.ArrowTree(
tree,
arrow_att="tree", # lines, tip
icon_collapsed_att="tree", # +
icon_expanded_att="tree", # -
icon_frame_att="tree", # [ ]
)
super().__init__(t)
class ContainerInfoView(WidgetBase, View):
"""
display info about container
"""
def __init__(self, ui, docker_container):
self.walker = urwid.SimpleFocusListWalker([])
super().__init__(ui, self.walker)
self.docker_container = docker_container
self.stop = threading.Event()
self.view_widgets = []
def refresh(self):
self.view_widgets.clear()
self.docker_container.refresh()
for row in data:
row_widgets = []
for idx, item in enumerate(row):
if idx in ignore_columns:
row_widgets.append(item)
else:
row_widgets.append((max_lengths[idx], item))
rows.append(
RowWidget(row_widgets, dividechars=dividechars)
)
return rows
class ResponsiveTable(WidgetBase):
def __init__(self, ui, walker, headers=None, dividechars=1, responsive=True):
"""
:param walker: list of ResponsiveRow instances
:param headers: list of widgets which should be displayed as headers
"""
self.dividechars = dividechars
self.responsive = responsive
super().__init__(ui, walker)
def render(self, size, focus=False):
screen_width = size[0]
# max text length for each column -- table
min_col_lengths = calculate_max_cols_length(self.body, size)
SingleTextRow(""),
SingleTextRow("Global Keybindings",
maps={"normal": "main_list_white", "focus": MAIN_LIST_FOCUS}),
SingleTextRow("")
]
template.extend(assemble_rows(
[[SelectableText(key, maps=get_map("main_list_yellow")),
SelectableText(command, maps=get_map("main_list_lg"))]
for key, command in self.global_keybinds.items()],
ignore_columns=[1], dividechars=3))
self.set_body(template)
self.set_focus(0)
class HelpCommandView(WidgetBase, View):
def __init__(self, ui, command):
"""
:param ui:
:param command: Command instance
"""
self.ui = ui
self.command = command
self.walker = urwid.SimpleFocusListWalker([])
super().__init__(ui, self.walker)
def refresh(self):
invocation = ["[{}=value]".format(a.name) for a in self.command.options_definitions] + \
["<{}>".format(o.name) for o in self.command.arguments_definitions]
template = [
SingleTextRow("Command: {} {}".format(self.command.name, " ".join(invocation)),