10 demandload(globals(), "errno getpass os re socket sys tempfile") |
10 demandload(globals(), "errno getpass os re socket sys tempfile") |
11 demandload(globals(), "ConfigParser mdiff templater traceback util") |
11 demandload(globals(), "ConfigParser mdiff templater traceback 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, traceback=False, parentui=None, |
15 interactive=True, traceback=False, parentui=None): |
16 readhooks=[]): |
|
17 self.overlay = {} |
16 self.overlay = {} |
18 if parentui is None: |
17 if parentui is None: |
19 # this is the parent of all ui children |
18 # this is the parent of all ui children |
20 self.parentui = None |
19 self.parentui = None |
21 self.readhooks = list(readhooks) |
20 self.readhooks = [] |
22 self.cdata = ConfigParser.SafeConfigParser() |
21 self.cdata = ConfigParser.SafeConfigParser() |
23 self.readconfig(util.rcpath()) |
22 self.readconfig(util.rcpath()) |
24 |
23 |
25 self.quiet = self.configbool("ui", "quiet") |
24 self.quiet = self.configbool("ui", "quiet") |
26 self.verbose = self.configbool("ui", "verbose") |
25 self.verbose = self.configbool("ui", "verbose") |
34 self.prev_header = [] |
33 self.prev_header = [] |
35 self.revlogopts = self.configrevlog() |
34 self.revlogopts = self.configrevlog() |
36 else: |
35 else: |
37 # parentui may point to an ui object which is already a child |
36 # parentui may point to an ui object which is already a child |
38 self.parentui = parentui.parentui or parentui |
37 self.parentui = parentui.parentui or parentui |
39 self.readhooks = list(parentui.readhooks or readhooks) |
38 self.readhooks = parentui.readhooks[:] |
40 parent_cdata = self.parentui.cdata |
39 parent_cdata = self.parentui.cdata |
41 self.cdata = ConfigParser.SafeConfigParser(parent_cdata.defaults()) |
40 self.cdata = ConfigParser.SafeConfigParser(parent_cdata.defaults()) |
42 # make interpolation work |
41 # make interpolation work |
43 for section in parent_cdata.sections(): |
42 for section in parent_cdata.sections(): |
44 self.cdata.add_section(section) |
43 self.cdata.add_section(section) |
49 return getattr(self.parentui, key) |
48 return getattr(self.parentui, key) |
50 |
49 |
51 def updateopts(self, verbose=False, debug=False, quiet=False, |
50 def updateopts(self, verbose=False, debug=False, quiet=False, |
52 interactive=True, traceback=False, config=[]): |
51 interactive=True, traceback=False, config=[]): |
53 self.quiet = (self.quiet or quiet) and not verbose and not debug |
52 self.quiet = (self.quiet or quiet) and not verbose and not debug |
54 self.verbose = (self.verbose or verbose) or debug |
53 self.verbose = ((self.verbose or verbose) or debug) and not self.quiet |
55 self.debugflag = (self.debugflag or debug) |
54 self.debugflag = (self.debugflag or debug) |
56 self.interactive = (self.interactive and interactive) |
55 self.interactive = (self.interactive and interactive) |
57 self.traceback = self.traceback or traceback |
56 self.traceback = self.traceback or traceback |
58 for cfg in config: |
57 for cfg in config: |
59 try: |
58 try: |
82 if path and "://" not in path and not os.path.isabs(path): |
81 if path and "://" not in path and not os.path.isabs(path): |
83 self.cdata.set("paths", name, os.path.join(root, path)) |
82 self.cdata.set("paths", name, os.path.join(root, path)) |
84 for hook in self.readhooks: |
83 for hook in self.readhooks: |
85 hook(self) |
84 hook(self) |
86 |
85 |
|
86 def addreadhook(self, hook): |
|
87 self.readhooks.append(hook) |
|
88 |
87 def setconfig(self, section, name, val): |
89 def setconfig(self, section, name, val): |
88 self.overlay[(section, name)] = val |
90 self.overlay[(section, name)] = val |
89 |
91 |
90 def config(self, section, name, default=None): |
92 def config(self, section, name, default=None): |
91 if self.overlay.has_key((section, name)): |
93 if self.overlay.has_key((section, name)): |
92 return self.overlay[(section, name)] |
94 return self.overlay[(section, name)] |
93 if self.cdata.has_option(section, name): |
95 if self.cdata.has_option(section, name): |
94 try: |
96 try: |
95 return self.cdata.get(section, name) |
97 return self.cdata.get(section, name) |
96 except ConfigParser.InterpolationError, inst: |
98 except ConfigParser.InterpolationError, inst: |
97 raise util.Abort(_("Error in configuration:\n%s") % inst) |
99 raise util.Abort(_("Error in configuration section [%s] " |
|
100 "parameter '%s':\n%s") |
|
101 % (section, name, inst)) |
98 if self.parentui is None: |
102 if self.parentui is None: |
99 return default |
103 return default |
100 else: |
104 else: |
101 return self.parentui.config(section, name, default) |
105 return self.parentui.config(section, name, default) |
102 |
106 |
114 return self.overlay[(section, name)] |
118 return self.overlay[(section, name)] |
115 if self.cdata.has_option(section, name): |
119 if self.cdata.has_option(section, name): |
116 try: |
120 try: |
117 return self.cdata.getboolean(section, name) |
121 return self.cdata.getboolean(section, name) |
118 except ConfigParser.InterpolationError, inst: |
122 except ConfigParser.InterpolationError, inst: |
119 raise util.Abort(_("Error in configuration:\n%s") % inst) |
123 raise util.Abort(_("Error in configuration section [%s] " |
|
124 "parameter '%s':\n%s") |
|
125 % (section, name, inst)) |
120 if self.parentui is None: |
126 if self.parentui is None: |
121 return default |
127 return default |
122 else: |
128 else: |
123 return self.parentui.configbool(section, name, default) |
129 return self.parentui.configbool(section, name, default) |
124 |
130 |
132 items = dict(self.parentui.configitems(section)) |
138 items = dict(self.parentui.configitems(section)) |
133 if self.cdata.has_section(section): |
139 if self.cdata.has_section(section): |
134 try: |
140 try: |
135 items.update(dict(self.cdata.items(section))) |
141 items.update(dict(self.cdata.items(section))) |
136 except ConfigParser.InterpolationError, inst: |
142 except ConfigParser.InterpolationError, inst: |
137 raise util.Abort(_("Error in configuration:\n%s") % inst) |
143 raise util.Abort(_("Error in configuration section [%s]:\n%s") |
|
144 % (section, inst)) |
138 x = items.items() |
145 x = items.items() |
139 x.sort() |
146 x.sort() |
140 return x |
147 return x |
141 |
148 |
142 def walkconfig(self, seen=None): |
149 def walkconfig(self, seen=None): |
144 seen = {} |
151 seen = {} |
145 for (section, name), value in self.overlay.iteritems(): |
152 for (section, name), value in self.overlay.iteritems(): |
146 yield section, name, value |
153 yield section, name, value |
147 seen[section, name] = 1 |
154 seen[section, name] = 1 |
148 for section in self.cdata.sections(): |
155 for section in self.cdata.sections(): |
149 for name, value in self.cdata.items(section): |
156 try: |
150 if (section, name) in seen: continue |
157 for name, value in self.cdata.items(section): |
151 yield section, name, value.replace('\n', '\\n') |
158 if (section, name) in seen: continue |
152 seen[section, name] = 1 |
159 yield section, name, value.replace('\n', '\\n') |
|
160 seen[section, name] = 1 |
|
161 except ConfigParser.InterpolationError, inst: |
|
162 raise util.Abort(_("Error in configuration section [%s]:\n%s") |
|
163 % (section, inst)) |
153 if self.parentui is not None: |
164 if self.parentui is not None: |
154 for parent in self.parentui.walkconfig(seen): |
165 for parent in self.parentui.walkconfig(seen): |
155 yield parent |
166 yield parent |
156 |
167 |
157 def extensions(self): |
168 def extensions(self): |