450
444
"""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,
502
447
def _get_option_policy(self, section, option_name):
503
448
"""Return the policy for the given (section, option_name) pair."""
504
449
return POLICY_NONE
750
667
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,
777
670
class LocationConfig(LockableConfig):
778
671
"""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():
1052
903
def set_user_option(self, name, value, store=STORE_BRANCH,
1053
904
warn_masked=False):
1054
905
if store == STORE_BRANCH:
1786
1598
configobj.write(out_file)
1787
1599
out_file.seek(0)
1788
1600
self._transport.put_file(self._filename, out_file)
1791
class cmd_config(commands.Command):
1792
__doc__ = """Display, set or remove a configuration option.
1794
Display the active value for a given option.
1796
If --all is specified, NAME is interpreted as a regular expression and all
1797
matching options are displayed mentioning their scope. The active value
1798
that bzr will take into account is the first one displayed for each option.
1800
If no NAME is given, --all .* is implied.
1802
Setting a value is achieved by using name=value without spaces. The value
1803
is set in the most relevant scope and can be checked by displaying the
1807
takes_args = ['name?']
1811
# FIXME: This should be a registry option so that plugins can register
1812
# their own config files (or not) -- vila 20101002
1813
commands.Option('scope', help='Reduce the scope to the specified'
1814
' configuration file',
1816
commands.Option('all',
1817
help='Display all the defined values for the matching options.',
1819
commands.Option('remove', help='Remove the option from'
1820
' the configuration file'),
1823
@commands.display_command
1824
def run(self, name=None, all=False, directory=None, scope=None,
1826
if directory is None:
1828
directory = urlutils.normalize_url(directory)
1830
raise errors.BzrError(
1831
'--all and --remove are mutually exclusive.')
1833
# Delete the option in the given scope
1834
self._remove_config_option(name, directory, scope)
1836
# Defaults to all options
1837
self._show_matching_options('.*', directory, scope)
1840
name, value = name.split('=', 1)
1842
# Display the option(s) value(s)
1844
self._show_matching_options(name, directory, scope)
1846
self._show_value(name, directory, scope)
1849
raise errors.BzrError(
1850
'Only one option can be set.')
1851
# Set the option value
1852
self._set_config_option(name, value, directory, scope)
1854
def _get_configs(self, directory, scope=None):
1855
"""Iterate the configurations specified by ``directory`` and ``scope``.
1857
:param directory: Where the configurations are derived from.
1859
:param scope: A specific config to start from.
1861
if scope is not None:
1862
if scope == 'bazaar':
1863
yield GlobalConfig()
1864
elif scope == 'locations':
1865
yield LocationConfig(directory)
1866
elif scope == 'branch':
1867
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
1869
yield br.get_config()
1872
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
1874
yield br.get_config()
1875
except errors.NotBranchError:
1876
yield LocationConfig(directory)
1877
yield GlobalConfig()
1879
def _show_value(self, name, directory, scope):
1881
for c in self._get_configs(directory, scope):
1884
for (oname, value, section, conf_id, parser) in c._get_options():
1886
# Display only the first value and exit
1888
# FIXME: We need to use get_user_option to take policies
1889
# into account and we need to make sure the option exists
1890
# too (hence the two for loops), this needs a better API
1892
value = c.get_user_option(name)
1893
# Quote the value appropriately
1894
value = parser._quote(value)
1895
self.outf.write('%s\n' % (value,))
1899
raise errors.NoSuchConfigOption(name)
1901
def _show_matching_options(self, name, directory, scope):
1902
name = re.compile(name)
1903
# We want any error in the regexp to be raised *now* so we need to
1904
# avoid the delay introduced by the lazy regexp.
1905
name._compile_and_collapse()
1908
for c in self._get_configs(directory, scope):
1909
for (oname, value, section, conf_id, parser) in c._get_options():
1910
if name.search(oname):
1911
if cur_conf_id != conf_id:
1912
# Explain where the options are defined
1913
self.outf.write('%s:\n' % (conf_id,))
1914
cur_conf_id = conf_id
1916
if (section not in (None, 'DEFAULT')
1917
and cur_section != section):
1918
# Display the section if it's not the default (or only)
1920
self.outf.write(' [%s]\n' % (section,))
1921
cur_section = section
1922
self.outf.write(' %s = %s\n' % (oname, value))
1924
def _set_config_option(self, name, value, directory, scope):
1925
for conf in self._get_configs(directory, scope):
1926
conf.set_user_option(name, value)
1929
raise errors.NoSuchConfig(scope)
1931
def _remove_config_option(self, name, directory, scope):
1933
raise errors.BzrCommandError(
1934
'--remove expects an option to remove.')
1936
for conf in self._get_configs(directory, scope):
1937
for (section_name, section, conf_id) in conf._get_sections():
1938
if scope is not None and conf_id != scope:
1939
# Not the right configuration file
1942
if conf_id != conf.config_id():
1943
conf = self._get_configs(directory, conf_id).next()
1944
# We use the first section in the first config where the
1945
# option is defined to remove it
1946
conf.remove_user_option(name, section_name)
1951
raise errors.NoSuchConfig(scope)
1953
raise errors.NoSuchConfigOption(name)