예제 #1
0
파일: viewer.py 프로젝트: chhsiao90/gviewer
class GViewer(object):  # pragma: no cover
    """ General viewer main class

    Attributes:
        main_context: DisplayerContext
        config: a Config instance
        others: any other args defined in urwid.MainLoop
    """
    def __init__(self, main_context, config=None, other_contexts=None, **kwargs):
        config = config or Config()

        self.context = Context(config, main_context, other_contexts)
        self.view = ParentFrame(self.context)

        self._default_urwid_options(kwargs)

        self.loop = urwid.MainLoop(
            self.view, **kwargs)

    def _default_urwid_options(self, kwargs):
        """ generate default urwid options

        handle_mouse with False
        unhandled_input with "q", "Q" to exit tui
        palette with predefined template in gviewer

        """
        if "handle_mouse" not in kwargs:
            kwargs["handle_mouse"] = False

        if "unhandled_input" not in kwargs:
            kwargs["unhandled_input"] = self._default_unhandled_input

        if "palette" in kwargs:
            kwargs["palette"] = self.context.config.template + kwargs["palette"]
        else:
            kwargs["palette"] = self.context.config.template

    def _default_unhandled_input(self, key):
        if key == "q":
            self.view.back()

    def start(self):
        """ Start the gviewer tui
        """
        for displayer_context in self.context.other_contexts:
            displayer_context.store.setup()
        # NOTE: main context should setup latest so that the info of curr/max number on the footer would be correct
        self.context.main_context.store.setup()

        self.loop.run()
예제 #2
0
파일: viewer.py 프로젝트: chhsiao90/gviewer
    def __init__(self, main_context, config=None, other_contexts=None, **kwargs):
        config = config or Config()

        self.context = Context(config, main_context, other_contexts)
        self.view = ParentFrame(self.context)

        self._default_urwid_options(kwargs)

        self.loop = urwid.MainLoop(
            self.view, **kwargs)
예제 #3
0
    def setUp(self):
        self.messages = [
            ["aaa1", "aaa2", "aaa3"],
            ["bbb1", "bbb2", "bbb3"],
            ["ccc1", "ccc2", "ccc3"]
        ]
        store = StaticDataStore(self.messages)
        main_context = DisplayerContext(store, self)

        messages2 = [
            ["ddd1", "ddd2", "ddd3"],
            ["eee1", "eee2", "eee3"],
            ["fff1", "fff2", "fff3"]
        ]
        store2 = StaticDataStore(messages2)
        self.other_context = DisplayerContext(store2, self)

        context = Context(
            Config(), main_context=main_context,
            other_contexts=[self.other_context])
        self.widget = ParentFrame(context)

        store.setup()
