From 1f2edeea5a14726f880c3dabd26c8433f71c2af4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Ricart?= Date: Fri, 23 Feb 2018 00:37:03 +0100 Subject: [PATCH] first release --- esp8266/uConfigParser.py | 97 +++++++++++++++++++++++++++++++++------- 1 file changed, 80 insertions(+), 17 deletions(-) diff --git a/esp8266/uConfigParser.py b/esp8266/uConfigParser.py index 1f8be91..73df532 100644 --- a/esp8266/uConfigParser.py +++ b/esp8266/uConfigParser.py @@ -8,41 +8,104 @@ class ConfigParser: return dict_to_return def add_section(self, section): - pass + self.config_dict[section] = {} def has_section(self, section): - pass + if section in self.config_dict.keys(): + return True + else: + return False def options(self, section): - pass + if not section in self.config_dict: + raise + return self.config_dict[section].keys() - def read(self, filename = None, fp = None): - if not fp: + def read(self, filename=None, fp=None): + if not fp and not filename: + print("ERROR : no filename and no fp") + raise + elif not fp and filename: fp = open(filename) - self.config_dict = {line.replace('[','').replace(']',''):{} for line in fp.read()\ - if line.startswith('[') and line.endwith(']') - } - + content = fp.read() + fp.close() + self.config_dict = {line.replace('[','').replace(']',''):{} for line in content.split('\n')\ + if line.startswith('[') and line.endswith(']') + } + striped_content = [line.strip() for line in content.split('\n')] + for section in self.config_dict.keys(): + start_index = striped_content.index('[%s]' % section) + end_flag = [line for line in striped_content[start_index + 1:] if line.startswith('[')] + if not end_flag: + end_index = None + else: + end_index = striped_content.index(end_flag[0]) + block = striped_content[start_index + 1 : end_index] + options = [line.split('=')[0].strip() for line in block if '=' in line] + for option in options: + start_flag = [line for line in block if line.startswith(option) and '=' in line] + start_index = block.index(start_flag[0]) + end_flag = [line for line in block[start_index + 1:] if '=' in line] + if not end_flag: + end_index = None + else: + end_index = block.index(end_flag[0]) + values = [value.split('=')[-1].strip() for value in block[start_index:end_index] if value] + if not values: + values = None + elif len(values) == 1: + values = values[0] + self.config_dict[section][option] = values def get(self, section, option): - - def items(self, section): - - def getboolean(self, section, option): + if not self.has_section(section) \ + or not self.has_option(section,option): + raise + return self.config_dict[section][option] def has_option(self, section, option): """Check for the existence of a given option in a given section.""" + if not section in self.config_dict: + raise + if option in self.config_dict[section].keys(): + return True + else: + return False - def set(self, section, option, value=None): - """Set an option.""" - - def write(self, fp): + def write(self, filename = None, fp = None): """Write an .ini-format representation of the configuration state.""" + if not fp and not filename: + print("ERROR : no filename and no fp") + raise + elif not fp and filename: + fp = open(filename,'w') + + for section in self.config_dict.keys(): + fp.write('[%s]\n' % section) + for option in self.config_dict[section].keys(): + fp.write('\n%s =' % option) + values = self.config_dict[section][option] + if type(values) == type([]): + fp.write('\n ') + values = '\n '.join(values) + else: + fp.write(' ') + fp.write(values) + fp.write('\n') + fp.write('\n') + def remove_option(self, section, option): """Remove an option.""" + if not self.has_section(section) \ + or not self.has_option(section,option): + raise + del self.config_dict[section][option] def remove_section(self, section): """Remove a file section.""" + if not self.has_section(section): + raise + del self.config_dict[section]