Statistics
| Branch: | Tag: | Revision:

root / image_creator / output / composite.py @ 12c97404

History | View | Annotate | Download (4.3 kB)

1 121f3bc0 Nikos Skalkotos
# -*- coding: utf-8 -*-
2 121f3bc0 Nikos Skalkotos
#
3 c3fe132c Nikos Skalkotos
# Copyright 2012 GRNET S.A. All rights reserved.
4 c3fe132c Nikos Skalkotos
#
5 c3fe132c Nikos Skalkotos
# Redistribution and use in source and binary forms, with or
6 c3fe132c Nikos Skalkotos
# without modification, are permitted provided that the following
7 c3fe132c Nikos Skalkotos
# conditions are met:
8 c3fe132c Nikos Skalkotos
#
9 c3fe132c Nikos Skalkotos
#   1. Redistributions of source code must retain the above
10 c3fe132c Nikos Skalkotos
#      copyright notice, this list of conditions and the following
11 c3fe132c Nikos Skalkotos
#      disclaimer.
12 c3fe132c Nikos Skalkotos
#
13 c3fe132c Nikos Skalkotos
#   2. Redistributions in binary form must reproduce the above
14 c3fe132c Nikos Skalkotos
#      copyright notice, this list of conditions and the following
15 c3fe132c Nikos Skalkotos
#      disclaimer in the documentation and/or other materials
16 c3fe132c Nikos Skalkotos
#      provided with the distribution.
17 c3fe132c Nikos Skalkotos
#
18 c3fe132c Nikos Skalkotos
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 c3fe132c Nikos Skalkotos
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 c3fe132c Nikos Skalkotos
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 c3fe132c Nikos Skalkotos
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 c3fe132c Nikos Skalkotos
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 c3fe132c Nikos Skalkotos
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 c3fe132c Nikos Skalkotos
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 c3fe132c Nikos Skalkotos
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 c3fe132c Nikos Skalkotos
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 c3fe132c Nikos Skalkotos
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 c3fe132c Nikos Skalkotos
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 c3fe132c Nikos Skalkotos
# POSSIBILITY OF SUCH DAMAGE.
30 c3fe132c Nikos Skalkotos
#
31 c3fe132c Nikos Skalkotos
# The views and conclusions contained in the software and
32 c3fe132c Nikos Skalkotos
# documentation are those of the authors and should not be
33 c3fe132c Nikos Skalkotos
# interpreted as representing official policies, either expressed
34 c3fe132c Nikos Skalkotos
# or implied, of GRNET S.A.
35 c3fe132c Nikos Skalkotos
36 121f3bc0 Nikos Skalkotos
"""This module implements the CompositeOutput output class"""
37 121f3bc0 Nikos Skalkotos
38 c3fe132c Nikos Skalkotos
from image_creator.output import Output
39 c3fe132c Nikos Skalkotos
40 c3fe132c Nikos Skalkotos
41 c3fe132c Nikos Skalkotos
class CompositeOutput(Output):
42 c3fe132c Nikos Skalkotos
    """This class can be used to composite different outputs into a single one
43 c3fe132c Nikos Skalkotos

44 c3fe132c Nikos Skalkotos
    You may create an instance of this class and then add other output
45 c3fe132c Nikos Skalkotos
    instances to it. Executing a method on this instance will cause the
46 c3fe132c Nikos Skalkotos
    execution of the same method in each output instance that has been added to
47 c3fe132c Nikos Skalkotos
    this one.
48 c3fe132c Nikos Skalkotos
    """
49 c3fe132c Nikos Skalkotos
50 c3fe132c Nikos Skalkotos
    def __init__(self, outputs=[]):
51 88f83027 Nikos Skalkotos
        """Add initial output instances"""
52 c3fe132c Nikos Skalkotos
        self._outputs = outputs
53 c3fe132c Nikos Skalkotos
54 c3fe132c Nikos Skalkotos
    def add(self, output):
55 88f83027 Nikos Skalkotos
        """Add another output instance"""
56 c3fe132c Nikos Skalkotos
        self._outputs.append(output)
57 c3fe132c Nikos Skalkotos
58 c3fe132c Nikos Skalkotos
    def remove(self, output):
59 88f83027 Nikos Skalkotos
        """Remove an output instance"""
60 c3fe132c Nikos Skalkotos
        self._outputs.remove(output)
61 c3fe132c Nikos Skalkotos
62 c3fe132c Nikos Skalkotos
    def error(self, msg, new_line=True):
63 88f83027 Nikos Skalkotos
        """Call the error method of each of the output instances"""
64 c3fe132c Nikos Skalkotos
        for out in self._outputs:
65 c3fe132c Nikos Skalkotos
            out.error(msg, new_line)
66 c3fe132c Nikos Skalkotos
67 c3fe132c Nikos Skalkotos
    def warn(self, msg, new_line=True):
68 88f83027 Nikos Skalkotos
        """Call the warn method of each of the output instances"""
69 c3fe132c Nikos Skalkotos
        for out in self._outputs:
70 c3fe132c Nikos Skalkotos
            out.warn(msg, new_line)
71 c3fe132c Nikos Skalkotos
72 c3fe132c Nikos Skalkotos
    def success(self, msg, new_line=True):
73 88f83027 Nikos Skalkotos
        """Call the success method of each of the output instances"""
74 c3fe132c Nikos Skalkotos
        for out in self._outputs:
75 c3fe132c Nikos Skalkotos
            out.success(msg, new_line)
76 c3fe132c Nikos Skalkotos
77 c3fe132c Nikos Skalkotos
    def output(self, msg='', new_line=True):
78 88f83027 Nikos Skalkotos
        """Call the output method of each of the output instances"""
79 c3fe132c Nikos Skalkotos
        for out in self._outputs:
80 c3fe132c Nikos Skalkotos
            out.output(msg, new_line)
81 c3fe132c Nikos Skalkotos
82 c3fe132c Nikos Skalkotos
    def cleanup(self):
83 88f83027 Nikos Skalkotos
        """Call the cleanup method of each of the output instances"""
84 c3fe132c Nikos Skalkotos
        for out in self._outputs:
85 c3fe132c Nikos Skalkotos
            out.cleanup()
86 c3fe132c Nikos Skalkotos
87 c3fe132c Nikos Skalkotos
    def clear(self):
88 88f83027 Nikos Skalkotos
        """Call the clear method of each of the output instances"""
89 c3fe132c Nikos Skalkotos
        for out in self._outputs:
90 c3fe132c Nikos Skalkotos
            out.clear()
91 c3fe132c Nikos Skalkotos
92 c3fe132c Nikos Skalkotos
    class _Progress(object):
93 88f83027 Nikos Skalkotos
        """Class used to composite different Progress objects"""
94 c3fe132c Nikos Skalkotos
95 c3fe132c Nikos Skalkotos
        def __init__(self, size, title, bar_type='default'):
96 88f83027 Nikos Skalkotos
            """Create a progress on each of the added output instances"""
97 c3fe132c Nikos Skalkotos
            self._progresses = []
98 c3fe132c Nikos Skalkotos
            for out in self.output._outputs:
99 c3fe132c Nikos Skalkotos
                self._progresses.append(out.Progress(size, title, bar_type))
100 c3fe132c Nikos Skalkotos
101 c3fe132c Nikos Skalkotos
        def goto(self, dest):
102 88f83027 Nikos Skalkotos
            """Call the goto method of each of the progress instances"""
103 c3fe132c Nikos Skalkotos
            for progress in self._progresses:
104 c3fe132c Nikos Skalkotos
                progress.goto(dest)
105 c3fe132c Nikos Skalkotos
106 c3fe132c Nikos Skalkotos
        def next(self):
107 88f83027 Nikos Skalkotos
            """Call the next method of each of the progress instances"""
108 c3fe132c Nikos Skalkotos
            for progress in self._progresses:
109 c3fe132c Nikos Skalkotos
                progress.next()
110 c3fe132c Nikos Skalkotos
111 c3fe132c Nikos Skalkotos
        def success(self, result):
112 88f83027 Nikos Skalkotos
            """Call the success method of each of the progress instances"""
113 c3fe132c Nikos Skalkotos
            for progress in self._progresses:
114 c3fe132c Nikos Skalkotos
                progress.success(result)
115 c3fe132c Nikos Skalkotos
116 c3fe132c Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :