changeset 8136:6b5522cb2ad2

ui: refactor option setting No more passing options as constructor keywords. Basic options are now always stored in the overlay for simplicity and consistency.
author Matt Mackall <mpm@selenic.com>
date Thu, 23 Apr 2009 15:40:10 -0500
parents 16771d7c64e2
children 7fd0616b3d80
files mercurial/dispatch.py mercurial/hgweb/hgweb_mod.py mercurial/hgweb/hgwebdir_mod.py mercurial/ui.py tests/test-non-interactive-wsgi tests/test-trusted.py tests/test-ui-verbosity
diffstat 7 files changed, 38 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/dispatch.py	Thu Apr 23 15:40:10 2009 -0500
+++ b/mercurial/dispatch.py	Thu Apr 23 15:40:10 2009 -0500
@@ -18,7 +18,9 @@
 def dispatch(args):
     "run the command specified in args"
     try:
-        u = _ui.ui(traceback='--traceback' in args)
+        u = _ui.ui()
+        if '--traceback' in args:
+            u.setconfig('ui', 'traceback', 'on')
     except util.Abort, inst:
         sys.stderr.write(_("abort: %s\n") % inst)
         return -1
@@ -256,7 +258,7 @@
     # (e.g. to change trust settings for reading .hg/hgrc)
     config = _earlygetopt(['--config'], args)
     if config:
-        ui.updateopts(config=_parseconfig(config))
+        ui.updateopts(_parseconfig(config))
 
     # check for cwd
     cwd = _earlygetopt(['--cwd'], args)
@@ -335,8 +337,14 @@
                 (t[4]-s[4], t[0]-s[0], t[2]-s[2], t[1]-s[1], t[3]-s[3]))
         atexit.register(print_time)
 
-    ui.updateopts(options["verbose"], options["debug"], options["quiet"],
-                 not options["noninteractive"], options["traceback"])
+    if options['verbose'] or options['debug'] or options['quiet']:
+        ui.setconfig('ui', 'verbose', str(bool(options['verbose'])))
+        ui.setconfig('ui', 'debug', str(bool(options['debug'])))
+        ui.setconfig('ui', 'quiet', str(bool(options['quiet'])))
+    if options['traceback']:
+        ui.setconfig('ui', 'traceback', 'on')
+    if options['noninteractive']:
+        ui.setconfig('ui', 'interactive', 'off')
 
     if options['help']:
         return commands.help_(ui, cmd, options['version'])
--- a/mercurial/hgweb/hgweb_mod.py	Thu Apr 23 15:40:10 2009 -0500
+++ b/mercurial/hgweb/hgweb_mod.py	Thu Apr 23 15:40:10 2009 -0500
@@ -25,7 +25,9 @@
 class hgweb(object):
     def __init__(self, repo, name=None):
         if isinstance(repo, str):
-            parentui = ui.ui(report_untrusted=False, interactive=False)
+            parentui = ui.ui()
+            parentui.setconfig('ui', 'report_untrusted', 'off')
+            parentui.setconfig('ui', 'interactive', 'off')
             self.repo = hg.repository(parentui, repo)
         else:
             self.repo = repo
--- a/mercurial/hgweb/hgwebdir_mod.py	Thu Apr 23 15:40:10 2009 -0500
+++ b/mercurial/hgweb/hgwebdir_mod.py	Thu Apr 23 15:40:10 2009 -0500
@@ -21,8 +21,13 @@
             return [(util.pconvert(name).strip('/'), path)
                     for name, path in items]
 
-        self.parentui = parentui or ui.ui(report_untrusted=False,
-                                          interactive = False)
+        if parentui:
+           self.parentui = parentui
+        else:
+            self.parentui = ui.ui()
+            self.parentui.setconfig('ui', 'report_untrusted', 'off')
+            self.parentui.setconfig('ui', 'interactive', 'off')
+
         self.motd = None
         self.style = 'paper'
         self.stripecount = None
--- a/mercurial/ui.py	Thu Apr 23 15:40:10 2009 -0500
+++ b/mercurial/ui.py	Thu Apr 23 15:40:10 2009 -0500
@@ -24,19 +24,14 @@
             dest.set(section, name, value)
 
 class ui(object):
-    def __init__(self, verbose=False, debug=False, quiet=False,
-                 interactive=True, traceback=False, report_untrusted=True,
-                 parentui=None):
+    def __init__(self, parentui=None):
         self.buffers = []
