Monkey-patch pythondialog to support form boxes
[snf-image-creator] / image_creator / dialog_util.py
index 00f8683..6c3b7ce 100644 (file)
@@ -1,5 +1,5 @@
-#!/usr/bin/env python
-
+# -*- coding: utf-8 -*-
+#
 # Copyright 2012 GRNET S.A. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
+"""Module providing useful functions for the dialog-based version of
+snf-image-creator.
+"""
+
 import os
 from image_creator.output.dialog import GaugeOutput
 from image_creator.util import MD5
@@ -42,35 +46,42 @@ WIDTH = 70
 
 
 def update_background_title(session):
+    """Update the backgroud title of the dialog page"""
     d = session['dialog']
-    dev = session['device']
+    disk = session['disk']
+    image = session['image']
 
     MB = 2 ** 20
 
-    size = (dev.size + MB - 1) // MB
+    size = (image.size + MB - 1) // MB
     shrinked = 'shrinked' in session and session['shrinked']
     postfix = " (shrinked)" if shrinked else ''
 
-    title = "OS: %s, Distro: %s, Size: %dMB%s" % \
-            (dev.ostype, dev.distro, size, postfix)
+    title = "OS: %s, Distro: %s, Size: %dMB%s, Source: %s" % \
+            (image.ostype, image.distro, size, postfix,
+             os.path.abspath(disk.source))
 
     d.setBackgroundTitle(title)
 
 
 def confirm_exit(d, msg=''):
+    """Ask the user to confirm when exiting the program"""
     return not d.yesno("%s Do you want to exit?" % msg, width=SMALL_WIDTH)
 
 
 def confirm_reset(d):
+    """Ask the user to confirm a reset action"""
     return not d.yesno("Are you sure you want to reset everything?",
                        width=SMALL_WIDTH, defaultno=1)
 
 
 class Reset(Exception):
+    """Exception used to reset the program"""
     pass
 
 
 def extract_metadata_string(session):
+    """Convert image metadata to text"""
     metadata = ['%s=%s' % (k, v) for (k, v) in session['metadata'].items()]
 
     if 'task_metadata' in session:
@@ -80,6 +91,7 @@ def extract_metadata_string(session):
 
 
 def extract_image(session):
+    """Dump the image to a local file"""
     d = session['dialog']
     dir = os.getcwd()
     while 1:
@@ -126,27 +138,25 @@ def extract_image(session):
 
         gauge = GaugeOutput(d, "Image Extraction", "Extracting image...")
         try:
-            dev = session['device']
-            out = dev.out
+            image = session['image']
+            out = image.out
             out.add(gauge)
             try:
                 if "checksum" not in session:
-                    size = dev.size
                     md5 = MD5(out)
-                    session['checksum'] = md5.compute(session['snapshot'],
-                                                      size)
+                    session['checksum'] = md5.compute(image.device, image.size)
 
                 # Extract image file
-                dev.dump(path)
+                image.dump(path)
 
                 # Extract metadata file
-                out.output("Extracting metadata file...")
+                out.output("Extracting metadata file ...")
                 with open('%s.meta' % path, 'w') as f:
                     f.write(extract_metadata_string(session))
                 out.success('done')
 
                 # Extract md5sum file
-                out.output("Extracting md5sum file...")
+                out.output("Extracting md5sum file ...")
                 md5str = "%s %s\n" % (session['checksum'], name)
                 with open('%s.md5sum' % path, 'w') as f:
                     f.write(md5str)