[node-patches] Change in ovirt-node[master]: Initial commit

fabiand at fedoraproject.org fabiand at fedoraproject.org
Tue Dec 11 20:09:31 UTC 2012


Fabian Deutsch has uploaded a new change for review.

Change subject: Initial commit
......................................................................

Initial commit

Change-Id: Ia1e7daff5720a06dcf99bb13d87322d0f0f5db5f
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
A scripts/tui/.gitignore
A scripts/tui/app
A scripts/tui/ovirt/__init__.py
A scripts/tui/ovirt/node/__init__.py
A scripts/tui/ovirt/node/plugins/__init__.py
A scripts/tui/ovirt/node/plugins/example.py
A scripts/tui/ovirt/node/plugins/example2.py
A scripts/tui/ovirt/node/tui.py
8 files changed, 285 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/59/9859/1

diff --git a/scripts/tui/.gitignore b/scripts/tui/.gitignore
new file mode 100644
index 0000000..c7c62df
--- /dev/null
+++ b/scripts/tui/.gitignore
@@ -0,0 +1,2 @@
+*~
+app.log
diff --git a/scripts/tui/app b/scripts/tui/app
new file mode 100755
index 0000000..b4cbd9d
--- /dev/null
+++ b/scripts/tui/app
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+PYTHONPATH=$PYTHONPATH:. python -B -m ovirt.node.tui "$@"
diff --git a/scripts/tui/ovirt/__init__.py b/scripts/tui/ovirt/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/scripts/tui/ovirt/__init__.py
diff --git a/scripts/tui/ovirt/node/__init__.py b/scripts/tui/ovirt/node/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/scripts/tui/ovirt/node/__init__.py
diff --git a/scripts/tui/ovirt/node/plugins/__init__.py b/scripts/tui/ovirt/node/plugins/__init__.py
new file mode 100644
index 0000000..73f1b17
--- /dev/null
+++ b/scripts/tui/ovirt/node/plugins/__init__.py
@@ -0,0 +1,51 @@
+
+import os
+import pkgutil
+import logging
+
+import ovirt.node.plugins
+
+LOGGER = logging.getLogger(__name__)
+
+def __walk_plugins():
+    package = ovirt.node.plugins
+    for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
+        yield (importer, modname, ispkg)
+
+
+def load_all():
+    modules = []
+    for importer, modname, ispkg in __walk_plugins():
+        #print("Found submodule %s (is a package: %s)" % (modname, ispkg))
+        module = __import__("ovirt.node.plugins." + modname, fromlist="dummy")
+        #print("Imported", module)
+        modules += [module]
+    return modules
+
+
+class NodePlugin(object):
+    def name(self):
+        raise Exception("Not yet implemented.")
+
+    def ui_name(self):
+        return self.name()
+
+    def ui_on_change(self, model):
+        """Called when some widget was changed
+        """
+        LOGGER.debug("changed: " + str(model))
+
+    def ui_on_save(self, model):
+        """Called when data should be saved
+        """
+        LOGGER.debug("saved")
+
+
+class Label(object):
+    def __init__(self, label):
+        self.label = label
+
+class Entry(object):
+    def __init__(self, path, label):
+        self.path = path
+        self.label = label
diff --git a/scripts/tui/ovirt/node/plugins/example.py b/scripts/tui/ovirt/node/plugins/example.py
new file mode 100644
index 0000000..baecdcd
--- /dev/null
+++ b/scripts/tui/ovirt/node/plugins/example.py
@@ -0,0 +1,21 @@
+
+
+import os.path
+import logging
+
+import ovirt.node.plugins
+
+LOGGER = logging.getLogger(__name__)
+
+class Plugin(ovirt.node.plugins.NodePlugin):
+    def __init__(self, tui):
+        pass
+
+    def name(self):
+        return os.path.basename(__file__)
+
+    def ui_content(self):
+        widgets = []
+        widgets.append(ovirt.node.plugins.Label("Subsection"))
+        widgets.append(ovirt.node.plugins.Entry("foo.bar", label=__file__))
+        return widgets
diff --git a/scripts/tui/ovirt/node/plugins/example2.py b/scripts/tui/ovirt/node/plugins/example2.py
new file mode 100644
index 0000000..baecdcd
--- /dev/null
+++ b/scripts/tui/ovirt/node/plugins/example2.py
@@ -0,0 +1,21 @@
+
+
+import os.path
+import logging
+
+import ovirt.node.plugins
+
+LOGGER = logging.getLogger(__name__)
+
+class Plugin(ovirt.node.plugins.NodePlugin):
+    def __init__(self, tui):
+        pass
+
+    def name(self):
+        return os.path.basename(__file__)
+
+    def ui_content(self):
+        widgets = []
+        widgets.append(ovirt.node.plugins.Label("Subsection"))
+        widgets.append(ovirt.node.plugins.Entry("foo.bar", label=__file__))
+        return widgets
diff --git a/scripts/tui/ovirt/node/tui.py b/scripts/tui/ovirt/node/tui.py
new file mode 100644
index 0000000..5041517
--- /dev/null
+++ b/scripts/tui/ovirt/node/tui.py
@@ -0,0 +1,187 @@
+#!/bin/env python
+
+import urwid
+
+import logging
+import os
+
+import ovirt.node
+import ovirt.node.plugins
+
+
+logging.basicConfig(level=logging.DEBUG,
+                    filename="app.log")
+LOGGER = logging.getLogger(__name__)
+
+
+class SelectableText(urwid.Text):
+    def selectable(self):
+        return True
+    def keypress(self, size, key):
+        return key
+
+
+class UrwidTUI(object):
+    __pages = {}
+    __hotkeys = {}
+
+    __loop = None
+    __main_frame = None
+    __page_frame = None
+
+    header = u"\n Configuration TUI\n"
+    footer = u"Press ctrl+c to exit"
+
+    palette = [('header', 'white', 'dark blue'),
+               ('reveal focus', 'white', 'light blue', 'standout'),
+               ('main.menu', 'black', ''),
+               ('main.menu.frame', 'light gray', ''),
+               ('plugin.entry', 'dark gray', ''),
+               ('plugin.entry.frame', 'light gray', ''),]
+
+    def __init__(self):
+        pass
+
+    def page_selected(self, widget, user_data):
+        LOGGER.debug(user_data)
+
+    def __pages_list(self):
+        items = []
+        for title, plugin in self.__pages.items():
+            item = SelectableText(title)
+            item.plugin = plugin
+            item = urwid.AttrMap(item, None, 'reveal focus')
+            items.append(item)
+        walker = urwid.SimpleListWalker(items)
+        listbox = urwid.ListBox(walker)
+        def __on_page_change():
+            widget, position = listbox.get_focus()
+            plugin = widget.original_widget.plugin
+            page = self.__build_plugin_widget(plugin)
+            self.__page_frame.body = page
+        urwid.connect_signal(walker, 'modified', __on_page_change)
+        attr_listbox = urwid.AttrMap(listbox, "main.menu")
+        return attr_listbox
+
+    def __create_screen(self):
+        menu = urwid.LineBox(self.__pages_list())
+        menu = urwid.AttrMap(menu, "main.menu.frame")
+        self.__page_frame = urwid.Frame(urwid.Filler(urwid.Text("-")))
+        page = self.__page_frame
+        body = urwid.Columns([
+            ("weight", 0.5, menu), page
+            ], 4)
+        header = urwid.Text(self.header, wrap='clip')
+        header = urwid.AttrMap(header, 'header')
+        footer = urwid.Text(self.footer, wrap='clip')
+        return urwid.Frame(body, header, footer)
+
+    def __build_plugin_widget(self, plugin):
+        """This method is building the widget for a plugin
+        """
+        widgets = []
+        for item in plugin.ui_content():
+            widget = None
+            if type(item) is ovirt.node.plugins.Entry:
+                label = urwid.Filler(urwid.Text(item.label + ":"))
+                edit = urwid.Edit()
+                def on_change(a, b):
+                    plugin.ui_on_change({item.path: edit.get_text()[0]})
+                urwid.connect_signal(edit, 'change', on_change)
+                entry = edit
+                entry = urwid.AttrMap(entry, "plugin.entry")
+                entry = urwid.LineBox(entry)
+                entry = urwid.AttrMap(entry, "plugin.entry.frame")
+                entry = urwid.Filler(entry)
+                widget = urwid.Columns([label, entry])
+            elif type(item) is ovirt.node.plugins.Label:
+                widget = urwid.Filler(urwid.Text(item.label))
+                widget = urwid.AttrMap(widget, "plugin.label")
+            widgets.append(widget)
+
+        save = urwid.Button("Save", plugin.ui_on_save)
+        save = urwid.Padding(save, "left", width=8)
+        save = urwid.Filler(save, ("fixed bottom", 1))
+        widgets.append(save)
+
+        widget = urwid.Pile(widgets)
+        return widget
+
+    def __filter_hotkeys(self, keys, raw):
+        key = str(keys)
+        LOGGER.debug("Keypress: %s" % key)
+        if key in self.__hotkeys.keys():
+            self.__hotkeys[key]()
+        return keys
+
+    def __register_default_hotkeys(self):
+        self.register_hotkey(["esc"], self.quit)
+        self.register_hotkey(["q"], self.quit)
+
+
+    def suspend(self):
+        urwid.raw_display.Screen.stop()
+
+    def resume(self):
+        urwid.raw_display.Screen.start()
+
+    def register_plugin(self, title, plugin):
+        """Register a plugin to be shown in the UI
+        """
+        self.__pages[title] = plugin
+
+    def register_hotkey(self, hotkey, cb):
+        """Register a hotkey
+        """
+        if type(hotkey) is str:
+            hotkey = [hotkey]
+        LOGGER.debug("Registering hotkey '%s': %s" % (hotkey, cb))
+        self.__hotkeys[str(hotkey)] = cb
+
+    def quit(self):
+        """Quit the UI
+        """
+        raise urwid.ExitMainLoop()
+
+    def run(self):
+        """Run the UI
+        """
+        self.__main_frame = self.__create_screen()
+        self.__register_default_hotkeys()
+
+        self.__loop = urwid.MainLoop(self.__main_frame,
+                              self.palette,
+                              input_filter=self.__filter_hotkeys)
+        self.__loop.run()
+
+
+class App(object):
+    plugins = []
+
+    ui = None
+
+    def __init__(self, ui):
+        self.ui = ui
+
+    def __load_plugins(self):
+        self.plugins = [m.Plugin(self) for m in ovirt.node.plugins.load_all()]
+
+        for plugin in self.plugins:
+            LOGGER.debug("Adding plugin " + plugin.name())
+            self.ui.register_plugin(plugin.ui_name(), plugin)
+
+    def __drop_to_shell(self):
+        self.ui.suspend()
+        os.system("reset ; bash")
+        self.ui.resume()
+
+    def run(self):
+        self.__load_plugins()
+        self.ui.register_hotkey("f12", self.__drop_to_shell)
+        self.ui.footer = "Press ctrl+x or esc to quit."
+        self.ui.run()
+
+if __name__ == '__main__':
+    ui = UrwidTUI()
+    app = App(ui)
+    app.run()


--
To view, visit http://gerrit.ovirt.org/9859
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia1e7daff5720a06dcf99bb13d87322d0f0f5db5f
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>



More information about the node-patches mailing list