comparison mercurial/ui.py @ 8139:9302404b60f3

ui: always have ucdata simplify readconfig logic
author Matt Mackall <mpm@selenic.com>
date Thu, 23 Apr 2009 15:40:10 -0500
parents 0ffb8f791b7c
children 7c47ac9620a8
comparison
equal deleted inserted replaced
8138:0ffb8f791b7c 8139:9302404b60f3
33 # this is the parent of all ui children 33 # this is the parent of all ui children
34 self.parentui = None 34 self.parentui = None
35 self.trusted_users = {} 35 self.trusted_users = {}
36 self.trusted_groups = {} 36 self.trusted_groups = {}
37 self.overlay = util.configparser() 37 self.overlay = util.configparser()
38 # if ucdata is not None, its keys must be a superset of cdata's
39 self.cdata = util.configparser() 38 self.cdata = util.configparser()
40 self.ucdata = None 39 self.ucdata = util.configparser()
41 # we always trust global config files 40 # we always trust global config files
42 self.readconfig(util.rcpath(), assumetrusted=True) 41 self.readconfig(util.rcpath(), assumetrusted=True)
43 else: 42 else:
44 # parentui may point to an ui object which is already a child 43 # parentui may point to an ui object which is already a child
45 self.parentui = parentui.parentui or parentui 44 self.parentui = parentui.parentui or parentui
46 self.trusted_users = parentui.trusted_users.copy() 45 self.trusted_users = parentui.trusted_users.copy()
47 self.trusted_groups = parentui.trusted_groups.copy() 46 self.trusted_groups = parentui.trusted_groups.copy()
48 self.cdata = dupconfig(self.parentui.cdata) 47 self.cdata = dupconfig(self.parentui.cdata)
49 self.overlay = dupconfig(self.parentui.overlay) 48 self.overlay = dupconfig(self.parentui.overlay)
50 if self.parentui.ucdata: 49 self.ucdata = dupconfig(self.parentui.ucdata)
51 self.ucdata = dupconfig(self.parentui.ucdata)
52 if self.parentui is not parentui: 50 if self.parentui is not parentui:
53 self.overlay = util.configparser() 51 self.overlay = util.configparser()
54 updateconfig(parentui.overlay, self.overlay) 52 updateconfig(parentui.overlay, self.overlay)
55 self.buffers = parentui.buffers 53 self.buffers = parentui.buffers
56 54
86 'user %s, group %s\n') % (f, user, group)) 84 'user %s, group %s\n') % (f, user, group))
87 return False 85 return False
88 return True 86 return True
89 87
90 def readconfig(self, fn, root=None, assumetrusted=False): 88 def readconfig(self, fn, root=None, assumetrusted=False):
89 cdata = util.configparser()
90
91 if isinstance(fn, basestring): 91 if isinstance(fn, basestring):
92 fn = [fn] 92 fn = [fn]
93 for f in fn: 93 for f in fn:
94 try: 94 try:
95 fp = open(f) 95 fp = open(f)
96 except IOError: 96 except IOError:
97 continue 97 continue
98 cdata = self.cdata 98
99 trusted = assumetrusted or self._is_trusted(fp, f) 99 trusted = assumetrusted or self._is_trusted(fp, f)
100 if not trusted:
101 if self.ucdata is None:
102 self.ucdata = dupconfig(self.cdata)
103 cdata = self.ucdata
104 elif self.ucdata is not None:
105 # use a separate configparser, so that we don't accidentally
106 # override ucdata settings later on.
107 cdata = util.configparser()
108 100
109 try: 101 try:
110 cdata.readfp(fp, f) 102 cdata.readfp(fp, f)
111 except ConfigParser.ParsingError, inst: 103 except ConfigParser.ParsingError, inst:
112 msg = _("Failed to parse %s\n%s") % (f, inst) 104 msg = _("Failed to parse %s\n%s") % (f, inst)
113 if trusted: 105 if trusted:
114 raise util.Abort(msg) 106 raise util.Abort(msg)
115 self.warn(_("Ignored: %s\n") % msg) 107 self.warn(_("Ignored: %s\n") % msg)
116 108
117 if trusted: 109 if trusted:
118 if cdata != self.cdata: 110 updateconfig(cdata, self.cdata)
119 updateconfig(cdata, self.cdata) 111 updateconfig(self.overlay, self.cdata)
120 if self.ucdata is not None: 112 updateconfig(cdata, self.ucdata)
121 updateconfig(cdata, self.ucdata) 113 updateconfig(self.overlay, self.ucdata)
122 # override data from config files with data set with ui.setconfig 114
123 updateconfig(self.overlay, self.cdata)
124 if root is None: 115 if root is None:
125 root = os.path.expanduser('~') 116 root = os.path.expanduser('~')
126 self.fixconfig(root=root) 117 self.fixconfig(root=root)
127 118
128 def readsections(self, filename, *sections): 119 def readsections(self, filename, *sections):
150 for section in sections: 141 for section in sections:
151 if not cdata.has_section(section): 142 if not cdata.has_section(section):
152 cdata.add_section(section) 143 cdata.add_section(section)
153 144
154 updateconfig(cdata, self.cdata, sections) 145 updateconfig(cdata, self.cdata, sections)
155 if self.ucdata: 146 updateconfig(cdata, self.ucdata, sections)
156 updateconfig(cdata, self.ucdata, sections)
157 147
158 def fixconfig(self, section=None, name=None, value=None, root=None): 148 def fixconfig(self, section=None, name=None, value=None, root=None):
159 # translate paths relative to root (or home) into absolute paths 149 # translate paths relative to root (or home) into absolute paths
160 if section is None or section == 'paths': 150 if section is None or section == 'paths':
161 if root is None: 151 if root is None:
162 root = os.getcwd() 152 root = os.getcwd()
163 items = section and [(name, value)] or [] 153 items = section and [(name, value)] or []
164 for cdata in self.cdata, self.ucdata, self.overlay: 154 for cdata in self.cdata, self.ucdata, self.overlay:
165 if not cdata: continue
166 if not items and cdata.has_section('paths'): 155 if not items and cdata.has_section('paths'):
167 pathsitems = cdata.items('paths') 156 pathsitems = cdata.items('paths')
168 else: 157 else:
169 pathsitems = items 158 pathsitems = items
170 for n, path in pathsitems: 159 for n, path in pathsitems:
193 for group in self.configlist('trusted', 'groups'): 182 for group in self.configlist('trusted', 'groups'):
194 self.trusted_groups[group] = 1 183 self.trusted_groups[group] = 1
195 184
196 def setconfig(self, section, name, value): 185 def setconfig(self, section, name, value):
197 for cdata in (self.overlay, self.cdata, self.ucdata): 186 for cdata in (self.overlay, self.cdata, self.ucdata):
198 if not cdata: continue
199 if not cdata.has_section(section): 187 if not cdata.has_section(section):
200 cdata.add_section(section) 188 cdata.add_section(section)
201 cdata.set(section, name, value) 189 cdata.set(section, name, value)
202 self.fixconfig(section, name, value) 190 self.fixconfig(section, name, value)
203 191
204 def _get_cdata(self, untrusted): 192 def _get_cdata(self, untrusted):
205 if untrusted and self.ucdata: 193 if untrusted:
206 return self.ucdata 194 return self.ucdata
207 return self.cdata 195 return self.cdata
208 196
209 def _config(self, section, name, default, funcname, untrusted, abort): 197 def _config(self, section, name, default, funcname, untrusted, abort):
210 cdata = self._get_cdata(untrusted) 198 cdata = self._get_cdata(untrusted)
221 return default 209 return default
222 210
223 def _configcommon(self, section, name, default, funcname, untrusted): 211 def _configcommon(self, section, name, default, funcname, untrusted):
224 value = self._config(section, name, default, funcname, 212 value = self._config(section, name, default, funcname,
225 untrusted, abort=True) 213 untrusted, abort=True)
226 if self.debugflag and not untrusted and self.ucdata: 214 if self.debugflag and not untrusted:
227 uvalue = self._config(section, name, None, funcname, 215 uvalue = self._config(section, name, None, funcname,
228 untrusted=True, abort=False) 216 untrusted=True, abort=False)
229 if uvalue is not None and uvalue != value: 217 if uvalue is not None and uvalue != value:
230 self.warn(_("Ignoring untrusted configuration option " 218 self.warn(_("Ignoring untrusted configuration option "
231 "%s.%s = %s\n") % (section, name, uvalue)) 219 "%s.%s = %s\n") % (section, name, uvalue))
266 self.warn(_("Ignored: %s\n") % msg) 254 self.warn(_("Ignored: %s\n") % msg)
267 return items 255 return items
268 256
269 def configitems(self, section, untrusted=False): 257 def configitems(self, section, untrusted=False):
270 items = self._configitems(section, untrusted=untrusted, abort=True) 258 items = self._configitems(section, untrusted=untrusted, abort=True)
271 if self.debugflag and not untrusted and self.ucdata: 259 if self.debugflag and not untrusted:
272 uitems = self._configitems(section, untrusted=True, abort=False) 260 uitems = self._configitems(section, untrusted=True, abort=False)
273 for k in util.sort(uitems): 261 for k in util.sort(uitems):
274 if uitems[k] != items.get(k): 262 if uitems[k] != items.get(k):
275 self.warn(_("Ignoring untrusted configuration option " 263 self.warn(_("Ignoring untrusted configuration option "
276 "%s.%s = %s\n") % (section, k, uitems[k])) 264 "%s.%s = %s\n") % (section, k, uitems[k]))