root / image_creator / dialog_wizard.py @ 3c33e68a
History | View | Annotate | Download (4 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
4 |
#
|
5 |
# Redistribution and use in source and binary forms, with or
|
6 |
# without modification, are permitted provided that the following
|
7 |
# conditions are met:
|
8 |
#
|
9 |
# 1. Redistributions of source code must retain the above
|
10 |
# copyright notice, this list of conditions and the following
|
11 |
# disclaimer.
|
12 |
#
|
13 |
# 2. Redistributions in binary form must reproduce the above
|
14 |
# copyright notice, this list of conditions and the following
|
15 |
# disclaimer in the documentation and/or other materials
|
16 |
# provided with the distribution.
|
17 |
#
|
18 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
19 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
25 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
26 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
27 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
28 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29 |
# POSSIBILITY OF SUCH DAMAGE.
|
30 |
#
|
31 |
# The views and conclusions contained in the software and
|
32 |
# documentation are those of the authors and should not be
|
33 |
# interpreted as representing official policies, either expressed
|
34 |
# or implied, of GRNET S.A.
|
35 |
|
36 |
import dialog |
37 |
|
38 |
from image_creator.kamaki_wrapper import Kamaki |
39 |
|
40 |
PAGE_WIDTH = 70
|
41 |
|
42 |
|
43 |
class Wizard: |
44 |
def __init__(self, session): |
45 |
self.session = session
|
46 |
self.pages = []
|
47 |
self.session['wizard'] = {} |
48 |
|
49 |
def add_page(self, page): |
50 |
self.pages.append(page)
|
51 |
|
52 |
def run(self): |
53 |
idx = 0
|
54 |
while True: |
55 |
idx += self.pages[idx].run(self.session, idx, len(self.pages)) |
56 |
|
57 |
if idx >= len(self.pages): |
58 |
break
|
59 |
|
60 |
if idx < 0: |
61 |
return False |
62 |
return True |
63 |
|
64 |
|
65 |
class WizardPage: |
66 |
NEXT = 1
|
67 |
PREV = -1
|
68 |
|
69 |
def __init__(self, name, message, **kargs): |
70 |
self.name = name
|
71 |
self.message = message
|
72 |
self.title = kargs['title'] if 'title' in kargs else '' |
73 |
self.init_value = kargs['init'] if 'init' in kargs else '' |
74 |
self.allow_empty = kargs['empty'] if 'empty' in kargs else False |
75 |
|
76 |
def run(self, session, index, total): |
77 |
d = session['dialog']
|
78 |
w = session['wizard']
|
79 |
|
80 |
init = w[self.name] if self.name in w else self.init_value |
81 |
while True: |
82 |
(code, answer) = d.inputbox(self.message, init=init,
|
83 |
width=PAGE_WIDTH, ok_label="Next", cancel="Back", |
84 |
title="(%d/%d) %s" % (index + 1, total, self.title)) |
85 |
|
86 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
87 |
return self.PREV |
88 |
|
89 |
value = answer.strip() |
90 |
if len(value) == 0 and self.allow_empty is False: |
91 |
d.msgbox("The value cannot be empty!", width=PAGE_WIDTH)
|
92 |
continue
|
93 |
w[self.name] = value
|
94 |
break
|
95 |
|
96 |
return self.NEXT |
97 |
|
98 |
|
99 |
def wizard(session): |
100 |
|
101 |
name = WizardPage("ImageName", "Please provide a name for the image:", |
102 |
title="Image Name", init=session['device'].distro) |
103 |
descr = WizardPage("ImageDescription",
|
104 |
"Please provide a description for the image:",
|
105 |
title="Image Description", empty=True, |
106 |
init=session['metadata']['DESCRIPTION'] if 'DESCRIPTION' in |
107 |
session['metadata'] else '') |
108 |
account = WizardPage("account",
|
109 |
"Please provide your ~okeanos account e-mail:",
|
110 |
title="~okeanos account information", init=Kamaki.get_account())
|
111 |
token = WizardPage("token",
|
112 |
"Please provide your ~okeanos account token:",
|
113 |
title="~okeanos account token", init=Kamaki.get_token())
|
114 |
|
115 |
w = Wizard(session) |
116 |
w.add_page(name) |
117 |
w.add_page(descr) |
118 |
w.add_page(account) |
119 |
w.add_page(token) |
120 |
|
121 |
return w.run()
|
122 |
|
123 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|