[node-patches] Change in ovirt-node[master]: Make ovirt.node.utils more fine granular (module)
fabiand at fedoraproject.org
fabiand at fedoraproject.org
Tue Dec 11 20:09:33 UTC 2012
Fabian Deutsch has uploaded a new change for review.
Change subject: Make ovirt.node.utils more fine granular (module)
......................................................................
Make ovirt.node.utils more fine granular (module)
Change-Id: I8c5030b434c8efe87727b58732535c573c034dc9
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M scripts/tui/src/ovirt/__init__.py
M scripts/tui/src/ovirt/node/__init__.py
M scripts/tui/src/ovirt/node/app.py
M scripts/tui/src/ovirt/node/plugins/__init__.py
M scripts/tui/src/ovirt/node/plugins/example.py
M scripts/tui/src/ovirt/node/plugins/features.py
M scripts/tui/src/ovirt/node/plugins/ping.py
M scripts/tui/src/ovirt/node/plugins/usage.py
M scripts/tui/src/ovirt/node/tui.py
D scripts/tui/src/ovirt/node/utils.py
A scripts/tui/src/ovirt/node/utils/__init__.py
A scripts/tui/src/ovirt/node/utils/fs.py
A scripts/tui/src/ovirt/node/utils/process.py
M scripts/tui/src/ovirt/node/valid.py
M scripts/tui/src/ovirt/node/widgets.py
15 files changed, 202 insertions(+), 176 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/76/9876/1
diff --git a/scripts/tui/src/ovirt/__init__.py b/scripts/tui/src/ovirt/__init__.py
index 6d10dcd..2e47949 100644
--- a/scripts/tui/src/ovirt/__init__.py
+++ b/scripts/tui/src/ovirt/__init__.py
@@ -1 +1,3 @@
-# oVirt Module
+"""
+oVirt Module
+"""
diff --git a/scripts/tui/src/ovirt/node/__init__.py b/scripts/tui/src/ovirt/node/__init__.py
index 26558f8..4c25e6f 100644
--- a/scripts/tui/src/ovirt/node/__init__.py
+++ b/scripts/tui/src/ovirt/node/__init__.py
@@ -1 +1,3 @@
-# oVirt Node Module
+"""
+oVirt Node Module
+"""
diff --git a/scripts/tui/src/ovirt/node/app.py b/scripts/tui/src/ovirt/node/app.py
index d362575..df8549d 100644
--- a/scripts/tui/src/ovirt/node/app.py
+++ b/scripts/tui/src/ovirt/node/app.py
@@ -7,6 +7,7 @@
import logging
import ovirt.node.tui
+import ovirt.node.utils
logging.basicConfig(level=logging.DEBUG,
filename="app.log", filemode="w")
@@ -30,7 +31,7 @@
def __drop_to_shell(self):
with self.ui.suspended():
- ovirt.node.utils.system("reset ; bash")
+ ovirt.node.utils.process.system("reset ; bash")
def run(self):
self.__load_plugins()
diff --git a/scripts/tui/src/ovirt/node/plugins/__init__.py b/scripts/tui/src/ovirt/node/plugins/__init__.py
index 437a5ba..1230368 100644
--- a/scripts/tui/src/ovirt/node/plugins/__init__.py
+++ b/scripts/tui/src/ovirt/node/plugins/__init__.py
@@ -1,4 +1,6 @@
-
+"""
+This contains much stuff related to plugins
+"""
import pkgutil
import logging
diff --git a/scripts/tui/src/ovirt/node/plugins/example.py b/scripts/tui/src/ovirt/node/plugins/example.py
index 0a4104d..4aabc8a 100644
--- a/scripts/tui/src/ovirt/node/plugins/example.py
+++ b/scripts/tui/src/ovirt/node/plugins/example.py
@@ -1,8 +1,6 @@
-
"""
-Example doc
+Example plugin with TUI
"""
-import os.path
import logging
import ovirt.node.plugins
@@ -44,10 +42,14 @@
This is an ordered list of (path, widget) tuples.
"""
widgets = [
- ("foo.section", ovirt.node.plugins.Header("Subsection")),
- ("foo.hostname", ovirt.node.plugins.Entry(label="Hostname")),
- ("foo.port", ovirt.node.plugins.Entry(label="Port")),
- ("foo.password", ovirt.node.plugins.PasswordEntry(label="Password")),
+ ("foo.section",
+ ovirt.node.plugins.Header("Subsection")),
+ ("foo.hostname",
+ ovirt.node.plugins.Entry(label="Hostname")),
+ ("foo.port",
+ ovirt.node.plugins.Entry(label="Port")),
+ ("foo.password",
+ ovirt.node.plugins.PasswordEntry(label="Password")),
]
self._widgets = dict(widgets)
return widgets
diff --git a/scripts/tui/src/ovirt/node/plugins/features.py b/scripts/tui/src/ovirt/node/plugins/features.py
index 2fc766d..5ced4d6 100644
--- a/scripts/tui/src/ovirt/node/plugins/features.py
+++ b/scripts/tui/src/ovirt/node/plugins/features.py
@@ -1,5 +1,6 @@
-
-
+"""
+A plugin illustrating the features of the TUI
+"""
import logging
import ovirt.node.plugins
diff --git a/scripts/tui/src/ovirt/node/plugins/ping.py b/scripts/tui/src/ovirt/node/plugins/ping.py
index 1dd94d6..d4fd6cf 100644
--- a/scripts/tui/src/ovirt/node/plugins/ping.py
+++ b/scripts/tui/src/ovirt/node/plugins/ping.py
@@ -6,7 +6,7 @@
import ovirt.node.plugins
import ovirt.node.valid
import ovirt.node.plugins
-import ovirt.node.utils
+import ovirt.node.utils.process
LOGGER = logging.getLogger(__name__)
@@ -93,6 +93,6 @@
cmd = "%s -c %s %s" % (cmd, count, addr)
out = ""
- for line in ovirt.node.utils.pipe_async(cmd):
+ for line in ovirt.node.utils.process.pipe_async(cmd):
out += line
self._widgets["ping.result"].text("Result:\n\n%s" % out)
diff --git a/scripts/tui/src/ovirt/node/plugins/usage.py b/scripts/tui/src/ovirt/node/plugins/usage.py
index b79799e..fed9a55 100644
--- a/scripts/tui/src/ovirt/node/plugins/usage.py
+++ b/scripts/tui/src/ovirt/node/plugins/usage.py
@@ -1,5 +1,6 @@
-
-
+"""
+A plugin illustrating how to use the TUI
+"""
import logging
import ovirt.node.plugins
diff --git a/scripts/tui/src/ovirt/node/tui.py b/scripts/tui/src/ovirt/node/tui.py
index 7161e63..9c0022e 100644
--- a/scripts/tui/src/ovirt/node/tui.py
+++ b/scripts/tui/src/ovirt/node/tui.py
@@ -1,5 +1,6 @@
-#!/bin/env python
-
+"""
+The urwid TUI base library
+"""
import urwid
import logging
@@ -129,6 +130,7 @@
elif type(item) in [ovirt.node.plugins.Button,
ovirt.node.plugins.SaveButton]:
widget = widget_class(item.text())
+
def on_widget_click_cb(widget, data=None):
if type(item) is ovirt.node.plugins.SaveButton:
plugin._on_ui_save()
@@ -144,6 +146,7 @@
elif type(item) in [ovirt.node.plugins.Options]:
widget = widget_class(item.label, item.options,
plugin.model()[path])
+
def on_widget_change_cb(widget, data):
LOGGER.debug(data)
item.option(data)
@@ -173,7 +176,6 @@
widgets.append(urwid.Filler(save))
widgets.append(urwid.Filler(urwid.Text("")))
-
pile = urwid.Pile(widgets)
# FIXME why is this fixed?
diff --git a/scripts/tui/src/ovirt/node/utils.py b/scripts/tui/src/ovirt/node/utils.py
deleted file mode 100644
index 93ebfdc..0000000
--- a/scripts/tui/src/ovirt/node/utils.py
+++ /dev/null
@@ -1,154 +0,0 @@
-"""
-Some convenience functions
-"""
-
-import subprocess
-import logging
-import shutil
-import os
-
-LOGGER = logging.getLogger(__name__)
-
-
-def popen_closefds(*args, **kwargs):
- """subprocess.Popen wrapper to not leak file descriptors
-
- Args:
- cmd: Cmdline to be run
-
- Returns:
- Popen object
- """
- kwargs.update({
- "close_fds": True
- })
- return subprocess.Popen(*args, **kwargs)
-
-
-def system(cmd):
- """Run a non-interactive command, or where the user shall input something
-
- Args:
- cmd: Cmdline to be run
-
- Returns:
- retval of the process
- """
- return popen_closefds(cmd, shell=True).wait()
-
-
-def pipe(cmd, stdin=None):
- """Run a command interactively and cath it's output.
- This functions allows to pass smoe input to a running command.
-
- Args:
- cmd: Commandline to be run
-
- Returns:
- A tuple (success, stdout)
- """
-
- LOGGER.debug("run '%s'" % cmd)
- system_cmd = popen_closefds(cmd, shell=True, stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
- stdout, stderr = system_cmd.communicate(stdin)
- if stdout:
- LOGGER.debug("out '%s'" % stdout)
- if stderr:
- LOGGER.warning("error '%s'" % stderr)
- return (system_cmd.returncode == 0, stdout)
-
-
-def pipe_async(cmd, stdin=None):
- """Run a command interactively and yields the process output.
- This functions allows to pass smoe input to a running command.
-
- Args:
- cmd: Commandline to be run
- stdin: Data to be written to cmd's stdin
-
- Yields:
- Lines read from stdout
- """
- LOGGER.debug("run async '%s'" % cmd)
- process = popen_closefds(cmd, shell=True, stdout=subprocess.PIPE,
- stderr=subprocess.PIPE, stdin=subprocess.PIPE)
- if stdin:
- process.stdin.write(stdin)
- while process.poll() != 0:
- yield process.stdout.readline()
-
-
-def copy_contents(src, dst):
- assert all([os.path.isfile(f) for f in [src, dst]]), \
- "Source and destination need to exist"
- with open(src, "r") as srcf, open(dst, "wb") as dstf:
- dstf.write(srcf.read())
-
-
-class BackupedFiles(object):
- """This context manager can be used to keep backup of files while messing
- with them.
-
- >>> txt = "Hello World!"
- >>> dst = "example.txt"
- >>> with open(dst, "w") as f:
- ... f.write(txt)
- >>> txt == open(dst).read()
- True
-
- >>> with BackupedFiles([dst]) as backup:
- ... b = backup.of(dst)
- ... txt == open(b).read()
- True
-
- >>> with BackupedFiles([dst]) as backup:
- ... try:
- ... open(dst, "w").write("Argh ...").close()
- ... raise Exception("Something goes wrong ...")
- ... except:
- ... backup.restore(dst)
- >>> txt == open(dst).read()
- True
-
- >>> os.remove(dst)
- """
-
- files = []
- backups = {}
- suffix = ".backup"
-
- def __init__(self, files, suffix=".backup"):
- assert type(files) is list, "A list of files is required"
- assert all([os.path.isfile(f) for f in files]), \
- "Not all files exist: %s" % files
- self.files = files
- self.suffix = suffix
-
- def __enter__(self):
- """Create backups when starting
- """
- for fn in self.files:
- backup = "%s%s" % (fn, self.suffix)
- assert not os.path.exists(backup)
- shutil.copy(fn, backup)
- self.backups[fn] = backup
- return self
-
- def __exit__(self, a, b, c):
- """Remove all backups when done
- """
- for fn in self.files:
- backup = self.backups[fn]
- os.remove(backup)
-
- def of(self, fn):
- """Returns the backup file for the given file
- """
- assert fn in self.backups, "No backup for '%s'" % fn
- return self.backups[fn]
-
- def restore(self, fn):
- """Restore contens of a previously backupe file
- """
- copy_contents(self.of(fn), fn)
diff --git a/scripts/tui/src/ovirt/node/utils/__init__.py b/scripts/tui/src/ovirt/node/utils/__init__.py
new file mode 100644
index 0000000..95de074
--- /dev/null
+++ b/scripts/tui/src/ovirt/node/utils/__init__.py
@@ -0,0 +1,3 @@
+"""
+Utility functions
+"""
diff --git a/scripts/tui/src/ovirt/node/utils/fs.py b/scripts/tui/src/ovirt/node/utils/fs.py
new file mode 100644
index 0000000..957899c
--- /dev/null
+++ b/scripts/tui/src/ovirt/node/utils/fs.py
@@ -0,0 +1,84 @@
+"""
+Some convenience functions realted to the filesystem
+"""
+
+import logging
+import shutil
+import os
+
+LOGGER = logging.getLogger(__name__)
+
+
+def copy_contents(src, dst):
+ assert all([os.path.isfile(f) for f in [src, dst]]), \
+ "Source and destination need to exist"
+ with open(src, "r") as srcf, open(dst, "wb") as dstf:
+ dstf.write(srcf.read())
+
+
+class BackupedFiles(object):
+ """This context manager can be used to keep backup of files while messing
+ with them.
+
+ >>> txt = "Hello World!"
+ >>> dst = "example.txt"
+ >>> with open(dst, "w") as f:
+ ... f.write(txt)
+ >>> txt == open(dst).read()
+ True
+
+ >>> with BackupedFiles([dst]) as backup:
+ ... b = backup.of(dst)
+ ... txt == open(b).read()
+ True
+
+ >>> with BackupedFiles([dst]) as backup:
+ ... try:
+ ... open(dst, "w").write("Argh ...").close()
+ ... raise Exception("Something goes wrong ...")
+ ... except:
+ ... backup.restore(dst)
+ >>> txt == open(dst).read()
+ True
+
+ >>> os.remove(dst)
+ """
+
+ files = []
+ backups = {}
+ suffix = ".backup"
+
+ def __init__(self, files, suffix=".backup"):
+ assert type(files) is list, "A list of files is required"
+ assert all([os.path.isfile(f) for f in files]), \
+ "Not all files exist: %s" % files
+ self.files = files
+ self.suffix = suffix
+
+ def __enter__(self):
+ """Create backups when starting
+ """
+ for fn in self.files:
+ backup = "%s%s" % (fn, self.suffix)
+ assert not os.path.exists(backup)
+ shutil.copy(fn, backup)
+ self.backups[fn] = backup
+ return self
+
+ def __exit__(self, a, b, c):
+ """Remove all backups when done
+ """
+ for fn in self.files:
+ backup = self.backups[fn]
+ os.remove(backup)
+
+ def of(self, fn):
+ """Returns the backup file for the given file
+ """
+ assert fn in self.backups, "No backup for '%s'" % fn
+ return self.backups[fn]
+
+ def restore(self, fn):
+ """Restore contens of a previously backupe file
+ """
+ copy_contents(self.of(fn), fn)
diff --git a/scripts/tui/src/ovirt/node/utils/process.py b/scripts/tui/src/ovirt/node/utils/process.py
new file mode 100644
index 0000000..655eba5
--- /dev/null
+++ b/scripts/tui/src/ovirt/node/utils/process.py
@@ -0,0 +1,77 @@
+"""
+Some convenience functions related to processes
+"""
+
+import subprocess
+import logging
+
+LOGGER = logging.getLogger(__name__)
+
+
+def popen_closefds(*args, **kwargs):
+ """subprocess.Popen wrapper to not leak file descriptors
+
+ Args:
+ cmd: Cmdline to be run
+
+ Returns:
+ Popen object
+ """
+ kwargs.update({
+ "close_fds": True
+ })
+ return subprocess.Popen(*args, **kwargs)
+
+
+def system(cmd):
+ """Run a non-interactive command, or where the user shall input something
+
+ Args:
+ cmd: Cmdline to be run
+
+ Returns:
+ retval of the process
+ """
+ return popen_closefds(cmd, shell=True).wait()
+
+
+def pipe(cmd, stdin=None):
+ """Run a command interactively and cath it's output.
+ This functions allows to pass smoe input to a running command.
+
+ Args:
+ cmd: Commandline to be run
+
+ Returns:
+ A tuple (success, stdout)
+ """
+
+ LOGGER.debug("run '%s'" % cmd)
+ system_cmd = popen_closefds(cmd, shell=True, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ stdout, stderr = system_cmd.communicate(stdin)
+ if stdout:
+ LOGGER.debug("out '%s'" % stdout)
+ if stderr:
+ LOGGER.warning("error '%s'" % stderr)
+ return (system_cmd.returncode == 0, stdout)
+
+
+def pipe_async(cmd, stdin=None):
+ """Run a command interactively and yields the process output.
+ This functions allows to pass smoe input to a running command.
+
+ Args:
+ cmd: Commandline to be run
+ stdin: Data to be written to cmd's stdin
+
+ Yields:
+ Lines read from stdout
+ """
+ LOGGER.debug("run async '%s'" % cmd)
+ process = popen_closefds(cmd, shell=True, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE, stdin=subprocess.PIPE)
+ if stdin:
+ process.stdin.write(stdin)
+ while process.poll() != 0:
+ yield process.stdout.readline()
diff --git a/scripts/tui/src/ovirt/node/valid.py b/scripts/tui/src/ovirt/node/valid.py
index 3ad4ef9..9907c6b 100644
--- a/scripts/tui/src/ovirt/node/valid.py
+++ b/scripts/tui/src/ovirt/node/valid.py
@@ -4,7 +4,6 @@
import re
import logging
import socket
-import urlparse
import ovirt.node.plugins
@@ -48,6 +47,7 @@
def raise_exception(self):
msg = self.__exception_msg.format(description=self.description)
raise ovirt.node.plugins.InvalidData(msg)
+
class RegexValidator(Validator):
"""A validator which uses a regular expression to validate a value.
@@ -122,6 +122,7 @@
"""
description = "a port number"
+
def __init__(self):
super(Port, self).__init__(1, 65535)
@@ -174,6 +175,7 @@
description = "a valid IPv4 address"
family = socket.AF_INET
+
def validate(self, value):
valid = True
try:
diff --git a/scripts/tui/src/ovirt/node/widgets.py b/scripts/tui/src/ovirt/node/widgets.py
index 225d2f1..bab83c2 100644
--- a/scripts/tui/src/ovirt/node/widgets.py
+++ b/scripts/tui/src/ovirt/node/widgets.py
@@ -209,7 +209,8 @@
self._button_attrmap = urwid.AttrMap(self._button,
"plugin.widget.button")
- self._padding = urwid.Padding(self._button_attrmap, width=len(label)+4)
+ self._padding = urwid.Padding(self._button_attrmap,
+ width=len(label) + 4)
super(Button, self).__init__(self._padding)
--
To view, visit http://gerrit.ovirt.org/9876
To unsubscribe, visit http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8c5030b434c8efe87727b58732535c573c034dc9
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