Cameron Simpson avatar Cameron Simpson committed 86e2383

cs.configutils: ConfigWatcher and ConfigSectionWatcher: implement __getitem__

Comments (0)

Files changed (2)


   import configparser
 from threading import Lock
 from cs.fileutils import file_property
+from cs.threads import locked_property
 from cs.logutils import Pfx, info, D
 def load_config(config_path, parser=None):
   def __init__(self, config_path):
     self._config_path = config_path
     self._config_lock = Lock()
+    self._mapping = None
   def config(self, path):
     return self._config_path
   def as_dict(self):
+    ''' Construct and return a dictionary containing an entry for each section
+        whose value is a dictionary of section items and values.
+    '''
     d = {}
     config = self.config
     for section in config.sections():
       d[section] = dict(config.items(section))
     return d
+  @locked_property
+  def mapping(self):
+    ''' The current config as a mapping as returned by as_dict().
+    '''
+    return self.as_dict()
+  def __getitem__(self, section):
+    return self.mapping[section]
 class ConfigSectionWatcher(object):
   ''' A class for monitoring a particular clause in a config file.
     return K
   def __getitem__(self, item):
-    config = self.config
-    section = self.section
-    if config.has_section(section) and config.has_option(section, item):
-      return config.get(section, item)
-    if self.defaults is None:
-      raise KeyError("%s: no defaults" % (item,))
-    return self.defaults[item]
+    return self.configwatcher.mapping[item]
   def get(self, item, default):
     with Pfx("get(%s)", item):


         if exc_info:
           exception("exception: %s", exc_info[1])
         elif result:
-            results.append(result)
+          results.append(result)
           debug("empty result, discarding")
       if not results:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.