From f4ffbe9b47afe2daf7844d58cfba51d24190007a Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Tue, 12 Jan 2016 12:16:50 +0100 Subject: [PATCH] renamed output_buffer.py to buffers.py --- buffers.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ output_buffer.py | 89 ------------------------------------------------------ 2 files changed, 89 insertions(+), 89 deletions(-) create mode 100644 buffers.py delete mode 100644 output_buffer.py diff --git a/buffers.py b/buffers.py new file mode 100644 index 0000000..ed45b61 --- /dev/null +++ b/buffers.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python + +# The software in this package is distributed under the GNU General +# Public License version 2 (with a special exception described below). +# +# A copy of GNU General Public License (GPL) is included in this distribution, +# in the file COPYING.GPL. +# +# As a special exception, if other files instantiate templates or use macros +# or inline functions from this file, or you compile this file and link it +# with other works to produce a work based on this file, this file +# does not by itself cause the resulting work to be covered +# by the GNU General Public License. +# +# However the source code for this file must still be made available +# in accordance with section (3) of the GNU General Public License. +# +# This exception does not invalidate any other reasons why a work based +# on this file might be covered by the GNU General Public License. + +""" +OutputBuffer: classes to buffer line-based output + +.. codeauthor:: Christian Herdtweck, christian.herdtweck@intra2net.com +""" + +class OutputLineBuffer: + """ circular buffer for text lines; saves last N lines + + can output them afterwards in correct order + + public attributes (please read only!): buffer_size, n_lines + """ + + buffer_size = None + _buffer = None + _buff_idx = None + n_lines = None + + def __init__(self, size): + """ initialize with N = size """ + self.buffer_size = size + self._buffer = ['[no output]' for line_idx in range(size)] + self._buff_idx = 0 + self.n_lines = 0 + + def add(self, line): + """ add a line to buffer -- might replace the oldest line """ + self._buffer[self._buff_idx] = line + self._buff_idx = (self._buff_idx + 1) % self.buffer_size + self.n_lines += 1 + + def output(self, output_func): + """ run output_func(line) on last saved lines """ + if self.n_lines >= self.buffer_size: + for idx in range(self._buff_idx, self.buffer_size): + output_func(self._buffer[idx]) + + for idx in range(0, self._buff_idx): + output_func(self._buffer[idx]) + + +def main(): + """ Main function, called when running file as script + + currently raises a NotImplementedError + """ + raise NotImplementedError + + line_buffer = OutputLineBuffer(3) + for output_line in proc.stdout: + progress_reporter.inc_current_points() + line_buffer.add(output_line) + + proc.stdout.close() + return_code = proc.wait() + + if return_code != 0: + self.logger.warning('extraction returned {0}!'.format(return_code)) + self.logger.warning('command was: {0}'.format(' '.join(cpio))) + self.logger.warning('last {0} output lines:'.format( + min(line_buffer.buffer_size, line_buffer.n_lines))) + line_buffer.output(self.logger.warning) + +# end: function main + + +if __name__ == '__main__': + main() diff --git a/output_buffer.py b/output_buffer.py deleted file mode 100644 index ed45b61..0000000 --- a/output_buffer.py +++ /dev/null @@ -1,89 +0,0 @@ -#!/usr/bin/env python - -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. - -""" -OutputBuffer: classes to buffer line-based output - -.. codeauthor:: Christian Herdtweck, christian.herdtweck@intra2net.com -""" - -class OutputLineBuffer: - """ circular buffer for text lines; saves last N lines - - can output them afterwards in correct order - - public attributes (please read only!): buffer_size, n_lines - """ - - buffer_size = None - _buffer = None - _buff_idx = None - n_lines = None - - def __init__(self, size): - """ initialize with N = size """ - self.buffer_size = size - self._buffer = ['[no output]' for line_idx in range(size)] - self._buff_idx = 0 - self.n_lines = 0 - - def add(self, line): - """ add a line to buffer -- might replace the oldest line """ - self._buffer[self._buff_idx] = line - self._buff_idx = (self._buff_idx + 1) % self.buffer_size - self.n_lines += 1 - - def output(self, output_func): - """ run output_func(line) on last saved lines """ - if self.n_lines >= self.buffer_size: - for idx in range(self._buff_idx, self.buffer_size): - output_func(self._buffer[idx]) - - for idx in range(0, self._buff_idx): - output_func(self._buffer[idx]) - - -def main(): - """ Main function, called when running file as script - - currently raises a NotImplementedError - """ - raise NotImplementedError - - line_buffer = OutputLineBuffer(3) - for output_line in proc.stdout: - progress_reporter.inc_current_points() - line_buffer.add(output_line) - - proc.stdout.close() - return_code = proc.wait() - - if return_code != 0: - self.logger.warning('extraction returned {0}!'.format(return_code)) - self.logger.warning('command was: {0}'.format(' '.join(cpio))) - self.logger.warning('last {0} output lines:'.format( - min(line_buffer.buffer_size, line_buffer.n_lines))) - line_buffer.output(self.logger.warning) - -# end: function main - - -if __name__ == '__main__': - main() -- 1.7.1