author | mpm@selenic.com |
Fri, 19 Aug 2005 18:47:05 -0800 | |
changeset 958 | d845a1f174bb |
parent 953 | 52d8d81e72ad |
child 960 | abfb5cc97fcd |
permissions | -rw-r--r-- |
207 | 1 |
# ui.py - user interface bits for mercurial |
2 |
# |
|
3 |
# Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
4 |
# |
|
5 |
# This software may be used and distributed according to the terms |
|
6 |
# of the GNU General Public License, incorporated herein by reference. |
|
7 |
||
613
5374955ec5b1
Demand-load most modules in the commands and ui modules.
Bryan O'Sullivan <bos@serpentine.com>
parents:
608
diff
changeset
|
8 |
import os, ConfigParser |
5374955ec5b1
Demand-load most modules in the commands and ui modules.
Bryan O'Sullivan <bos@serpentine.com>
parents:
608
diff
changeset
|
9 |
from demandload import * |
5374955ec5b1
Demand-load most modules in the commands and ui modules.
Bryan O'Sullivan <bos@serpentine.com>
parents:
608
diff
changeset
|
10 |
demandload(globals(), "re socket sys util") |
207 | 11 |
|
12 |
class ui: |
|
13 |
def __init__(self, verbose=False, debug=False, quiet=False, |
|
14 |
interactive=True): |
|
285 | 15 |
self.cdata = ConfigParser.SafeConfigParser() |
951
859de3ebc041
Read global config file /etc/mercurial/hgrc and fix reading hgrc on Windows.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
691
diff
changeset
|
16 |
self.cdata.read([os.path.normpath(hgrc) for hgrc in |
859de3ebc041
Read global config file /etc/mercurial/hgrc and fix reading hgrc on Windows.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
691
diff
changeset
|
17 |
"/etc/mercurial/hgrc", os.path.expanduser("~/.hgrc")]) |
285 | 18 |
|
19 |
self.quiet = self.configbool("ui", "quiet") |
|
20 |
self.verbose = self.configbool("ui", "verbose") |
|
21 |
self.debugflag = self.configbool("ui", "debug") |
|
22 |
self.interactive = self.configbool("ui", "interactive", True) |
|
23 |
||
24 |
self.quiet = (self.quiet or quiet) and not verbose and not debug |
|
25 |
self.verbose = (self.verbose or verbose) or debug |
|
26 |
self.debugflag = (self.debugflag or debug) |
|
27 |
self.interactive = (self.interactive and interactive) |
|
28 |
||
337 | 29 |
def readconfig(self, fp): |
30 |
self.cdata.readfp(fp) |
|
31 |
||
285 | 32 |
def config(self, section, val, default=None): |
33 |
if self.cdata.has_option(section, val): |
|
34 |
return self.cdata.get(section, val) |
|
35 |
return default |
|
36 |
||
37 |
def configbool(self, section, val, default=False): |
|
38 |
if self.cdata.has_option(section, val): |
|
39 |
return self.cdata.getboolean(section, val) |
|
40 |
return default |
|
41 |
||
42 |
def configitems(self, section): |
|
43 |
if self.cdata.has_section(section): |
|
44 |
return self.cdata.items(section) |
|
45 |
return [] |
|
46 |
||
608
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
47 |
def username(self): |
691
61c6b4178b9e
HG environment variables take precedence over hgrc
mpm@selenic.com
parents:
662
diff
changeset
|
48 |
return (os.environ.get("HGUSER") or |
61c6b4178b9e
HG environment variables take precedence over hgrc
mpm@selenic.com
parents:
662
diff
changeset
|
49 |
self.config("ui", "username") or |
608
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
50 |
os.environ.get("EMAIL") or |
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
51 |
(os.environ.get("LOGNAME", |
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
52 |
os.environ.get("USERNAME", "unknown")) |
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
53 |
+ '@' + socket.getfqdn())) |
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
54 |
|
506 | 55 |
def expandpath(self, loc): |
56 |
paths = {} |
|
57 |
for name, path in self.configitems("paths"): |
|
58 |
paths[name] = path |
|
59 |
||
60 |
return paths.get(loc, loc) |
|
61 |
||
207 | 62 |
def write(self, *args): |
63 |
for a in args: |
|
64 |
sys.stdout.write(str(a)) |
|
565 | 65 |
|
66 |
def write_err(self, *args): |
|
67 |
sys.stdout.flush() |
|
68 |
for a in args: |
|
69 |
sys.stderr.write(str(a)) |
|
70 |
||
207 | 71 |
def readline(self): |
72 |
return sys.stdin.readline()[:-1] |
|
73 |
def prompt(self, msg, pat, default = "y"): |
|
74 |
if not self.interactive: return default |
|
75 |
while 1: |
|
76 |
self.write(msg, " ") |
|
77 |
r = self.readline() |
|
78 |
if re.match(pat, r): |
|
79 |
return r |
|
80 |
else: |
|
81 |
self.write("unrecognized response\n") |
|
82 |
def status(self, *msg): |
|
83 |
if not self.quiet: self.write(*msg) |
|
234
3427806d5ab9
ui.warn can use more than one argument like the other ui methods.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
207
diff
changeset
|
84 |
def warn(self, *msg): |
565 | 85 |
self.write_err(*msg) |
207 | 86 |
def note(self, *msg): |
87 |
if self.verbose: self.write(*msg) |
|
88 |
def debug(self, *msg): |
|
89 |
if self.debugflag: self.write(*msg) |
|
90 |
def edit(self, text): |
|
249 | 91 |
import tempfile |
207 | 92 |
(fd, name) = tempfile.mkstemp("hg") |
93 |
f = os.fdopen(fd, "w") |
|
94 |
f.write(text) |
|
95 |
f.close() |
|
96 |
||
691
61c6b4178b9e
HG environment variables take precedence over hgrc
mpm@selenic.com
parents:
662
diff
changeset
|
97 |
editor = (os.environ.get("HGEDITOR") or |
61c6b4178b9e
HG environment variables take precedence over hgrc
mpm@selenic.com
parents:
662
diff
changeset
|
98 |
self.config("ui", "editor") or |
608
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
99 |
os.environ.get("EDITOR", "vi")) |
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
100 |
|
662
b55a78595ef6
Pass username to hgeditor, remove temporary file
Radoslaw "AstralStorm" Szkodzinski <astralstorm@gorzow.mm.pl>
parents:
613
diff
changeset
|
101 |
os.environ["HGUSER"] = self.username() |
508 | 102 |
util.system("%s %s" % (editor, name), errprefix = "edit failed") |
207 | 103 |
|
104 |
t = open(name).read() |
|
105 |
t = re.sub("(?m)^HG:.*\n", "", t) |
|
106 |
||
662
b55a78595ef6
Pass username to hgeditor, remove temporary file
Radoslaw "AstralStorm" Szkodzinski <astralstorm@gorzow.mm.pl>
parents:
613
diff
changeset
|
107 |
os.unlink(name) |
b55a78595ef6
Pass username to hgeditor, remove temporary file
Radoslaw "AstralStorm" Szkodzinski <astralstorm@gorzow.mm.pl>
parents:
613
diff
changeset
|
108 |
|
207 | 109 |
return t |