- integration

- add test
This commit is contained in:
cclecle
2022-05-25 19:03:46 +02:00
parent 3be2854a40
commit 1f7e6d61ac
5 changed files with 162 additions and 34 deletions

View File

@@ -3,6 +3,7 @@ from typing import Union
class ChaChaINI_KeyNotFoundException(RuntimeError):
pass
class ChaChaINI_SectionNotFoundException(RuntimeError):
pass
@@ -10,6 +11,7 @@ class ChaChaSimpleINI_key:
def __init__(self,name:str,value:str):
self.name = name
self.value = value
#print("add key with value: {0}".format(self.value))
def format(self,bBeautify:bool = False) -> str:
if bBeautify:
@@ -26,7 +28,6 @@ class ChaChaSimpleINI_section:
return "[{0}]".format(self.name)
def formatAll(self,bBeautify:bool = False) -> str:
result = ""
result = self.format() + "\n"
for key in self.ar_keys:
result = result + key.format(bBeautify) + "\n"
@@ -50,10 +51,13 @@ class ChaChaSimpleINI_section:
if name == key.name:
if result is None:
result = key
#print("value found ({0})".format(key.value))
elif isinstance(result,ChaChaSimpleINI_key):
result = [result,key]
#print("another value found, switching to composed value")
else: #array
result.append(key)
#print("another value found, add to result array")
if result is None:
raise ChaChaINI_KeyNotFoundException()
return result
@@ -61,18 +65,23 @@ class ChaChaSimpleINI_section:
def getKeyValue(self,name:str) -> Union[str,list[str]]:
keys = self.getKey(name)
if isinstance(keys,ChaChaSimpleINI_key):
#print("a")
return keys.value
else: #array
#print("b")
result = []
for key in keys:
result.append(key.value)
return result
def setKeyValue(self,name:str,value:str) -> Union[str,list[str]]:
try :
keys = self.getKey(name)
except KeyNotFoundException:
def setAddKeyValue(self,name:str,value:str,bForceAddKey:bool = False) -> Union[str,list[str]]:
if bForceAddKey:
keys = self.appendKey(name,'')
else:
try :
keys = self.getKey(name)
except ChaChaINI_KeyNotFoundException:
keys = self.appendKey(name,'')
if isinstance(keys,ChaChaSimpleINI_key):
keys.value = value
@@ -87,18 +96,24 @@ class ChaChaSimpleINI:
for _line in file:
line = _line.rstrip()
if result := re.search(r'^\s*$',line):
print("find empty line, ignoring this line")
#print("find empty line, ignoring this line")
pass
elif result := re.search(r'^\s*;',line):
print("find comment, ignoring this line")
#print("find comment, ignoring this line")
pass
elif result := re.search(r'^\s*\[(?P<section_name>.*)\]\s*$',line):
print("find section")
#print("find section")
self.addSection(result.group('section_name').strip())
elif result := re.search(r'^\s*(?P<key_name>[^=]*)=(?P<key_value>.*)',line):
print("find key")
#print("find key {0}, with value: {1}".format(result.group('key_name').strip(),result.group('key_value').strip()))
self.ar_sections[-1].appendKey(result.group('key_name').strip(),result.group('key_value').strip())
else:
print("find unknown, ignoring this line")
#print("find unknown, ignoring this line")
pass
def sefFilePath(self,filepath:str):
self.filepath = filepath
def formatAll(self,bBeautify:bool = False) -> str:
result = ""
for section in self.ar_sections:
@@ -154,36 +169,61 @@ class ChaChaSimpleINI:
result = []
for section in sections:
try:
result.extend(section.getKey(keyName))
except KeyNotFoundException:
pass
if not result:
raise ChaChaINI_KeyNotFoundException()
return result
def getKeyValue(self,sectionName:str,keyName:str)->Union[str,list[str]]:
sections = self.getSection(sectionName)
if isinstance(sections,ChaChaSimpleINI_section):
return sections.getKeyValue(keyName)
else:
result = []
for section in sections:
try:
result.extend(section.getKeyValue(keyName))
keys = section.getKey(keyName)
if isinstance(keys,ChaChaSimpleINI_key):
#print("d {0}".format(keys))
result.append(keys)
else: #array
#print("e {0}".format(keys))
result.extend(keys)
except ChaChaINI_KeyNotFoundException:
pass
if not result:
raise ChaChaINI_KeyNotFoundException()
return result
if len(result) == 1:
return result[0]
else:
return result
def setKeyValue(self,sectionName:str,keyName:str,keyValue:str)->Union[str,list[str]]:
try:
sections = self.getSection(sectionName)
except ChaChaINI_SectionNotFoundException:
def getKeyValue(self,sectionName:str,keyName:str)->Union[str,list[str]]:
sections = self.getSection(sectionName)
if isinstance(sections,ChaChaSimpleINI_section):
#print("c")
return sections.getKeyValue(keyName)
else: # array
#print("d")
result = []
for section in sections:
try:
keys = section.getKeyValue(keyName)
if isinstance(keys,str):
#print("d {0}".format(keys))
result.append(keys)
else: #array
#print("e {0}".format(keys))
result.extend(keys)
except ChaChaINI_KeyNotFoundException:
pass
if not result:
raise ChaChaINI_KeyNotFoundException()
if len(result) == 1:
return result[0]
else:
return result
def setAddKeyValue(self,sectionName:str,keyName:str,keyValue:str,bForceAddKey:bool = False,bForceAddSection:bool = False)->Union[str,list[str]]:
if bForceAddSection:
sections = self.addSection(sectionName)
else:
try:
sections = self.getSection(sectionName)
except ChaChaINI_SectionNotFoundException:
sections = self.addSection(sectionName)
if isinstance(sections,ChaChaSimpleINI_section):
sections.setKeyValue(keyName,keyValue)
sections.setAddKeyValue(keyName,keyValue,bForceAddKey)
else: #array: in this case we set value of the last section
sections[-1].setKeyValue(keyName,keyValue)
sections[-1].setAddKeyValue(keyName,keyValue,bForceAddKey)

