author | mpm@selenic.com |
Tue, 16 Aug 2005 19:17:01 -0800 | |
changeset 926 | b765e970c9ff |
parent 691 | 61c6b4178b9e |
child 951 | 859de3ebc041 |
child 953 | 52d8d81e72ad |
child 981 | 4f81068ed8cd |
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() |
16 |
self.cdata.read(os.path.expanduser("~/.hgrc")) |
|
17 |
||
18 |
self.quiet = self.configbool("ui", "quiet") |
|
19 |
self.verbose = self.configbool("ui", "verbose") |
|
20 |
self.debugflag = self.configbool("ui", "debug") |
|
21 |
self.interactive = self.configbool("ui", "interactive", True) |
|
22 |
||
23 |
self.quiet = (self.quiet or quiet) and not verbose and not debug |
|
24 |
self.verbose = (self.verbose or verbose) or debug |
|
25 |
self.debugflag = (self.debugflag or debug) |
|
26 |
self.interactive = (self.interactive and interactive) |
|
27 |
||
337 | 28 |
def readconfig(self, fp): |
29 |
self.cdata.readfp(fp) |
|
30 |
||
285 | 31 |
def config(self, section, val, default=None): |
32 |
if self.cdata.has_option(section, val): |
|
33 |
return self.cdata.get(section, val) |
|
34 |
return default |
|
35 |
||
36 |
def configbool(self, section, val, default=False): |
|
37 |
if self.cdata.has_option(section, val): |
|
38 |
return self.cdata.getboolean(section, val) |
|
39 |
return default |
|
40 |
||
41 |
def configitems(self, section): |
|
42 |
if self.cdata.has_section(section): |
|
43 |
return self.cdata.items(section) |
|
44 |
return [] |
|
45 |
||
608
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
46 |
def username(self): |
691
61c6b4178b9e
HG environment variables take precedence over hgrc
mpm@selenic.com
parents:
662
diff
changeset
|
47 |
return (os.environ.get("HGUSER") or |
61c6b4178b9e
HG environment variables take precedence over hgrc
mpm@selenic.com
parents:
662
diff
changeset
|
48 |
self.config("ui", "username") or |
608
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
49 |
os.environ.get("EMAIL") or |
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
50 |
(os.environ.get("LOGNAME", |
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
51 |
os.environ.get("USERNAME", "unknown")) |
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
52 |
+ '@' + socket.getfqdn())) |
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
53 |
|
506 | 54 |
def expandpath(self, loc): |
55 |
paths = {} |
|
56 |
for name, path in self.configitems("paths"): |
|
57 |
paths[name] = path |
|
58 |
||
59 |
return paths.get(loc, loc) |
|
60 |
||
207 | 61 |
def write(self, *args): |
62 |
for a in args: |
|
63 |
sys.stdout.write(str(a)) |
|
565 | 64 |
|
65 |
def write_err(self, *args): |
|
66 |
sys.stdout.flush() |
|
67 |
for a in args: |
|
68 |
sys.stderr.write(str(a)) |
|
69 |
||
207 | 70 |
def readline(self): |
71 |
return sys.stdin.readline()[:-1] |
|
72 |
def prompt(self, msg, pat, default = "y"): |
|
73 |
if not self.interactive: return default |
|
74 |
while 1: |
|
75 |
self.write(msg, " ") |
|
76 |
r = self.readline() |
|
77 |
if re.match(pat, r): |
|
78 |
return r |
|
79 |
else: |
|
80 |
self.write("unrecognized response\n") |
|
81 |
def status(self, *msg): |
|
82 |
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
|
83 |
def warn(self, *msg): |
565 | 84 |
self.write_err(*msg) |
207 | 85 |
def note(self, *msg): |
86 |
if self.verbose: self.write(*msg) |
|
87 |
def debug(self, *msg): |
|
88 |
if self.debugflag: self.write(*msg) |
|
89 |
def edit(self, text): |
|
249 | 90 |
import tempfile |
207 | 91 |
(fd, name) = tempfile.mkstemp("hg") |
92 |
f = os.fdopen(fd, "w") |
|
93 |
f.write(text) |
|
94 |
f.close() |
|
95 |
||
691
61c6b4178b9e
HG environment variables take precedence over hgrc
mpm@selenic.com
parents:
662
diff
changeset
|
96 |
editor = (os.environ.get("HGEDITOR") or |
61c6b4178b9e
HG environment variables take precedence over hgrc
mpm@selenic.com
parents:
662
diff
changeset
|
97 |
self.config("ui", "editor") or |
608
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
98 |
os.environ.get("EDITOR", "vi")) |
d2994b5298fb
Add username/merge/editor to .hgrc
Matt Mackall <mpm@selenic.com>
parents:
565
diff
changeset
|
99 |
|
662
b55a78595ef6
Pass username to hgeditor, remove temporary file
Radoslaw "AstralStorm" Szkodzinski <astralstorm@gorzow.mm.pl>
parents:
613
diff
changeset
|
100 |
os.environ["HGUSER"] = self.username() |
508 | 101 |
util.system("%s %s" % (editor, name), errprefix = "edit failed") |
207 | 102 |
|
103 |
t = open(name).read() |
|
104 |
t = re.sub("(?m)^HG:.*\n", "", t) |
|
105 |
||
662
b55a78595ef6
Pass username to hgeditor, remove temporary file
Radoslaw "AstralStorm" Szkodzinski <astralstorm@gorzow.mm.pl>
parents:
613
diff
changeset
|
106 |
os.unlink(name) |
b55a78595ef6
Pass username to hgeditor, remove temporary file
Radoslaw "AstralStorm" Szkodzinski <astralstorm@gorzow.mm.pl>
parents:
613
diff
changeset
|
107 |
|
207 | 108 |
return t |