home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / getpass.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  3.3 KB  |  148 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. import os
  5. import sys
  6. import warnings
  7. __all__ = [
  8.     'getpass',
  9.     'getuser',
  10.     'GetPassWarning']
  11.  
  12. class GetPassWarning(UserWarning):
  13.     pass
  14.  
  15.  
  16. def unix_getpass(prompt = 'Password: ', stream = None):
  17.     fd = None
  18.     tty = None
  19.     
  20.     try:
  21.         fd = os.open('/dev/tty', os.O_RDWR | os.O_NOCTTY)
  22.         tty = os.fdopen(fd, 'w+', 1)
  23.         input = tty
  24.         if not stream:
  25.             stream = tty
  26.     except EnvironmentError:
  27.         e = None
  28.         
  29.         try:
  30.             fd = sys.stdin.fileno()
  31.         except (AttributeError, ValueError):
  32.             passwd = fallback_getpass(prompt, stream)
  33.  
  34.         input = sys.stdin
  35.         if not stream:
  36.             stream = sys.stderr
  37.         
  38.  
  39.     if fd is not None:
  40.         passwd = None
  41.         
  42.         try:
  43.             old = termios.tcgetattr(fd)
  44.             new = old[:]
  45.             new[3] &= ~(termios.ECHO | termios.ISIG)
  46.             tcsetattr_flags = termios.TCSAFLUSH
  47.             if hasattr(termios, 'TCSASOFT'):
  48.                 tcsetattr_flags |= termios.TCSASOFT
  49.             
  50.             try:
  51.                 termios.tcsetattr(fd, tcsetattr_flags, new)
  52.                 passwd = _raw_input(prompt, stream, input = input)
  53.             finally:
  54.                 termios.tcsetattr(fd, tcsetattr_flags, old)
  55.                 stream.flush()
  56.  
  57.         except termios.error:
  58.             e = None
  59.             if passwd is not None:
  60.                 raise 
  61.             del input
  62.             del tty
  63.             passwd = fallback_getpass(prompt, stream)
  64.         
  65.  
  66.     stream.write('\n')
  67.     return passwd
  68.  
  69.  
  70. def win_getpass(prompt = 'Password: ', stream = None):
  71.     if sys.stdin is not sys.__stdin__:
  72.         return fallback_getpass(prompt, stream)
  73.     import msvcrt
  74.     for c in prompt:
  75.         msvcrt.putch(c)
  76.     
  77.     pw = ''
  78.     while None:
  79.         c = msvcrt.getch()
  80.         if c == '\r' or c == '\n':
  81.             break
  82.         if c == '\x03':
  83.             raise KeyboardInterrupt
  84.         if c == '\x08':
  85.             pw = pw[:-1]
  86.             continue
  87.         pw = pw + c
  88.         continue
  89.         msvcrt.putch('\n')
  90.         return pw
  91.  
  92.  
  93. def fallback_getpass(prompt = 'Password: ', stream = None):
  94.     warnings.warn('Can not control echo on the terminal.', GetPassWarning, stacklevel = 2)
  95.     if not stream:
  96.         stream = sys.stderr
  97.     print >>stream, 'Warning: Password input may be echoed.'
  98.     return _raw_input(prompt, stream)
  99.  
  100.  
  101. def _raw_input(prompt = '', stream = None, input = None):
  102.     if not stream:
  103.         stream = sys.stderr
  104.     if not input:
  105.         input = sys.stdin
  106.     prompt = str(prompt)
  107.     if prompt:
  108.         stream.write(prompt)
  109.         stream.flush()
  110.     line = input.readline()
  111.     if not line:
  112.         raise EOFError
  113.     if line[-1] == '\n':
  114.         line = line[:-1]
  115.     return line
  116.  
  117.  
  118. def getuser():
  119.     import os
  120.     for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
  121.         user = os.environ.get(name)
  122.         if user:
  123.             return user
  124.     
  125.     import pwd
  126.     return pwd.getpwuid(os.getuid())[0]
  127.  
  128.  
  129. try:
  130.     import termios
  131.     (termios.tcgetattr, termios.tcsetattr)
  132. except (ImportError, AttributeError):
  133.     
  134.     try:
  135.         import msvcrt
  136.     except ImportError:
  137.         
  138.         try:
  139.             from EasyDialogs import AskPassword
  140.         except ImportError:
  141.             getpass = fallback_getpass
  142.  
  143.         getpass = AskPassword
  144.  
  145.     getpass = win_getpass
  146.  
  147. getpass = unix_getpass
  148.