Refactoring: move stdin/out up to __main__

Use explicit stream variable in inner functions,
sys streams are thus only in use in the upper layer.
This commit is contained in:
Johann Dreo 2013-05-26 10:39:52 +02:00
commit 02a79ff10e

View file

@ -5,6 +5,7 @@
# Licensed under the GPL version 3 # Licensed under the GPL version 3
# 2012 (c) nojhan <nojhan@nojhan.net> # 2012 (c) nojhan <nojhan@nojhan.net>
import sys
import re import re
import random import random
import os import os
@ -330,15 +331,15 @@ def colortheme(item, theme):
return item return item
def write(colored): def write(colored, stream = sys.stdout):
""" """
Write "colored" on sys.stdout, then flush. Write "colored" on sys.stdout, then flush.
""" """
sys.stdout.write(colored) stream.write(colored)
sys.stdout.flush() stream.flush()
def map_write( stream, function, *args ): def map_write( stream_in, stream_out, function, *args ):
""" """
Read the given file-like object as a non-blocking stream Read the given file-like object as a non-blocking stream
and call the function on each item (line), and call the function on each item (line),
@ -351,12 +352,12 @@ def map_write( stream, function, *args ):
""" """
while True: while True:
try: try:
item = stream.readline() item = stream_in.readline()
except KeyboardInterrupt: except KeyboardInterrupt:
break break
if not item: if not item:
break break
write( function(item, *args) ) write( function(item, *args), stream_out )
def colorgen(stream, pattern, color="red", style="normal", on_groups=False): def colorgen(stream, pattern, color="red", style="normal", on_groups=False):
@ -396,7 +397,6 @@ def __args_dirty__(argv, usage=""):
>>> colout.__args_dirty__(["colout","pattern","colors","styles","True"],"usage") >>> colout.__args_dirty__(["colout","pattern","colors","styles","True"],"usage")
('pattern', 'colors', 'styles', True) ('pattern', 'colors', 'styles', True)
""" """
import sys
# Use a dirty argument picker # Use a dirty argument picker
# Check for bad usage or an help flag # Check for bad usage or an help flag
@ -495,19 +495,18 @@ def __args_parse__(argv, usage=""):
args.colormap, args.theme, args.source, args.all, args.scale args.colormap, args.theme, args.source, args.all, args.scale
def stdin_write( as_all, function, *args ): def write_all( as_all, stream_in, stream_out, function, *args ):
""" """
If as_all, print function(*args) on the whole stream, If as_all, print function(*args) on the whole stream,
else, print it for each line. else, print it for each line.
""" """
if as_all: if as_all:
write( function( sys.stdin.read(), *args ) ) write( function( stream_in.read(), *args ), stream_out )
else: else:
map_write( sys.stdin, function, *args ) map_write( stream_in, stream_out, function, *args )
if __name__ == "__main__": if __name__ == "__main__":
import sys
usage = "A regular expression based formatter that color up an arbitrary text stream." usage = "A regular expression based formatter that color up an arbitrary text stream."
@ -535,7 +534,7 @@ if __name__ == "__main__":
# if theme # if theme
if as_theme: if as_theme:
assert(pattern in themes.keys()) assert(pattern in themes.keys())
stdin_write( as_all, colortheme, themes[pattern].theme() ) write_all( as_all, sys.stdin, sys.stdout, colortheme, themes[pattern].theme() )
# if pygments # if pygments
elif as_source: elif as_source:
@ -551,9 +550,9 @@ if __name__ == "__main__":
else: else:
formatter = TerminalFormatter() formatter = TerminalFormatter()
stdin_write( as_all, highlight, lexer, formatter ) write_all( as_all, sys.stdin, sys.stdout, highlight, lexer, formatter )
# if color # if color
else: else:
stdin_write( as_all, colorup, pattern, color, style, on_groups ) write_all( as_all, sys.stdin, sys.stdout, colorup, pattern, color, style, on_groups )