From f71dff9d2ba54b4673e9229d1deb6782f8547adf Mon Sep 17 00:00:00 2001 From: Christian Van Wambeke Date: Sat, 5 May 2018 15:35:28 +0200 Subject: [PATCH] fix returnCode.py --- data/local.pyconf | 6 ++-- src/debug.py | 18 +++++++++-- src/returnCode.py | 24 +++++++++++---- test/test_020_debug.py | 9 ++++-- test/test_022_returnCode.py | 61 +++++++++++++++++++++++++++++-------- test/test_025_pyconf.py | 57 ++++++++++++++++++++++++++++++++-- test/test_500_APPLI_TEST.py | 2 +- 7 files changed, 147 insertions(+), 30 deletions(-) diff --git a/data/local.pyconf b/data/local.pyconf index 9583550..fcbbb9f 100644 --- a/data/local.pyconf +++ b/data/local.pyconf @@ -12,11 +12,11 @@ { project_file_paths : [ - "/volatile/wambeke/SAT5/SAT5_S840_MATIX24/SAT_SALOME/salome.pyconf", + ##"/volatile/wambeke/SAT5/SAT5_S840_MATIX24/SAT_SALOME/salome.pyconf", # "/home/uranietm/proJET/saTJOBS/saT5/uranie.pyconf", # cloned 2017/12 for matix - "/home/matix/GitRepo/uranie/saT5/uranie.pyconf", - "/volatile/wambeke/SAT5/SAT_MATIX/matix.pyconf" + ##"/home/matix/GitRepo/uranie/saT5/uranie.pyconf", + ##"/volatile/wambeke/SAT5/SAT_MATIX/matix.pyconf" #"/home/christian/SAT_SALOME/salome.pyconf" #"/home/christian/SAT_MATIX/matix.pyconf" #"/home/christian/SAT_MATIX" diff --git a/src/debug.py b/src/debug.py index 74f90ec..30b6ba3 100644 --- a/src/debug.py +++ b/src/debug.py @@ -179,11 +179,23 @@ def _saveConfigRecursiveDbg(config, aStream, indent, path): indentp = indentp + 2 indstr = indent * ' ' # '':no indent, ' ':indent strType = str(type(config)) + print "zzzstrType", path, strType + if "Sequence" in strType: for i in range(len(config)): _saveConfigRecursiveDbg(config[i], aStream, indentp, path+"[%i]" % i) return - try: + ''' + if "Reference" in strType: + try: + #evaluate = value.resolve(config) + aStream.write("%s%s : %s --> '%s'\n" % (indstr, path, config, str(config))) + except Exception as e: + aStream.write("%s%s : !!! ERROR: %s !!!\n" % (indstr, path, e.message)) + return + ''' + + try: #type config, mapping order = object.__getattribute__(config, 'order') data = object.__getattribute__(config, 'data') except: @@ -192,7 +204,7 @@ def _saveConfigRecursiveDbg(config, aStream, indent, path): for key in sorted(data): #order): # data as sort alphabetical, order as initial order value = data[key] strType = str(type(value)) - if debug: print indstr + 'strType = %s' % strType, key + if debug: print 'strType', path, key, strType if "Config" in strType: _saveConfigRecursiveDbg(value, aStream, indentp, path+"."+key) continue @@ -201,7 +213,7 @@ def _saveConfigRecursiveDbg(config, aStream, indent, path): continue if "Sequence" in strType: for i in range(len(value)): - _saveConfigRecursiveDbg(value[i], aStream, indentp, path+"."+key+"[%i]" % i) + _saveConfigRecursiveDbg(value.data[i], aStream, indentp, path+"."+key+"[%i]" % i) continue if "Expression" in strType: try: diff --git a/src/returnCode.py b/src/returnCode.py index 7010ccc..861d557 100644 --- a/src/returnCode.py +++ b/src/returnCode.py @@ -26,6 +26,7 @@ This file contains ReturnCode class import pprint as PP +# global module variable _OK_STATUS = "OK" _KO_STATUS = "KO" _NA_STATUS = "NA" # not applicable @@ -67,8 +68,9 @@ class ReturnCode(object): | >> doSomethingWithValue(rc.getValue()) # here i am sure that is OK """ + # redunctant but useful class variables OK_STATUS = _OK_STATUS - KO_STATUS = _OK_STATUS + KO_STATUS = _KO_STATUS NA_STATUS = _NA_STATUS # not applicable UNKNOWN_STATUS = _UNKNOWN_STATUS # not defined KNOWNFAILURE_STATUS = _KNOWNFAILURE_STATUS @@ -92,7 +94,7 @@ class ReturnCode(object): TIMEOUT_STATUS: TOSYS, } _DEFAULT_WHY = "No given explanation" - _DEFAULT_VALUE = "Not set" + _DEFAULT_VALUE = None def __init__(self, status=None, why=None, value=None): self._why = self._DEFAULT_WHY @@ -104,12 +106,12 @@ class ReturnCode(object): def __repr__(self): """complete with value, 'ok, why, value' message""" - res = '%s: "%s" for value: %s' % (self._status, self._why, PP.pformat(self._value)) + res = '%s: %s --value: %s' % (self._status, self._why, PP.pformat(self._value)) return res def __str__(self): """without value, only simple 'ok, why' message""" - res = '%s: "%s"' % (self._status, self._why) + res = '%s: %s' % (self._status, self._why) return res def indent(self, text, amount=5, ch=' '): @@ -124,9 +126,16 @@ class ReturnCode(object): newWhy = self._toList(self.getWhy()) + self._toList(rc2.getWhy()) newValue = self._toList(self.getValue()) + self._toList(rc2.getValue()) if isOk: - return ReturnCode("OK", newWhy, newValue ) + return ReturnCode("OK", newWhy, newValue) else: - return ReturnCode("KO", newWhy, newValue ) + return ReturnCode("KO", newWhy, newValue) + + def __radd__(self, other): + # see http://www.marinamele.com/2014/04/modifying-add-method-of-python-class.html + if other == 0: + return self + else: + return self.__add__(other) def _toList(self, strOrList): """internal use""" @@ -153,6 +162,8 @@ class ReturnCode(object): return self._value def setValue(self, value): + """choice as not deep copying if mutables value""" + # TODO deepcopy maybe for value, not yet self._value = value def setStatus(self, status, why=None, value=None): @@ -169,6 +180,7 @@ class ReturnCode(object): self._why = "Error status '%s' for '%s'" % (status, aWhy) if value is not None: + # TODO deepcopy maybe for value, not yet self._value = value else: self._value = self._DEFAULT_VALUE diff --git a/test/test_020_debug.py b/test/test_020_debug.py index f847789..1354474 100755 --- a/test/test_020_debug.py +++ b/test/test_020_debug.py @@ -99,10 +99,13 @@ class TestCase(unittest.TestCase): inStream = DBG.InStream(_EXAMPLES[2]) cfg = PYF.Config(inStream) res = DBG.getStrConfigDbg(cfg) - DBG.write("test_020 cfg dbg", res) + DBG.write("test_020 cfg dbg", res, True) ress = res.split("\n") - self.assertTrue(".aa : '111'" in ress[0]) - self.assertTrue(".bb : $aa + 222 --> '333'" in ress[1]) + self.assertTrue(".aa" in ress[0]) + self.assertTrue(": '111'" in ress[0]) + self.assertTrue(".bb" in ress[1]) + self.assertTrue(": $aa + 222 " in ress[1]) + self.assertTrue("--> '333'" in ress[1]) def test_025(self): inStream = DBG.InStream(_EXAMPLES[1]) diff --git a/test/test_022_returnCode.py b/test/test_022_returnCode.py index 4e20f43..65ead2e 100755 --- a/test/test_022_returnCode.py +++ b/test/test_022_returnCode.py @@ -45,12 +45,12 @@ class TestCase(unittest.TestCase): DBG.write("test_010 str", rrc) self.assertIn("ND:", rrc) self.assertIn("No given explanation", rrc) - self.assertNotIn("for value", rrc) + self.assertNotIn("--value", rrc) rrc = repr(rc) DBG.write("test_010 repr", rrc) self.assertIn("ND:", rrc) self.assertIn("No given explanation", rrc) - self.assertIn("for value", rrc) + self.assertIn("--value", rrc) def test_015(self): rc = RC("OK", "all is good") @@ -59,12 +59,12 @@ class TestCase(unittest.TestCase): DBG.write("test_015 str", rrc) self.assertIn("OK:", rrc) self.assertIn("all is good", rrc) - self.assertNotIn("for value", rrc) + self.assertNotIn("--value", rrc) rrc = repr(rc) DBG.write("test_015 repr", rrc) self.assertIn("OK:", rrc) self.assertIn("all is good", rrc) - self.assertIn("Not set", rrc) + self.assertIn("None", rrc) aVal = "I am a value result" rc.setValue(aVal) self.assertTrue(rc.isOk()) @@ -98,17 +98,54 @@ class TestCase(unittest.TestCase): aVal = "I am a value result" rc1 = RC("OK", "all is good1", aVal + "1") self.assertTrue(rc1.isOk()) - rc1.setStatus("KO") # change status raz why and value + rc1.setStatus("KO") # raz status and why and value self.assertFalse(rc1.isOk()) - print rc0 - print rc1 self.assertEqual(repr(rc0), repr(rc1)) - rc1 = RC("OK", "all is good1", aVal + "1") - rc2 = rc0 + rc1 + rc1 + rc0 + rc1 - DBG.write("test_025 repr", rc2, True) - rrc = repr(rc2) - self.assertIn("KO:", rrc) + def test_026(self): + rc0 = RC("KO") + aVal = "I am a value result" + rc1 = RC("OK", "all is good1", aVal) + rc2 = rc0 + rc1 # make list with two why and two value + DBG.write("test_026 str", str(rc2)) + DBG.write("test_026 repr", repr(rc2)) + self.assertFalse(rc2.isOk()) + self.assertIn("KO:", repr(rc2)) + self.assertIn("KO:", str(rc2)) + self.assertEqual(len(rc2.getWhy()), 2) + self.assertEqual(len(rc2.getValue()), 2) + + def test_027(self): + rc0 = RC("KO") + aVal = "I am a value result" + rc1 = RC("OK", "all is good1", aVal) + rc2 = sum([rc0, rc1]) # make list with two why and two value + self.assertFalse(rc2.isOk()) + self.assertIn("KO:", repr(rc2)) + self.assertIn("KO:", str(rc2)) + self.assertEqual(len(rc2.getWhy()), 2) + self.assertEqual(len(rc2.getValue()), 2) + + def test_028(self): + rc0 = RC("OK") + aVal = ["I am a mutable list value result"] + rc1 = RC("OK", "all is good1", aVal) + rc2 = rc0 + rc1 + rc1 + rc0 # make list with why and value + DBG.write("test_028 repr", str(rc2)) + DBG.write("test_028 repr", repr(rc2)) + self.assertTrue(rc2.isOk()) + self.assertIn("OK:", repr(rc2)) + self.assertIn("OK:", str(rc2)) + self.assertEqual(len(rc2.getWhy()), 4) + self.assertEqual(len(rc2.getValue()), 4) + + aVal[0] = "modified mutable list value result" + DBG.write("test_028 repr", repr(rc1)) + DBG.write("test_028 repr", repr(rc2)) + # deepcopy value no clearly assumed, could be tricky + self.assertIn("modified mutable", repr(rc1)) + self.assertNotIn("modified mutable", repr(rc2)) + def test_999(self): # one shot tearDown() for this TestCase diff --git a/test/test_025_pyconf.py b/test/test_025_pyconf.py index 83af408..11bc8a7 100755 --- a/test/test_025_pyconf.py +++ b/test/test_025_pyconf.py @@ -64,6 +64,23 @@ _EXAMPLES = { bb: "Hervé" # avoid Hervé -> 'utf8' codec can't decode byte """, +5 : """\ + aa: Yves + bb: "Herve" + cc: [ + cc1 + cc2 + cc3 + $bb + " hello" + ] + dd: { + d1 : dd11 + d2 : dd22 + d3 : dd33 + d4 : $bb + " bye" + } +""", + } @@ -133,14 +150,50 @@ Bienvenue, Yves def test_045(self): """TODO: make Hervé valid with pyconf.py as 0.3.9""" inStream = DBG.InStream(_EXAMPLES[4]) - cfg = PYF9.Config(inStream) outStream = DBG.OutStream() - cfg.save(outStream) # sat renamed save() in __save__() + cfg = PYF9.Config(inStream) + cfg.save(outStream) # OK + # TODO: cfg = PYF.Config(inStream) + # cfg.__save__(outStream) # KO and sat renamed save() in __save__() res = outStream.value DBG.write("test_045 cfg", res) self.assertTrue("aa : 'Yves'" in res) self.assertTrue(r"bb : 'Herv\xc3\xa9'" in res) self.assertEqual(cfg.bb, "Hervé") + + def test_100(self): + inStream = DBG.InStream(_EXAMPLES[5]) + outStream = DBG.OutStream() + cfg = PYF.Config(inStream) # KO + cfg.__save__(outStream) # sat renamed save() in __save__() + res = outStream.value + DBG.write("test_100 cfg save", res, True) + DBG.write("test_100 cfg debug", cfg, True) + DBG.write("test_100 cfg debug", cfg.cc, True) + + cc = cfg.cc + # DBG.write("test_100 type cc[3]", dir(cc), True) + # self.ssave(cc) + DBG.write("test_100 cc", [cc.data[i] for i in range(len(cc))], True) + + def ssave(self, obj): #, stream): + """ + Save this instance to the specified stream. + @param stream: A stream to which the configuration is written. + @type stream: A write-only stream (file-like object). + @param indent: The indentation level for the output, > 0 + @type indent: int + """ + data = object.__getattribute__(obj, 'data') + for i in range(0, len(data)): + value = data[i] + print "[%i]" % i, type(value) + """if isinstance(value, PYF.Container): + print "Container [%i]" % i + #value.writeToStream(stream, indent, self) + else: + print "Other [%i]" % i + #self.writeValue(value, stream, indent)""" def test_999(self): # one shot tearDown() for this TestCase diff --git a/test/test_500_APPLI_TEST.py b/test/test_500_APPLI_TEST.py index a580247..f66bbb9 100755 --- a/test/test_500_APPLI_TEST.py +++ b/test/test_500_APPLI_TEST.py @@ -58,7 +58,7 @@ class TestCase(unittest.TestCase): DBG.write("s.getConfig()", s.getConfig()) #none DBG.write("s.__dict__", s.__dict__) # have returnCode = s.execute_cli(cmd) - DBG.write("test_010 returnCode", returnCode, True) + DBG.write("test_010 returnCode", returnCode) logs = self.logger.getLogs() DBG.write("test_010 logger", logs, True) self.assertTrue(returnCode.isOk()) -- 2.39.2