diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-09-23 16:52:37 +0100 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-09-23 16:52:37 +0100 |
commit | 944b42381f0e1f94c8c9d43385eaa42a9eb74121 (patch) | |
tree | 4127bd073e3e4abfbdd394cc4b401b053dc305a5 | |
parent | 17ce819a6f051bf2d59a8307668f6d955f993aa2 (diff) | |
download | cliapp-baserock/sam/python3.tar.gz |
Continue to allow %s in settings valuesbaserock/sam/python3
We need to use the special 'LegacyInterpolation' class with Python 3's
'configparser' module, otherwise %-formats in config values get parsed
and can trigger a ValueError, which doesn't happen with Python 2.
-rw-r--r-- | cliapp/settings.py | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/cliapp/settings.py b/cliapp/settings.py index de058c9..298cd6a 100644 --- a/cliapp/settings.py +++ b/cliapp/settings.py @@ -301,7 +301,20 @@ class Settings(object): self._add_default_settings() self._config_files = None - self._cp = configparser.ConfigParser() + + self._cp = self._create_configparser() + + def _create_configparser(self): + if sys.version_info[0] == 2: + cp = configparser.ConfigParser() + else: + # The default interpolation in Python 3 is + # configparser.BasicInterpolation. This class parses % symbols in + # the strings, which can break existing cliapp.settings users. The + # LegacyInterpolation class preserves the Python 2 behaviour. + cp = configparser.RawConfigParser( + interpolation=configparser.LegacyInterpolation()) + return cp def _add_default_settings(self): self.string(['output'], @@ -751,7 +764,7 @@ class Settings(object): ''' - cp = configparser.ConfigParser() + cp = self._create_configparser() cp.add_section('config') for pathname in self.config_files: @@ -787,7 +800,7 @@ class Settings(object): meanings it desires to the section names. ''' - cp = configparser.ConfigParser() + cp = self._create_configparser() cp.add_section('config') for name in self._canonical_names: cp.set('config', name, self._settingses[name].format()) |