Show More
@@ -1,155 +1,155 b'' | |||||
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 i18n import gettext as _ |
|
9 | from i18n import gettext as _ | |
10 | from demandload import * |
|
10 | from demandload import * | |
11 | demandload(globals(), "re socket sys util") |
|
11 | demandload(globals(), "re socket sys 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): |
|
15 | interactive=True): | |
16 | self.overlay = {} |
|
16 | self.overlay = {} | |
17 | self.cdata = ConfigParser.SafeConfigParser() |
|
17 | self.cdata = ConfigParser.SafeConfigParser() | |
18 | self.readconfig(util.rcpath) |
|
18 | self.readconfig(util.rcpath) | |
19 |
|
19 | |||
20 | self.quiet = self.configbool("ui", "quiet") |
|
20 | self.quiet = self.configbool("ui", "quiet") | |
21 | self.verbose = self.configbool("ui", "verbose") |
|
21 | self.verbose = self.configbool("ui", "verbose") | |
22 | self.debugflag = self.configbool("ui", "debug") |
|
22 | self.debugflag = self.configbool("ui", "debug") | |
23 | self.interactive = self.configbool("ui", "interactive", True) |
|
23 | self.interactive = self.configbool("ui", "interactive", True) | |
24 |
|
24 | |||
25 | self.updateopts(verbose, debug, quiet, interactive) |
|
25 | self.updateopts(verbose, debug, quiet, interactive) | |
26 |
|
26 | |||
27 | def updateopts(self, verbose=False, debug=False, quiet=False, |
|
27 | def updateopts(self, verbose=False, debug=False, quiet=False, | |
28 | interactive=True): |
|
28 | interactive=True): | |
29 | self.quiet = (self.quiet or quiet) and not verbose and not debug |
|
29 | self.quiet = (self.quiet or quiet) and not verbose and not debug | |
30 | self.verbose = (self.verbose or verbose) or debug |
|
30 | self.verbose = (self.verbose or verbose) or debug | |
31 | self.debugflag = (self.debugflag or debug) |
|
31 | self.debugflag = (self.debugflag or debug) | |
32 | self.interactive = (self.interactive and interactive) |
|
32 | self.interactive = (self.interactive and interactive) | |
33 |
|
33 | |||
34 | def readconfig(self, fn): |
|
34 | def readconfig(self, fn): | |
35 | if isinstance(fn, basestring): |
|
35 | if isinstance(fn, basestring): | |
36 | fn = [fn] |
|
36 | fn = [fn] | |
37 | for f in fn: |
|
37 | for f in fn: | |
38 | try: |
|
38 | try: | |
39 | self.cdata.read(f) |
|
39 | self.cdata.read(f) | |
40 | except ConfigParser.ParsingError, inst: |
|
40 | except ConfigParser.ParsingError, inst: | |
41 | raise util.Abort(_("Failed to parse %s\n%s") % (f, inst)) |
|
41 | raise util.Abort(_("Failed to parse %s\n%s") % (f, inst)) | |
42 |
|
42 | |||
43 | def setconfig(self, section, name, val): |
|
43 | def setconfig(self, section, name, val): | |
44 | self.overlay[(section, name)] = val |
|
44 | self.overlay[(section, name)] = val | |
45 |
|
45 | |||
46 | def config(self, section, name, default=None): |
|
46 | def config(self, section, name, default=None): | |
47 | if self.overlay.has_key((section, name)): |
|
47 | if self.overlay.has_key((section, name)): | |
48 | return self.overlay[(section, name)] |
|
48 | return self.overlay[(section, name)] | |
49 | if self.cdata.has_option(section, name): |
|
49 | if self.cdata.has_option(section, name): | |
50 | return self.cdata.get(section, name) |
|
50 | return self.cdata.get(section, name) | |
51 | return default |
|
51 | return default | |
52 |
|
52 | |||
53 | def configbool(self, section, name, default=False): |
|
53 | def configbool(self, section, name, default=False): | |
54 | if self.overlay.has_key((section, name)): |
|
54 | if self.overlay.has_key((section, name)): | |
55 | return self.overlay[(section, name)] |
|
55 | return self.overlay[(section, name)] | |
56 | if self.cdata.has_option(section, name): |
|
56 | if self.cdata.has_option(section, name): | |
57 | return self.cdata.getboolean(section, name) |
|
57 | return self.cdata.getboolean(section, name) | |
58 | return default |
|
58 | return default | |
59 |
|
59 | |||
60 | def configitems(self, section): |
|
60 | def configitems(self, section): | |
61 | if self.cdata.has_section(section): |
|
61 | if self.cdata.has_section(section): | |
62 | return self.cdata.items(section) |
|
62 | return self.cdata.items(section) | |
63 | return [] |
|
63 | return [] | |
64 |
|
64 | |||
65 | def walkconfig(self): |
|
65 | def walkconfig(self): | |
66 | seen = {} |
|
66 | seen = {} | |
67 | for (section, name), value in self.overlay.iteritems(): |
|
67 | for (section, name), value in self.overlay.iteritems(): | |
68 | yield section, name, value |
|
68 | yield section, name, value | |
69 | seen[section, name] = 1 |
|
69 | seen[section, name] = 1 | |
70 | for section in self.cdata.sections(): |
|
70 | for section in self.cdata.sections(): | |
71 | for name, value in self.cdata.items(section): |
|
71 | for name, value in self.cdata.items(section): | |
72 | if (section, name) in seen: continue |
|
72 | if (section, name) in seen: continue | |
73 | yield section, name, value.replace('\n', '\\n') |
|
73 | yield section, name, value.replace('\n', '\\n') | |
74 | seen[section, name] = 1 |
|
74 | seen[section, name] = 1 | |
75 |
|
75 | |||
76 | def extensions(self): |
|
76 | def extensions(self): | |
77 | return self.configitems("extensions") |
|
77 | return self.configitems("extensions") | |
78 |
|
78 | |||
79 | def username(self): |
|
79 | def username(self): | |
80 | return (os.environ.get("HGUSER") or |
|
80 | return (os.environ.get("HGUSER") or | |
81 | self.config("ui", "username") or |
|
81 | self.config("ui", "username") or | |
82 | os.environ.get("EMAIL") or |
|
82 | os.environ.get("EMAIL") or | |
83 | (os.environ.get("LOGNAME", |
|
83 | (os.environ.get("LOGNAME", | |
84 | os.environ.get("USERNAME", "unknown")) |
|
84 | os.environ.get("USERNAME", "unknown")) | |
85 | + '@' + socket.getfqdn())) |
|
85 | + '@' + socket.getfqdn())) | |
86 |
|
86 | |||
87 | def shortuser(self, user): |
|
87 | def shortuser(self, user): | |
88 | """Return a short representation of a user name or email address.""" |
|
88 | """Return a short representation of a user name or email address.""" | |
89 | if not self.verbose: |
|
89 | if not self.verbose: | |
90 | f = user.find('@') |
|
90 | f = user.find('@') | |
91 | if f >= 0: |
|
91 | if f >= 0: | |
92 | user = user[:f] |
|
92 | user = user[:f] | |
93 | f = user.find('<') |
|
93 | f = user.find('<') | |
94 | if f >= 0: |
|
94 | if f >= 0: | |
95 | user = user[f+1:] |
|
95 | user = user[f+1:] | |
96 | return user |
|
96 | return user | |
97 |
|
97 | |||
98 | def expandpath(self, loc, root=""): |
|
98 | def expandpath(self, loc, root=""): | |
99 | paths = {} |
|
99 | paths = {} | |
100 | for name, path in self.configitems("paths"): |
|
100 | for name, path in self.configitems("paths"): | |
101 | m = path.find("://") |
|
101 | m = path.find("://") | |
102 | if m == -1: |
|
102 | if m == -1: | |
103 | path = os.path.join(root, path) |
|
103 | path = os.path.join(root, path) | |
104 | paths[name] = path |
|
104 | paths[name] = path | |
105 |
|
105 | |||
106 | return paths.get(loc, loc) |
|
106 | return paths.get(loc, loc) | |
107 |
|
107 | |||
108 | def write(self, *args): |
|
108 | def write(self, *args): | |
109 | for a in args: |
|
109 | for a in args: | |
110 | sys.stdout.write(str(a)) |
|
110 | sys.stdout.write(str(a)) | |
111 |
|
111 | |||
112 | def write_err(self, *args): |
|
112 | def write_err(self, *args): | |
113 | sys.stdout.flush() |
|
113 | sys.stdout.flush() | |
114 | for a in args: |
|
114 | for a in args: | |
115 | sys.stderr.write(str(a)) |
|
115 | sys.stderr.write(str(a)) | |
116 |
|
116 | |||
117 | def readline(self): |
|
117 | def readline(self): | |
118 | return sys.stdin.readline()[:-1] |
|
118 | return sys.stdin.readline()[:-1] | |
119 | def prompt(self, msg, pat, default="y"): |
|
119 | def prompt(self, msg, pat, default="y"): | |
120 | if not self.interactive: return default |
|
120 | if not self.interactive: return default | |
121 | while 1: |
|
121 | while 1: | |
122 | self.write(msg, " ") |
|
122 | self.write(msg, " ") | |
123 | r = self.readline() |
|
123 | r = self.readline() | |
124 | if re.match(pat, r): |
|
124 | if re.match(pat, r): | |
125 | return r |
|
125 | return r | |
126 | else: |
|
126 | else: | |
127 | self.write(_("unrecognized response\n")) |
|
127 | self.write(_("unrecognized response\n")) | |
128 | def status(self, *msg): |
|
128 | def status(self, *msg): | |
129 | if not self.quiet: self.write(*msg) |
|
129 | if not self.quiet: self.write(*msg) | |
130 | def warn(self, *msg): |
|
130 | def warn(self, *msg): | |
131 | self.write_err(*msg) |
|
131 | self.write_err(*msg) | |
132 | def note(self, *msg): |
|
132 | def note(self, *msg): | |
133 | if self.verbose: self.write(*msg) |
|
133 | if self.verbose: self.write(*msg) | |
134 | def debug(self, *msg): |
|
134 | def debug(self, *msg): | |
135 | if self.debugflag: self.write(*msg) |
|
135 | if self.debugflag: self.write(*msg) | |
136 | def edit(self, text): |
|
136 | def edit(self, text): | |
137 | import tempfile |
|
137 | import tempfile | |
138 | (fd, name) = tempfile.mkstemp("hg") |
|
138 | (fd, name) = tempfile.mkstemp("hg") | |
139 | f = os.fdopen(fd, "w") |
|
139 | f = os.fdopen(fd, "w") | |
140 | f.write(text) |
|
140 | f.write(text) | |
141 | f.close() |
|
141 | f.close() | |
142 |
|
142 | |||
143 | editor = (os.environ.get("HGEDITOR") or |
|
143 | editor = (os.environ.get("HGEDITOR") or | |
144 | self.config("ui", "editor") or |
|
144 | self.config("ui", "editor") or | |
145 | os.environ.get("EDITOR", "vi")) |
|
145 | os.environ.get("EDITOR", "vi")) | |
146 |
|
146 | |||
147 | os.environ["HGUSER"] = self.username() |
|
147 | os.environ["HGUSER"] = self.username() | |
148 | util.system("%s %s" % (editor, name), errprefix=_("edit failed")) |
|
148 | util.system("%s \"%s\"" % (editor, name), errprefix=_("edit failed")) | |
149 |
|
149 | |||
150 | t = open(name).read() |
|
150 | t = open(name).read() | |
151 | t = re.sub("(?m)^HG:.*\n", "", t) |
|
151 | t = re.sub("(?m)^HG:.*\n", "", t) | |
152 |
|
152 | |||
153 | os.unlink(name) |
|
153 | os.unlink(name) | |
154 |
|
154 | |||
155 | return t |
|
155 | return t |
General Comments 0
You need to be logged in to leave comments.
Login now