Make progress class contain and Ouput instance
[snf-image-creator] / image_creator / output.py
index 0f33f6d..4eabc06 100644 (file)
@@ -36,19 +36,28 @@ from progress.bar import Bar
 from colors import red, green, yellow
 
 
-def error(msg, new_line=True):
+def error(msg, new_line=True, color=True):
     nl = "\n" if new_line else ''
-    sys.stderr.write(red('Error: %s' % msg) + nl)
+    if color:
+        sys.stderr.write(red('Error: %s' % msg) + nl)
+    else:
+        sys.stderr.write('Error: %s' % msg + nl)
 
 
-def warn(msg, new_line=True):
+def warn(msg, new_line=True, color=True):
     nl = "\n" if new_line else ''
-    sys.stderr.write(yellow("Warning: %s" % msg) + nl)
+    if color:
+        sys.stderr.write(yellow("Warning: %s" % msg) + nl)
+    else:
+        sys.stderr.write("Warning: %s" % msg + nl)
 
 
-def success(msg, new_line=True):
+def success(msg, new_line=True, color=True):
     nl = "\n" if new_line else ''
-    sys.stdout.write(green(msg) + nl)
+    if color:
+        sys.stdout.write(green(msg) + nl)
+    else:
+        sys.stdout.write(msg + nl)
     if not nl:
         sys.stdout.flush()
 
@@ -61,21 +70,29 @@ def output(msg='', new_line=True):
 
 
 class Output(object):
+
     def error(self, msg, new_line=True):
-        error(msg, new_line)
+        error(msg, new_line, False)
 
     def warn(self, msg, new_line=True):
-        warn(msg, new_line)
+        warn(msg, new_line, False)
 
     def success(self, msg, new_line=True):
-        success(msg, new_line)
+        success(msg, new_line, False)
 
     def output(self, msg='', new_line=True):
         output(msg, new_line)
 
-    class Progress(object):
+    def _get_progress(self):
+        progress = self._Progress
+        progress.output = self
+        return progress
+
+    Progress = property(_get_progress)
+
+    class _Progress(object):
         def __init__(self, title, bar_type='default'):
-            output("%s..." % title, False)
+            self.output.output("%s..." % title, False)
 
         def goto(self, dest):
             pass
@@ -84,14 +101,12 @@ class Output(object):
             pass
 
         def success(self, result):
-            sucess(result)
-
-    def progress_gen(self, message):
-        
-        progress = getattr(self, 'Progress')
+            self.output.success(result)
 
+    def progress_generator(self, message):
         def generator(n):
-            progressbar = progress(message, 'default')
+            progressbar = self.Progress(message, 'default')
+            progressbar.max = n
 
             for _ in range(n):
                 yield
@@ -102,8 +117,19 @@ class Output(object):
         return generator
 
 
-class Output_with_progress(Output):
-    class Progress(Bar):
+class Output_wth_colors(Output):
+    def error(self, msg, new_line=True):
+        error(msg, new_line)
+
+    def warn(self, msg, new_line=True):
+        warn(msg, new_line)
+
+    def success(self, msg, new_line=True):
+        success(msg, new_line)
+
+
+class Output_wth_progress(Output_wth_colors):
+    class _Progress(Bar):
         MESSAGE_LENGTH = 30
 
         template = {
@@ -115,7 +141,7 @@ class Output_with_progress(Output):
         }
 
         def __init__(self, title, bar_type='default'):
-            super(Output_with_progress.Progress, self).__init__()
+            super(Output_wth_progress._Progress, self).__init__()
             self.title = title
             self.fill = '#'
             self.bar_prefix = ' ['
@@ -124,8 +150,8 @@ class Output_with_progress(Output):
             self.suffix = self.template[bar_type]
 
         def success(self, result):
-            output("\r%s... \033[K" % self.title, False)
-            success(result)
+            self.output.output("\r%s... \033[K" % self.title, False)
+            self.output.success(result)
 
 
 class Silent(Output):
@@ -138,11 +164,9 @@ class Silent(Output):
     def output(self, msg='', new_line=True):
         pass
 
-    class Progress(Output.Progress):
-        def __init__(self, title, bar_type):
-            pass
 
-        def success(self, result):
-            pass
+class Silent_wth_colors(Silent):
+    def error(self, msg, new_line=True):
+        error(msg, new_line)
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :