From 3be2854a40ca8dee8d5d26a86d34341235dacbb3 Mon Sep 17 00:00:00 2001 From: cclecle Date: Tue, 24 May 2022 22:29:23 +0200 Subject: [PATCH] import code --- .project | 17 ++ .pydevproject | 8 + ChaChaSimpleINI/ChaChaSimpleINI/__init__.py | 6 + ChaChaSimpleINI/ChaChaSimpleINI/core.py | 189 ++++++++++++++++++++ ChaChaSimpleINI/setup.py | 0 5 files changed, 220 insertions(+) create mode 100644 .project create mode 100644 .pydevproject create mode 100644 ChaChaSimpleINI/ChaChaSimpleINI/__init__.py create mode 100644 ChaChaSimpleINI/ChaChaSimpleINI/core.py create mode 100644 ChaChaSimpleINI/setup.py diff --git a/.project b/.project new file mode 100644 index 0000000..2c3e8ad --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + ChaChaSimpleINI + + + + + + org.python.pydev.PyDevBuilder + + + + + + org.python.pydev.pythonNature + + diff --git a/.pydevproject b/.pydevproject new file mode 100644 index 0000000..86dde05 --- /dev/null +++ b/.pydevproject @@ -0,0 +1,8 @@ + + + Default + python interpreter + + /${PROJECT_DIR_NAME}/ChaChaSimpleINI + + diff --git a/ChaChaSimpleINI/ChaChaSimpleINI/__init__.py b/ChaChaSimpleINI/ChaChaSimpleINI/__init__.py new file mode 100644 index 0000000..8e50190 --- /dev/null +++ b/ChaChaSimpleINI/ChaChaSimpleINI/__init__.py @@ -0,0 +1,6 @@ +from .core import ChaChaINI_KeyNotFoundException, \ + ChaChaINI_SectionNotFoundException + +from .core import ChaChaSimpleINI_key, \ + ChaChaSimpleINI_section, \ + ChaChaSimpleINI \ No newline at end of file diff --git a/ChaChaSimpleINI/ChaChaSimpleINI/core.py b/ChaChaSimpleINI/ChaChaSimpleINI/core.py new file mode 100644 index 0000000..d65fbf2 --- /dev/null +++ b/ChaChaSimpleINI/ChaChaSimpleINI/core.py @@ -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.*)\]\s*$',line): + print("find section") + self.addSection(result.group('section_name').strip()) + elif result := re.search(r'^\s*(?P[^=]*)=(?P.*)',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) + \ No newline at end of file diff --git a/ChaChaSimpleINI/setup.py b/ChaChaSimpleINI/setup.py new file mode 100644 index 0000000..e69de29