Last active
September 12, 2023 19:34
-
-
Save ievans3024/752bd86cfbfce39b5623ccd62dbc54e8 to your computer and use it in GitHub Desktop.
Extension of the configparser library for some much needed but ignored features
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from configparser import * | |
| # copypasta from configparser source | |
| # see https://github.com/python/cpython/blob/3.8/Lib/configparser.py#L357 | |
| _UNSET = object() | |
| class ConfigParser(ConfigParser): | |
| def __init__(self, *args, sectionless=False, strip_quotes=False, **kwargs): | |
| self.__sectionless = sectionless | |
| self.__strip_quotes = strip_quotes | |
| self.__optionxforms = [] | |
| super(ConfigParser, self).__init__(*args, **kwargs) | |
| def __getitem__(self, key): | |
| if self.__sectionless: | |
| v = super(ConfigParser, self).__getitem__('DEFAULT').__getitem__(key) | |
| else: | |
| v = super(ConfigParser, self).__getitem__(key) | |
| if self.__strip_quotes: | |
| v = v.strip('"').strip("'") | |
| else: | |
| return v | |
| def __setitem__(self, key, value): | |
| if self.__strip_quotes: | |
| value = value.strip('"').strip("'") | |
| if self.__sectionless: | |
| return super(ConfigParser, self).__getitem__('DEFAULT').__setitem__(key, value) | |
| else: | |
| return super(ConfigParser, self).__setitem__(key, value) | |
| def __optionxform(self, option): | |
| if self.__optionxforms: | |
| for f in self.__optionxforms: | |
| option = f(option) | |
| return option | |
| else: | |
| return optionxform(option) | |
| @property | |
| def optionxform(self): | |
| return self.__optionxform | |
| @optionxform.setter | |
| def optionxform(self, value): | |
| if self.__optionxforms: | |
| self.__optionxforms[0] = value | |
| else: | |
| self.__optionxforms.append(value) | |
| def add_optionxform(self, fn): | |
| self.__optionxforms.append(fn) | |
| def get(self, section, option=None, *, raw=False, vars=None, fallback=_UNSET): | |
| if self.__sectionless: | |
| option = section | |
| section = 'DEFAULT' | |
| return super(self.__class__, self).get(section, option, *, raw=raw, vars=vars, fallback=fallback) | |
| @property | |
| def SECTIONLESS(self): | |
| return self.__sectionless | |
| @SECTIONLESS.setter | |
| def SECTIONLESS(self, value): | |
| self.__sectionless = bool(value) | |
| @property | |
| def STRIP_QUOTES(self): | |
| return self.__strip_quotes | |
| @STRIP_QUOTES.setter | |
| def STRIP_QUOTES(self, value): | |
| self.__strip_quotes = bool(value) | |
| def read(self, filename): | |
| if self.__sectionless: | |
| with open(filename) as configfile: | |
| c = '[DEFAULT]\n' + configfile.read() | |
| return self.read_string(c) | |
| else: | |
| return super(ConfigParser, self).read(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment