comparison mercurial/ui.py @ 46823:f1f2961d2816

path: move handling of "default" (*) suboptions value inside __init__ With the introduction of `path://` scheme the handling of default value will need to be subtler. We do simple code movement first to clarify the future changes. Differential Revision: https://phab.mercurial-scm.org/D10260
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Thu, 18 Mar 2021 10:12:55 +0100
parents d4ba4d51f85f
children 57218b7ffb2a
comparison
equal deleted inserted replaced
46822:c71e8d9e7f2a 46823:f1f2961d2816
2188 """ 2188 """
2189 2189
2190 def __init__(self, ui): 2190 def __init__(self, ui):
2191 dict.__init__(self) 2191 dict.__init__(self)
2192 2192
2193 _path, base_sub_options = ui.configsuboptions(b'paths', b'*')
2194 for name, loc in ui.configitems(b'paths', ignoresub=True): 2193 for name, loc in ui.configitems(b'paths', ignoresub=True):
2195 # No location is the same as not existing. 2194 # No location is the same as not existing.
2196 if not loc: 2195 if not loc:
2197 continue 2196 continue
2198 loc, sub = ui.configsuboptions(b'paths', name) 2197 loc, sub_opts = ui.configsuboptions(b'paths', name)
2199 sub_opts = base_sub_options.copy()
2200 sub_opts.update(sub)
2201 self[name] = path(ui, name, rawloc=loc, suboptions=sub_opts) 2198 self[name] = path(ui, name, rawloc=loc, suboptions=sub_opts)
2202 self._default_sub_opts = base_sub_options
2203 2199
2204 def getpath(self, ui, name, default=None): 2200 def getpath(self, ui, name, default=None):
2205 """Return a ``path`` from a string, falling back to default. 2201 """Return a ``path`` from a string, falling back to default.
2206 2202
2207 ``name`` can be a named path or locations. Locations are filesystem 2203 ``name`` can be a named path or locations. Locations are filesystem
2232 return self[name] 2228 return self[name]
2233 except KeyError: 2229 except KeyError:
2234 # Try to resolve as a local path or URI. 2230 # Try to resolve as a local path or URI.
2235 try: 2231 try:
2236 # we pass the ui instance are warning might need to be issued 2232 # we pass the ui instance are warning might need to be issued
2237 return path( 2233 return path(ui, None, rawloc=name)
2238 ui, None, rawloc=name, suboptions=self._default_sub_opts
2239 )
2240 except ValueError: 2234 except ValueError:
2241 raise error.RepoError(_(b'repository %s does not exist') % name) 2235 raise error.RepoError(_(b'repository %s does not exist') % name)
2242 2236
2243 2237
2244 _pathsuboptions = {} 2238 _pathsuboptions = {}
2332 raise ValueError( 2326 raise ValueError(
2333 b'location is not a URL or path to a local ' 2327 b'location is not a URL or path to a local '
2334 b'repo: %s' % rawloc 2328 b'repo: %s' % rawloc
2335 ) 2329 )
2336 2330
2337 suboptions = suboptions or {} 2331 _path, sub_opts = ui.configsuboptions(b'paths', b'*')
2332 if suboptions is not None:
2333 sub_opts.update(suboptions)
2338 2334
2339 # Now process the sub-options. If a sub-option is registered, its 2335 # Now process the sub-options. If a sub-option is registered, its
2340 # attribute will always be present. The value will be None if there 2336 # attribute will always be present. The value will be None if there
2341 # was no valid sub-option. 2337 # was no valid sub-option.
2342 for suboption, (attr, func) in pycompat.iteritems(_pathsuboptions): 2338 for suboption, (attr, func) in pycompat.iteritems(_pathsuboptions):
2343 if suboption not in suboptions: 2339 if suboption not in sub_opts:
2344 setattr(self, attr, None) 2340 setattr(self, attr, None)
2345 continue 2341 continue
2346 2342
2347 value = func(ui, self, suboptions[suboption]) 2343 value = func(ui, self, sub_opts[suboption])
2348 setattr(self, attr, value) 2344 setattr(self, attr, value)
2349 2345
2350 def _isvalidlocalpath(self, path): 2346 def _isvalidlocalpath(self, path):
2351 """Returns True if the given path is a potentially valid repository. 2347 """Returns True if the given path is a potentially valid repository.
2352 This is its own function so that extensions can change the definition of 2348 This is its own function so that extensions can change the definition of