Show More
@@ -1,200 +1,203 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 |
|
8 | import 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(), "os 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, parentui=None): |
|
15 | interactive=True, parentui=None): | |
16 | self.overlay = {} |
|
16 | self.overlay = {} | |
17 | self.cdata = ConfigParser.SafeConfigParser() |
|
17 | self.cdata = ConfigParser.SafeConfigParser() | |
18 | self.parentui = parentui and parentui.parentui or parentui |
|
18 | self.parentui = parentui and parentui.parentui or parentui | |
19 | if parentui is None: |
|
19 | if parentui is None: | |
20 | self.readconfig(util.rcpath) |
|
20 | self.readconfig(util.rcpath) | |
21 |
|
21 | |||
22 | self.quiet = self.configbool("ui", "quiet") |
|
22 | self.quiet = self.configbool("ui", "quiet") | |
23 | self.verbose = self.configbool("ui", "verbose") |
|
23 | self.verbose = self.configbool("ui", "verbose") | |
24 | self.debugflag = self.configbool("ui", "debug") |
|
24 | self.debugflag = self.configbool("ui", "debug") | |
25 | self.interactive = self.configbool("ui", "interactive", True) |
|
25 | self.interactive = self.configbool("ui", "interactive", True) | |
26 |
|
26 | |||
27 | self.updateopts(verbose, debug, quiet, interactive) |
|
27 | self.updateopts(verbose, debug, quiet, interactive) | |
28 | self.diffcache = None |
|
28 | self.diffcache = None | |
|
29 | else: | |||
|
30 | self.cdata._defaults = parentui.cdata._defaults | |||
|
31 | for key, value in parentui.cdata._sections.iteritems(): | |||
|
32 | self.cdata._sections[key] = value.copy() | |||
29 |
|
33 | |||
30 | def __getattr__(self, key): |
|
34 | def __getattr__(self, key): | |
31 | return getattr(self.parentui, key) |
|
35 | return getattr(self.parentui, key) | |
32 |
|
36 | |||
33 | def updateopts(self, verbose=False, debug=False, quiet=False, |
|
37 | def updateopts(self, verbose=False, debug=False, quiet=False, | |
34 | interactive=True): |
|
38 | interactive=True): | |
35 | self.quiet = (self.quiet or quiet) and not verbose and not debug |
|
39 | self.quiet = (self.quiet or quiet) and not verbose and not debug | |
36 | self.verbose = (self.verbose or verbose) or debug |
|
40 | self.verbose = (self.verbose or verbose) or debug | |
37 | self.debugflag = (self.debugflag or debug) |
|
41 | self.debugflag = (self.debugflag or debug) | |
38 | self.interactive = (self.interactive and interactive) |
|
42 | self.interactive = (self.interactive and interactive) | |
39 |
|
43 | |||
40 | def readconfig(self, fn): |
|
44 | def readconfig(self, fn): | |
41 | if isinstance(fn, basestring): |
|
45 | if isinstance(fn, basestring): | |
42 | fn = [fn] |
|
46 | fn = [fn] | |
43 | for f in fn: |
|
47 | for f in fn: | |
44 | try: |
|
48 | try: | |
45 | self.cdata.read(f) |
|
49 | self.cdata.read(f) | |
46 | except ConfigParser.ParsingError, inst: |
|
50 | except ConfigParser.ParsingError, inst: | |
47 | raise util.Abort(_("Failed to parse %s\n%s") % (f, inst)) |
|
51 | raise util.Abort(_("Failed to parse %s\n%s") % (f, inst)) | |
48 |
|
52 | |||
49 | def setconfig(self, section, name, val): |
|
53 | def setconfig(self, section, name, val): | |
50 | self.overlay[(section, name)] = val |
|
54 | self.overlay[(section, name)] = val | |
51 |
|
55 | |||
52 | def config(self, section, name, default=None): |
|
56 | def config(self, section, name, default=None): | |
53 | if self.overlay.has_key((section, name)): |
|
57 | if self.overlay.has_key((section, name)): | |
54 | return self.overlay[(section, name)] |
|
58 | return self.overlay[(section, name)] | |
55 | if self.cdata.has_option(section, name): |
|
59 | if self.cdata.has_option(section, name): | |
56 | return self.cdata.get(section, name) |
|
60 | return self.cdata.get(section, name) | |
57 | if self.parentui is None: |
|
61 | if self.parentui is None: | |
58 | return default |
|
62 | return default | |
59 | else: |
|
63 | else: | |
60 | return self.parentui.config(section, name, default) |
|
64 | return self.parentui.config(section, name, default) | |
61 |
|
65 | |||
62 | def configbool(self, section, name, default=False): |
|
66 | def configbool(self, section, name, default=False): | |
63 | if self.overlay.has_key((section, name)): |
|
67 | if self.overlay.has_key((section, name)): | |
64 | return self.overlay[(section, name)] |
|
68 | return self.overlay[(section, name)] | |
65 | if self.cdata.has_option(section, name): |
|
69 | if self.cdata.has_option(section, name): | |
66 | return self.cdata.getboolean(section, name) |
|
70 | return self.cdata.getboolean(section, name) | |
67 | if self.parentui is None: |
|
71 | if self.parentui is None: | |
68 | return default |
|
72 | return default | |
69 | else: |
|
73 | else: | |
70 | return self.parentui.configbool(section, name, default) |
|
74 | return self.parentui.configbool(section, name, default) | |
71 |
|
75 | |||
72 | def configitems(self, section): |
|
76 | def configitems(self, section): | |
73 | items = {} |
|
77 | items = {} | |
74 | if self.parentui is not None: |
|
78 | if self.parentui is not None: | |
75 | items = dict(self.parentui.configitems(section)) |
|
79 | items = dict(self.parentui.configitems(section)) | |
76 | if self.cdata.has_section(section): |
|
80 | if self.cdata.has_section(section): | |
77 | items.update(dict(self.cdata.items(section))) |
|
81 | items.update(dict(self.cdata.items(section))) | |
78 | x = items.items() |
|
82 | x = items.items() | |
79 | x.sort() |
|
83 | x.sort() | |
80 | return x |
|
84 | return x | |
81 |
|
85 | |||
82 | def walkconfig(self, seen=None): |
|
86 | def walkconfig(self, seen=None): | |
83 | if seen is None: |
|
87 | if seen is None: | |
84 | seen = {} |
|
88 | seen = {} | |
85 | for (section, name), value in self.overlay.iteritems(): |
|
89 | for (section, name), value in self.overlay.iteritems(): | |
86 | yield section, name, value |
|
90 | yield section, name, value | |
87 | seen[section, name] = 1 |
|
91 | seen[section, name] = 1 | |
88 | for section in self.cdata.sections(): |
|
92 | for section in self.cdata.sections(): | |
89 | for name, value in self.cdata.items(section): |
|
93 | for name, value in self.cdata.items(section): | |
90 | if (section, name) in seen: continue |
|
94 | if (section, name) in seen: continue | |
91 | yield section, name, value.replace('\n', '\\n') |
|
95 | yield section, name, value.replace('\n', '\\n') | |
92 | seen[section, name] = 1 |
|
96 | seen[section, name] = 1 | |
93 | if self.parentui is not None: |
|
97 | if self.parentui is not None: | |
94 | for parent in self.parentui.walkconfig(seen): |
|
98 | for parent in self.parentui.walkconfig(seen): | |
95 | yield parent |
|
99 | yield parent | |
96 |
|
100 | |||
97 | def extensions(self): |
|
101 | def extensions(self): | |
98 | return self.configitems("extensions") |
|
102 | return self.configitems("extensions") | |
99 |
|
103 | |||
100 | def diffopts(self): |
|
104 | def diffopts(self): | |
101 | if self.diffcache: |
|
105 | if self.diffcache: | |
102 | return self.diffcache |
|
106 | return self.diffcache | |
103 | ret = { 'showfunc' : True, 'ignorews' : False} |
|
107 | ret = { 'showfunc' : True, 'ignorews' : False} | |
104 | for x in self.configitems("diff"): |
|
108 | for x in self.configitems("diff"): | |
105 | k = x[0].lower() |
|
109 | k = x[0].lower() | |
106 | v = x[1] |
|
110 | v = x[1] | |
107 | if v: |
|
111 | if v: | |
108 | v = v.lower() |
|
112 | v = v.lower() | |
109 | if v == 'true': |
|
113 | if v == 'true': | |
110 | value = True |
|
114 | value = True | |
111 | else: |
|
115 | else: | |
112 | value = False |
|
116 | value = False | |
113 | ret[k] = value |
|
117 | ret[k] = value | |
114 | self.diffcache = ret |
|
118 | self.diffcache = ret | |
115 | return ret |
|
119 | return ret | |
116 |
|
120 | |||
117 | def username(self): |
|
121 | def username(self): | |
118 | return (os.environ.get("HGUSER") or |
|
122 | return (os.environ.get("HGUSER") or | |
119 | self.config("ui", "username") or |
|
123 | self.config("ui", "username") or | |
120 | os.environ.get("EMAIL") or |
|
124 | os.environ.get("EMAIL") or | |
121 | (os.environ.get("LOGNAME", |
|
125 | (os.environ.get("LOGNAME", | |
122 | os.environ.get("USERNAME", "unknown")) |
|
126 | os.environ.get("USERNAME", "unknown")) | |
123 | + '@' + socket.getfqdn())) |
|
127 | + '@' + socket.getfqdn())) | |
124 |
|
128 | |||
125 | def shortuser(self, user): |
|
129 | def shortuser(self, user): | |
126 | """Return a short representation of a user name or email address.""" |
|
130 | """Return a short representation of a user name or email address.""" | |
127 | if not self.verbose: |
|
131 | if not self.verbose: | |
128 | f = user.find('@') |
|
132 | f = user.find('@') | |
129 | if f >= 0: |
|
133 | if f >= 0: | |
130 | user = user[:f] |
|
134 | user = user[:f] | |
131 | f = user.find('<') |
|
135 | f = user.find('<') | |
132 | if f >= 0: |
|
136 | if f >= 0: | |
133 | user = user[f+1:] |
|
137 | user = user[f+1:] | |
134 | return user |
|
138 | return user | |
135 |
|
139 | |||
136 | def expandpath(self, loc, root=""): |
|
140 | def expandpath(self, loc, root=""): | |
137 | paths = {} |
|
141 | paths = {} | |
138 | for name, path in self.configitems("paths"): |
|
142 | for name, path in self.configitems("paths"): | |
139 | m = path.find("://") |
|
143 | m = path.find("://") | |
140 | if m == -1: |
|
144 | if m == -1: | |
141 | path = os.path.join(root, path) |
|
145 | path = os.path.join(root, path) | |
142 | paths[name] = path |
|
146 | paths[name] = path | |
143 |
|
147 | |||
144 | return paths.get(loc, loc) |
|
148 | return paths.get(loc, loc) | |
145 |
|
149 | |||
146 | def write(self, *args): |
|
150 | def write(self, *args): | |
147 | for a in args: |
|
151 | for a in args: | |
148 | sys.stdout.write(str(a)) |
|
152 | sys.stdout.write(str(a)) | |
149 |
|
153 | |||
150 | def write_err(self, *args): |
|
154 | def write_err(self, *args): | |
151 | if not sys.stdout.closed: sys.stdout.flush() |
|
155 | if not sys.stdout.closed: sys.stdout.flush() | |
152 | for a in args: |
|
156 | for a in args: | |
153 | sys.stderr.write(str(a)) |
|
157 | sys.stderr.write(str(a)) | |
154 |
|
158 | |||
155 | def flush(self): |
|
159 | def flush(self): | |
156 | try: |
|
160 | try: | |
157 | sys.stdout.flush() |
|
161 | sys.stdout.flush() | |
158 | finally: |
|
162 | finally: | |
159 | sys.stderr.flush() |
|
163 | sys.stderr.flush() | |
160 |
|
164 | |||
161 | def readline(self): |
|
165 | def readline(self): | |
162 | return sys.stdin.readline()[:-1] |
|
166 | return sys.stdin.readline()[:-1] | |
163 | def prompt(self, msg, pat, default="y"): |
|
167 | def prompt(self, msg, pat, default="y"): | |
164 | if not self.interactive: return default |
|
168 | if not self.interactive: return default | |
165 | while 1: |
|
169 | while 1: | |
166 | self.write(msg, " ") |
|
170 | self.write(msg, " ") | |
167 | r = self.readline() |
|
171 | r = self.readline() | |
168 | if re.match(pat, r): |
|
172 | if re.match(pat, r): | |
169 | return r |
|
173 | return r | |
170 | else: |
|
174 | else: | |
171 | self.write(_("unrecognized response\n")) |
|
175 | self.write(_("unrecognized response\n")) | |
172 | def status(self, *msg): |
|
176 | def status(self, *msg): | |
173 | if not self.quiet: self.write(*msg) |
|
177 | if not self.quiet: self.write(*msg) | |
174 | def warn(self, *msg): |
|
178 | def warn(self, *msg): | |
175 | self.write_err(*msg) |
|
179 | self.write_err(*msg) | |
176 | def note(self, *msg): |
|
180 | def note(self, *msg): | |
177 | if self.verbose: self.write(*msg) |
|
181 | if self.verbose: self.write(*msg) | |
178 | def debug(self, *msg): |
|
182 | def debug(self, *msg): | |
179 | if self.debugflag: self.write(*msg) |
|
183 | if self.debugflag: self.write(*msg) | |
180 | def edit(self, text): |
|
184 | def edit(self, text): | |
181 | import tempfile |
|
185 | import tempfile | |
182 | (fd, name) = tempfile.mkstemp("hg") |
|
186 | (fd, name) = tempfile.mkstemp("hg") | |
183 | f = os.fdopen(fd, "w") |
|
187 | f = os.fdopen(fd, "w") | |
184 | f.write(text) |
|
188 | f.write(text) | |
185 | f.close() |
|
189 | f.close() | |
186 |
|
190 | |||
187 | editor = (os.environ.get("HGEDITOR") or |
|
191 | editor = (os.environ.get("HGEDITOR") or | |
188 | self.config("ui", "editor") or |
|
192 | self.config("ui", "editor") or | |
189 | os.environ.get("EDITOR", "vi")) |
|
193 | os.environ.get("EDITOR", "vi")) | |
190 |
|
194 | |||
191 | os.environ["HGUSER"] = self.username() |
|
195 | os.environ["HGUSER"] = self.username() | |
192 | util.system("%s \"%s\"" % (editor, name), errprefix=_("edit failed")) |
|
196 | util.system("%s \"%s\"" % (editor, name), errprefix=_("edit failed")) | |
193 |
|
197 | |||
194 | t = open(name).read() |
|
198 | t = open(name).read() | |
195 | t = re.sub("(?m)^HG:.*\n", "", t) |
|
199 | t = re.sub("(?m)^HG:.*\n", "", t) | |
196 |
|
200 | |||
197 | os.unlink(name) |
|
201 | os.unlink(name) | |
198 |
|
202 | |||
199 | return t |
|
203 | return t | |
200 |
|
General Comments 0
You need to be logged in to leave comments.
Login now