eculocate

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

eculocate-gtk (2980B)


      1 #!/usr/bin/env python
      2 
      3 import gi
      4 gi.require_version("Gtk", "3.0")
      5 from gi.repository import Gtk, GLib
      6 import sys
      7 import os
      8 
      9 import eculocate
     10 
     11 class MyWindow(Gtk.Window):
     12     def widgets(self):
     13         main = Gtk.VBox()
     14 
     15         main.add(Gtk.Label(label = "Services"))
     16 
     17         self.hosts_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
     18         main.add(self.hosts_box)
     19 
     20         self.status = Gtk.Label(label = "initializing")
     21         main.add(self.status)
     22 
     23         start = Gtk.ToggleButton(label="Start")
     24         main.add(start)
     25         self.start_button = start
     26         self.add(main)
     27 
     28     def update_host(self, name):
     29         if name in self.host_buttons:
     30             print(f"already seen {name}")
     31         else:
     32             group = self.hosts_box.get_children()
     33             print(f"group {group}")
     34             if group:
     35                 n = Gtk.RadioButton.new_with_label_from_widget(group[0], name)
     36             else:
     37                 n = Gtk.RadioButton.new_with_label(None, name)
     38             n.set_mode(False)
     39             n.connect("clicked", self.choose_service, name)
     40             print(f"service {n}")
     41             self.host_buttons[name] = n
     42             self.hosts_box.add(n)
     43             self.hosts_box.show_all()
     44 
     45     def choose_service(self, e, name):
     46         print(f"clikec {name}")
     47         self.selected_service_name = name
     48 
     49     def __init__(self):
     50         super().__init__(title="Eculocate")
     51         self.host_buttons = {}
     52         self.widgets()
     53         self.selected_service_name = None
     54         self.update_host("othher.host.local")
     55 
     56 class GtkClient(eculocate.Client):
     57     def __init__(self, signing_key, window):
     58         super().__init__(signing_key)
     59         print("hre")
     60         self.window = window
     61         window.start_button.connect("clicked", self.on_start_button_clicked)
     62 
     63     def on_start_button_clicked(self, widget):
     64         print("Hello World")
     65         if self.window.selected_service_name:
     66             self.select_service_name(self.window.selected_service_name)
     67             print(f"toggle record for {self.selected_service_name}")
     68             if self.started:
     69                 self.stop_recording()
     70                 widget.set_label("start")
     71             else:
     72                 self.start_recording()
     73                 widget.set_label("stop")
     74 
     75     def on_services_changed(self):
     76         for name, service in self.host_addresses.items():
     77             print(f"updating {name}")
     78             self.window.update_host(name)
     79 
     80 
     81 if __name__ == '__main__':
     82     conf_file = os.getenv("ECULOCATE_CONFIG") or "/etc/eculocate.conf"
     83     config = eculocate.Config(conf_file)
     84 
     85     win = MyWindow()
     86     client = GtkClient(config, win)
     87     win.connect("destroy", Gtk.main_quit)
     88     win.show_all()
     89 
     90     def update_byte_counter():
     91         label = f"recorded {client.bytes_recorded} bytes" if client.started else "not recording"
     92         if win.status:
     93             win.status.set_text(label)
     94         return True
     95 
     96     GLib.timeout_add_seconds(1,  update_byte_counter)
     97 
     98     Gtk.main()