changeset 3763:0284379adbc8

some config refactorings, add WikiConfig macro The WikiConfig macro shows all wiki settings that have non-default values; it is restricted to superusers.
author Johannes Berg <johannes AT sipsolutions DOT net>
date Sun, 22 Jun 2008 10:46:18 +0200
parents 95266d0c8a6f
children 0747f38efe23
files MoinMoin/config/multiconfig.py MoinMoin/macro/WikiConfig.py
diffstat 2 files changed, 113 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/config/multiconfig.py	Sun Jun 22 02:51:15 2008 +0200
+++ b/MoinMoin/config/multiconfig.py	Sun Jun 22 10:46:18 2008 +0200
@@ -202,14 +202,25 @@
     """ just a container for stuff we cache """
     pass
 
+class ConfigFunctionality(object):
+    """ Configuration base class with config class behaviour.
 
-class DefaultConfig(object):
-    """ Configuration base class with default config values
-        (added below)
+        This class contains the functionality for the DefaultConfig
+        class for the benefit of the WikiConfig macro.
     """
-
     def __init__(self, siteid):
         """ Init Config instance """
+        # keep first!
+        settings = {}
+        for k in self.__dict__:
+            settings[k] = True
+
+        # will be assigned in by loader code (see above, _makeConfig)
+        self.cfg_mtime = None
+
+        # will be lazily loaded by interwiki code when needed (?)
+        self.shared_intermap_files = None
+
         self.siteid = siteid
         self.cache = CacheClass()
 
@@ -350,6 +361,13 @@
         if self.url_prefix_local is None:
             self.url_prefix_local = self.url_prefix_static
 
+        # keep last!!
+        self.computed_settings = {}
+        for k in self.__dict__:
+            if k in settings:
+                continue
+            self.computed_settings[k] = True
+
 
     def load_meta_dict(self):
         """ The meta_dict contains meta data about the wiki instance. """
@@ -564,6 +582,14 @@
         """ Make it possible to access a config object like a dict """
         return getattr(self, item)
 
+class DefaultConfig(ConfigFunctionality):
+    """ Configuration base class with default config values
+        (added below)
+    """
+    # Do not add anything into this class. Functionality must
+    # be added above to avoid having the methods show up in
+    # the WikiConfig macro. Settings must be added below to
+    # the options dictionary.
 
 def _default_password_checker(request, username, password):
     """ Check if a password is secure enough.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/macro/WikiConfig.py	Sun Jun 22 10:46:18 2008 +0200
@@ -0,0 +1,83 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - Wiki Configuration
+"""
+from MoinMoin.config import multiconfig
+
+Dependencies = ['']
+generates_headings = True
+
+def macro_WikiConfig(macro):
+    request = macro.request
+    _ = request.getText
+    f = macro.request.formatter
+    ret = []
+
+    if not request.user or not request.user.isSuperUser():
+        return ''
+
+    settings = {}
+    for groupname in multiconfig.options:
+        heading, desc, opts = multiconfig.options[groupname]
+        for name, default, description in opts:
+            name = groupname + '_' + name
+            if isinstance(default, multiconfig.DefaultExpression):
+                default = default.value
+            settings[name] = default
+    for groupname in multiconfig.options_no_group_name:
+        heading, desc, opts = multiconfig.options_no_group_name[groupname]
+        for name, default, description in opts:
+            if isinstance(default, multiconfig.DefaultExpression):
+                default = default.value
+            settings[name] = default
+
+    ret.extend([
+        f.heading(1, 1, id='current_config'),
+        f.text(_("Wiki configuration")),
+        f.heading(0, 1),
+    ])
+    ret.extend([
+        f.table(1),
+        f.table_row(1),
+        f.table_cell(1), f.strong(1), f.text(_('Variable name')), f.strong(0), f.table_cell(0),
+        f.table_cell(1), f.strong(1), f.text(_('Setting')), f.strong(0), f.table_cell(0),
+        f.table_row(0),
+    ])
+
+    def iter_vnames(cfg):
+        dedup = {}
+        for name in cfg.__dict__:
+            dedup[name] = True
+            yield name, cfg.__dict__[name]
+        for cls in cfg.__class__.mro():
+            if cls == multiconfig.ConfigFunctionality:
+                break
+            for name in cls.__dict__:
+                if not name in dedup:
+                    dedup[name] = True
+                    yield name, cls.__dict__[name]
+
+    found = []
+    for vname, value in iter_vnames(request.cfg):
+        if hasattr(multiconfig.ConfigFunctionality, vname):
+            continue
+        if vname in request.cfg.computed_settings:
+            continue
+        if vname in settings and settings[vname] == value:
+            continue
+        found.append((vname, value))
+    found.sort()
+    for vname, value in found:
+        vname = f.text(vname)
+        if not vname in settings:
+            vname = f.emphasis(1) + vname + f.emphasis(0)
+        vtxt = '%r' % (value, )
+        ret.extend([
+            f.table_row(1),
+            f.table_cell(1), vname, f.table_cell(0),
+            f.table_cell(1), f.code(1, css="backtick"), f.text(vtxt), f.code(0), f.table_cell(0),
+            f.table_row(0),
+        ])
+    ret.append(f.table(0))
+
+    return ''.join(ret)