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