Преглед на файлове

threaded progress bar update

Benoît Hubert преди 7 години
родител
ревизия
1c30fbf93f
променени са 1 файла, в които са добавени 64 реда и са изтрити 13 реда
  1. 64 13
      gtk3-listdomains.py

+ 64 - 13
gtk3-listdomains.py

@@ -1,9 +1,12 @@
 import gi
 gi.require_version('Gtk', '3.0')
-from gi.repository import Gtk, GObject
-import spur
+from gi.repository import GLib, Gtk, GObject
 import json
 import re
+import spur
+import threading
+import time
+
 
 fp = open('creds.json', 'r')
 creds = json.load(fp)
@@ -23,12 +26,12 @@ def ssh_command(command, sudo=False):
         result = process.wait_for_result()
     return result.output.decode()
 
-def check_domain(domain):
-    print(domain)
-    p = re.compile('DNS:([0-9a-z-.]+)')
-    cert_data = ssh_command("sudo openssl x509 -text -in /etc/letsencrypt/live/" + domain + "/fullchain.pem", True)
-    print(cert_data)
-    print(p.findall (cert_data))
+#
+# def get_check_domain_command(domain):
+#     return "sudo openssl x509 -text -in /etc/letsencrypt/live/" + domain + "/fullchain.pem"
+#
+# def join_commands(commands):
+#     return " && ".join(commands)
 
 class EntryWindow(Gtk.Window):
 
@@ -38,8 +41,15 @@ class EntryWindow(Gtk.Window):
 
         self.timeout_id = None
 
+        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
+        self.add(vbox)
+
+        self.progressbar = Gtk.ProgressBar(show_text=True)
+        # self.progressbar.set_fraction(0.0)
+        vbox.pack_start(self.progressbar, True, True, 0)
+
         hbox = Gtk.Box(spacing=6)
-        self.add(hbox)
+        vbox.add(hbox)
 
         self.entry_passphrase = Gtk.Entry()
         # https://developer.gnome.org/gtk3/stable/GtkEntry.html#gtk-entry-set-invisible-char
@@ -56,23 +66,64 @@ class EntryWindow(Gtk.Window):
         self.button.connect("clicked", self.on_button_clicked)
         hbox.pack_start(self.button, True, True, 0)
 
+
+    def update_progess(self, domain):
+        self.progressbar.pulse()
+        self.progressbar.set_text('Done: ' + domain)
+        return False
+
+    def example_target(self):
+        for d in self.domains:
+            GLib.idle_add(self.get_https_subdomains_for_domain, d)
+            time.sleep(0.3)
+
+
     def on_button_clicked(self, widget):
         decoded = ssh_command("ls /etc/letsencrypt/live", True)
         domains = decoded.split("\n")
         domains.pop()
 
-        for d in domains:
-            if d == "benoithubert.net":
-                check_domain(d)
+        self.domains = domains
+        self.num_domains = len(domains)
+        self.num_done = 0
+
+        thread = threading.Thread(target=self.example_target)
+        thread.daemon = True
+        thread.start()
+
+        # subdomains = [self.get_https_subdomains_for_domain(d) for d in domains]
+        # subdomains_dict = dict(zip(domains, subdomains))
+        # print(subdomains_dict)
+
+
+    def get_https_subdomains_for_domain(self, domain):
+        print(domain)
+        p = re.compile('DNS:([0-9a-z-.]+)')
+        cert_data = ssh_command("sudo openssl x509 -text -in /etc/letsencrypt/live/" + domain + "/fullchain.pem", True)
+        self.num_done += 1
+        percent_done = self.num_done * 1.0 / self.num_domains
+        # print(percent_done)
+        self.progressbar.set_fraction(percent_done)
+        # self.progressbar.set_text('Done: ' + domain)
+
+        # print(cert_data)
+        # return p.findall (cert_data)
+        return False
 
 
 def app_main():
     win = EntryWindow()
     win.connect("delete-event", Gtk.main_quit)
     win.show_all()
-    Gtk.main()
+
+
 
 if __name__ == '__main__':
     import signal
     signal.signal(signal.SIGINT, signal.SIG_DFL)
+
+    # Calling GObject.threads_init() is not needed for PyGObject 3.10.2+
+    GObject.threads_init()
+
     app_main()
+    Gtk.main()