예제 #4
0
class TestParentFrame(unittest.TestCase):
    def setUp(self):
        self.messages = [
            ["aaa1", "aaa2", "aaa3"],
            ["bbb1", "bbb2", "bbb3"],
            ["ccc1", "ccc2", "ccc3"]
        ]
        store = StaticDataStore(self.messages)
        main_context = DisplayerContext(store, self)

        messages2 = [
            ["ddd1", "ddd2", "ddd3"],
            ["eee1", "eee2", "eee3"],
            ["fff1", "fff2", "fff3"]
        ]
        store2 = StaticDataStore(messages2)
        self.other_context = DisplayerContext(store2, self)

        context = Context(
            Config(), main_context=main_context,
            other_contexts=[self.other_context])
        self.widget = ParentFrame(context)

        store.setup()

    def summary(self, message):
        return ";".join(message)

    def get_name(self):
        return "GViewer"

    def get_views(self):
        return [
            ("view1", self.view),
            ("view2", self.view)
        ]

    def view(self, message):
        return View([Group("foo", [Text(m) for m in message])])

    def test_render(self):
        self.assertEqual(
            render_to_content(self.widget, (30, 10)),
            render_widgets_to_content([
                urwid.AttrMap(urwid.Text("General Viewer"), "header"),
                urwid.AttrMap(urwid.Text(";".join(self.messages[0])), "summary"),
                urwid.AttrMap(urwid.Text(";".join(self.messages[1])), "summary"),
                urwid.AttrMap(urwid.Text(";".join(self.messages[2])), "summary"),
                urwid.Text(""),
                urwid.Text(""),
                urwid.Text(""),
                urwid.Text(""),
                Footer(helper=Helper(info_widget=urwid.Text("GViewer[1/3]")))
            ], (30, 10))
        )

    def test_initial_with_main(self):
        self.assertIs(
            self.widget.contents["body"][0],
            self.widget.main
        )

    def test_back(self):
        self.widget.open_view(BasicWidget(widget=urwid.Text("")))
        self.widget.back()
        self.assertIs(
            self.widget.contents["body"][0],
            self.widget.main
        )

    def test_back_to_exit(self):
        self.widget.back()

        self.assertEqual(self.widget.focus_position, "footer")
        self.assertIsNotNone(self.widget.footer.notification._edit_widget)
        self.assertIsInstance(
            self.widget.footer.notification._edit_widget,
            ReadonlyConfirmWidget)

        with self.assertRaises(urwid.ExitMainLoop):
            self.widget.keypress((0, 0), "q")

    def test_open_error(self):
        try:
            raise ValueError("error")
        except:
            self.widget.open_error()

        self.assertIsInstance(
            self.widget.contents["body"][0],
            ErrorWidget
        )

    def test_notify(self):
        self.widget.notify("test")

        self.assertEqual(
            render_to_text(self.widget.footer.notification, (5,)),
            ["test "])

    def test_open_edit(self):
        self.widget.open_edit(urwid.Edit("/"))
        self.assertEqual(self.widget.focus_position, "footer")
        self.assertEqual(
            render_to_text(self.widget.footer.notification, (5,)), ["/    "])

    def test_close_edit(self):
        self.widget.open_edit(urwid.Edit("/"))
        self.widget.close_edit()
        self.assertEqual(
            render_to_text(self.widget.footer.notification, (5,)), ["     "])

    def test_run_before_keypress(self):
        self.widget.notify("test")
        self.widget.run_before_keypress()
        self.assertEqual(
            render_to_text(self.widget.footer.notification, (5,)),
            ["     "])

    def test_open_view(self):
        open_view = urwid.ListBox(urwid.SimpleFocusListWalker([]))
        open_view = BasicWidget(widget=open_view)
        self.widget.open_view(open_view)

        self.assertIs(self.widget.contents["body"][0], open_view)
        self.assertEqual(len(self.widget.histories), 1)
        self.assertIs(self.widget.histories[0], self.widget.main)

    def test_open_same_view_twice(self):
        open_view = urwid.ListBox(urwid.SimpleFocusListWalker([]))
        open_view = BasicWidget(widget=open_view)
        self.widget.open_view(open_view)

        self.assertIs(self.widget.contents["body"][0], open_view)

        self.widget.open_view(open_view)
        self.assertEqual(len(self.widget.histories), 1)

    def test_open_view_by_context(self):
        self.assertEqual(len(self.widget.others), 1)
        other_widget = self.widget.others[self.other_context]
        self.assertIsInstance(other_widget, SummaryListWidget)

        self.widget.open_view_by_context(self.other_context)
        self.assertIs(
            self.widget.contents["body"][0], other_widget)
        self.assertEqual(len(self.widget.histories), 1)
        self.assertIs(self.widget.histories[0], self.widget.main)

    def test_open_view_by_context_failed(self):
        self.widget.open_view_by_context(DisplayerContext(None, None))
        self.assertIsInstance(
            self.widget.contents["body"][0], ErrorWidget)
        self.assertEqual(len(self.widget.histories), 1)
        self.assertIs(self.widget.histories[0], self.widget.main)

    def test_update_info(self):
        self.widget.update_info(
            self.widget.contents["body"][0], "test")
        self.assertEqual(
            self.widget.contents["footer"][0].helper.info_widget.text,
            "test")

        self.widget.update_info(
            urwid.Text("test"), "not update")
        self.assertEqual(
            self.widget.contents["footer"][0].helper.info_widget.text,
            "test")