changeset 21:ffef7df076e8

client: rewrite config()
author Idan Kamara <idankk86@gmail.com>
date Thu, 11 Aug 2011 16:02:01 +0300
parents 6a9d16ddae31
children 297df22d6091
files hglib/client.py tests/test-config.py
diffstat 2 files changed, 53 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/hglib/client.py	Thu Aug 11 15:42:59 2011 +0300
+++ b/hglib/client.py	Thu Aug 11 16:02:01 2011 +0300
@@ -1,4 +1,4 @@
-import subprocess, os, struct, cStringIO, collections
+import subprocess, os, struct, cStringIO, collections, re
 import hglib, error, util, templates
 
 from util import cmdbuilder
@@ -26,7 +26,6 @@
                                        stdout=subprocess.PIPE, env=env)
 
         self._readhello()
-        self._config = {}
 
     def _readhello(self):
         """ read the hello message the server sends when started """
@@ -196,18 +195,36 @@
         rev, node = out.splitlines()[-1].rsplit(':')
         return int(rev.split()[-1]), node
 
-    def config(self, refresh=False):
-        if not self._config or refresh:
-            self._config.clear()
+    def config(self, names=[], untrusted=False, showsource=False):
+        """
+        Return a list of (section, key, value) config settings from all hgrc files
 
-            out = self.rawcommand(['showconfig'])
+        When showsource is specified, return (source, section, key, value) where
+        source is of the form filename:[line]
+        """
+        def splitline(s):
+            k, value = s.rstrip().split('=', 1)
+            section, key = k.split('.', 1)
+            return (section, key, value)
+
+        if not isinstance(names, list):
+            names = [names]
 
-            for entry in cStringIO.StringIO(out):
-                k, v = entry.rstrip().split('=', 1)
-                section, name = k.split('.', 1)
-                self._config.setdefault(section, {})[name] = v
+        args = cmdbuilder('showconfig', *names, u=untrusted, debug=showsource)
+        out = self.rawcommand(args)
 
-        return self._config
+        conf = []
+        if showsource:
+            out = util.skiplines(out, 'read config from: ')
+            for line in out.splitlines():
+                m = re.match(r"(.+?:(?:\d+:)?) (.*)", line)
+                t = splitline(m.group(2))
+                conf.append((m.group(1)[:-1], t[0], t[1], t[2]))
+        else:
+            for line in out.splitlines():
+                conf.append(splitline(line))
+
+        return conf
 
     @property
     def encoding(self):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-config.py	Thu Aug 11 16:02:01 2011 +0300
@@ -0,0 +1,25 @@
+import os, common, hglib
+
+class test_config(common.basetest):
+    def setUp(self):
+        common.basetest.setUp(self)
+        open('.hg/hgrc', 'a').write('[section]\nkey=value\n')
+        self.client = hglib.open()
+
+    def test_basic(self):
+        config = self.client.config()
+
+        self.assertTrue(('section', 'key', 'value') in self.client.config())
+
+        self.assertTrue([('section', 'key', 'value')],
+                        self.client.config('section'))
+        self.assertTrue([('section', 'key', 'value')],
+                        self.client.config(['section', 'foo']))
+        self.assertRaises(hglib.error.CommandError,
+                          self.client.config, ['a.b', 'foo'])
+
+    def test_show_source(self):
+        config = self.client.config(showsource=True)
+
+        self.assertTrue((os.path.abspath('.hg/hgrc') + ':2',
+                         'section', 'key', 'value') in config)