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, parser._quote(value), section_name,
447
502
def _get_option_policy(self, section, option_name):
448
503
"""Return the policy for the given (section, option_name) pair."""
449
504
return POLICY_NONE
667
750
self._write_config_file()
753
def _get_sections(self, name=None):
754
"""See IniBasedConfig._get_sections()."""
755
parser = self._get_parser()
756
# We don't give access to options defined outside of any section, we
757
# used the DEFAULT section by... default.
758
if name in (None, 'DEFAULT'):
759
# This could happen for an empty file where the DEFAULT section
760
# doesn't exist yet. So we force DEFAULT when yielding
762
if 'DEFAULT' not in parser:
763
parser['DEFAULT']= {}
764
yield (name, parser[name], self.config_id())
767
def remove_user_option(self, option_name, section_name=None):
768
if section_name is None:
769
# We need to force the default section.
770
section_name = 'DEFAULT'
771
# We need to avoid the LockableConfig implementation or we'll lock
773
super(LockableConfig, self).remove_user_option(option_name,
670
777
class LocationConfig(LockableConfig):
671
778
"""A configuration object that gives the policy for a location."""
1026
def _get_sections(self, name=None):
1027
"""See IniBasedConfig.get_sections()."""
1028
for source in self.option_sources:
1029
for section in source()._get_sections(name):
1032
def _get_options(self, sections=None):
1034
# First the locations options
1035
for option in self._get_location_config()._get_options():
1037
# Then the branch options
1038
branch_config = self._get_branch_data_config()
1039
if sections is None:
1040
sections = [('DEFAULT', branch_config._get_parser())]
1041
# FIXME: We shouldn't have to duplicate the code in IniBasedConfig but
1042
# Config itself has no notion of sections :( -- vila 20101001
1043
config_id = self.config_id()
1044
for (section_name, section) in sections:
1045
for (name, value) in section.iteritems():
1046
yield (name, value, section_name,
1047
config_id, branch_config._get_parser())
1048
# Then the global options
1049
for option in self._get_global_config()._get_options():
903
1052
def set_user_option(self, name, value, store=STORE_BRANCH,
904
1053
warn_masked=False):
905
1054
if store == STORE_BRANCH:
1598
1769
configobj.write(out_file)
1599
1770
out_file.seek(0)
1600
1771
self._transport.put_file(self._filename, out_file)
1774
class cmd_config(commands.Command):
1775
__doc__ = """Display, set or remove a configuration option.
1777
Display the active value for a given option.
1779
If --all is specified, NAME is interpreted as a regular expression and all
1780
matching options are displayed mentioning their scope. The active value
1781
that bzr will take into account is the first one displayed for each option.
1783
If no NAME is given, --all .* is implied.
1785
Setting a value is achieved by using name=value without spaces. The value
1786
is set in the most relevant scope and can be checked by displaying the
1790
takes_args = ['name?']
1794
# FIXME: This should be a registry option so that plugins can register
1795
# their own config files (or not) -- vila 20101002
1796
commands.Option('scope', help='Reduce the scope to the specified'
1797
' configuration file',
1799
commands.Option('all',
1800
help='Display all the defined values for the matching options.',
1802
commands.Option('remove', help='Remove the option from'
1803
' the configuration file'),
1806
@commands.display_command
1807
def run(self, name=None, all=False, directory=None, scope=None,
1809
if directory is None:
1811
directory = urlutils.normalize_url(directory)
1813
raise errors.BzrError(
1814
'--all and --remove are mutually exclusive.')
1816
# Delete the option in the given scope
1817
self._remove_config_option(name, directory, scope)
1819
# Defaults to all options
1820
self._show_matching_options('.*', directory, scope)
1823
name, value = name.split('=', 1)
1825
# Display the option(s) value(s)
1827
self._show_matching_options(name, directory, scope)
1829
self._show_value(name, directory, scope)
1832
raise errors.BzrError(
1833
'Only one option can be set.')
1834
# Set the option value
1835
self._set_config_option(name, value, directory, scope)
1837
def _get_configs(self, directory, scope=None):
1838
"""Iterate the configurations specified by ``directory`` and ``scope``.
1840
:param directory: Where the configurations are derived from.
1842
:param scope: A specific config to start from.
1844
if scope is not None:
1845
if scope == 'bazaar':
1846
yield GlobalConfig()
1847
elif scope == 'locations':
1848
yield LocationConfig(directory)
1849
elif scope == 'branch':
1850
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
1852
yield br.get_config()
1855
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
1857
yield br.get_config()
1858
except errors.NotBranchError:
1859
yield LocationConfig(directory)
1860
yield GlobalConfig()
1862
def _show_value(self, name, directory, scope):
1864
for c in self._get_configs(directory, scope):
1867
for (oname, value, section, conf_id, parser) in c._get_options():
1869
# Display only the first value and exit
1871
# FIXME: We need to use get_user_option to take policies
1872
# into account and we need to make sure the option exists
1873
# too (hence the two for loops), this needs a better API
1875
value = c.get_user_option(name)
1876
# Quote the value appropriately
1877
value = parser._quote(value)
1878
self.outf.write('%s\n' % (value,))
1882
raise errors.NoSuchConfigOption(name)
1884
def _show_matching_options(self, name, directory, scope):
1885
name = re.compile(name)
1886
# We want any error in the regexp to be raised *now* so we need to
1887
# avoid the delay introduced by the lazy regexp.
1888
name._compile_and_collapse()
1891
for c in self._get_configs(directory, scope):
1892
for (oname, value, section, conf_id, parser) in c._get_options():
1893
if name.search(oname):
1894
if cur_conf_id != conf_id:
1895
# Explain where the options are defined
1896
self.outf.write('%s:\n' % (conf_id,))
1897
cur_conf_id = conf_id
1899
if (section not in (None, 'DEFAULT')
1900
and cur_section != section):
1901
# Display the section if it's not the default (or only)
1903
self.outf.write(' [%s]\n' % (section,))
1904
cur_section = section
1905
self.outf.write(' %s = %s\n' % (oname, value))
1907
def _set_config_option(self, name, value, directory, scope):
1908
for conf in self._get_configs(directory, scope):
1909
conf.set_user_option(name, value)
1912
raise errors.NoSuchConfig(scope)
1914
def _remove_config_option(self, name, directory, scope):
1916
raise errors.BzrCommandError(
1917
'--remove expects an option to remove.')
1919
for conf in self._get_configs(directory, scope):
1920
for (section_name, section, conf_id) in conf._get_sections():
1921
if scope is not None and conf_id != scope:
1922
# Not the right configuration file
1925
if conf_id != conf.config_id():
1926
conf = self._get_configs(directory, conf_id).next()
1927
# We use the first section in the first config where the
1928
# option is defined to remove it
1929
conf.remove_user_option(name, section_name)
1934
raise errors.NoSuchConfig(scope)
1936
raise errors.NoSuchConfigOption(name)