100
POLICY_APPENDPATH = 2
104
POLICY_NORECURSE: 'norecurse',
105
POLICY_APPENDPATH: 'appendpath',
110
'norecurse': POLICY_NORECURSE,
111
'appendpath': POLICY_APPENDPATH,
115
STORE_LOCATION = POLICY_NONE
116
STORE_LOCATION_NORECURSE = POLICY_NORECURSE
117
STORE_LOCATION_APPENDPATH = POLICY_APPENDPATH
122
96
class ConfigObj(configobj.ConfigObj):
124
98
def get_bool(self, section, key):
288
262
raise errors.ParseConfigError(e.errors, e.config.filename)
289
263
return self._parser
291
def _get_matching_sections(self):
292
"""Return an ordered list of (section_name, extra_path) pairs.
294
If the section contains inherited configuration, extra_path is
295
a string containing the additional path components.
297
section = self._get_section()
298
if section is not None:
299
return [(section, '')]
303
265
def _get_section(self):
304
266
"""Override this to define the section used by the config."""
307
def _get_option_policy(self, section, option_name):
308
"""Return the policy for the given (section, option_name) pair."""
311
269
def _get_signature_checking(self):
312
270
"""See Config._get_signature_checking."""
313
271
policy = self._get_user_option('check_signatures')
327
285
def _get_user_option(self, option_name):
328
286
"""See Config._get_user_option."""
329
for (section, extra_path) in self._get_matching_sections():
331
value = self._get_parser().get_value(section, option_name)
334
policy = self._get_option_policy(section, option_name)
335
if policy == POLICY_NONE:
337
elif policy == POLICY_NORECURSE:
338
# norecurse items only apply to the exact path
343
elif policy == POLICY_APPENDPATH:
345
value = urlutils.join(value, extra_path)
348
raise AssertionError('Unexpected config policy %r' % policy)
288
return self._get_parser().get_value(self._get_section(),
352
293
def _gpg_signing_command(self):
353
294
"""See Config.gpg_signing_command."""
445
386
location = urlutils.local_path_from_url(location)
446
387
self.location = location
448
def _get_matching_sections(self):
449
"""Return an ordered list of section names matching this location."""
389
def _get_section(self):
390
"""Get the section we should look in for config items.
392
Returns None if none exists.
393
TODO: perhaps return a NullSection that thunks through to the
450
396
sections = self._get_parser()
451
397
location_names = self.location.split('/')
452
398
if self.location.endswith('/'):
476
422
# if section is longer, no match.
477
423
if len(section_names) > len(location_names):
479
matches.append((len(section_names), section,
480
'/'.join(location_names[len(section_names):])))
425
# if path is longer, and recurse is not true, no match
426
if len(section_names) < len(location_names):
428
if not self._get_parser()[section].as_bool('recurse'):
432
matches.append((len(section_names), section))
481
435
matches.sort(reverse=True)
483
for (length, section, extra_path) in matches:
484
sections.append((section, extra_path))
485
# should we stop looking for parent configs here?
487
if self._get_parser()[section].as_bool('ignore_parents'):
493
def _get_option_policy(self, section, option_name):
494
"""Return the policy for the given (section, option_name) pair."""
495
# check for the old 'recurse=False' flag
497
recurse = self._get_parser()[section].as_bool('recurse')
501
return POLICY_NORECURSE
503
policy_key = option_name + ':policy'
505
policy_name = self._get_parser()[section][policy_key]
509
return _policy_value[policy_name]
511
def _set_option_policy(self, section, option_name, option_policy):
512
"""Set the policy for the given option name in the given section."""
513
# The old recurse=False option affects all options in the
514
# section. To handle multiple policies in the section, we
515
# need to convert it to a policy_norecurse key.
517
recurse = self._get_parser()[section].as_bool('recurse')
521
symbol_versioning.warn(
522
'The recurse option is deprecated as of 0.14. '
523
'The section "%s" has been converted to use policies.'
526
del self._get_parser()[section]['recurse']
528
for key in self._get_parser()[section].keys():
529
if not key.endswith(':policy'):
530
self._get_parser()[section][key +
531
':policy'] = 'norecurse'
533
policy_key = option_name + ':policy'
534
policy_name = _policy_name[option_policy]
535
if policy_name is not None:
536
self._get_parser()[section][policy_key] = policy_name
538
if policy_key in self._get_parser()[section]:
539
del self._get_parser()[section][policy_key]
541
def set_user_option(self, option, value, store=STORE_LOCATION):
438
def set_user_option(self, option, value):
542
439
"""Save option and its value in the configuration."""
543
assert store in [STORE_LOCATION,
544
STORE_LOCATION_NORECURSE,
545
STORE_LOCATION_APPENDPATH], 'bad storage policy'
546
440
# FIXME: RBC 20051029 This should refresh the parser and also take a
547
441
# file lock on locations.conf.
548
442
conf_dir = os.path.dirname(self._get_filename())
641
def set_user_option(self, name, value, store=STORE_BRANCH):
642
if store == STORE_BRANCH:
533
def set_user_option(self, name, value, local=False):
535
self._get_location_config().set_user_option(name, value)
643
537
self._get_branch_data_config().set_option(value, name)
644
elif store == STORE_GLOBAL:
645
self._get_global_config().set_user_option(name, value)
647
self._get_location_config().set_user_option(name, value, store)
649
540
def _gpg_signing_command(self):
650
541
"""See Config.gpg_signing_command."""
759
if sys.platform == 'win32':
760
name = win32utils.get_user_name_unicode()
762
raise errors.BzrError("Cannot autodetect user name.\n"
763
"Please, set your name with command like:\n"
764
'bzr whoami "Your Name <name@domain.com>"')
765
host = win32utils.get_host_name_unicode()
767
host = socket.gethostname()
768
return name, (name + '@' + host)
650
# XXX: Any good way to get real user name on win32?