Browse Source

Use TreeStore instead of ListStore and populate it

Benoît Hubert 7 years ago
parent
commit
01ebe74e9d
1 changed files with 47 additions and 9 deletions
  1. 47 9
      gtk3-listdomains.py

+ 47 - 9
gtk3-listdomains.py

@@ -18,6 +18,31 @@ if not os.path.isdir(VHOSTS_PATH):
 fp = open('creds.json', 'r')
 creds = json.load(fp)
 
+class Error(Exception):
+    """Base class for exceptions in this module."""
+    pass
+
+class InvalidDomainError(Error):
+    """Exception raised for invalid domains.
+
+    Attributes:
+        domain -- domain name in which the error occurred
+    """
+
+    def __init__(self, domain):
+        self.message = 'Invalid domain name: ' + domain
+
+def is_valid_domain(domain):
+    p = re.compile('^[0-9a-z-.]+$')
+    return p.match(domain)
+
+def is_domain_root(domain):
+    return len(domain.split('.')) == 2
+
+def get_domain_root(subdomain):
+    bits = subdomain.split('.')
+    return '.'.join(bits[-2:])
+
 def ssh_command(command, sudo=False):
     shell = spur.SshShell(
         hostname=creds['host'],
@@ -81,13 +106,11 @@ class EntryWindow(Gtk.Window):
         vbox.add(self.grid)
 
         #Creating the TreeStore model
-        self.vhost_treestore = Gtk.ListStore(int, str, str)
-        vhost_list = [
-#            (80, "00-benoithubert.net", "benoithubert.net"),
-#            (80, "jsx.fr", "jsx.fr")
-        ]
-        for vhost in vhost_list:
-            self.vhost_treestore.append(list(vhost))
+        self.vhost_treestore = Gtk.TreeStore(int, str, str)
+
+        vhost_list = []
+        # for vhost in vhost_list:
+        #     self.vhost_treestore.append(list(vhost))
         self.language_filter = self.vhost_treestore.filter_new()
         self.language_filter.set_visible_func(self.filter_func)
 
@@ -120,7 +143,7 @@ class EntryWindow(Gtk.Window):
     def get_nginx_vhosts(self):
         for v in self.vhosts:
             GLib.idle_add(self.get_nginx_vhost, v)
-            time.sleep(0.4)
+            time.sleep(0.1)
 
     def start_thread(self, func):
         thread = threading.Thread(target=func)
@@ -184,8 +207,23 @@ class EntryWindow(Gtk.Window):
                     subd_trimmed = directive[1].strip()
                     subdomains = subd_trimmed.split(' ')
             port_subdmomains[port] = subdomains
+            parents = {}
             for subd in subdomains:
-                self.vhost_treestore.append([port, vhost, subd])
+                # subdomain = '*' + subd if is_domain_root(subd) else subd
+                # print(subdomain)
+#                self.vhost_treestore.append([port, vhost, subd])
+                root = get_domain_root(subd)
+                print('processing', root, '=>', subd)
+                if not is_valid_domain(subd):
+                    raise InvalidDomainError(subd)
+
+                if not root in parents.keys():
+                    parents[root] = self.vhost_treestore.append(None, (port, vhost, subd))
+                if root != subd:
+                    parent = parents[root]
+                    self.vhost_treestore.append(parent, (port, '', subd))
+                # print(parents)
+
 
         # print(port_subdmomains)
         self.vhosts_done += 1