PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/hgext/hgconfig.py

https://bitbucket.org/sborho/hgconfig
Python | 177 lines | 161 code | 2 blank | 14 comment | 3 complexity | 13786da0d744ea4f615dca4f38d226a1 MD5 | raw file
  1. # GUI configuration tools for Mercurial
  2. #
  3. # Copyright 2007 Steve Borho <steve@borho.org>
  4. #
  5. # This software may be used and distributed according to the terms
  6. # of the GNU General Public License, incorporated herein by reference.
  7. from mercurial import util
  8. # every command must take a ui and and repo as arguments.
  9. # opts is a dict where you can find other command line flags
  10. #
  11. # Other parameters are taken in order from items on the command line that
  12. # don't start with a dash. If no default value is given in the parameter list,
  13. # they are required.
  14. def config(ui, repo, *extras, **opts):
  15. """Apply a change or query a Mercurial configuration file.
  16. By default changes are applied globally to a user's ~/.hgrc (or
  17. equivalent) unless the --local flag is given, in which case the
  18. changes are applied to this repository's $(hg root)/.hg/hgrc
  19. file. The configuration file may also be explicitly specified
  20. via the --file argument.
  21. Examples:
  22. hg config --get --local paths.default
  23. hg config --get --bool diff.git
  24. hg config --local paths.default ~/clone-parent
  25. hg config ui.ssh "/path/to/ssh -C"
  26. hg config --file /etc/mercurial/hgrc extensions.hgext.mq
  27. To launch one of the provided dialogs:
  28. hg config [--local] username
  29. hg config paths # only operates on local repo
  30. hg config webconf # only operates on local repo
  31. hg config [--local] extensions
  32. The --get and --file options are by ignored dialog commands.
  33. Get commands return 0 if successful, a boolean 0 or 1 if
  34. --return-bool was specified, or 2 if an error occurred. The key
  35. value is returned on stdout and all warnings are piped to stderr.
  36. Note that 'hg config' is parsing a value from a particular file,
  37. while Mercurial parses a series of files at startup in order
  38. to read its configuration (man 5 hgrc). To see the actual value
  39. that Mercurial will use, use the 'hg showconfig' command.
  40. Set commands return 0 if successful, or 1 if a write failure
  41. occurred.
  42. """
  43. if len(extras) == 0 or len(extras) > 2:
  44. ui.warn('wrong number of arguments\n')
  45. return 2
  46. elif '.' not in extras[0]:
  47. if opts['remove_section']:
  48. from hgconf.config import rmconfig
  49. return rmconfig(ui, repo, extras[0], '', **opts)
  50. if extras[0] == "username" or extras[0] == "uname":
  51. _username(ui, repo, *extras, **opts)
  52. elif extras[0] == "webconf" or extras[0] == "web":
  53. _webconf(ui, repo, *extras, **opts)
  54. elif extras[0] == "paths":
  55. _paths(ui, repo, *extras, **opts)
  56. elif extras[0] == "extensions" or extras[0] == "ext":
  57. _extensions(ui, repo, *extras, **opts)
  58. else:
  59. ui.warn('unknown dialog %s\n' % extras[0])
  60. return 2
  61. elif opts['get'] or opts['remove']:
  62. if opts['remove_section']:
  63. ui.warn('invalid section name %s\n' % extras[0])
  64. return 2
  65. if len(extras) > 1:
  66. ui.warn('wrong number of arguments\n')
  67. return 2
  68. (section, name) = extras[0].split('.', 1)
  69. if opts['remove']:
  70. from hgconf.config import rmconfig
  71. return rmconfig(ui, repo, section, name, **opts)
  72. else:
  73. from hgconf.config import getconfig
  74. return getconfig(ui, repo, section, name, **opts)
  75. else:
  76. from hgconf.config import setconfig
  77. (section, name) = extras[0].split('.', 1)
  78. if len(extras) > 1:
  79. value = extras[1]
  80. else:
  81. value = ''
  82. return setconfig(ui, repo, section, name, value, **opts)
  83. return 0
  84. def _username(ui, repo, *extras, **opts):
  85. """Open dialog to configure username"""
  86. try:
  87. import gtk
  88. from hgconf.uname_gtk import UsernameDialogGtk
  89. ud = UsernameDialogGtk(ui, repo, opts['local'] is not None)
  90. gtk.main()
  91. except ImportError:
  92. try:
  93. import PyQt4.QtGui
  94. from hgconf.uname_qt import UsernameDialogQt
  95. app = PyQt4.QtGui.QApplication([])
  96. dialog = UsernameDialogQt(ui, repo, opts['local'] is not None)
  97. dialog.show()
  98. app.exec_()
  99. except ImportError:
  100. ui.warn('Unable to open username dialog\n')
  101. def _webconf(ui, repo, *extras, **opts):
  102. """Open dialog to configure repository [web]"""
  103. try:
  104. from PyQt4 import QtCore, QtGui
  105. from hgconf.webconf_qt import WebConfDialogQt
  106. app = QtGui.QApplication([])
  107. dialog = WebConfDialogQt(ui, repo)
  108. dialog.show()
  109. app.exec_()
  110. except ImportError:
  111. try:
  112. import gtk
  113. from hgconf.webconf_gtk import WebConfDialogGtk
  114. wd = WebConfDialogGtk(ui, repo)
  115. gtk.main()
  116. except ImportError:
  117. ui.warn('Unable to open web configuration dialog\n')
  118. def _paths(ui, repo, *extras, **opts):
  119. """Open dialog to configure repository [paths]"""
  120. try:
  121. import PyQt4.QtGui
  122. from hgconf.paths_qt import PathsDialogQt
  123. app = PyQt4.QtGui.QApplication([])
  124. dialog = PathsDialogQt(ui, repo)
  125. dialog.show()
  126. app.exec_()
  127. except ImportError:
  128. try:
  129. import gtk
  130. from hgconf.paths_gtk import PathsDialogGtk
  131. pd = PathsDialogGtk(ui, repo)
  132. gtk.main()
  133. except ImportError, e:
  134. ui.warn('Unable to open paths dialog\n')
  135. def _extensions(ui, repo, *extras, **opts):
  136. """Open dialog to configure [extensions]"""
  137. try:
  138. from PyQt4 import QtCore, QtGui
  139. from hgconf.extensions import ExtensionsDialog
  140. except ImportError, e:
  141. import sys
  142. print >>sys.stderr, "Unable to open extensions dialog"
  143. print >>sys.stderr, e
  144. else:
  145. import os
  146. app = QtGui.QApplication([])
  147. dialog = ExtensionsDialog(ui, repo, os.path.dirname(__file__), **opts)
  148. dialog.show()
  149. app.exec_()
  150. cmdtable = {
  151. "config" : (config,
  152. [('g', 'get', None, 'get value for specified key'),
  153. ('i', 'int', None, 'force retrieved value to be an integer'),
  154. ('b', 'bool', None, 'force retrieved value to be a 0 or 1'),
  155. ('B', 'return-bool', None, 'command returns 0 or 1 based on retrieved value'),
  156. ('l', 'local', None, 'operate on configuration of local repository'),
  157. ('r', 'remove', None, 'remove specified key from file'),
  158. ('', 'remove-section', None, 'remove specified section from file'),
  159. ('f', 'file', [], 'operate on specified config file')],
  160. "hg config [<file-option>] [-gibBr] section.name [newvalue]")
  161. }