home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_584 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.2 KB  |  158 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  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:
  32.             passwd = fallback_getpass(prompt, stream)
  33.  
  34.         input = sys.stdin
  35.         if not stream:
  36.             stream = sys.stderr
  37.         
  38.     except:
  39.         stream
  40.  
  41.     if fd is not None:
  42.         passwd = None
  43.         
  44.         try:
  45.             old = termios.tcgetattr(fd)
  46.             new = old[:]
  47.             new[3] &= ~(termios.ECHO)
  48.             
  49.             try:
  50.                 termios.tcsetattr(fd, termios.TCSADRAIN, new)
  51.                 passwd = _raw_input(prompt, stream, input = input)
  52.             finally:
  53.                 termios.tcsetattr(fd, termios.TCSADRAIN, old)
  54.  
  55.         except termios.error:
  56.             e = None
  57.             if passwd is not None:
  58.                 raise 
  59.             passwd is not None
  60.             del input
  61.             del tty
  62.             passwd = fallback_getpass(prompt, stream)
  63.         except:
  64.             None<EXCEPTION MATCH>termios.error
  65.         
  66.  
  67.     None<EXCEPTION MATCH>termios.error
  68.     stream.write('\n')
  69.     return passwd
  70.  
  71.  
  72. def win_getpass(prompt = 'Password: ', stream = None):
  73.     if sys.stdin is not sys.__stdin__:
  74.         return fallback_getpass(prompt, stream)
  75.     import msvcrt
  76.     for c in prompt:
  77.         msvcrt.putch(c)
  78.     
  79.     pw = ''
  80.     while None:
  81.         c = msvcrt.getch()
  82.         if c == '\r' or c == '\n':
  83.             break
  84.         
  85.         if c == '\x03':
  86.             raise KeyboardInterrupt
  87.         if c == '\x08':
  88.             pw = pw[:-1]
  89.             continue
  90.         pw = pw + c
  91.         continue
  92.         msvcrt.putch('\r')
  93.         msvcrt.putch('\n')
  94.         return pw
  95.  
  96.  
  97. def fallback_getpass(prompt = 'Password: ', stream = None):
  98.     warnings.warn('Can not control echo on the terminal.', GetPassWarning, stacklevel = 2)
  99.     if not stream:
  100.         stream = sys.stderr
  101.     
  102.     print >>stream, 'Warning: Password input may be echoed.'
  103.     return _raw_input(prompt, stream)
  104.  
  105.  
  106. def _raw_input(prompt = '', stream = None, input = None):
  107.     if not stream:
  108.         stream = sys.stderr
  109.     
  110.     if not input:
  111.         input = sys.stdin
  112.     
  113.     prompt = str(prompt)
  114.     if prompt:
  115.         stream.write(prompt)
  116.         stream.flush()
  117.     
  118.     line = input.readline()
  119.     if not line:
  120.         raise EOFError
  121.     line
  122.     if line[-1] == '\n':
  123.         line = line[:-1]
  124.     
  125.     return line
  126.  
  127.  
  128. def getuser():
  129.     import os
  130.     for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
  131.         user = os.environ.get(name)
  132.         if user:
  133.             return user
  134.     
  135.     import pwd
  136.     return pwd.getpwuid(os.getuid())[0]
  137.  
  138.  
  139. try:
  140.     import termios
  141.     (termios.tcgetattr, termios.tcsetattr)
  142. except (ImportError, AttributeError):
  143.     
  144.     try:
  145.         import msvcrt
  146.     except ImportError:
  147.         
  148.         try:
  149.             from EasyDialogs import AskPassword
  150.         except ImportError:
  151.             getpass = fallback_getpass
  152.  
  153.         getpass = AskPassword
  154.  
  155.     getpass = win_getpass
  156.  
  157. getpass = unix_getpass
  158.