home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_Lib_getpass.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  2.9 KB  |  121 lines

  1. """Utilities to get a password and/or the current user name.
  2.  
  3. getpass(prompt) - prompt for a password, with echo turned off
  4. getuser() - get the user name from the environment or password database
  5.  
  6. On Windows, the msvcrt module will be used.
  7. On the Mac EasyDialogs.AskPassword is used, if available.
  8.  
  9. """
  10.  
  11. # Authors: Piers Lauder (original)
  12. #          Guido van Rossum (Windows support and cleanup)
  13.  
  14. import sys
  15.  
  16. __all__ = ["getpass","getuser"]
  17.  
  18. def unix_getpass(prompt='Password: '):
  19.     """Prompt for a password, with echo turned off.
  20.  
  21.     Restore terminal settings at end.
  22.     """
  23.  
  24.     try:
  25.         fd = sys.stdin.fileno()
  26.     except:
  27.         return default_getpass(prompt)
  28.  
  29.     getpass = default_getpass
  30.     old = termios.tcgetattr(fd)     # a copy to save
  31.     new = old[:]
  32.  
  33.     new[3] = new[3] & ~termios.ECHO # 3 == 'lflags'
  34.     try:
  35.         termios.tcsetattr(fd, termios.TCSADRAIN, new)
  36.         passwd = _raw_input(prompt)
  37.     finally:
  38.         termios.tcsetattr(fd, termios.TCSADRAIN, old)
  39.  
  40.     sys.stdout.write('\n')
  41.     return passwd
  42.  
  43.  
  44. def win_getpass(prompt='Password: '):
  45.     """Prompt for password with echo off, using Windows getch()."""
  46.     import msvcrt
  47.     for c in prompt:
  48.         msvcrt.putch(c)
  49.     pw = ""
  50.     while 1:
  51.         c = msvcrt.getch()
  52.         if c == '\r' or c == '\n':
  53.             break
  54.         if c == '\003':
  55.             raise KeyboardInterrupt
  56.         if c == '\b':
  57.             pw = pw[:-1]
  58.         else:
  59.             pw = pw + c
  60.     msvcrt.putch('\r')
  61.     msvcrt.putch('\n')
  62.     return pw
  63.  
  64.  
  65. def default_getpass(prompt='Password: '):
  66.     print "Warning: Problem with getpass. Passwords may be echoed."
  67.     return _raw_input(prompt)
  68.  
  69.  
  70. def _raw_input(prompt=""):
  71.     # A raw_input() replacement that doesn't save the string in the
  72.     # GNU readline history.
  73.     import sys
  74.     prompt = str(prompt)
  75.     if prompt:
  76.         sys.stdout.write(prompt)
  77.     line = sys.stdin.readline()
  78.     if not line:
  79.         raise EOFError
  80.     if line[-1] == '\n':
  81.         line = line[:-1]
  82.     return line
  83.  
  84.  
  85. def getuser():
  86.     """Get the username from the environment or password database.
  87.  
  88.     First try various environment variables, then the password
  89.     database.  This works on Windows as long as USERNAME is set.
  90.  
  91.     """
  92.  
  93.     import os
  94.  
  95.     for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
  96.         user = os.environ.get(name)
  97.         if user:
  98.             return user
  99.  
  100.     # If this fails, the exception will "explain" why
  101.     import pwd
  102.     return pwd.getpwuid(os.getuid())[0]
  103.  
  104. # Bind the name getpass to the appropriate function
  105. try:
  106.     import termios
  107. except ImportError:
  108.     try:
  109.         import msvcrt
  110.     except ImportError:
  111.         try:
  112.             from EasyDialogs import AskPassword
  113.         except ImportError:
  114.             getpass = default_getpass
  115.         else:
  116.             getpass = AskPassword
  117.     else:
  118.         getpass = win_getpass
  119. else:
  120.     getpass = unix_getpass
  121.