comparison mercurial/ui.py @ 8208:32a2a1e244f1

ui: make interactive a method
author Matt Mackall <mpm@selenic.com>
date Sun, 26 Apr 2009 16:50:44 -0500
parents cce63ef1045b
children 6e6ebeb52899
comparison
equal deleted inserted replaced
8207:dd8d5be57d65 8208:32a2a1e244f1
14 14
15 class ui(object): 15 class ui(object):
16 def __init__(self, src=None): 16 def __init__(self, src=None):
17 self._buffers = [] 17 self._buffers = []
18 self.quiet = self.verbose = self.debugflag = self._traceback = False 18 self.quiet = self.verbose = self.debugflag = self._traceback = False
19 self.interactive = self._reportuntrusted = True 19 self._reportuntrusted = True
20 self._ocfg = config.config() # overlay 20 self._ocfg = config.config() # overlay
21 self._tcfg = config.config() # trusted 21 self._tcfg = config.config() # trusted
22 self._ucfg = config.config() # untrusted 22 self._ucfg = config.config() # untrusted
23 self._trustusers = {} 23 self._trustusers = {}
24 self._trustgroups = {} 24 self._trustgroups = {}
35 for f in util.rcpath(): 35 for f in util.rcpath():
36 self.readconfig(f, trust=True) 36 self.readconfig(f, trust=True)
37 def copy(self): 37 def copy(self):
38 return ui(self) 38 return ui(self)
39 39
40 _isatty = None
41 def isatty(self):
42 if ui._isatty is None:
43 try:
44 ui._isatty = sys.stdin.isatty()
45 except AttributeError: # not a real file object
46 ui._isatty = False
47 except IOError:
48 # access to stdin is unsafe in a WSGI environment
49 ui._isatty = False
50 return ui._isatty
51
52 def _is_trusted(self, fp, f): 40 def _is_trusted(self, fp, f):
53 st = util.fstat(fp) 41 st = util.fstat(fp)
54 if util.isowner(fp, st): 42 if util.isowner(fp, st):
55 return True 43 return True
56 44
110 self.verbose = self.debugflag or self.configbool('ui', 'verbose') 98 self.verbose = self.debugflag or self.configbool('ui', 'verbose')
111 self.quiet = not self.debugflag and self.configbool('ui', 'quiet') 99 self.quiet = not self.debugflag and self.configbool('ui', 'quiet')
112 if self.verbose and self.quiet: 100 if self.verbose and self.quiet:
113 self.quiet = self.verbose = False 101 self.quiet = self.verbose = False
114 self._reportuntrusted = self.configbool("ui", "report_untrusted", True) 102 self._reportuntrusted = self.configbool("ui", "report_untrusted", True)
115 self.interactive = self.configbool("ui", "interactive", self.isatty())
116 self._traceback = self.configbool('ui', 'traceback', False) 103 self._traceback = self.configbool('ui', 'traceback', False)
117 104
118 # update trust information 105 # update trust information
119 for user in self.configlist('trusted', 'users'): 106 for user in self.configlist('trusted', 'users'):
120 self._trustusers[user] = 1 107 self._trustusers[user] = 1
257 try: sys.stdout.flush() 244 try: sys.stdout.flush()
258 except: pass 245 except: pass
259 try: sys.stderr.flush() 246 try: sys.stderr.flush()
260 except: pass 247 except: pass
261 248
249 def interactive(self):
250 return self.configbool("ui", "interactive") or sys.stdin.isatty()
251
262 def _readline(self, prompt=''): 252 def _readline(self, prompt=''):
263 if self.isatty(): 253 if sys.stdin.isatty():
264 try: 254 try:
265 # magically add command line editing support, where 255 # magically add command line editing support, where
266 # available 256 # available
267 import readline 257 import readline
268 # force demandimport to really load the module 258 # force demandimport to really load the module
280 def prompt(self, msg, pat=None, default="y"): 270 def prompt(self, msg, pat=None, default="y"):
281 """Prompt user with msg, read response, and ensure it matches pat 271 """Prompt user with msg, read response, and ensure it matches pat
282 272
283 If not interactive -- the default is returned 273 If not interactive -- the default is returned
284 """ 274 """
285 if not self.interactive: 275 if not self.interactive():
286 self.note(msg, ' ', default, "\n") 276 self.note(msg, ' ', default, "\n")
287 return default 277 return default
288 while True: 278 while True:
289 try: 279 try:
290 r = self._readline(msg + ' ') 280 r = self._readline(msg + ' ')
296 self.write(_("unrecognized response\n")) 286 self.write(_("unrecognized response\n"))
297 except EOFError: 287 except EOFError:
298 raise util.Abort(_('response expected')) 288 raise util.Abort(_('response expected'))
299 289
300 def getpass(self, prompt=None, default=None): 290 def getpass(self, prompt=None, default=None):
301 if not self.interactive: return default 291 if not self.interactive(): return default
302 try: 292 try:
303 return getpass.getpass(prompt or _('password: ')) 293 return getpass.getpass(prompt or _('password: '))
304 except EOFError: 294 except EOFError:
305 raise util.Abort(_('response expected')) 295 raise util.Abort(_('response expected'))
306 def status(self, *msg): 296 def status(self, *msg):