[PATCH logilab-common] Make "configuration" classes get call __getitem__

Philippe Pepiot philippe.pepiot at logilab.fr
Thu Sep 12 16:44:45 CEST 2019


# HG changeset patch
# User Philippe Pepiot <philippe.pepiot at logilab.fr>
# Date 1568298316 -7200
#      Thu Sep 12 16:25:16 2019 +0200
# Node ID 5bfba850fddf7f63508c74e40afa8ec9d0ba468b
# Parent  6f6a25d6893611505a6ae98a456723feaca8e6a5
Make "configuration" classes get call __getitem__

In both ConfigurationMixIn and OptionsManager2ConfigurationAdapter.
Otherwise a child-class defining it's own __getitem__ wouldn't be called.

In ConfigurationMixIn.get, also catch for OptionError like before.
We could think about caching OptionError in __getitem__ but there's test
asserting this method can raise OptionError, so...

diff --git a/logilab/common/configuration.py b/logilab/common/configuration.py
--- a/logilab/common/configuration.py
+++ b/logilab/common/configuration.py
@@ -970,8 +970,8 @@ class ConfigurationMixIn(OptionsManagerM
 
     def get(self, key, default=None):
         try:
-            return getattr(self.config, self.option_attrname(key))
-        except (OptionError, AttributeError):
+            return self[key]
+        except (OptionError, KeyError):
             return default
 
 
@@ -1014,10 +1014,9 @@ class OptionsManager2ConfigurationAdapte
         self.config.global_set_option(self.config.option_attrname(key), value)
 
     def get(self, key, default=None):
-        provider = self.config._all_options[key]
         try:
-            return getattr(provider.config, provider.option_attrname(key))
-        except AttributeError:
+            return self[key]
+        except KeyError:
             return default
 
 # other functions ##############################################################



More information about the cubicweb-devel mailing list