[sources] method to ease modification of a source config through c-c shell
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 25 Oct 2010 15:44:37 +0200
changeset 6627 430b7703b3fd
parent 6626 5c20a005bddc
child 6628 7cf5d55cbb8a
[sources] method to ease modification of a source config through c-c shell
entities/schemaobjs.py
server/serverconfig.py
--- a/entities/schemaobjs.py	Mon Oct 25 15:42:42 2010 +0200
+++ b/entities/schemaobjs.py	Mon Oct 25 15:44:37 2010 +0200
@@ -24,6 +24,7 @@
 
 from logilab.common.decorators import cached
 from logilab.common.textutils import text_to_dict
+from logilab.common.configuration import OptionError
 
 from yams.schema import role_name
 
@@ -33,14 +34,34 @@
 from cubicweb.entities import AnyEntity, fetch_config
 
 
-class CWSource(AnyEntity):
-    __regid__ = 'CWSource'
-    fetch_attrs, fetch_order = fetch_config(['name', 'type'])
-
+class _CWSourceCfgMixIn(object):
     @property
     def dictconfig(self):
         return self.config and text_to_dict(self.config) or {}
 
+    def update_config(self, skip_unknown=False, **config):
+        from cubicweb.server import SOURCE_TYPES
+        from cubicweb.server.serverconfig import (SourceConfiguration,
+                                                  generate_source_config)
+        cfg = self.dictconfig
+        cfg.update(config)
+        options = SOURCE_TYPES[self.type].options
+        sconfig = SourceConfiguration(self._cw.vreg.config, options=options)
+        for opt, val in cfg.iteritems():
+            try:
+                sconfig.set_option(opt, val)
+            except OptionError:
+                if skip_unknown:
+                    continue
+                raise
+        cfgstr = unicode(generate_source_config(sconfig), self._cw.encoding)
+        self.set_attributes(config=cfgstr)
+
+
+class CWSource(_CWSourceCfgMixIn, AnyEntity):
+    __regid__ = 'CWSource'
+    fetch_attrs, fetch_order = fetch_config(['name', 'type'])
+
     @property
     def host_config(self):
         dictconfig = self.dictconfig
@@ -56,14 +77,10 @@
         return self.reverse_cw_host_config_of
 
 
-class CWSourceHostConfig(AnyEntity):
+class CWSourceHostConfig(_CWSourceCfgMixIn, AnyEntity):
     __regid__ = 'CWSourceHostConfig'
     fetch_attrs, fetch_order = fetch_config(['match_host', 'config'])
 
-    @property
-    def dictconfig(self):
-        return self.config and text_to_dict(self.config) or {}
-
     def match(self, hostname):
         return re.match(self.match_host, hostname)
 
--- a/server/serverconfig.py	Mon Oct 25 15:42:42 2010 +0200
+++ b/server/serverconfig.py	Mon Oct 25 15:44:37 2010 +0200
@@ -78,12 +78,12 @@
     sconfig.input_config(inputlevel=inputlevel)
     return sconfig
 
-def generate_source_config(sconfig):
+def generate_source_config(sconfig, encoding=sys.stdin.encoding):
     """serialize a repository source configuration as text"""
     stream = StringIO()
     optsbysect = list(sconfig.options_by_section())
     assert len(optsbysect) == 1, 'all options for a source should be in the same group'
-    lgconfig.ini_format(stream, optsbysect[0][1], sys.stdin.encoding)
+    lgconfig.ini_format(stream, optsbysect[0][1], encoding)
     return stream.getvalue()