login.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. '''login -- PythonWin user ID and password dialog box
  2. (Adapted from originally distributed with Mark Hammond's PythonWin -
  3. this now replaces it!)
  4. login.GetLogin() displays a modal "OK/Cancel" dialog box with input
  5. fields for a user ID and password. The password field input is masked
  6. with *'s. GetLogin takes two optional parameters, a window title, and a
  7. default user ID. If these parameters are omitted, the title defaults to
  8. "Login", and the user ID is left blank. GetLogin returns a (userid, password)
  9. tuple. GetLogin can be called from scripts running on the console - i.e. you
  10. don't need to write a full-blown GUI app to use it.
  11. login.GetPassword() is similar, except there is no username field.
  12. Example:
  13. import pywin.dialogs.login
  14. title = "FTP Login"
  15. def_user = "fred"
  16. userid, password = pywin.dialogs.login.GetLogin(title, def_user)
  17. Jim Eggleston, 28 August 1996
  18. Merged with dlgpass and moved to pywin.dialogs by Mark Hammond Jan 1998.
  19. '''
  20. import win32ui
  21. import win32api
  22. import win32con
  23. from pywin.mfc import dialog
  24. def MakeLoginDlgTemplate(title):
  25. style = win32con.DS_MODALFRAME | win32con.WS_POPUP | win32con.WS_VISIBLE | win32con.WS_CAPTION | win32con.WS_SYSMENU | win32con.DS_SETFONT
  26. cs = win32con.WS_CHILD | win32con.WS_VISIBLE
  27. # Window frame and title
  28. dlg = [ [title, (0, 0, 184, 40), style, None, (8, "MS Sans Serif")], ]
  29. # ID label and text box
  30. dlg.append([130, "User ID:", -1, (7, 9, 69, 9), cs | win32con.SS_LEFT])
  31. s = cs | win32con.WS_TABSTOP | win32con.WS_BORDER
  32. dlg.append(['EDIT', None, win32ui.IDC_EDIT1, (50, 7, 60, 12), s])
  33. # Password label and text box
  34. dlg.append([130, "Password:", -1, (7, 22, 69, 9), cs | win32con.SS_LEFT])
  35. s = cs | win32con.WS_TABSTOP | win32con.WS_BORDER
  36. dlg.append(['EDIT', None, win32ui.IDC_EDIT2, (50, 20, 60, 12), s | win32con.ES_PASSWORD])
  37. # OK/Cancel Buttons
  38. s = cs | win32con.WS_TABSTOP
  39. dlg.append([128, "OK", win32con.IDOK, (124, 5, 50, 14), s | win32con.BS_DEFPUSHBUTTON])
  40. s = win32con.BS_PUSHBUTTON | s
  41. dlg.append([128, "Cancel", win32con.IDCANCEL, (124, 20, 50, 14), s])
  42. return dlg
  43. def MakePasswordDlgTemplate(title):
  44. style = win32con.DS_MODALFRAME | win32con.WS_POPUP | win32con.WS_VISIBLE | win32con.WS_CAPTION | win32con.WS_SYSMENU | win32con.DS_SETFONT
  45. cs = win32con.WS_CHILD | win32con.WS_VISIBLE
  46. # Window frame and title
  47. dlg = [ [title, (0, 0, 177, 45), style, None, (8, "MS Sans Serif")], ]
  48. # Password label and text box
  49. dlg.append([130, "Password:", -1, (7, 7, 69, 9), cs | win32con.SS_LEFT])
  50. s = cs | win32con.WS_TABSTOP | win32con.WS_BORDER
  51. dlg.append(['EDIT', None, win32ui.IDC_EDIT1, (50, 7, 60, 12), s | win32con.ES_PASSWORD])
  52. # OK/Cancel Buttons
  53. s = cs | win32con.WS_TABSTOP | win32con.BS_PUSHBUTTON
  54. dlg.append([128, "OK", win32con.IDOK, (124, 5, 50, 14), s | win32con.BS_DEFPUSHBUTTON])
  55. dlg.append([128, "Cancel", win32con.IDCANCEL, (124, 22, 50, 14), s])
  56. return dlg
  57. class LoginDlg(dialog.Dialog):
  58. Cancel = 0
  59. def __init__(self, title):
  60. dialog.Dialog.__init__(self, MakeLoginDlgTemplate(title) )
  61. self.AddDDX(win32ui.IDC_EDIT1,'userid')
  62. self.AddDDX(win32ui.IDC_EDIT2,'password')
  63. def GetLogin(title='Login', userid='', password=''):
  64. d = LoginDlg(title)
  65. d['userid'] = userid
  66. d['password'] = password
  67. if d.DoModal() != win32con.IDOK:
  68. return (None, None)
  69. else:
  70. return (d['userid'], d['password'])
  71. class PasswordDlg(dialog.Dialog):
  72. def __init__(self, title):
  73. dialog.Dialog.__init__(self, MakePasswordDlgTemplate(title) )
  74. self.AddDDX(win32ui.IDC_EDIT1,'password')
  75. def GetPassword(title='Password', password=''):
  76. d = PasswordDlg(title)
  77. d['password'] = password
  78. if d.DoModal()!=win32con.IDOK:
  79. return None
  80. return d['password']
  81. if __name__ == "__main__":
  82. import sys
  83. title = 'Login'
  84. def_user = ''
  85. if len(sys.argv) > 1:
  86. title = sys.argv[1]
  87. if len(sys.argv) > 2:
  88. def_userid = sys.argv[2]
  89. userid, password = GetLogin(title, def_user)
  90. if userid == password == None:
  91. print "User pressed Cancel"
  92. else:
  93. print "User ID: ", userid
  94. print "Password:", password
  95. newpassword = GetPassword("Reenter just for fun", password)
  96. if newpassword is None:
  97. print "User cancelled"
  98. else:
  99. what = ""
  100. if newpassword != password:
  101. what = "not "
  102. print "The passwords did %smatch" % (what)