444
450
"""Override this to define the section used by the config."""
453
def _get_sections(self, name=None):
454
"""Returns an iterator of the sections specified by ``name``.
456
:param name: The section name. If None is supplied, the default
457
configurations are yielded.
459
:return: A tuple (name, section, config_id) for all sections that will
460
be walked by user_get_option() in the 'right' order. The first one
461
is where set_user_option() will update the value.
463
parser = self._get_parser()
465
yield (name, parser[name], self.config_id())
467
# No section name has been given so we fallback to the configobj
468
# itself which holds the variables defined outside of any section.
469
yield (None, parser, self.config_id())
471
def _get_options(self, sections=None):
472
"""Return an ordered list of (name, value, section, config_id) tuples.
474
All options are returned with their associated value and the section
475
they appeared in. ``config_id`` is a unique identifier for the
476
configuration file the option is defined in.
478
:param sections: Default to ``_get_matching_sections`` if not
479
specified. This gives a better control to daughter classes about
480
which sections should be searched. This is a list of (name,
485
parser = self._get_parser()
487
for (section_name, _) in self._get_matching_sections():
489
section = parser[section_name]
491
# This could happen for an empty file for which we define a
492
# DEFAULT section. FIXME: Force callers to provide sections
493
# instead ? -- vila 20100930
495
sections.append((section_name, section))
496
config_id = self.config_id()
497
for (section_name, section) in sections:
498
for (name, value) in section.iteritems():
499
yield (name, value, section_name, config_id)
447
501
def _get_option_policy(self, section, option_name):
448
502
"""Return the policy for the given (section, option_name) pair."""
449
503
return POLICY_NONE
536
590
def _get_nickname(self):
537
591
return self.get_user_option('nickname')
593
def remove_user_option(self, option_name, section_name=None):
594
"""Remove a user option and save the configuration file.
596
:param option_name: The option to be removed.
598
:param section_name: The section the option is defined in, default to
602
parser = self._get_parser()
603
if section_name is None:
606
section = parser[section_name]
608
del section[option_name]
610
raise errors.NoSuchConfigOption(option_name)
611
self._write_config_file()
539
613
def _write_config_file(self):
540
614
if self.file_name is None:
541
615
raise AssertionError('We cannot save, self.file_name is None')
667
749
self._write_config_file()
752
def _get_sections(self, name=None):
753
"""See IniBasedConfig._get_sections()."""
754
parser = self._get_parser()
755
# We don't give access to options defined outside of any section, we
756
# used the DEFAULT section by... default.
757
if name in (None, 'DEFAULT'):
758
# This could happen for an empty file where the DEFAULT section
759
# doesn't exist yet. So we force DEFAULT when yielding
761
if 'DEFAULT' not in parser:
762
parser['DEFAULT']= {}
763
yield (name, parser[name], self.config_id())
766
def remove_user_option(self, option_name, section_name=None):
767
if section_name is None:
768
# We need to force the default section.
769
section_name = 'DEFAULT'
770
# We need to avoid the LockableConfig implementation or we'll lock
772
super(LockableConfig, self).remove_user_option(option_name,
670
776
class LocationConfig(LockableConfig):
671
777
"""A configuration object that gives the policy for a location."""
853
def _get_sections(self, name=None):
854
"""See IniBasedConfig._get_sections()."""
855
# We ignore the name here as the only sections handled are named with
856
# the location path and we don't expose embedded sections either.
857
parser = self._get_parser()
858
for name, extra_path in self._get_matching_sections():
859
yield (name, parser[name], self.config_id())
743
861
def _get_option_policy(self, section, option_name):
744
862
"""Return the policy for the given (section, option_name) pair."""
745
863
# check for the old 'recurse=False' flag
1025
def _get_sections(self, name=None):
1026
"""See IniBasedConfig.get_sections()."""
1027
for source in self.option_sources:
1028
for section in source()._get_sections(name):
1031
def _get_options(self, sections=None):
1033
# First the locations options
1034
for option in self._get_location_config()._get_options():
1036
# Then the branch options
1037
branch_config = self._get_branch_data_config()
1038
if sections is None:
1039
sections = [('DEFAULT', branch_config._get_parser())]
1040
# FIXME: We shouldn't have to duplicate the code in IniBasedConfig but
1041
# Config itself has no notion of sections :( -- vila 20101001
1042
config_id = self.config_id()
1043
for (section_name, section) in sections:
1044
for (name, value) in section.iteritems():
1045
yield (name, value, section_name, config_id)
1046
# Then the global options
1047
for option in self._get_global_config()._get_options():
903
1050
def set_user_option(self, name, value, store=STORE_BRANCH,
904
1051
warn_masked=False):
905
1052
if store == STORE_BRANCH:
1087
1237
def set_option(self, value, name, section=None):
1088
1238
"""Set a per-branch configuration option"""
1239
# FIXME: We shouldn't need to lock explicitly here but rather rely on
1240
# higher levels providing the right lock -- vila 20101004
1089
1241
self.branch.lock_write()
1091
1243
self._config.set_option(value, name, section)
1093
1245
self.branch.unlock()
1247
def remove_option(self, option_name, section_name=None):
1248
# FIXME: We shouldn't need to lock explicitly here but rather rely on
1249
# higher levels providing the right lock -- vila 20101004
1250
self.branch.lock_write()
1252
self._config.remove_option(option_name, section_name)
1254
self.branch.unlock()
1096
1257
class AuthenticationConfig(object):
1097
1258
"""The authentication configuration file based on a ini file.
1598
1767
configobj.write(out_file)
1599
1768
out_file.seek(0)
1600
1769
self._transport.put_file(self._filename, out_file)
1772
class cmd_config(commands.Command):
1773
__doc__ = """Display, set or remove a configuration option.
1775
Display the MATCHING configuration options mentioning their scope (the
1776
configuration file they are defined in). The active value that bzr will
1777
take into account is the first one displayed.
1779
Setting a value is achieved by using name=value without spaces. The value
1780
is set in the most relevant scope and can be checked by displaying the
1785
takes_args = ['matching?']
1789
# FIXME: This should be a registry option so that plugins can register
1790
# their own config files (or not) -- vila 20101002
1791
commands.Option('scope', help='Reduce the scope to the specified'
1792
' configuration file',
1794
commands.Option('remove', help='Remove the option from'
1795
' the configuration file'),
1798
@commands.display_command
1799
def run(self, matching=None, directory=None, scope=None, remove=False):
1800
if directory is None:
1802
directory = urlutils.normalize_url(directory)
1803
if matching is None:
1804
self._show_config('*', directory)
1807
self._remove_config_option(matching, directory, scope)
1809
pos = matching.find('=')
1811
self._show_config(matching, directory)
1813
self._set_config_option(matching[:pos], matching[pos+1:],
1816
def _get_configs(self, directory, scope=None):
1817
"""Iterate the configurations specified by ``directory`` and ``scope``.
1819
:param directory: Where the configurations are derived from.
1821
:param scope: A specific config to start from.
1823
if scope is not None:
1824
if scope == 'bazaar':
1825
yield GlobalConfig()
1826
elif scope == 'locations':
1827
yield LocationConfig(directory)
1828
elif scope == 'branch':
1829
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
1831
yield br.get_config()
1834
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
1836
yield br.get_config()
1837
except errors.NotBranchError:
1838
yield LocationConfig(directory)
1839
yield GlobalConfig()
1841
def _show_config(self, matching, directory):
1842
# Turn the glob into a regexp
1843
matching_re = re.compile(fnmatch.translate(matching))
1845
for c in self._get_configs(directory):
1846
for (name, value, section, conf_id) in c._get_options():
1847
if matching_re.search(name):
1848
if cur_conf_id != conf_id:
1849
self.outf.write('%s:\n' % (conf_id,))
1850
cur_conf_id = conf_id
1851
self.outf.write(' %s = %s\n' % (name, value))
1853
def _set_config_option(self, name, value, directory, scope):
1854
for conf in self._get_configs(directory, scope):
1855
conf.set_user_option(name, value)
1858
raise errors.NoSuchConfig(scope)
1860
def _remove_config_option(self, name, directory, scope):
1862
for conf in self._get_configs(directory, scope):
1863
for (section_name, section, conf_id) in conf._get_sections():
1864
if scope is not None and conf_id != scope:
1865
# Not the right configuration file
1868
if conf_id != conf.config_id():
1869
conf = self._get_configs(directory, conf_id).next()
1870
# We use the first section in the first config where the
1871
# option is defined to remove it
1872
conf.remove_user_option(name, section_name)
1877
raise errors.NoSuchConfig(scope)
1879
raise errors.NoSuchConfigOption(name)