+        self.quiet = self.verbose = self.debugflag = self.traceback = False
+        self.interactive = self.report_untrusted = True
+
         if parentui is None:
             # this is the parent of all ui children
             self.parentui = None
-            self.quiet = quiet
-            self.verbose = verbose
-            self.debugflag = debug
-            self.interactive = interactive
-            self.traceback = traceback
-            self.report_untrusted = report_untrusted
             self.trusted_users = {}
             self.trusted_groups = {}
             self.overlay = util.configparser()
@@ -45,7 +40,6 @@
             self.ucdata = None
             # we always trust global config files
             self.readconfig(util.rcpath(), assumetrusted=True)
-            self.updateopts(verbose, debug, quiet, interactive)
         else:
             # parentui may point to an ui object which is already a child
             self.parentui = parentui.parentui or parentui
@@ -66,25 +60,16 @@
     _isatty = None
     def isatty(self):
         if ui._isatty is None:
-            ui._isatty = sys.stdin.isatty()
+            try:
+                ui._isatty = sys.stdin.isatty()
+            except AttributeError: # not a real file object
+                ui._isatty = False
         return ui._isatty
 
-    def updateopts(self, verbose=False, debug=False, quiet=False,
-                   interactive=True, traceback=False, config=[]):
+    def updateopts(self, config):
         for section, name, value in config:
             self.setconfig(section, name, value)
 
-        if quiet or verbose or debug:
-            self.setconfig('ui', 'quiet', str(bool(quiet)))
-            self.setconfig('ui', 'verbose', str(bool(verbose)))
-            self.setconfig('ui', 'debug', str(bool(debug)))
-
-        if not interactive:
-            self.setconfig('ui', 'interactive', 'False')
-            self.interactive = False
-
-        self.traceback = self.traceback or traceback
-
     def verbosity_constraints(self):
         self.quiet = self.configbool('ui', 'quiet')
         self.verbose = self.configbool('ui', 'verbose')
@@ -215,6 +200,7 @@
             if name is None or name == 'report_untrusted':
                 self.report_untrusted = (
                     self.configbool("ui", "report_untrusted", True))
+            self.traceback = self.configbool('ui', 'traceback', False)
 
         # update trust information
         if (section is None or section == 'trusted') and self.trusted_users:
--- a/tests/test-non-interactive-wsgi	Thu Apr 23 15:40:10 2009 -0500
+++ b/tests/test-non-interactive-wsgi	Thu Apr 23 15:40:10 2009 -0500
@@ -30,9 +30,6 @@
     def readline(self):
         print >> sys.__stdout__, 'READLINE'
         return self.real.readline()
-    def isatty(self):
-        print >> sys.__stdout__, 'ISATTY'
-        return False
 
 sys.stdin = FileLike(sys.stdin)
 errors = StringIO()
--- a/tests/test-trusted.py	Thu Apr 23 15:40:10 2009 -0500
+++ b/tests/test-trusted.py	Thu Apr 23 15:40:10 2009 -0500
@@ -60,7 +60,7 @@
                                      trusted)
 
     parentui = ui.ui()
-    parentui.updateopts(debug=debug)
+    parentui.setconfig('ui', 'debug', str(bool(debug)))
     u = ui.ui(parentui=parentui)
     u.readconfig('.hg/hgrc')
     if silent:
@@ -145,7 +145,7 @@
 print
 print "# read trusted, untrusted, new ui, trusted"
 u = ui.ui()
-u.updateopts(debug=True)
+u.setconfig('ui', 'debug', 'on')
 u.readconfig(filename)
 u2 = ui.ui(parentui=u)
 def username(uid=None):
--- a/tests/test-ui-verbosity	Thu Apr 23 15:40:10 2009 -0500
+++ b/tests/test-ui-verbosity	Thu Apr 23 15:40:10 2009 -0500
@@ -31,7 +31,10 @@
     f.close()
 
     u = ui.ui()
-    u.updateopts(quiet=cmd_quiet, verbose=cmd_verbose, debug=cmd_debug)
+    if cmd_quiet or cmd_debug or cmd_verbose:
+        u.setconfig('ui', 'quiet', str(bool(cmd_quiet)))
+        u.setconfig('ui', 'verbose', str(bool(cmd_verbose)))
+        u.setconfig('ui', 'debug', str(bool(cmd_debug)))
 
     check = ''
     if u.debugflag: