Mercurial > hg > mercurial-crew-with-dirclash
comparison mercurial/ui.py @ 3044:fcadf7a32425
Merge with mpm
author | Josef "Jeff" Sipek <jeffpc@josefsipek.net> |
---|---|
date | Sun, 03 Sep 2006 06:06:02 -0400 |
parents | 731f6b3d27c2 |
children | 494521a3f142 |
comparison
equal
deleted
inserted
replaced
3043:2a4d4aecb2b4 | 3044:fcadf7a32425 |
---|---|
1 # ui.py - user interface bits for mercurial | 1 # ui.py - user interface bits for mercurial |
2 # | 2 # |
3 # Copyright 2005 Matt Mackall <mpm@selenic.com> | 3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
4 # | 4 # |
5 # This software may be used and distributed according to the terms | 5 # This software may be used and distributed according to the terms |
6 # of the GNU General Public License, incorporated herein by reference. | 6 # of the GNU General Public License, incorporated herein by reference. |
7 | 7 |
8 from i18n import gettext as _ | 8 from i18n import gettext as _ |
9 from demandload import * | 9 from demandload import * |
10 demandload(globals(), "errno getpass os re smtplib socket sys tempfile") | 10 demandload(globals(), "errno getpass os re socket sys tempfile") |
11 demandload(globals(), "ConfigParser templater traceback util") | 11 demandload(globals(), "ConfigParser mdiff templater traceback util") |
12 | 12 |
13 class ui(object): | 13 class ui(object): |
14 def __init__(self, verbose=False, debug=False, quiet=False, | 14 def __init__(self, verbose=False, debug=False, quiet=False, |
15 interactive=True, traceback=False, parentui=None): | 15 interactive=True, traceback=False, parentui=None, |
16 readhooks=[]): | |
16 self.overlay = {} | 17 self.overlay = {} |
17 if parentui is None: | 18 if parentui is None: |
18 # this is the parent of all ui children | 19 # this is the parent of all ui children |
19 self.parentui = None | 20 self.parentui = None |
21 self.readhooks = list(readhooks) | |
20 self.cdata = ConfigParser.SafeConfigParser() | 22 self.cdata = ConfigParser.SafeConfigParser() |
21 self.readconfig(util.rcpath()) | 23 self.readconfig(util.rcpath()) |
22 | 24 |
23 self.quiet = self.configbool("ui", "quiet") | 25 self.quiet = self.configbool("ui", "quiet") |
24 self.verbose = self.configbool("ui", "verbose") | 26 self.verbose = self.configbool("ui", "verbose") |
32 self.prev_header = [] | 34 self.prev_header = [] |
33 self.revlogopts = self.configrevlog() | 35 self.revlogopts = self.configrevlog() |
34 else: | 36 else: |
35 # parentui may point to an ui object which is already a child | 37 # parentui may point to an ui object which is already a child |
36 self.parentui = parentui.parentui or parentui | 38 self.parentui = parentui.parentui or parentui |
39 self.readhooks = list(parentui.readhooks or readhooks) | |
37 parent_cdata = self.parentui.cdata | 40 parent_cdata = self.parentui.cdata |
38 self.cdata = ConfigParser.SafeConfigParser(parent_cdata.defaults()) | 41 self.cdata = ConfigParser.SafeConfigParser(parent_cdata.defaults()) |
39 # make interpolation work | 42 # make interpolation work |
40 for section in parent_cdata.sections(): | 43 for section in parent_cdata.sections(): |
41 self.cdata.add_section(section) | 44 self.cdata.add_section(section) |
76 if root is None: | 79 if root is None: |
77 root = os.path.expanduser('~') | 80 root = os.path.expanduser('~') |
78 for name, path in self.configitems("paths"): | 81 for name, path in self.configitems("paths"): |
79 if path and "://" not in path and not os.path.isabs(path): | 82 if path and "://" not in path and not os.path.isabs(path): |
80 self.cdata.set("paths", name, os.path.join(root, path)) | 83 self.cdata.set("paths", name, os.path.join(root, path)) |
84 for hook in self.readhooks: | |
85 hook(self) | |
81 | 86 |
82 def setconfig(self, section, name, val): | 87 def setconfig(self, section, name, val): |
83 self.overlay[(section, name)] = val | 88 self.overlay[(section, name)] = val |
84 | 89 |
85 def config(self, section, name, default=None): | 90 def config(self, section, name, default=None): |
167 result = {} | 172 result = {} |
168 for key, value in self.configitems("revlog"): | 173 for key, value in self.configitems("revlog"): |
169 result[key.lower()] = value | 174 result[key.lower()] = value |
170 return result | 175 return result |
171 | 176 |
172 def diffopts(self): | |
173 if self.diffcache: | |
174 return self.diffcache | |
175 result = {'showfunc': True, 'ignorews': False, | |
176 'ignorewsamount': False, 'ignoreblanklines': False} | |
177 for key, value in self.configitems("diff"): | |
178 if value: | |
179 result[key.lower()] = (value.lower() == 'true') | |
180 self.diffcache = result | |
181 return result | |
182 | |
183 def username(self): | 177 def username(self): |
184 """Return default username to be used in commits. | 178 """Return default username to be used in commits. |
185 | 179 |
186 Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL | 180 Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL |
187 and stop searching if one of these is set. | 181 and stop searching if one of these is set. |
214 | 208 |
215 path = self.config("paths", loc) | 209 path = self.config("paths", loc) |
216 if not path and default is not None: | 210 if not path and default is not None: |
217 path = self.config("paths", default) | 211 path = self.config("paths", default) |
218 return path or loc | 212 return path or loc |
219 | |
220 def setconfig_remoteopts(self, **opts): | |
221 if opts.get('ssh'): | |
222 self.setconfig("ui", "ssh", opts['ssh']) | |
223 if opts.get('remotecmd'): | |
224 self.setconfig("ui", "remotecmd", opts['remotecmd']) | |
225 | 213 |
226 def write(self, *args): | 214 def write(self, *args): |
227 if self.header: | 215 if self.header: |
228 if self.header != self.prev_header: | 216 if self.header != self.prev_header: |
229 self.prev_header = self.header | 217 self.prev_header = self.header |
296 finally: | 284 finally: |
297 os.unlink(name) | 285 os.unlink(name) |
298 | 286 |
299 return t | 287 return t |
300 | 288 |
301 def sendmail(self): | |
302 '''send mail message. object returned has one method, sendmail. | |
303 call as sendmail(sender, list-of-recipients, msg).''' | |
304 | |
305 def smtp(): | |
306 '''send mail using smtp.''' | |
307 | |
308 local_hostname = self.config('smtp', 'local_hostname') | |
309 s = smtplib.SMTP(local_hostname=local_hostname) | |
310 mailhost = self.config('smtp', 'host') | |
311 if not mailhost: | |
312 raise util.Abort(_('no [smtp]host in hgrc - cannot send mail')) | |
313 mailport = int(self.config('smtp', 'port', 25)) | |
314 self.note(_('sending mail: smtp host %s, port %s\n') % | |
315 (mailhost, mailport)) | |
316 s.connect(host=mailhost, port=mailport) | |
317 if self.configbool('smtp', 'tls'): | |
318 self.note(_('(using tls)\n')) | |
319 s.ehlo() | |
320 s.starttls() | |
321 s.ehlo() | |
322 username = self.config('smtp', 'username') | |
323 password = self.config('smtp', 'password') | |
324 if username and password: | |
325 self.note(_('(authenticating to mail server as %s)\n') % | |
326 (username)) | |
327 s.login(username, password) | |
328 return s | |
329 | |
330 class sendmail(object): | |
331 '''send mail using sendmail.''' | |
332 | |
333 def __init__(self, ui, program): | |
334 self.ui = ui | |
335 self.program = program | |
336 | |
337 def sendmail(self, sender, recipients, msg): | |
338 cmdline = '%s -f %s %s' % ( | |
339 self.program, templater.email(sender), | |
340 ' '.join(map(templater.email, recipients))) | |
341 self.ui.note(_('sending mail: %s\n') % cmdline) | |
342 fp = os.popen(cmdline, 'w') | |
343 fp.write(msg) | |
344 ret = fp.close() | |
345 if ret: | |
346 raise util.Abort('%s %s' % ( | |
347 os.path.basename(self.program.split(None, 1)[0]), | |
348 util.explain_exit(ret)[0])) | |
349 | |
350 method = self.config('email', 'method', 'smtp') | |
351 if method == 'smtp': | |
352 mail = smtp() | |
353 else: | |
354 mail = sendmail(self, method) | |
355 return mail | |
356 | |
357 def print_exc(self): | 289 def print_exc(self): |
358 '''print exception traceback if traceback printing enabled. | 290 '''print exception traceback if traceback printing enabled. |
359 only to call in exception handler. returns true if traceback | 291 only to call in exception handler. returns true if traceback |
360 printed.''' | 292 printed.''' |
361 if self.traceback: | 293 if self.traceback: |