View File

@@ -0,0 +1,54 @@
from pprint import pprint
from pathlib import Path
import unittest
from ChaChaSimpleINI import *
class Test_ChaChaSimpleINI_base(unittest.TestCase):
def setUp(self):
[f.unlink() for f in Path("tmp").glob("*")]
print("======================")
def test_simpleread_value(self):
testini = ChaChaSimpleINI("testfiles/test_simpleread.ini")
self.assertEqual(testini.getKeyValue("testsection", "key1"),"test")
self.assertEqual(testini.getKeyValue("testsection", "key2"), "2")
self.assertEqual(testini.getKeyValue("testsection", "key3"), "43")
self.assertEqual(testini.getKeyValue("testsection", "key4"), "0.54")
def test_complexread1_value(self):
testini = ChaChaSimpleINI("testfiles/test_complexread1.ini")
self.assertEqual(testini.getKeyValue("testsection1", "key1"),"test1")
self.assertEqual(testini.getKeyValue("testsection1", "key2"),"test2")
self.assertEqual(testini.getKeyValue("testsection1", "key3"),"test3")
self.assertEqual(testini.getKeyValue("testsection1", "key4"),"test4")
self.assertEqual(testini.getKeyValue("testsection2", "key1"),"test1")
self.assertEqual(testini.getKeyValue("testsection2", "key2"),["test2","test3"])
self.assertEqual(testini.getKeyValue("testsection2", "keya"),"0")
self.assertEqual(testini.getKeyValue("test section 4", "test key two"),"test value two")
def test_complexreadwrite_value(self):
testini = ChaChaSimpleINI("testfiles/test_complexreadwrite.ini")
self.assertEqual(testini.getKeyValue("testsection", "key"), ["test1","test2","test3"])
testini.sefFilePath("tmp/out.ini")
testini.writeFile(False)
testinitmp = ChaChaSimpleINI("tmp/out.ini")
self.assertEqual(testinitmp.getKeyValue("testsection", "key"), ["test1","test2","test3"])
testini.setAddKeyValue("testsection", "key", "test4", True)
testini.sefFilePath("tmp/out2.ini")
testini.writeFile(False)
testinitmp = ChaChaSimpleINI("tmp/out2.ini")
self.assertEqual(testinitmp.getKeyValue("testsection", "key"), ["test1","test2","test3","test4"])
testini.setAddKeyValue("testsection", "key", "test4", True,True)
testini.sefFilePath("tmp/out3.ini")
testini.writeFile(True)
testinitmp = ChaChaSimpleINI("tmp/out3.ini")
self.assertEqual(testinitmp.getKeyValue("testsection", "key"), ["test1","test2","test3","test4","test4"])

View File

@@ -0,0 +1,25 @@
[testsection2]
key1=test1
key2=test2
key2=test3
[testsection1]
key1=test1
key2=test2
[testsection1]
key3=test3
key4=test4
[testsection2]
keya=0
[test section 3]
test key=test value one
[ test section 4 ]
test key two = test value two

View File

@@ -0,0 +1,4 @@
[testsection]
key=test1
key=test2
key=test3

View File

@@ -0,0 +1,5 @@
[testsection]
key1=test
key2=2
key3 = 43
key4= 0.54