Revision d415dda2 image_creator/dialog_wizard.py

b/image_creator/dialog_wizard.py
208 208
        return self.NEXT
209 209

  
210 210

  
211
class WizardFormPage(WizardPage):
212
    """Represents a Form in a wizard"""
213

  
214
    def __init__(self, name, display_name, text, fields, **kargs):
215
        super(WizardFormPage, self).__init__(name, display_name, text, **kargs)
216
        self.fields = fields
217

  
218
    def run(self, session, title):
219
        d = session['dialog']
220
        w = session['wizard']
221

  
222
        field_lenght = len(self.fields())
223
        form_height = field_lenght if field_lenght < PAGE_HEIGHT - 4 \
224
            else PAGET_HEIGHT - 4
225

  
226
        (code, output) = d.form(
227
            self.text, width=PAGE_WIDTH, height=PAGE_HEIGHT,
228
            form_height=form_height, ok_label="Next", cancel="Back",
229
            fields=self.fields(), title=title)
230

  
231
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
232
            return self.PREV
233

  
234
        w[self.name] = self.validate(output)
235
        self.default = output
236
        self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
237

  
238
        return self.NEXT
239

  
240

  
211 241
class WizardMenuPage(WizardPageWthChoices):
212 242
    """Represents a menu dialog with available choices in a wizard"""
213 243

  
......
250 280
def start_wizard(session):
251 281
    """Run the image creation wizard"""
252 282

  
253
    distro = session['image'].distro
254
    ostype = session['image'].ostype
283
    image = session['image']
284
    distro = image.distro
285
    ostype = image.ostype
255 286

  
287
    # Create Cloud Wizard Page
256 288
    def cloud_choices():
257 289
        choices = []
258 290
        for (name, cloud) in Kamaki.get_clouds().items():
......
279 311
                if edit_cloud(session, cloud):
280 312
                    return cloud
281 313

  
282
            raise WizardInvalidData
314
            raise WizardReloadPage
283 315

  
284 316
        return cloud
285 317

  
......
289 321
        choices=cloud_choices, extra_label="Add", extra=cloud_add,
290 322
        title="Clouds", validate=cloud_validate, fallback=cloud_none_available)
291 323

  
324
    # Create Image Name Wizard Page
292 325
    name = WizardInputPage(
293 326
        "ImageName", "Image Name", "Please provide a name for the image:",
294 327
        title="Image Name", default=ostype if distro == "unknown" else distro)
295 328

  
329
    # Create Image Description Wizard Page
296 330
    descr = WizardInputPage(
297 331
        "ImageDescription", "Image Description",
298 332
        "Please provide a description for the image:",
299 333
        title="Image Description", default=session['metadata']['DESCRIPTION']
300 334
        if 'DESCRIPTION' in session['metadata'] else '')
301 335

  
336
    # Create Sysprep Params Wizard Page
337
    needed = image.os.needed_sysprep_params()
338

  
339
    def sysprep_params_fields():
340
        fields = []
341
        available = image.os.sysprep_params
342
        for param in needed:
343
            text = param.description
344
            default = available[param.name] if param.name in available else ""
345
            fields.append(("%s: " % text, default, param.length))
346
        return fields
347

  
348
    def sysprep_params_validate(answer):
349
        params = {}
350
        for i in range(len(answer)):
351
            if needed[i].validator(answer):
352
                params[needed[i].name] = answer[i]
353
            else:
354
                session['dialog'].msgbox("Invalid value for parameter `%s'" %
355
                                         needed[i].name)
356
                raise WizardReloadPage
357
        return params
358

  
359
    def sysprep_params_display(params):
360
        return ",".join(["%s=%s" % (key, val) for key, val in params.items()])
361

  
362
    sysprep_params = WizardFormPage(
363
        "SysprepParams", "Sysprep Parameters",
364
        "Prease fill in the following system preparation parameters:",
365
        title="System Preparation Parameters", fields=sysprep_params_fields,
366
        display=sysprep_params_display, validate=sysprep_params_validate
367
    ) if len(needed) != 0 else None
368

  
369
    # Create Image Registration Wizard Page
302 370
    def registration_choices():
303 371
        return [("Private", "Image is accessible only by this user"),
304 372
                ("Public", "Everyone can create VMs from this image")]
......
313 381
    w.add_page(cloud)
314 382
    w.add_page(name)
315 383
    w.add_page(descr)
384
    if sysprep_params is not None:
385
        w.add_page(sysprep_params)
316 386
    w.add_page(registration)
317 387

  
318 388
    if w.run():
......
336 406
        out.clear()
337 407

  
338 408
        #Sysprep
409
        image.os.sysprep_params.update(wizard['SysprepParams'])
339 410
        image.os.do_sysprep()
340 411
        metadata = image.os.meta
341 412

  

Also available in: Unified diff