fondazionebordoni / nemesys

Code for Network Measurement System Software for Monitoring of Fixed Broadband Quality of Service (QoS) Project
https://www.misurainternet.it/
GNU General Public License v3.0
7 stars 2 forks source link

Forse è possibile fare a meno della GUI #20

Closed avwarez closed 4 years ago

avwarez commented 4 years ago

In tutto il codice ho trovato un riferimento alla libreria Tkinter solo nel file login.py. Questa libreria viene usata per interagire con l'utente tramite GUI e leggere da input la username e la password di accesso.

C'è un'altro modo per ottenere lo stesso risultato senza GUI: leggere l'input da linea di comando (cioè la stessa modalità di interazione usata per installare tramite dpkg). Questo eviterebbe di installare alcune dipendenze (legate a X11) traducendosi quindi in installazioni più rapide e snelle.

Installazione: pip install PyInquirer

Patch da esperimento riuscito (sicuramente migliorabile):

--- a/login.py  2020-05-23 23:43:29.000000000 +0000
+++ b/login.py  2020-05-24 00:02:10.482786487 +0000
@@ -16,18 +16,17 @@
 # You should have received a copy of the GNU General Public License
 # along with this program. If not, see <http://www.gnu.org/licenses/>.

-import Tkinter
 import hashlib
 import logging
 import os
 import sys
-import tkMessageBox
 import urllib2

 from common import httputils
 from common import utils
 from nemesys import log_conf
 from common import paths
+from PyInquirer import prompt

 CANCEL_MESSAGE = '''L'autenticazione non e' andata a buon fine.
 Procedere con la disinstallazione e reinstallare nuovamente Ne.Me.Sys. \
@@ -110,15 +109,8 @@
     """
     Apre una finestra che chiede il codice licenza. Resituisce il codice licenza e chiude la finestra.
     """
-    root = Tkinter.Tk()
-    if utils.is_windows():
-        root.wm_iconbitmap('Nemesys.ico')
-    app = LoginGui(master=root)
-    app.master.title("Attivazione Ne.Me.Sys")
-    app.mainloop()
+    app = LoginGui()
     serial_code = str(app.result)
-    if root:
-        root.destroy()

     if serial_code == 'Cancel':
         logger.info('Utente ha premuto Cancel')
@@ -131,24 +123,15 @@

 def ErrorDialog(message):
-    root = Tkinter.Tk()
-    if utils.is_windows():
-        root.wm_iconbitmap('Nemesys.ico')
-    root.withdraw()
     title = 'Errore'
-    tkMessageBox.showerror(title, message, parent=root)
-    root.destroy()
+    print(title, message)
+

 def OkDialog():
-    root = Tkinter.Tk()
-    if utils.is_windows():
-        root.wm_iconbitmap('Nemesys.ico')
-    root.withdraw()
     title = 'Ne.Me.Sys autenticazione corretta'
     message = 'Username e password corrette e verificate'
-    tkMessageBox.showinfo(title, message, parent=root)
-    root.destroy()
+    print(title, message)

 def getActivationFile(serial_code, path):
@@ -181,62 +164,35 @@
         raise Exception('Errore nel file di configurazione')

-class LoginGui(Tkinter.Frame):
+class LoginGui():
     """
     finestra di codice licenza
     """

-    def sendMsg(self):
-        inserted_username = self.username.get()
-        inserted_password = self.password.get()
-        if inserted_username and inserted_password:
-            self.result = "{}|{}".format(self.username.get(), hashlib.sha1(self.password.get()).hexdigest())
-        self.quit()
+    def createWidgets(self):
+        questions = [
+            {
+                'type': 'input',
+                'name': 'inserted_username',
+                'message': 'Inserisci la username di accesso all area personale',
+             },
+            {
+                'type': 'password',
+                'name': 'inserted_password',
+                'message': 'Inserisci la password di accesso all area personale',
+             }
+        ]
+        answers = prompt(questions)

-    def cancel(self):
-        self.result = 'Cancel'
-        self.quit()
+        inserted_username = answers['inserted_username']
+        inserted_password = answers['inserted_password']
+        if inserted_username and inserted_password:
+            self.result = "{}|{}".format(inserted_username, hashlib.sha1(inserted_password).hexdigest())
+        else:
+            self.result = 'None'

-    def createWidgets(self):
-        self.Title = Tkinter.Label(self, padx=60, pady=8)
-        self.Title["text"] = '''Inserisci i codici di accesso (username e password)
-        che hai usato per accedere all'area personale'''
-        self.Title.grid(column=0, row=0, columnspan=2)
-
-        username_label = Tkinter.Label(self, text="username:")
-        username_label.grid(column=0, row=1)
-
-        self.username = Tkinter.Entry(self, width=30)
-        self.username.grid(column=1, row=1)
-
-        password_label = Tkinter.Label(self, text="password:")
-        password_label.grid(column=0, row=2)
-
-        self.password = Tkinter.Entry(self, width=30)
-        self.password["show"] = "*"
-        self.password.grid(column=1, row=2)
-
-        self.button_frame = Tkinter.Frame(self)
-        self.button_frame.grid(column=1, row=3, columnspan=2, pady=8)
-
-        self.invio = Tkinter.Button(self.button_frame)
-        self.invio["text"] = "Accedi",
-        self.invio["command"] = self.sendMsg
-        self.invio.grid(column=0, row=0, padx=4)
-
-        self.cancl = Tkinter.Button(self.button_frame)
-        self.cancl["text"] = "Cancel",
-        self.cancl["command"] = self.cancel
-        self.cancl.grid(column=1, row=0, padx=4)
-
-    def __init__(self, master=None):
-        Tkinter.Frame.__init__(self, master)
-        self.config(width="800")
-        if utils.is_windows():
-            self.master.wm_iconbitmap('Nemesys.ico')
-        self.pack()
+    def __init__(self):
         self.createWidgets()
-        self.result = None

 def try_to_activate():
ewedlund commented 4 years ago

Salve, Nemesys è pensato per essere installato come pacchetto software e non eseguito con l'interprete Python, e deve poter girare su diverse piattaforme (Windows, OSX) anche da utenti inesperti. La presenza del codice su GitHub è per motivi di trasparenza.