Show More
@@ -1,105 +1,108 | |||
|
1 | 1 | # ui.py - user interface bits for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms |
|
6 | 6 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | 7 | |
|
8 | 8 | import os, ConfigParser |
|
9 | 9 | from demandload import * |
|
10 | 10 | demandload(globals(), "re socket sys util") |
|
11 | 11 | |
|
12 | 12 | class ui: |
|
13 | 13 | def __init__(self, verbose=False, debug=False, quiet=False, |
|
14 | 14 | interactive=True): |
|
15 | 15 | self.cdata = ConfigParser.SafeConfigParser() |
|
16 | 16 | self.cdata.read(os.path.expanduser("~/.hgrc")) |
|
17 | 17 | |
|
18 | 18 | self.quiet = self.configbool("ui", "quiet") |
|
19 | 19 | self.verbose = self.configbool("ui", "verbose") |
|
20 | 20 | self.debugflag = self.configbool("ui", "debug") |
|
21 | 21 | self.interactive = self.configbool("ui", "interactive", True) |
|
22 | 22 | |
|
23 | 23 | self.quiet = (self.quiet or quiet) and not verbose and not debug |
|
24 | 24 | self.verbose = (self.verbose or verbose) or debug |
|
25 | 25 | self.debugflag = (self.debugflag or debug) |
|
26 | 26 | self.interactive = (self.interactive and interactive) |
|
27 | 27 | |
|
28 | 28 | def readconfig(self, fp): |
|
29 | 29 | self.cdata.readfp(fp) |
|
30 | 30 | |
|
31 | 31 | def config(self, section, val, default=None): |
|
32 | 32 | if self.cdata.has_option(section, val): |
|
33 | 33 | return self.cdata.get(section, val) |
|
34 | 34 | return default |
|
35 | 35 | |
|
36 | 36 | def configbool(self, section, val, default=False): |
|
37 | 37 | if self.cdata.has_option(section, val): |
|
38 | 38 | return self.cdata.getboolean(section, val) |
|
39 | 39 | return default |
|
40 | 40 | |
|
41 | 41 | def configitems(self, section): |
|
42 | 42 | if self.cdata.has_section(section): |
|
43 | 43 | return self.cdata.items(section) |
|
44 | 44 | return [] |
|
45 | 45 | |
|
46 | 46 | def username(self): |
|
47 | 47 | return (self.config("ui", "username") or |
|
48 | 48 | os.environ.get("HGUSER") or |
|
49 | 49 | os.environ.get("EMAIL") or |
|
50 | 50 | (os.environ.get("LOGNAME", |
|
51 | 51 | os.environ.get("USERNAME", "unknown")) |
|
52 | 52 | + '@' + socket.getfqdn())) |
|
53 | 53 | |
|
54 | 54 | def expandpath(self, loc): |
|
55 | 55 | paths = {} |
|
56 | 56 | for name, path in self.configitems("paths"): |
|
57 | 57 | paths[name] = path |
|
58 | 58 | |
|
59 | 59 | return paths.get(loc, loc) |
|
60 | 60 | |
|
61 | 61 | def write(self, *args): |
|
62 | 62 | for a in args: |
|
63 | 63 | sys.stdout.write(str(a)) |
|
64 | 64 | |
|
65 | 65 | def write_err(self, *args): |
|
66 | 66 | sys.stdout.flush() |
|
67 | 67 | for a in args: |
|
68 | 68 | sys.stderr.write(str(a)) |
|
69 | 69 | |
|
70 | 70 | def readline(self): |
|
71 | 71 | return sys.stdin.readline()[:-1] |
|
72 | 72 | def prompt(self, msg, pat, default = "y"): |
|
73 | 73 | if not self.interactive: return default |
|
74 | 74 | while 1: |
|
75 | 75 | self.write(msg, " ") |
|
76 | 76 | r = self.readline() |
|
77 | 77 | if re.match(pat, r): |
|
78 | 78 | return r |
|
79 | 79 | else: |
|
80 | 80 | self.write("unrecognized response\n") |
|
81 | 81 | def status(self, *msg): |
|
82 | 82 | if not self.quiet: self.write(*msg) |
|
83 | 83 | def warn(self, *msg): |
|
84 | 84 | self.write_err(*msg) |
|
85 | 85 | def note(self, *msg): |
|
86 | 86 | if self.verbose: self.write(*msg) |
|
87 | 87 | def debug(self, *msg): |
|
88 | 88 | if self.debugflag: self.write(*msg) |
|
89 | 89 | def edit(self, text): |
|
90 | 90 | import tempfile |
|
91 | 91 | (fd, name) = tempfile.mkstemp("hg") |
|
92 | 92 | f = os.fdopen(fd, "w") |
|
93 | 93 | f.write(text) |
|
94 | 94 | f.close() |
|
95 | 95 | |
|
96 | 96 | editor = (self.config("ui", "editor") or |
|
97 | 97 | os.environ.get("HGEDITOR") or |
|
98 | 98 | os.environ.get("EDITOR", "vi")) |
|
99 | 99 | |
|
100 | os.environ["HGUSER"] = self.username() | |
|
100 | 101 | util.system("%s %s" % (editor, name), errprefix = "edit failed") |
|
101 | 102 | |
|
102 | 103 | t = open(name).read() |
|
103 | 104 | t = re.sub("(?m)^HG:.*\n", "", t) |
|
104 | 105 | |
|
106 | os.unlink(name) | |
|
107 | ||
|
105 | 108 | return t |
General Comments 0
You need to be logged in to leave comments.
Login now