Skip to content
Snippets Groups Projects
Commit 545a958d authored by Nikos Skalkotos's avatar Nikos Skalkotos
Browse files

Change the format of the dialog.form method

The method was hot-patched into the pythondialog library if it was
missing. Use the same format found in pythondialog 3.x
parent 846ef8f9
No related branches found
No related tags found
No related merge requests found
...@@ -128,30 +128,37 @@ def create_image(d, media, out, tmp, snapshot): ...@@ -128,30 +128,37 @@ def create_image(d, media, out, tmp, snapshot):
return 0 return 0
def _dialog_form(self, text, height=20, width=60, form_height=15, fields=[], def _dialog_form(self, text, elements, height=20, width=60, form_height=15,
**kwargs): **kwargs):
"""Display a form box. """Display a form box.
Each element of *elements* must itself be a sequence
fields is in the form: [(label1, item1, item_length1), ...] :samp:`({label}, {yl}, {xl}, {item}, {yi}, {xi}, {field_length},
{input_length})` containing the various parameters concerning a
given field and the associated label.
*label* is a string that will be displayed at row *yl*, column
*xl*. *item* is a string giving the initial value for the field,
which will be displayed at row *yi*, column *xi* (row and column
numbers starting from 1).
*field_length* and *input_length* are integers that respectively
specify the number of characters used for displaying the field
and the maximum number of characters that can be entered for
this field. These two integers also determine whether the
contents of the field can be modified, as follows:
- if *field_length* is zero, the field cannot be altered and
its contents determines the displayed length;
- if *field_length* is negative, the field cannot be altered
and the opposite of *field_length* gives the displayed
length;
- if *input_length* is zero, it is set to *field_length*.
""" """
cmd = ["--form", text, str(height), str(width), str(form_height)] cmd = ["--form", text, str(height), str(width), str(form_height)]
label_len = 0 for element in elements:
for field in fields: label, yl, xl, item, yi, xi, field_len, input_len = element[:8]
if len(field[0]) > label_len:
label_len = len(field[0]) cmd.extend((label, unicode(yl), unicode(xl), item, unicode(yi),
unicode(xi), unicode(field_len), unicode(input_len)))
input_len = width - label_len - 1
line = 1
for field in fields:
label = field[0]
item = field[1]
item_len = field[2]
cmd.extend((label, str(line), str(1), item, str(line),
str(label_len + 1), str(input_len), str(item_len)))
line += 1
code, output = self._perform(*(cmd,), **kwargs) code, output = self._perform(*(cmd,), **kwargs)
......
...@@ -35,7 +35,7 @@ from image_creator.help import get_help_file ...@@ -35,7 +35,7 @@ from image_creator.help import get_help_file
from image_creator.dialog_util import SMALL_WIDTH, WIDTH, \ from image_creator.dialog_util import SMALL_WIDTH, WIDTH, \
update_background_title, confirm_reset, confirm_exit, Reset, \ update_background_title, confirm_reset, confirm_exit, Reset, \
extract_image, add_cloud, edit_cloud, update_sysprep_param, select_file, \ extract_image, add_cloud, edit_cloud, update_sysprep_param, select_file, \
copy_file copy_file, create_form_elements
CONFIGURATION_TASKS = [ CONFIGURATION_TASKS = [
("Partition table manipulation", ["FixPartitionTable"], lambda x: True), ("Partition table manipulation", ["FixPartitionTable"], lambda x: True),
...@@ -121,8 +121,8 @@ def upload_image(session): ...@@ -121,8 +121,8 @@ def upload_image(session):
fields = [("Remote Name:", name, 60), ("Container:", container, 60)] fields = [("Remote Name:", name, 60), ("Container:", container, 60)]
(code, output) = d.form("Please provide the following upload info:", (code, output) = d.form("Please provide the following upload info:",
height=11, width=WIDTH, form_height=2, create_form_elements(fields), height=11,
fields=fields) width=WIDTH, form_height=2)
if code in (d.CANCEL, d.ESC): if code in (d.CANCEL, d.ESC):
return False return False
...@@ -224,8 +224,9 @@ def register_image(session): ...@@ -224,8 +224,9 @@ def register_image(session):
("Description (optional):", description, 80)] ("Description (optional):", description, 80)]
(code, output) = d.form( (code, output) = d.form(
"Please provide the following registration info:", height=11, "Please provide the following registration info:",
width=WIDTH, form_height=2, fields=fields) create_form_elements(fields), height=11, width=WIDTH,
form_height=2)
if code in (d.CANCEL, d.ESC): if code in (d.CANCEL, d.ESC):
return False return False
......
...@@ -278,8 +278,9 @@ def add_cloud(session): ...@@ -278,8 +278,9 @@ def add_cloud(session):
("Authentication URL: ", url, 200), ("Authentication URL: ", url, 200),
("Token:", token, 100)] ("Token:", token, 100)]
(code, output) = d.form("Add a new cloud account:", height=13, (code, output) = d.form("Add a new cloud account:",
width=WIDTH, form_height=4, fields=fields) create_form_elements(fields), height=13,
width=WIDTH, form_height=4)
if code in (d.CANCEL, d.ESC): if code in (d.CANCEL, d.ESC):
return False return False
...@@ -324,8 +325,9 @@ def edit_cloud(session, name): ...@@ -324,8 +325,9 @@ def edit_cloud(session, name):
("Authentication URL: ", url, 200), ("Authentication URL: ", url, 200),
("Token:", token, 100)] ("Token:", token, 100)]
(code, output) = d.form("Edit cloud account: `%s'" % name, height=13, (code, output) = d.form("Edit cloud account: `%s'" % name,
width=WIDTH, form_height=3, fields=fields) create_form_elements(fields), height=13,
width=WIDTH, form_height=3)
if code in (d.CANCEL, d.ESC): if code in (d.CANCEL, d.ESC):
return False return False
...@@ -467,4 +469,26 @@ def copy_file(d, src, dest): ...@@ -467,4 +469,26 @@ def copy_file(d, src, dest):
d.msgbox("File: `%s' was successfully written!") d.msgbox("File: `%s' was successfully written!")
return True return True
def create_form_elements(fields, width=WIDTH):
"""Transform a list of (label, default, length) fields and transform it
to the element format that dialog.form() expects.
"""
assert len(fields) > 0
assert width > 0
max_label = max([len(f[0]) for f in fields])
input_length = width - max_label - 1
elements = []
line = 1
for field in fields:
elements.append((field[0], line, 1, field[1], line, max_label+1,
input_length, field[2]))
line += 1
return elements
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
...@@ -28,7 +28,7 @@ from image_creator.kamaki_wrapper import Kamaki, ClientError, CONTAINER ...@@ -28,7 +28,7 @@ from image_creator.kamaki_wrapper import Kamaki, ClientError, CONTAINER
from image_creator.util import FatalError, virtio_versions from image_creator.util import FatalError, virtio_versions
from image_creator.output.cli import OutputWthProgress from image_creator.output.cli import OutputWthProgress
from image_creator.dialog_util import extract_image, update_background_title, \ from image_creator.dialog_util import extract_image, update_background_title, \
add_cloud, edit_cloud, update_sysprep_param add_cloud, edit_cloud, update_sysprep_param, create_form_elements
PAGE_WIDTH = 70 PAGE_WIDTH = 70
PAGE_HEIGHT = 12 PAGE_HEIGHT = 12
...@@ -206,8 +206,10 @@ class WizardFormPage(WizardPage): ...@@ -206,8 +206,10 @@ class WizardFormPage(WizardPage):
form_height = field_lenght if field_lenght < PAGE_HEIGHT - 4 \ form_height = field_lenght if field_lenght < PAGE_HEIGHT - 4 \
else PAGE_HEIGHT - 4 else PAGE_HEIGHT - 4
(code, output) = dialog.form(self.text(), form_height=form_height, (code, output) = dialog.form(self.text(),
fields=self.fields(), title=title, create_form_elements(self.fields(),
self.dargs['width']),
form_height=form_height, title=title,
extra_label=self.extra_label(), extra_label=self.extra_label(),
default_item=self.default, **self.dargs) default_item=self.default, **self.dargs)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment