This class is helping to manipulate by output streams stderr
, stdout
, and file
. Are there any cons and errors in this code?
class OutSplitter(object):
""" splitter """
def __init__(self, out_splitter):
self.__pipes=None
if isinstance(out_splitter, OutSplitter):
self.__pipes = Set(out_splitter.pipes)
elif out_splitter:
if hasattr(out_splitter,"__iter__") : #
for pn in out_splitter:
self.add_pipe(pn)
else:
self.add_pipe(out_splitter)
@property
def pipes(self):
return self.__pipes
def add_pipe(self, pn):
if isinstance(pn, basestring):
if pn == 'stderr': x = sys.stderr
elif pn == 'stdout': x = sys.stdout
else:
p = os.path.dirname(pn)
if p and not os.path.exists(p):
os.makedirs(p)
x = open(pn,'w+')
else: x = pn
if hasattr(x, 'write') and hasattr(x, 'flush'):
if self.__pipes is None:
self.__pipes=Set()
self.__pipes.add(x)
def write(self,s):
if self.__pipes:
for p in self.__pipes:
p.write(s)
def flush(self):
if self.__pipes:
for p in self.__pipes:
p.flush()