comparison hglib/client.py @ 21:ffef7df076e8

client: rewrite config()
author Idan Kamara <idankk86@gmail.com>
date Thu, 11 Aug 2011 16:02:01 +0300
parents 6a9d16ddae31
children 297df22d6091
comparison
equal deleted inserted replaced
20:6a9d16ddae31 21:ffef7df076e8
1 import subprocess, os, struct, cStringIO, collections 1 import subprocess, os, struct, cStringIO, collections, re
2 import hglib, error, util, templates 2 import hglib, error, util, templates
3 3
4 from util import cmdbuilder 4 from util import cmdbuilder
5 5
6 class hgclient(object): 6 class hgclient(object):
24 24
25 self.server = subprocess.Popen(args, stdin=subprocess.PIPE, 25 self.server = subprocess.Popen(args, stdin=subprocess.PIPE,
26 stdout=subprocess.PIPE, env=env) 26 stdout=subprocess.PIPE, env=env)
27 27
28 self._readhello() 28 self._readhello()
29 self._config = {}
30 29
31 def _readhello(self): 30 def _readhello(self):
32 """ read the hello message the server sends when started """ 31 """ read the hello message the server sends when started """
33 ch, msg = self._readchannel() 32 ch, msg = self._readchannel()
34 assert ch == 'o' 33 assert ch == 'o'
194 193
195 out = self.rawcommand(args) 194 out = self.rawcommand(args)
196 rev, node = out.splitlines()[-1].rsplit(':') 195 rev, node = out.splitlines()[-1].rsplit(':')
197 return int(rev.split()[-1]), node 196 return int(rev.split()[-1]), node
198 197
199 def config(self, refresh=False): 198 def config(self, names=[], untrusted=False, showsource=False):
200 if not self._config or refresh: 199 """
201 self._config.clear() 200 Return a list of (section, key, value) config settings from all hgrc files
202 201
203 out = self.rawcommand(['showconfig']) 202 When showsource is specified, return (source, section, key, value) where
204 203 source is of the form filename:[line]
205 for entry in cStringIO.StringIO(out): 204 """
206 k, v = entry.rstrip().split('=', 1) 205 def splitline(s):
207 section, name = k.split('.', 1) 206 k, value = s.rstrip().split('=', 1)
208 self._config.setdefault(section, {})[name] = v 207 section, key = k.split('.', 1)
209 208 return (section, key, value)
210 return self._config 209
210 if not isinstance(names, list):
211 names = [names]
212
213 args = cmdbuilder('showconfig', *names, u=untrusted, debug=showsource)
214 out = self.rawcommand(args)
215
216 conf = []
217 if showsource:
218 out = util.skiplines(out, 'read config from: ')
219 for line in out.splitlines():
220 m = re.match(r"(.+?:(?:\d+:)?) (.*)", line)
221 t = splitline(m.group(2))
222 conf.append((m.group(1)[:-1], t[0], t[1], t[2]))
223 else:
224 for line in out.splitlines():
225 conf.append(splitline(line))
226
227 return conf
211 228
212 @property 229 @property
213 def encoding(self): 230 def encoding(self):
214 """ get the servers encoding """ 231 """ get the servers encoding """
215 if not 'getencoding' in self.capabilities: 232 if not 'getencoding' in self.capabilities: