import code

This commit is contained in:
cclecle
2022-05-24 22:29:23 +02:00
parent 783fffd5f1
commit 3be2854a40
5 changed files with 220 additions and 0 deletions

17
.project Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ChaChaSimpleINI</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>

8
.pydevproject Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/${PROJECT_DIR_NAME}/ChaChaSimpleINI</path>
</pydev_pathproperty>
</pydev_project>

View File

@@ -0,0 +1,6 @@
from .core import ChaChaINI_KeyNotFoundException, \
ChaChaINI_SectionNotFoundException
from .core import ChaChaSimpleINI_key, \
ChaChaSimpleINI_section, \
ChaChaSimpleINI

View File

@@ -0,0 +1,189 @@
import re
from typing import Union
class ChaChaINI_KeyNotFoundException(RuntimeError):
pass
class ChaChaINI_SectionNotFoundException(RuntimeError):
pass
class ChaChaSimpleINI_key:
def __init__(self,name:str,value:str):
self.name = name
self.value = value
def format(self,bBeautify:bool = False) -> str:
if bBeautify:
return "{0} = {1}".format(self.name,self.value)
else:
return "{0}={1}".format(self.name,self.value)
class ChaChaSimpleINI_section:
def __init__(self,name:str):
self.name = name
self.ar_keys=[]
def format(self) -> str:
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"
return result
def appendKey(self,name,value) -> None:
key = ChaChaSimpleINI_key(name,value)
self.ar_keys.append(key)
#print("add key {0} in {1}".format(name,self.name))
return key
def getAllKeyNames(self)->list[str]:
result=[]
for key in self.ar_keys:
result.append(key.name)
return result
def getKey(self,name:str) -> Union[ChaChaSimpleINI_key,list[ChaChaSimpleINI_key]]:
result = None
for key in self.ar_keys:
if name == key.name:
if result is None:
result = key
elif isinstance(result,ChaChaSimpleINI_key):
result = [result,key]
else: #array
result.append(key)
if result is None:
raise ChaChaINI_KeyNotFoundException()
return result
def getKeyValue(self,name:str) -> Union[str,list[str]]:
keys = self.getKey(name)
if isinstance(keys,ChaChaSimpleINI_key):
return keys.value
else: #array
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:
keys = self.appendKey(name,'')
if isinstance(keys,ChaChaSimpleINI_key):
keys.value = value
else: #array: in this case we set value of the last key
keys[-1].value = value
class ChaChaSimpleINI:
def __init__(self,filepath:str):
self.filepath = filepath
self.ar_sections=[]
with open(filepath) as file:
for _line in file:
line = _line.rstrip()
if result := re.search(r'^\s*$',line):
print("find empty line, ignoring this line")
elif result := re.search(r'^\s*;',line):
print("find comment, ignoring this line")
elif result := re.search(r'^\s*\[(?P<section_name>.*)\]\s*$',line):
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")
self.ar_sections[-1].appendKey(result.group('key_name').strip(),result.group('key_value').strip())
else:
print("find unknown, ignoring this line")
def formatAll(self,bBeautify:bool = False) -> str:
result = ""
for section in self.ar_sections:
result = result + section.formatAll(bBeautify)
if bBeautify:
result = result + "\n"
return result
def addSection(self,name:str) -> None:
section = ChaChaSimpleINI_section(name)
self.ar_sections.append(section)
return section
def writeFile(self,bBeautify:bool = False) -> None:
with open(self.filepath,'w') as file:
file.write(self.formatAll(bBeautify))
def getAllSectionNames(self) -> list[str]:
result=[]
for section in self.ar_sections:
result.append(section.name)
return result
def getSection(self,name:str) -> Union[ChaChaSimpleINI_section,list[ChaChaSimpleINI_section]]:
result = None
for section in self.ar_sections:
if name == section.name:
if result is None:
result = section
elif isinstance(result,ChaChaSimpleINI_section):
result = [result,section]
else:
result.append(section)
if result is None:
raise ChaChaINI_SectionNotFoundException()
return result
def getAllKeyNames(self,sectionName:str)->list[str]:
sections = self.getSection(sectionName)
if isinstance(sections,ChaChaSimpleINI_section):
return sections.getAllKeyNames()
else:
result = []
for section in sections:
result.extend(section.getAllKeyNames())
return result
def getKey(self,sectionName:str,keyName:str)->Union[str,list[str]]:
sections = self.getSection(sectionName)
if isinstance(sections,ChaChaSimpleINI_section):
return sections.getKey(keyName)
else:
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))
except ChaChaINI_KeyNotFoundException:
pass
if not result:
raise ChaChaINI_KeyNotFoundException()
return result
def setKeyValue(self,sectionName:str,keyName:str,keyValue:str)->Union[str,list[str]]:
try:
sections = self.getSection(sectionName)
except ChaChaINI_SectionNotFoundException:
sections = self.addSection(sectionName)
if isinstance(sections,ChaChaSimpleINI_section):
sections.setKeyValue(keyName,keyValue)
else: #array: in this case we set value of the last section
sections[-1].setKeyValue(keyName,keyValue)

0
ChaChaSimpleINI/setup.py Normal file
View File