Move wizard code out of dialog_main.py
[snf-image-creator] / image_creator / dialog_wizard.py
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 class WizardPage:
39     NEXT = 1
40     PREV = -1
41
42     def __init__(self, session, title):
43         self.session = session
44         self.title = title
45
46     def run(self):
47         raise NotImplementedError
48
49
50 class ImageName(WizardPage):
51     def run(self):
52         d = self.session['dialog']
53         w = self.session['wizard']
54         
55         init = w['ImageName'] if 'ImageName' in w else ""
56         while 1:
57             (code, answer) = d.inputbox("Please provide a name for the image:",
58                                         init=init, width=INPUTBOX_WIDTH,
59                                         ok_label="Next", cancel="Back",
60                                         title=self.title)
61
62             if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
63                 return self.PREV
64
65             name = answer.strip()
66             if len(name) == 0:
67                 d.msgbox("Image name cannot be empty", width=MSGBOX_WIDTH)
68                 continue
69             w['ImageName'] = name
70             break
71
72         return self.NEXT
73
74
75 class ImageDescription(WizardPage):
76     def run(self):
77         d = self.session['dialog']
78         w = self.session['wizard']
79
80         init = w['ImageDescription'] if 'ImageDescription' in w else ""
81
82         while 1:
83             (code, answer) = d.inputbox(
84                                 "Please provide a description for the image:",
85                                 init=init, width=INPUTBOX_WIDTH,
86                                 ok_label="Next", cancel="Back",
87                                 title=self.title)
88
89             if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
90                 return self.PREV
91
92             name = answer.strip()
93             if len(filename) == 0:
94                 # Description is allowed to be empty
95                 del w['ImageDescription']
96             else:
97                 w['ImageDescription'] = name
98             break
99
100         return self.NEXT
101
102
103 def wizard(session):
104     session['wizard'] = {}
105
106     steps = []
107     steps.append(ImageName(session, "(1/5) Image Name"))
108     steps.append(ImageDescription(session, "(2/5) Image Description"))
109
110     return True
111
112
113 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :