~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

Merged bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1772
1772
class cmd_config(commands.Command):
1773
1773
    __doc__ = """Display, set or remove a configuration option.
1774
1774
 
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.
 
1775
    Display the active value for a given option.
 
1776
 
 
1777
    If --all is specified, NAME is interpreted as a regular expression and all
 
1778
    matching options are displayed mentioning their scope. The active value
 
1779
    that bzr will take into account is the first one displayed for each option.
 
1780
 
 
1781
    If no NAME is given, --all .* is implied.
1778
1782
 
1779
1783
    Setting a value is achieved by using name=value without spaces. The value
1780
1784
    is set in the most relevant scope and can be checked by displaying the
1781
1785
    option again.
1782
1786
    """
1783
1787
 
1784
 
    aliases = ['conf']
1785
 
    takes_args = ['matching?']
 
1788
    takes_args = ['name?']
1786
1789
 
1787
1790
    takes_options = [
1788
1791
        'directory',
1791
1794
        commands.Option('scope', help='Reduce the scope to the specified'
1792
1795
                        ' configuration file',
1793
1796
                        type=unicode),
 
1797
        commands.Option('all',
 
1798
            help='Display all the defined values for the matching options.',
 
1799
            ),
1794
1800
        commands.Option('remove', help='Remove the option from'
1795
1801
                        ' the configuration file'),
1796
1802
        ]
1797
1803
 
1798
1804
    @commands.display_command
1799
 
    def run(self, matching=None, directory=None, scope=None, remove=False):
 
1805
    def run(self, name=None, all=False, directory=None, scope=None,
 
1806
            remove=False):
1800
1807
        if directory is None:
1801
1808
            directory = '.'
1802
1809
        directory = urlutils.normalize_url(directory)
1803
 
        if matching is None:
1804
 
            self._show_config('*', directory)
 
1810
        if remove and all:
 
1811
            raise errors.BzrError(
 
1812
                '--all and --remove are mutually exclusive.')
 
1813
        elif remove:
 
1814
            # Delete the option in the given scope
 
1815
            self._remove_config_option(name, directory, scope)
 
1816
        elif name is None:
 
1817
            # Defaults to all options
 
1818
            self._show_matching_options('.*', directory, scope)
1805
1819
        else:
1806
 
            if remove:
1807
 
                self._remove_config_option(matching, directory, scope)
 
1820
            try:
 
1821
                name, value = name.split('=', 1)
 
1822
            except ValueError:
 
1823
                # Display the option(s) value(s)
 
1824
                if all:
 
1825
                    self._show_matching_options(name, directory, scope)
 
1826
                else:
 
1827
                    self._show_value(name, directory, scope)
1808
1828
            else:
1809
 
                pos = matching.find('=')
1810
 
                if pos == -1:
1811
 
                    self._show_config(matching, directory)
1812
 
                else:
1813
 
                    self._set_config_option(matching[:pos], matching[pos+1:],
1814
 
                                            directory, scope)
 
1829
                if all:
 
1830
                    raise errors.BzrError(
 
1831
                        'Only one option can be set.')
 
1832
                # Set the option value
 
1833
                self._set_config_option(name, value, directory, scope)
1815
1834
 
1816
1835
    def _get_configs(self, directory, scope=None):
1817
1836
        """Iterate the configurations specified by ``directory`` and ``scope``.
1838
1857
                yield LocationConfig(directory)
1839
1858
                yield GlobalConfig()
1840
1859
 
1841
 
    def _show_config(self, matching, directory):
1842
 
        # Turn the glob into a regexp
1843
 
        matching_re = re.compile(fnmatch.translate(matching))
 
1860
    def _show_value(self, name, directory, scope):
 
1861
        displayed = False
 
1862
        for c in self._get_configs(directory, scope):
 
1863
            if displayed:
 
1864
                break
 
1865
            for (oname, value, section, conf_id) in c._get_options():
 
1866
                if name == oname:
 
1867
                    # Display only the first value and exit
 
1868
                    self.outf.write('%s\n' % (value))
 
1869
                    displayed = True
 
1870
                    break
 
1871
        if not displayed:
 
1872
            raise errors.NoSuchConfigOption(name)
 
1873
 
 
1874
    def _show_matching_options(self, name, directory, scope):
 
1875
        name = re.compile(name)
 
1876
        # We want any error in the regexp to be raised *now* so we need to
 
1877
        # avoid the delay introduced by the lazy regexp.
 
1878
        name._compile_and_collapse()
1844
1879
        cur_conf_id = None
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):
 
1880
        for c in self._get_configs(directory, scope):
 
1881
            for (oname, value, section, conf_id) in c._get_options():
 
1882
                if name.search(oname):
1848
1883
                    if cur_conf_id != conf_id:
 
1884
                        # Explain where the options are defined
1849
1885
                        self.outf.write('%s:\n' % (conf_id,))
1850
1886
                        cur_conf_id = conf_id
1851
 
                    self.outf.write('  %s = %s\n' % (name, value))
 
1887
                    self.outf.write('  %s = %s\n' % (oname, value))
1852
1888
 
1853
1889
    def _set_config_option(self, name, value, directory, scope):
1854
1890
        for conf in self._get_configs(directory, scope):
1858
1894
            raise errors.NoSuchConfig(scope)
1859
1895
 
1860
1896
    def _remove_config_option(self, name, directory, scope):
 
1897
        if name is None:
 
1898
            raise errors.BzrCommandError(
 
1899
                '--remove expects an option to remove.')
1861
1900
        removed = False
1862
1901
        for conf in self._get_configs(directory, scope):
1863
1902
            for (section_name, section, conf_id) in conf._get_sections():