# HG changeset patch # User Matt Mackall # Date 2009-04-23 20:40:10 # Node ID 6b5522cb2ad29601a9e31e3e6d191b37c9aba820 # Parent 16771d7c64e2ee81c90306e3c10f6c731202bc91 ui: refactor option setting No more passing options as constructor keywords. Basic options are now always stored in the overlay for simplicity and consistency. diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py --- a/mercurial/dispatch.py +++ b/mercurial/dispatch.py @@ -18,7 +18,9 @@ def run(): 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 @@ def _dispatch(ui, args): # (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 @@ def _dispatch(ui, args): (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']) diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py +++ b/mercurial/hgweb/hgweb_mod.py @@ -25,7 +25,9 @@ perms = { 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 diff --git a/mercurial/hgweb/hgwebdir_mod.py b/mercurial/hgweb/hgwebdir_mod.py --- a/mercurial/hgweb/hgwebdir_mod.py +++ b/mercurial/hgweb/hgwebdir_mod.py @@ -21,8 +21,13 @@ class hgwebdir(object): 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 diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -24,19 +24,14 @@ def updateconfig(source, dest, sections= 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 @@ class ui(object): 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 @@ class ui(object): _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 @@ class ui(object): 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: diff --git a/tests/test-non-interactive-wsgi b/tests/test-non-interactive-wsgi --- a/tests/test-non-interactive-wsgi +++ b/tests/test-non-interactive-wsgi @@ -30,9 +30,6 @@ class FileLike(object): 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() diff --git a/tests/test-trusted.py b/tests/test-trusted.py --- a/tests/test-trusted.py +++ b/tests/test-trusted.py @@ -60,7 +60,7 @@ def testui(user='foo', group='bar', tuse 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 u.config('foobar', 'baz') 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): diff --git a/tests/test-ui-verbosity b/tests/test-ui-verbosity --- a/tests/test-ui-verbosity +++ b/tests/test-ui-verbosity @@ -31,7 +31,10 @@ for i in xrange(64): 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: