/hgext/hgconfig.py
Python | 177 lines | 161 code | 2 blank | 14 comment | 3 complexity | 13786da0d744ea4f615dca4f38d226a1 MD5 | raw file
- # GUI configuration tools for Mercurial
- #
- # Copyright 2007 Steve Borho <steve@borho.org>
- #
- # This software may be used and distributed according to the terms
- # of the GNU General Public License, incorporated herein by reference.
- from mercurial import util
- # every command must take a ui and and repo as arguments.
- # opts is a dict where you can find other command line flags
- #
- # Other parameters are taken in order from items on the command line that
- # don't start with a dash. If no default value is given in the parameter list,
- # they are required.
- def config(ui, repo, *extras, **opts):
- """Apply a change or query a Mercurial configuration file.
- By default changes are applied globally to a user's ~/.hgrc (or
- equivalent) unless the --local flag is given, in which case the
- changes are applied to this repository's $(hg root)/.hg/hgrc
- file. The configuration file may also be explicitly specified
- via the --file argument.
- Examples:
- hg config --get --local paths.default
- hg config --get --bool diff.git
- hg config --local paths.default ~/clone-parent
- hg config ui.ssh "/path/to/ssh -C"
- hg config --file /etc/mercurial/hgrc extensions.hgext.mq
- To launch one of the provided dialogs:
- hg config [--local] username
- hg config paths # only operates on local repo
- hg config webconf # only operates on local repo
- hg config [--local] extensions
- The --get and --file options are by ignored dialog commands.
- Get commands return 0 if successful, a boolean 0 or 1 if
- --return-bool was specified, or 2 if an error occurred. The key
- value is returned on stdout and all warnings are piped to stderr.
- Note that 'hg config' is parsing a value from a particular file,
- while Mercurial parses a series of files at startup in order
- to read its configuration (man 5 hgrc). To see the actual value
- that Mercurial will use, use the 'hg showconfig' command.
- Set commands return 0 if successful, or 1 if a write failure
- occurred.
- """
- if len(extras) == 0 or len(extras) > 2:
- ui.warn('wrong number of arguments\n')
- return 2
- elif '.' not in extras[0]:
- if opts['remove_section']:
- from hgconf.config import rmconfig
- return rmconfig(ui, repo, extras[0], '', **opts)
- if extras[0] == "username" or extras[0] == "uname":
- _username(ui, repo, *extras, **opts)
- elif extras[0] == "webconf" or extras[0] == "web":
- _webconf(ui, repo, *extras, **opts)
- elif extras[0] == "paths":
- _paths(ui, repo, *extras, **opts)
- elif extras[0] == "extensions" or extras[0] == "ext":
- _extensions(ui, repo, *extras, **opts)
- else:
- ui.warn('unknown dialog %s\n' % extras[0])
- return 2
- elif opts['get'] or opts['remove']:
- if opts['remove_section']:
- ui.warn('invalid section name %s\n' % extras[0])
- return 2
- if len(extras) > 1:
- ui.warn('wrong number of arguments\n')
- return 2
- (section, name) = extras[0].split('.', 1)
- if opts['remove']:
- from hgconf.config import rmconfig
- return rmconfig(ui, repo, section, name, **opts)
- else:
- from hgconf.config import getconfig
- return getconfig(ui, repo, section, name, **opts)
- else:
- from hgconf.config import setconfig
- (section, name) = extras[0].split('.', 1)
- if len(extras) > 1:
- value = extras[1]
- else:
- value = ''
- return setconfig(ui, repo, section, name, value, **opts)
- return 0
- def _username(ui, repo, *extras, **opts):
- """Open dialog to configure username"""
- try:
- import gtk
- from hgconf.uname_gtk import UsernameDialogGtk
- ud = UsernameDialogGtk(ui, repo, opts['local'] is not None)
- gtk.main()
- except ImportError:
- try:
- import PyQt4.QtGui
- from hgconf.uname_qt import UsernameDialogQt
- app = PyQt4.QtGui.QApplication([])
- dialog = UsernameDialogQt(ui, repo, opts['local'] is not None)
- dialog.show()
- app.exec_()
- except ImportError:
- ui.warn('Unable to open username dialog\n')
- def _webconf(ui, repo, *extras, **opts):
- """Open dialog to configure repository [web]"""
- try:
- from PyQt4 import QtCore, QtGui
- from hgconf.webconf_qt import WebConfDialogQt
- app = QtGui.QApplication([])
- dialog = WebConfDialogQt(ui, repo)
- dialog.show()
- app.exec_()
- except ImportError:
- try:
- import gtk
- from hgconf.webconf_gtk import WebConfDialogGtk
- wd = WebConfDialogGtk(ui, repo)
- gtk.main()
- except ImportError:
- ui.warn('Unable to open web configuration dialog\n')
- def _paths(ui, repo, *extras, **opts):
- """Open dialog to configure repository [paths]"""
- try:
- import PyQt4.QtGui
- from hgconf.paths_qt import PathsDialogQt
- app = PyQt4.QtGui.QApplication([])
- dialog = PathsDialogQt(ui, repo)
- dialog.show()
- app.exec_()
- except ImportError:
- try:
- import gtk
- from hgconf.paths_gtk import PathsDialogGtk
- pd = PathsDialogGtk(ui, repo)
- gtk.main()
- except ImportError, e:
- ui.warn('Unable to open paths dialog\n')
- def _extensions(ui, repo, *extras, **opts):
- """Open dialog to configure [extensions]"""
- try:
- from PyQt4 import QtCore, QtGui
- from hgconf.extensions import ExtensionsDialog
- except ImportError, e:
- import sys
- print >>sys.stderr, "Unable to open extensions dialog"
- print >>sys.stderr, e
- else:
- import os
- app = QtGui.QApplication([])
- dialog = ExtensionsDialog(ui, repo, os.path.dirname(__file__), **opts)
- dialog.show()
- app.exec_()
- cmdtable = {
- "config" : (config,
- [('g', 'get', None, 'get value for specified key'),
- ('i', 'int', None, 'force retrieved value to be an integer'),
- ('b', 'bool', None, 'force retrieved value to be a 0 or 1'),
- ('B', 'return-bool', None, 'command returns 0 or 1 based on retrieved value'),
- ('l', 'local', None, 'operate on configuration of local repository'),
- ('r', 'remove', None, 'remove specified key from file'),
- ('', 'remove-section', None, 'remove specified section from file'),
- ('f', 'file', [], 'operate on specified config file')],
- "hg config [<file-option>] [-gibBr] section.name [newvalue]")
- }