Check if stdout is a tty
authorNikos Skalkotos <skalkoto@grnet.gr>
Wed, 6 Jun 2012 11:02:28 +0000 (14:02 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Wed, 6 Jun 2012 11:05:31 +0000 (14:05 +0300)
If not then don't output progressbars and colors

image_creator/main.py
image_creator/output.py

index 30fa7d6..9a5aeab 100644 (file)
@@ -37,7 +37,8 @@ from image_creator import __version__ as version
 from image_creator import util
 from image_creator.disk import Disk
 from image_creator.util import get_command, FatalError, MD5
 from image_creator import util
 from image_creator.disk import Disk
 from image_creator.util import get_command, FatalError, MD5
-from image_creator.output import Output, Output_with_progress, Silent, error
+from image_creator.output import Output, Output_wth_progress, Silent, \
+                                                    Silent_wth_colors, error
 from image_creator.os_type import get_os_class
 from image_creator.kamaki_wrapper import Kamaki
 import sys
 from image_creator.os_type import get_os_class
 from image_creator.kamaki_wrapper import Kamaki
 import sys
@@ -159,9 +160,9 @@ def image_creator():
                                                                 "must be set")
 
     if options.silent:
                                                                 "must be set")
 
     if options.silent:
-        out = Silent()
+        out = Silent_wth_colors() if sys.stdout.isatty() else Silent()
     else:
     else:
-        out = Output_with_progress()
+        out = Output_wth_progress() if sys.stdout.isatty() else Output()
 
     title = 'snf-image-creator %s' % version
     out.output(title)
 
     title = 'snf-image-creator %s' % version
     out.output(title)
@@ -281,7 +282,10 @@ def main():
         ret = image_creator()
         sys.exit(ret)
     except FatalError as e:
         ret = image_creator()
         sys.exit(ret)
     except FatalError as e:
-        error(e)
+        if sys.stdout.isatty():
+            error(e)
+        else:
+            error(e, True, False)
         sys.exit(1)
 
 
         sys.exit(1)
 
 
index fc3c422..51312e1 100644 (file)
@@ -36,19 +36,28 @@ from progress.bar import Bar
 from colors import red, green, yellow
 
 
 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 ''
     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 ''
     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 ''
     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()
 
     if not nl:
         sys.stdout.flush()
 
@@ -62,13 +71,13 @@ def output(msg='', new_line=True):
 
 class Output(object):
     def error(self, 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):
 
     def warn(self, msg, new_line=True):
-        warn(msg, new_line)
+        warn(msg, new_line, False)
 
     def success(self, msg, new_line=True):
 
     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)
 
     def output(self, msg='', new_line=True):
         output(msg, new_line)
@@ -100,7 +109,18 @@ class Output(object):
         return generator
 
 
         return generator
 
 
-class Output_with_progress(Output):
+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
 
     class Progress(Bar):
         MESSAGE_LENGTH = 30
 
@@ -113,7 +133,7 @@ class Output_with_progress(Output):
         }
 
         def __init__(self, title, bar_type='default'):
         }
 
         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 = ' ['
             self.title = title
             self.fill = '#'
             self.bar_prefix = ' ['
@@ -137,10 +157,14 @@ class Silent(Output):
         pass
 
     class Progress(Output.Progress):
         pass
 
     class Progress(Output.Progress):
-        def __init__(self, title, bar_type):
+        def __init__(self, title, bar_type='default'):
             pass
 
         def success(self, result):
             pass
 
             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 :
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :