Share Pastebin
Guest
Public paste!

Mirko Pagliai

By: a guest | Feb 8th, 2010 | Syntax: Python | Size: 5.37 KB | Hits: 165 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. #!/usr/bin/env python
  2. # -*- coding: latin-1 -*-
  3. # Author: Mirko Pagliai
  4. # Mail: mirko.pagliai@gmail.com
  5. # Copyright: 2010 by Mirko Pagliai
  6. # generated by wxGlade 0.6.3 on Mon Feb  8 14:59:59 2010
  7.  
  8. import wx, random, string
  9.  
  10. version = "1.0.1"
  11.  
  12. class MyFrame(wx.Frame):
  13.     def __init__(self, *args, **kwds):
  14.         # begin wxGlade: MyFrame.__init__
  15.         kwds["style"] = wx.DEFAULT_FRAME_STYLE
  16.         wx.Frame.__init__(self, *args, **kwds)
  17.         self.label_1 = wx.StaticText(self, -1, "Lunghezza password:")
  18.         self.label_2 = wx.StaticText(self, -1, "Tipo password:")
  19.         self.label_3 = wx.StaticText(self, -1, "Tipo lettere:")
  20.         self.pwdlength = wx.TextCtrl(self, -1, "8")
  21.         self.pwdtype = wx.ComboBox(self, -1, choices=["lettere e numeri", "solo lettere", "solo numeri", "lettere, numeri e simboli"], style=wx.CB_DROPDOWN)
  22.         self.letterstype = wx.ComboBox(self, -1, choices=["maiuscole e minuscole", "solo maiuscole", "solo minuscole"], style=wx.CB_DROPDOWN)
  23.         self.go = wx.Button(self, -1, "Genera password")
  24.         self.label_4 = wx.StaticText(self, -1, "Password generata:")
  25.         self.pwdbox = wx.TextCtrl(self, -1, "", style=wx.TE_READONLY)
  26.        
  27.         # Menu Bar
  28.         self.frame_menubar = wx.MenuBar()
  29.         wxglade_tmp_menu = wx.Menu()
  30.         wxglade_tmp_menu.Append(wx.NewId(), "Informazioni", "", wx.ITEM_NORMAL)
  31.         self.frame_menubar.Append(wxglade_tmp_menu, "Aiuto")
  32.         self.SetMenuBar(self.frame_menubar)
  33.         # Menu Bar end
  34.  
  35.         self.__set_properties()
  36.         self.__do_layout()
  37.  
  38.         self.Bind(wx.EVT_BUTTON, self.generatespwd, self.go)
  39.         self.Bind(wx.EVT_MENU, self.showinfo, id=-1)
  40.         # end wxGlade
  41.  
  42.     def __set_properties(self):
  43.         # begin wxGlade: MyFrame.__set_properties
  44.         self.SetTitle("Generatore password")
  45.         self.pwdtype.SetMinSize((190, 27))
  46.         self.pwdtype.SetSelection(0)
  47.         self.letterstype.SetMinSize((195, 27))
  48.         self.letterstype.SetSelection(0)
  49.         self.pwdbox.SetMinSize((200, 27))
  50.         # end wxGlade
  51.  
  52.     def __do_layout(self):
  53.         # begin wxGlade: MyFrame.__do_layout
  54.         sizer = wx.BoxSizer(wx.VERTICAL)
  55.         grid_sizer_2 = wx.FlexGridSizer(1, 3, 5, 15)
  56.         grid_sizer_1 = wx.FlexGridSizer(2, 3, 5, 15)
  57.         grid_sizer_1.Add(self.label_1, 0, wx.ADJUST_MINSIZE, 0)
  58.         grid_sizer_1.Add(self.label_2, 0, wx.ADJUST_MINSIZE, 0)
  59.         grid_sizer_1.Add(self.label_3, 0, wx.ADJUST_MINSIZE, 0)
  60.         grid_sizer_1.Add(self.pwdlength, 0, wx.ADJUST_MINSIZE, 0)
  61.         grid_sizer_1.Add(self.pwdtype, 0, wx.ADJUST_MINSIZE, 0)
  62.         grid_sizer_1.Add(self.letterstype, 0, wx.ADJUST_MINSIZE, 0)
  63.         sizer.Add(grid_sizer_1, 0, wx.ALL|wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 10)
  64.         grid_sizer_2.Add(self.go, 0, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
  65.         grid_sizer_2.Add(self.label_4, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
  66.         grid_sizer_2.Add(self.pwdbox, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
  67.         sizer.Add(grid_sizer_2, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 10)
  68.         self.SetSizer(sizer)
  69.         sizer.Fit(self)
  70.         self.Layout()
  71.         # end wxGlade
  72.  
  73.     #Provoca un alert con errore
  74.     def alert(self, text, title="Errore", icon=wx.ICON_ERROR):
  75.         alert = wx.MessageDialog(self, text, title, wx.OK | icon)
  76.         alert.ShowModal()
  77.         alert.Destroy()
  78.  
  79.     #Mostra le informazioni sull'applicazione
  80.     def showinfo(self, event): # wxGlade: MyFrame.<event_handler>
  81.         self.alert("pwdgenerator v. "+version+"\n\nCopyright © 2010\nby Mirko Pagliai\n(mirko.pagliai@gmail.com)", "Informazioni", wx.ICON_INFORMATION)
  82.         event.Skip()
  83.  
  84.     #Genera la password
  85.     def generatespwd(self, event): # wxGlade: MyFrame.<event_handler>
  86.         #Se la password ha la lunghezza corretta
  87.         if int(self.pwdlength.GetValue()) >= 3 and int(self.pwdlength.GetValue()) <= 20:
  88.                 #Inizializzazione variabili e liste
  89.                 self.letters = []
  90.                 self.pwd = ""
  91.                 self.source = []
  92.                 #Imposta le lettere da usare a seconda della scelta
  93.                 if self.letterstype.GetValue() == "maiuscole e minuscole":
  94.                         self.letters = string.letters
  95.                 elif self.letterstype.GetValue() == "solo maiuscole":
  96.                         self.letters = string.uppercase
  97.                 elif self.letterstype.GetValue() == "solo minuscole":
  98.                         self.letters = string.lowercase
  99.                 #Imposta i caratteri sorgenti da usare a seconda della scelta
  100.                 if self.pwdtype.GetValue() == "solo lettere":
  101.                         self.source = self.letters
  102.                 elif self.pwdtype.GetValue() == "solo numeri":
  103.                         self.source = string.digits
  104.                 elif self.pwdtype.GetValue() == "lettere e numeri":
  105.                         self.source = self.letters + string.digits
  106.                 elif self.pwdtype.GetValue() == "lettere, numeri e simboli":
  107.                         self.source = self.letters + string.digits + string.punctuation
  108.  
  109.                 #Genera la password
  110.                 for n in range(int(self.pwdlength.GetValue())):
  111.                         self.pwd = self.pwd + random.choice(self.source)
  112.  
  113.                 #Imposta la password generata nel suo box
  114.                 self.pwdbox.SetValue(self.pwd)
  115.  
  116.         #In caso di password troppo lunga o troppo corta
  117.         else:
  118.                 self.alert("La password deve avere una lunghezza tra i 3 e i 20 caratteri")
  119.         event.Skip()
  120.  
  121. # end of class MyFrame
  122.  
  123. if __name__ == "__main__":
  124.     app = wx.PySimpleApp(0)
  125.     wx.InitAllImageHandlers()
  126.     frame = MyFrame(None, -1, "")
  127.     app.SetTopWindow(frame)
  128.     frame.Show()
  129.     app.MainLoop()