Revision baa62724

b/image_creator/dialog_wizard.py
58 58
        self.session = session
59 59
        self.pages = []
60 60
        self.session['wizard'] = {}
61
        self.d = session['dialog']
61 62

  
62 63
    def add_page(self, page):
63 64
        self.pages.append(page)
......
73 74
                continue
74 75

  
75 76
            if idx >= len(self.pages):
76
                break
77
                msg = "All necessary information has been gathered:\n\n"
78
                for page in self.pages:
79
                    msg += " * %s\n" % page.info
80
                msg += "\nConfirm and Proceed."
81

  
82
                ret = self.d.yesno(
83
                    msg, width=PAGE_WIDTH, height=12, ok_label="Yes",
84
                    cancel="Back", extra_button=1, extra_label="Quit",
85
                    title="Confirmation")
86

  
87
                if ret == self.d.DIALOG_CANCEL:
88
                    idx -= 1
89
                elif ret == self.d.DIALOG_EXTRA:
90
                    return False
91
                elif ret == self.d.DIALOG_OK:
92
                    return True
77 93

  
78 94
            if idx < 0:
79 95
                return False
80
        return True
81 96

  
82 97

  
83
class WizardPage:
98
class WizardPage(object):
84 99
    NEXT = 1
85 100
    PREV = -1
86 101

  
102
    def __init__(self, **kargs):
103
        if 'validate' in kargs:
104
            validate = kargs['validate']
105
        else:
106
            validate = lambda x: x
107
        setattr(self, "validate", validate)
108

  
109
        if 'display' in kargs:
110
            display = kargs['display']
111
        else:
112
            display = lambda x: x
113
        setattr(self, "display", display)
114

  
87 115
    def run(self, session, index, total):
88 116
        raise NotImplementedError
89 117

  
90 118

  
91 119
class WizardRadioListPage(WizardPage):
92 120

  
93
    def __init__(self, name, message, choices, **kargs):
121
    def __init__(self, name, printable, message, choices, **kargs):
122
        super(WizardRadioListPage, self).__init__(**kargs)
94 123
        self.name = name
124
        self.printable = printable
95 125
        self.message = message
96 126
        self.choices = choices
97 127
        self.title = kargs['title'] if 'title' in kargs else ''
98
        self.default = kargs['default'] if 'default' in kargs else 0
128
        self.default = kargs['default'] if 'default' in kargs else ""
99 129

  
100 130
    def run(self, session, index, total):
101 131
        d = session['dialog']
......
106 136
            default = 1 if self.choices[i][0] == self.default else 0
107 137
            choices.append((self.choices[i][0], self.choices[i][1], default))
108 138

  
109
        while True:
110
            (code, answer) = \
111
                d.radiolist(self.message, height=10, width=PAGE_WIDTH,
112
                            ok_label="Next", cancel="Back", choices=choices,
113
                            title="(%d/%d) %s" % (index + 1, total, self.title)
114
                            )
139
        (code, answer) = d.radiolist(
140
            self.message, height=10, width=PAGE_WIDTH, ok_label="Next",
141
            cancel="Back", choices=choices,
142
            title="(%d/%d) %s" % (index + 1, total, self.title))
115 143

  
116
            if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
117
                return self.PREV
144
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
145
            return self.PREV
118 146

  
119
            w[self.name] = answer
120
            self.default = answer
147
        w[self.name] = self.validate(answer)
148
        self.default = answer
149
        self.info = "%s: %s" % (self.printable, self.display(w[self.name]))
121 150

  
122
            return self.NEXT
151
        return self.NEXT
123 152

  
124 153

  
125 154
class WizardInputPage(WizardPage):
126 155

  
127
    def __init__(self, name, message, **kargs):
156
    def __init__(self, name, printable, message, **kargs):
157
        super(WizardInputPage, self).__init__(**kargs)
128 158
        self.name = name
159
        self.printable = printable
129 160
        self.message = message
130 161
        self.title = kargs['title'] if 'title' in kargs else ''
131 162
        self.init = kargs['init'] if 'init' in kargs else ''
132
        if 'validate' in kargs:
133
            validate = kargs['validate']
134
        else:
135
            validate = lambda x: x
136

  
137
        setattr(self, "validate", validate)
138 163

  
139 164
    def run(self, session, index, total):
140 165
        d = session['dialog']
141 166
        w = session['wizard']
142 167

  
143
        while True:
144
            (code, answer) = \
145
                d.inputbox(self.message, init=self.init,
146
                           width=PAGE_WIDTH, ok_label="Next", cancel="Back",
147
                           title="(%d/%d) %s" % (index + 1, total, self.title))
168
        (code, answer) = d.inputbox(
169
            self.message, init=self.init, width=PAGE_WIDTH, ok_label="Next",
170
            cancel="Back", title="(%d/%d) %s" % (index + 1, total, self.title))
148 171

  
149
            if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
150
                return self.PREV
172
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
173
            return self.PREV
151 174

  
152
            value = answer.strip()
153
            self.init = value
154
            w[self.name] = self.validate(value)
155
            break
175
        value = answer.strip()
176
        self.init = value
177
        w[self.name] = self.validate(value)
178
        self.info = "%s: %s" % (self.printable, self.display(w[self.name]))
156 179

  
157 180
        return self.NEXT
158 181

  
159 182

  
160
class WizardYesNoPage(WizardPage):
161

  
162
    def __init__(self, message, **kargs):
163
        self.message = message
164
        self.title = kargs['title'] if 'title' in kargs else ''
165

  
166
    def run(self, session, index, total):
167
        d = session['dialog']
168

  
169
        while True:
170
            ret = d.yesno(self.message, width=PAGE_WIDTH, ok_label="Yes",
171
                          cancel="Back", extra_button=1, extra_label="Quit",
172
                          title="(%d/%d) %s" % (index + 1, total, self.title))
173

  
174
            if ret == d.DIALOG_CANCEL:
175
                return self.PREV
176
            elif ret == d.DIALOG_EXTRA:
177
                raise WizardExit
178
            elif ret == d.DIALOG_OK:
179
                return self.NEXT
180

  
181

  
182 183
def wizard(session):
183 184

  
184 185
    init_token = Kamaki.get_token()
185 186
    if init_token is None:
186 187
        init_token = ""
187 188

  
188
    name = WizardInputPage("ImageName", "Please provide a name for the image:",
189
                           title="Image Name", init=session['device'].distro)
189
    name = WizardInputPage(
190
        "ImageName", "Image Name", "Please provide a name for the image:",
191
        title="Image Name", init=session['device'].distro)
192

  
190 193
    descr = WizardInputPage(
191
        "ImageDescription", "Please provide a description for the image:",
194
        "ImageDescription", "Image Description",
195
        "Please provide a description for the image:",
192 196
        title="Image Description", init=session['metadata']['DESCRIPTION'] if
193 197
        'DESCRIPTION' in session['metadata'] else '')
198

  
194 199
    registration = WizardRadioListPage(
195
        "ImageRegistration", "Please provide a registration type:",
200
        "ImageRegistration", "Registration Type",
201
        "Please provide a registration type:",
196 202
        [("Private", "Image is accessible only by this user"),
197 203
         ("Public", "Everyone can create VMs from this image")],
198 204
        title="Registration Type", default="Private")
......
211 217
        return account
212 218

  
213 219
    account = WizardInputPage(
214
        "account", "Please provide your ~okeanos authentication token:",
215
        title="~okeanos account", init=init_token, validate=validate_account)
216

  
217
    msg = "All necessary information has been gathered. Confirm and Proceed."
218
    proceed = WizardYesNoPage(msg, title="Confirmation")
220
        "Account", "Account",
221
        "Please provide your ~okeanos authentication token:",
222
        title="~okeanos account", init=init_token, validate=validate_account,
223
        display=lambda account: account['username'])
219 224

  
220 225
    w = Wizard(session)
221 226

  
......
223 228
    w.add_page(descr)
224 229
    w.add_page(registration)
225 230
    w.add_page(account)
226
    w.add_page(proceed)
227 231

  
228 232
    if w.run():
229 233
        create_image(session)
......
242 246
    wizard = session['wizard']
243 247

  
244 248
    # Save Kamaki credentials
245
    Kamaki.save_token(wizard['account']['auth_token'])
249
    Kamaki.save_token(wizard['Account']['auth_token'])
246 250

  
247 251
    with_progress = OutputWthProgress(True)
248 252
    out = disk.out
......
276 280
        out.output()
277 281
        try:
278 282
            out.output("Uploading image to pithos:")
279
            kamaki = Kamaki(wizard['account'], out)
283
            kamaki = Kamaki(wizard['Account'], out)
280 284

  
281 285
            name = "%s-%s.diskdump" % (wizard['ImageName'],
282 286
                                       time.strftime("%Y%m%d%H%M"))

Also available in: Unified diff