Author: bugman Date: Fri Aug 19 12:23:30 2011 New Revision: 14398 URL: http://svn.gna.org/viewcvs/relax?rev=14398&view=rev Log: Clean up of the multi-processor IO module. The Multiplex_stdout class has been deleted as this is the wrong way of handling the IO streams and is unused anyway. The PrependStringIO class has had the getvalue() and truncate() methods removed as these need not be implemented as it is provided by StringIO. Modified: branches/gui_testing/multi/prependStringIO.py Modified: branches/gui_testing/multi/prependStringIO.py URL: http://svn.gna.org/viewcvs/relax/branches/gui_testing/multi/prependStringIO.py?rev=14398&r1=14397&r2=14398&view=diff ============================================================================== --- branches/gui_testing/multi/prependStringIO.py (original) +++ branches/gui_testing/multi/prependStringIO.py Fri Aug 19 12:23:30 2011 @@ -26,49 +26,7 @@ from threading import currentThread -class Multiplex_stdout(StringIO): - def __init__(self): - StringIO.__init__(self) - self.thread_stream_map = {} - - - def add_stream(self, stream): - thread_id = self.current_thread_id() - self.thread_stream_map[thread_id] = stream - - - def current_thread_id(self): - return self.thread_id(currentThread()) - - - def get_stream(self, thread=None): - if thread == None: - thread_id = self.current_thread_id() - else: - thread_id = self.thread_id(thread) - - return self.thread_stream_map[thread_id] - - - def getvalue(self): - return self.get_stream().getvalue() - - - def thread_id(self, thread): - # wanted to use thread.get_ident but main thread barfes on it could use -1? - return id(thread) - - - def write(self, string): - thread = currentThread() - thread_id = self.thread_id(thread) - - stream = self.thread_stream_map[thread_id] - return stream.write(string) - - - -class PrependOut(StringIO): +class PrependOut: """Class for adding a token to the end of all newlines.""" def __init__(self, token, stream): @@ -102,45 +60,59 @@ self.stream.write(string) # Flush both STDOUT and STDERR. - sys.__stdout__.flush() - sys.__stderr__.flush() + sys.stdout.flush() + sys.stderr.flush() -#TODO: maybe this hsould be a delegate to a stringio rather than being a stringio as this will speed things up and simplify things class PrependStringIO(StringIO): - def __init__(self, token, target_stream=None): + """Class for adding a token to the end of all newlines.""" + + def __init__(self, token, stream=None): + """Set up the class for stream manipulation. + + @param token: The string to add to the end of all newlines. + @type token: str + @param stream: The IO stream + @type stream: IO stream + """ + + # Execute the base class __init__() method. StringIO.__init__(self) + + # Store the args. self.token = token + + # Set up the stream. + if stream == None: + self.stream = self + else: + self.stream = stream + + # Initialise. self.token_length = len(token) self.first_time = True - if target_stream == None: - self.target_stream = self - else: - self.target_stream = target_stream - - - def getvalue(self): - result = StringIO.getvalue(self) - if len(result) > 0 and result[-1] == '\n': - result = result[0:-self.token_length-1] - result = result+'\n' - - return result - - - def truncate(self, size=None): - if size == 0: - self.first_time = True - #PY3K: should be a call to super but StringIO is a old style class - StringIO.truncate(self, size) def write(self, string): + """Replacement write() method for prepending the token to each line of STDOUT and STDERR. + + @param string: The line of text to write to STDOUT or STDERR. + @type string: str + """ + # FIXME: raising an exception here wedges mpi4py + # Append the token to all newline chars. string = string.replace('\n', '\n' + self.token) + + # Handle the first line of output. if self.first_time == True: string = '\n' + self.token + string self.first_time = False - StringIO.write(self.target_stream, string) + # Write the string to the stream. + StringIO.write(self.stream, string) + + # Flush both STDOUT and STDERR. + sys.stdout.flush() + sys.stderr.flush()