Commit 4e58b51b authored by Nikos Skalkotos's avatar Nikos Skalkotos
Browse files

Create a new output subpackage

parent 710adfb2
......@@ -37,10 +37,8 @@ from kamaki.config import Config
from kamaki.clients import ClientError
from kamaki.clients.image import ImageClient
from kamaki.clients.pithos import PithosClient
from progress.bar import Bar
from image_creator.util import FatalError
from image_creator.output import output, warn
CONTAINER = "images"
......
......@@ -37,8 +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.output import Output, Output_wth_progress, Silent, \
Silent_wth_colors, error
from image_creator.output.cli import SilentOutput, SimpleOutput, \
OutputWthProgress
from image_creator.os_type import get_os_class
from image_creator.kamaki_wrapper import Kamaki
import sys
......@@ -160,9 +160,10 @@ def image_creator():
"must be set")
if options.silent:
out = Silent_wth_colors() if sys.stdout.isatty() else Silent()
out = SilentOutput()
else:
out = Output_wth_progress() if sys.stdout.isatty() else Output()
out = OutputWthProgress(True) if sys.stderr.isatty() else \
SimpleOutput(False)
title = 'snf-image-creator %s' % version
out.output(title)
......
# Copyright 2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
class Output(object):
def error(self, msg, new_line=True):
pass
def warn(self, msg, new_line=True):
pass
def success(self, msg, new_line=True):
pass
def output(self, msg='', new_line=True):
pass
def _get_progress(self):
progress = self._Progress
progress.output = self
return progress
Progress = property(_get_progress)
class _Progress(object):
def __init__(self, size, title, bar_type='default'):
self.size = size
self.output.output("%s..." % title, False)
def goto(self, dest):
pass
def next(self):
pass
def success(self, result):
self.output.success(result)
def progress_generator(self, message):
def generator(n):
progressbar = self.Progress(n, message)
for _ in range(n):
yield
progressbar.next()
progressbar.success('done')
yield
return generator
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
......@@ -31,105 +31,55 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from image_creator.output import Output
import sys
from progress.bar import Bar
from colors import red, green, yellow
from progress.bar import Bar
def error(msg, new_line=True, color=True):
def output(msg='', new_line=True, decorate=lambda x: x):
nl = "\n" if new_line else ' '
if color:
sys.stderr.write(red('Error: %s' % msg) + nl)
else:
sys.stderr.write('Error: %s' % msg + nl)
sys.stderr.write(decorate(msg) + nl)
def warn(msg, new_line=True, color=True):
nl = "\n" if new_line else ' '
if color:
sys.stderr.write(yellow("Warning: %s" % msg) + nl)
else:
sys.stderr.write("Warning: %s" % msg + nl)
def error(msg, new_line=True, colored=True):
color = red if colored else lambda x: x
output("Error: %s" % msg, new_line, color)
def success(msg, new_line=True, color=True):
nl = "\n" if new_line else ' '
if color:
sys.stdout.write(green(msg) + nl)
else:
sys.stdout.write(msg + nl)
if not nl:
sys.stdout.flush()
def warn(msg, new_line=True, colored=True):
color = yellow if colored else lambda x: x
output("Warning: %s" % msg, new_line, color)
def output(msg='', new_line=True):
nl = "\n" if new_line else ' '
sys.stdout.write(msg + nl)
if not nl:
sys.stdout.flush()
def success(msg, new_line=True, colored=True):
color = green if colored else lambda x: x
output(msg, new_line, color)
class SilentOutput(Output):
pass
class Output(object):
class SimpleOutput(Output):
def __init__(self, colored=True):
self.colored = colored
def error(self, msg, new_line=True):
error(msg, new_line, False)
error(msg, new_line, self.colored)
def warn(self, msg, new_line=True):
warn(msg, new_line, False)
warn(msg, new_line, self.colored)
def success(self, msg, new_line=True):
success(msg, new_line, False)
success(msg, new_line, self.colored)
def output(self, msg='', new_line=True):
output(msg, new_line)
def _get_progress(self):
progress = self._Progress
progress.output = self
return progress
Progress = property(_get_progress)
class _Progress(object):
def __init__(self, size, title, bar_type='default'):
self.output.output("%s..." % title, False)
self.size = size
def goto(self, dest):
pass
def next(self):
pass
def success(self, result):
self.output.success(result)
def progress_generator(self, message):
def generator(n):
progressbar = self.Progress(message, 'default')
progressbar.max = n
for _ in range(n):
yield
progressbar.next()
progressbar.success('done')
yield
return generator
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 OutputWthProgress(SimpleOutput):
class _Progress(Bar):
MESSAGE_LENGTH = 30
......@@ -142,7 +92,7 @@ class Output_wth_progress(Output_wth_colors):
}
def __init__(self, size, title, bar_type='default'):
super(Output_wth_progress._Progress, self).__init__()
super(OutputWthProgress._Progress, self).__init__()
self.title = title
self.fill = '#'
self.bar_prefix = ' ['
......@@ -159,19 +109,4 @@ class Output_wth_progress(Output_wth_colors):
self.output.success(result)
class Silent(Output):
def warn(self, msg, new_line=True):
pass
def success(self, msg, new_line=True):
pass
def output(self, msg='', new_line=True):
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 :
......@@ -64,7 +64,7 @@ class MD5:
BLOCKSIZE = 4 * MB # 4MB
prog_size = ((size + MB - 1) // MB) # in MB
progressbar = self.out.Progress(prog_size, "Calculating md5sum:", 'mb')
progressbar = self.out.Progress(prog_size, "Calculating md5sum", 'mb')
md5 = hashlib.md5()
with open(filename, "r") as src:
left = size
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment