##// END OF EJS Templates
ui: fold readsections into readconfig...
Matt Mackall -
r8142:912bfef1 default
parent child Browse files
Show More
@@ -74,7 +74,7 b' def hook(ui, repo, hooktype, node=None, '
74 user = getpass.getuser()
74 user = getpass.getuser()
75 cfg = ui.config('acl', 'config')
75 cfg = ui.config('acl', 'config')
76 if cfg:
76 if cfg:
77 ui.readsections(cfg, 'acl.allow', 'acl.deny')
77 ui.readconfig(cfg, sections = ['acl.allow', 'acl.deny'])
78 allow = buildmatch(ui, repo, user, 'acl.allow')
78 allow = buildmatch(ui, repo, user, 'acl.allow')
79 deny = buildmatch(ui, repo, user, 'acl.deny')
79 deny = buildmatch(ui, repo, user, 'acl.deny')
80
80
@@ -139,7 +139,7 b' class bugzilla_2_16(object):'
139 timeout = int(self.ui.config('bugzilla', 'timeout', 5))
139 timeout = int(self.ui.config('bugzilla', 'timeout', 5))
140 usermap = self.ui.config('bugzilla', 'usermap')
140 usermap = self.ui.config('bugzilla', 'usermap')
141 if usermap:
141 if usermap:
142 self.ui.readsections(usermap, 'usermap')
142 self.ui.readconfig(usermap, 'usermap')
143 self.ui.note(_('connecting to %s:%s as %s, password %s\n') %
143 self.ui.note(_('connecting to %s:%s as %s, password %s\n') %
144 (host, db, user, '*' * len(passwd)))
144 (host, db, user, '*' * len(passwd)))
145 self.conn = MySQLdb.connect(host=host, user=user, passwd=passwd,
145 self.conn = MySQLdb.connect(host=host, user=user, passwd=passwd,
@@ -99,7 +99,7 b' class notifier(object):'
99 self.ui = ui
99 self.ui = ui
100 cfg = self.ui.config('notify', 'config')
100 cfg = self.ui.config('notify', 'config')
101 if cfg:
101 if cfg:
102 self.ui.readsections(cfg, 'usersubs', 'reposubs')
102 self.ui.readconfig(cfg, sections=['usersubs', 'reposubs'])
103 self.repo = repo
103 self.repo = repo
104 self.stripcount = int(self.ui.config('notify', 'strip', 0))
104 self.stripcount = int(self.ui.config('notify', 'strip', 0))
105 self.root = self.strip(self.repo.root)
105 self.root = self.strip(self.repo.root)
@@ -20,6 +20,8 b' def updateconfig(source, dest, sections='
20 for section in sections:
20 for section in sections:
21 if not dest.has_section(section):
21 if not dest.has_section(section):
22 dest.add_section(section)
22 dest.add_section(section)
23 if not source.has_section(section):
24 continue
23 for name, value in source.items(section, raw=True):
25 for name, value in source.items(section, raw=True):
24 dest.set(section, name, value)
26 dest.set(section, name, value)
25
27
@@ -39,7 +41,8 b' class ui(object):'
39 self.ucdata = util.configparser()
41 self.ucdata = util.configparser()
40
42
41 # we always trust global config files
43 # we always trust global config files
42 self.readconfig(util.rcpath(), assumetrusted=True)
44 for f in util.rcpath():
45 self.readconfig(f, assumetrusted=True)
43 else:
46 else:
44 # parentui may point to an ui object which is already a child
47 # parentui may point to an ui object which is already a child
45 self.parentui = parentui.parentui or parentui
48 self.parentui = parentui.parentui or parentui
@@ -51,6 +54,7 b' class ui(object):'
51
54
52 # we want the overlay from the parent, not the root
55 # we want the overlay from the parent, not the root
53 self.overlay = dupconfig(parentui.overlay)
56 self.overlay = dupconfig(parentui.overlay)
57 self.fixconfig()
54
58
55 def __getattr__(self, key):
59 def __getattr__(self, key):
56 return getattr(self.parentui, key)
60 return getattr(self.parentui, key)
@@ -84,66 +88,36 b' class ui(object):'
84 'user %s, group %s\n') % (f, user, group))
88 'user %s, group %s\n') % (f, user, group))
85 return False
89 return False
86
90
87 def readconfig(self, fn, root=None, assumetrusted=False):
91 def readconfig(self, filename, root=None, assumetrusted=False,
88 cdata = util.configparser()
92 sections = None):
93 try:
94 fp = open(filename)
95 except IOError:
96 if not sections: # ignore unless we were looking for something
97 return
98 raise
89
99
90 if isinstance(fn, basestring):
100 cdata = util.configparser()
91 fn = [fn]
101 trusted = sections or assumetrusted or self._is_trusted(fp, filename)
92 for f in fn:
93 try:
94 fp = open(f)
95 except IOError:
96 continue
97
98 trusted = assumetrusted or self._is_trusted(fp, f)
99
102
100 try:
103 try:
101 cdata.readfp(fp, f)
104 cdata.readfp(fp, filename)
102 except ConfigParser.ParsingError, inst:
105 except ConfigParser.ParsingError, inst:
103 msg = _("Failed to parse %s\n%s") % (f, inst)
106 msg = _("Failed to parse %s\n%s") % (filename, inst)
104 if trusted:
107 if trusted:
105 raise util.Abort(msg)
108 raise util.Abort(msg)
106 self.warn(_("Ignored: %s\n") % msg)
109 self.warn(_("Ignored: %s\n") % msg)
107
110
108 if trusted:
111 if trusted:
109 updateconfig(cdata, self.cdata)
112 updateconfig(cdata, self.cdata, sections)
110 updateconfig(self.overlay, self.cdata)
113 updateconfig(self.overlay, self.cdata, sections)
111 updateconfig(cdata, self.ucdata)
114 updateconfig(cdata, self.ucdata, sections)
112 updateconfig(self.overlay, self.ucdata)
115 updateconfig(self.overlay, self.ucdata, sections)
113
116
114 if root is None:
117 if root is None:
115 root = os.path.expanduser('~')
118 root = os.path.expanduser('~')
116 self.fixconfig(root=root)
119 self.fixconfig(root=root)
117
120
118 def readsections(self, filename, *sections):
119 """Read filename and add only the specified sections to the config data
120
121 The settings are added to the trusted config data.
122 """
123 if not sections:
124 return
125
126 cdata = util.configparser()
127 try:
128 try:
129 fp = open(filename)
130 except IOError, inst:
131 raise util.Abort(_("unable to open %s: %s") %
132 (filename, getattr(inst, "strerror", inst)))
133 try:
134 cdata.readfp(fp, filename)
135 finally:
136 fp.close()
137 except ConfigParser.ParsingError, inst:
138 raise util.Abort(_("failed to parse %s\n%s") % (filename, inst))
139
140 for section in sections:
141 if not cdata.has_section(section):
142 cdata.add_section(section)
143
144 updateconfig(cdata, self.cdata, sections)
145 updateconfig(cdata, self.ucdata, sections)
146
147 def fixconfig(self, section=None, name=None, value=None, root=None):
121 def fixconfig(self, section=None, name=None, value=None, root=None):
148 # translate paths relative to root (or home) into absolute paths
122 # translate paths relative to root (or home) into absolute paths
149 if section is None or section == 'paths':
123 if section is None or section == 'paths':
@@ -473,10 +473,10 b' adding foo/file.txt revisions'
473 adding quux/file.py revisions
473 adding quux/file.py revisions
474 added 3 changesets with 3 changes to 3 files
474 added 3 changesets with 3 changes to 3 files
475 calling hook pretxnchangegroup.acl: hgext.acl.hook
475 calling hook pretxnchangegroup.acl: hgext.acl.hook
476 error: pretxnchangegroup.acl hook failed: unable to open ../acl.config: No such file or directory
476 error: pretxnchangegroup.acl hook raised an exception: [Errno 2] No such file or directory: '../acl.config'
477 transaction abort!
477 transaction abort!
478 rollback completed
478 rollback completed
479 abort: unable to open ../acl.config: No such file or directory
479 abort: No such file or directory: ../acl.config
480 no rollback information available
480 no rollback information available
481 0:6675d58eff77
481 0:6675d58eff77
482
482
@@ -60,7 +60,7 b' EOF'
60
60
61 echo % fail for config file is missing
61 echo % fail for config file is missing
62 hg --cwd b rollback
62 hg --cwd b rollback
63 hg --cwd b pull ../a 2>&1 | grep 'unable to open.*\.notify\.conf' > /dev/null && echo pull failed
63 hg --cwd b pull ../a 2>&1 | grep 'error.*\.notify\.conf' > /dev/null && echo pull failed
64
64
65 touch "$HGTMP/.notify.conf"
65 touch "$HGTMP/.notify.conf"
66
66
@@ -133,13 +133,13 b" testui(user='abc', group='def', cuser=No"
133 print "# prints debug warnings"
133 print "# prints debug warnings"
134 u = testui(user='abc', group='def', cuser='foo', debug=True)
134 u = testui(user='abc', group='def', cuser='foo', debug=True)
135
135
136 print "# ui.readsections"
136 print "# ui.readconfig sections"
137 filename = 'foobar'
137 filename = 'foobar'
138 f = open(filename, 'w')
138 f = open(filename, 'w')
139 f.write('[foobar]\n')
139 f.write('[foobar]\n')
140 f.write('baz = quux\n')
140 f.write('baz = quux\n')
141 f.close()
141 f.close()
142 u.readsections(filename, 'foobar')
142 u.readconfig(filename, sections = ['foobar'])
143 print u.config('foobar', 'baz')
143 print u.config('foobar', 'baz')
144
144
145 print
145 print
@@ -165,7 +165,7 b' untrusted'
165 .Ignoring untrusted configuration option paths.local = /another/path
165 .Ignoring untrusted configuration option paths.local = /another/path
166 . local = /another/path
166 . local = /another/path
167
167
168 # ui.readsections
168 # ui.readconfig sections
169 quux
169 quux
170
170
171 # read trusted, untrusted, new ui, trusted
171 # read trusted, untrusted, new ui, trusted
General Comments 0
You need to be logged in to leave comments. Login now