Created
December 16, 2023 22:23
-
-
Save jimkring/d3ad8fc156cdcfe744f7ac28698cc796 to your computer and use it in GitHub Desktop.
A python ConfigParser that works better when reading INI files created with LabVIEW.
This file contains 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
import configparser | |
class ConfigParserLabVIEW(configparser.ConfigParser): | |
""" | |
A ConfigParser that handles LabVIEW-generated config files with fewer errors than the default configparser: | |
- disables the interpolation feature | |
- disables the strict validation feature | |
- removes double-quotes from around values | |
""" | |
def __init__(self, *args, **kwargs): | |
# Initialize the ConfigParserLabVIEW object, overriding defaults of ConfigParser. | |
# disable interpolation - it's a very non-standard and pythonic feature | |
kwargs['interpolation'] = None | |
# disable since we need flexibility instead of strict validation | |
kwargs['strict'] = False | |
# call parent (ConfigParser) constructor | |
super().__init__(*args, **kwargs) | |
def get(self, section, option, *, raw=False, vars=None, fallback=configparser._UNSET): | |
# Override the get() method to remove double-quotes from around values. | |
value = super().get(section, option, raw=raw, vars=vars, fallback=fallback) | |
if value.startswith('"') and value.endswith('"'): | |
value = value[1:-1] | |
return value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment