Skip to content

Commit

Permalink
add stderrflag and stoutflag for std streams (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotan authored Nov 30, 2021
1 parent 25ba099 commit 0160973
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2021-11-30 Jan Kotanski <jankotan@gmail.com>
* add stderrflag and stoutflag for std streams
* tagged as 3.2.0

2021-07-29 Jan Kotanski <jankotan@gmail.com>
* currenttime from the nxstools package used
* tagged as 3.1.2
Expand Down
2 changes: 1 addition & 1 deletion nxswriter/Release.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@


#: package version
__version__ = "3.1.2"
__version__ = "3.2.0"
17 changes: 12 additions & 5 deletions nxswriter/StreamSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
import weakref


# (:obj:`bool`) write stream to stdout
stdoutflag = False

# (:obj:`bool`) write stream to stderrr
stderrflag = True


class StreamSet(object):

def __init__(self, streams):
Expand Down Expand Up @@ -59,7 +66,7 @@ def __init__(self, streams):
if hasattr(streams(), "log_debug"):
self.log_debug = streams().log_debug

def fatal(self, message, std=True):
def fatal(self, message, std=stderrflag):
""" writes fatal error message
:param message: error message
Expand All @@ -77,7 +84,7 @@ def fatal(self, message, std=True):
except Exception:
print(message)

def error(self, message, std=True):
def error(self, message, std=stderrflag):
""" writes error message
:param message: error message
Expand All @@ -95,7 +102,7 @@ def error(self, message, std=True):
except Exception:
print(message)

def warn(self, message, std=True):
def warn(self, message, std=stderrflag):
""" writes warning message
:param message: warning message
Expand All @@ -113,7 +120,7 @@ def warn(self, message, std=True):
except Exception:
print(message)

def info(self, message, std=True):
def info(self, message, std=stdoutflag):
""" writes info message
:param message: info message
Expand All @@ -131,7 +138,7 @@ def info(self, message, std=True):
except Exception:
print(message)

def debug(self, message, std=True):
def debug(self, message, std=stdoutflag):
""" writes debug message
:param message: debug message
Expand Down
42 changes: 23 additions & 19 deletions test/StreamSet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,29 @@
import unittest
import os
import sys
# import subprocess
import random
import time
import struct
import binascii

try:
from cStringIO import StringIO
except Exception:
from io import StringIO
import time

from nxswriter import StreamSet

# if 64-bit machione
IS64BIT = (struct.calcsize("P") == 8)
# import string
if sys.version_info > (3,):
from io import StringIO
else:
from StringIO import StringIO


if sys.version_info > (3,):
long = int


# if 64-bit machione
IS64BIT = (struct.calcsize("P") == 8)


# test fixture
class StreamSetTest(unittest.TestCase):

Expand Down Expand Up @@ -75,7 +79,7 @@ def getRandomString(self, maxsize):
# test starter
# \brief Common set up
def setUp(self):
print("SEED = %s" % self.__seed)
print("SEED =%s" % self.__seed)
print("\nsetting up...")
self.streams = StreamSet.StreamSet(None)
hasattr(self.streams, "log_fatal")
Expand Down Expand Up @@ -328,10 +332,7 @@ def test_info(self):
name = self.getRandomString(100)
sys.stdout = self.mystdout = StringIO()
sys.stderr = self.mystderr = StringIO()
if i % 2:
self.streams.info(name)
else:
self.streams.info(name, std=True)
self.streams.info(name, std=True)
self.assertEqual(self.streams.log_fatal, None)
self.assertEqual(self.streams.log_error, None)
self.assertEqual(self.streams.log_warn, None)
Expand All @@ -349,7 +350,10 @@ def test_info_nostd(self):
name = self.getRandomString(100)
sys.stdout = self.mystdout = StringIO()
sys.stderr = self.mystderr = StringIO()
self.streams.info(name, std=False)
if i % 2 == 0:
self.streams.info(name)
else:
self.streams.info(name, std=False)
self.assertEqual(self.streams.log_fatal, None)
self.assertEqual(self.streams.log_error, None)
self.assertEqual(self.streams.log_warn, None)
Expand Down Expand Up @@ -397,10 +401,7 @@ def test_debug(self):
name = self.getRandomString(100)
sys.stdout = self.mystdout = StringIO()
sys.stderr = self.mystderr = StringIO()
if i % 2:
self.streams.debug(name)
else:
self.streams.debug(name, std=True)
self.streams.debug(name, std=True)
self.assertEqual(self.streams.log_fatal, None)
self.assertEqual(self.streams.log_error, None)
self.assertEqual(self.streams.log_warn, None)
Expand All @@ -418,7 +419,10 @@ def test_debug_nostd(self):
name = self.getRandomString(100)
sys.stdout = self.mystdout = StringIO()
sys.stderr = self.mystderr = StringIO()
self.streams.debug(name, std=False)
if i % 2:
self.streams.debug(name)
else:
self.streams.debug(name, std=False)
self.assertEqual(self.streams.log_fatal, None)
self.assertEqual(self.streams.log_error, None)
self.assertEqual(self.streams.log_warn, None)
Expand Down

0 comments on commit 0160973

Please sign in to comment.