def on_message(bus: Gst.Bus, message:Gst.Message, loop: GLib.MainLoop): mtype = message.type if mtype == Gst.MessageType.EOS: print('End Of Stream') loop.quit() elif mtype == Gst.MessageType.ERROR: err, debug = message.parse_error() print(err, debug) loop.quit() elif mtype == Gst.MessageType.WARNING: err, debug = message.parse_warning() print(err, debug) return True
def bus_call(bus: Gst.Bus, message: Gst.Message, loop: GLib.MainLoop): t = message.type if t == Gst.MessageType.EOS: sys.stdout.write("End-of-stream\n") loop.quit() elif t == Gst.MessageType.ERROR: err, debug = message.parse_error() sys.stderr.write("Error: %s: %s\n" % (err, debug)) loop.quit() elif t == Gst.MessageType.WARNING: # Handle warnings err, debug = message.parse_warning() sys.stderr.write("Warning: %s: %s\n" % (err, debug)) return True
def on_message(bus: Gst.Bus, message: Gst.Message, loop: GLib.MainLoop): mtype = message.type """ Gstreamer Message Types and how to parse https://lazka.github.io/pgi-docs/Gst-1.0/flags.html#Gst.MessageType """ if mtype == Gst.MessageType.EOS: print("End of stream") loop.quit() elif mtype == Gst.MessageType.ERROR: err, debug = message.parse_error() print(err, debug) loop.quit() elif mtype == Gst.MessageType.WARNING: err, debug = message.parse_warning() print(err, debug) return True
def main(self): main_loop = MainLoop() self.connect('ipc_shutdown', lambda self: main_loop.quit()) main_loop.run()
class BatteryMonitor: ''' Class for Battery Monitoring ''' def __init__(self, batn, design): self.bus = SystemBus() self.loop = MainLoop() self.bat = self.upower = None self.use_design_capacity = design self.bat_path = f'/org/freedesktop/UPower/devices/battery_BAT{batn}' def run(self): ''' Run the event loop ''' self.upower = self.bus.get('org.freedesktop.UPower', '/org/freedesktop/UPower') if self.bat_path in self.upower.EnumerateDevices(): self.add_battery(self.bat_path) else: self.upower.onDeviceAdded = self.add_battery # Handle SIGTERM and SIGINT signal endloop = lambda signum, fname: self.loop.quit() signal.signal(signal.SIGINT, endloop) signal.signal(signal.SIGTERM, endloop) self.loop.run() return 0 def add_battery(self, dev_path): ''' Add the battery when the battery exists ''' if dev_path == self.bat_path: self.upower.onDeviceRemoved = self.remove_battery if self.upower.onDeviceAdded is not None: self.upower.onDeviceAdded = None self.bat = self.bus.get('org.freedesktop.UPower', self.bat_path) self.bat.onPropertiesChanged = self.update_battery self.update_battery() def remove_battery(self, dev_path): ''' Remove the battery when the battery not exists ''' if dev_path == self.bat_path: self.upower.onDeviceAdded = self.add_battery self.upower.onDeviceRemoved = None self.bat.onPropertiesChanged = None self.bat = None self.update_battery() def update_battery(self, ifname=None, changed_props=None, invalid_props=None): ''' Print the updated battery info ''' del ifname, invalid_props if (changed_props is None) or (('Energy' in changed_props) or ('State' in changed_props)): text = '' css_class = '' if self.bat is not None: bat_state = BAT_STATE[self.bat.State] bat_energy_empty = self.bat.EnergyEmpty bat_energy_full = self.bat.EnergyFullDesign if \ self.use_design_capacity else self.bat.EnergyFull # Calculate the battery capacity percentage bat_capacity = 100 * (self.bat.Energy - bat_energy_empty) \ / (bat_energy_full - bat_energy_empty) # Find the battery capacity label for key, value in BAT_LEVEL.items(): if bat_capacity <= key: bat_level = value break if bat_state['class'] == BAT_STATE[2]['class']: if bat_capacity <= BAT_STATE[2]['crit']['level']: css_class = BAT_STATE[2]['crit']['class'] elif bat_capacity <= BAT_STATE[2]['warn']['level']: css_class = BAT_STATE[2]['warn']['class'] if css_class == '': css_class = bat_state['class'] text = f"{bat_state['label']}{bat_level}{bat_capacity:.2f}%" print(f'{{"text": "{text}", "class": "{css_class}"}}', flush=True)
class Daemon(ManagesProcesses): INTERFACE_NAME = "pro.wizardsoftheweb.pyrofibus.daemon" dbus = """ <node> <interface name='{}'> <method name='is_running'> <arg type='b' name='response' direction='out'/> </method> <method name='start'> </method> <method name='stop'> </method> <method name='load_apps'> <arg type='i' name='response' direction='out'/> </method> </interface> </node> """.format(INTERFACE_NAME) _is_running = False def __init__(self, bus=None, loop=None, *args, **kwargs): super(Daemon, self).__init__(*args, **kwargs) self.initialize_bus(bus, loop) def initialize_bus(self, bus=None, loop=None): if bus is None: self.bus = SessionBus() else: self.bus = bus if loop is None: self.loop = MainLoop() else: self.loop = loop self.bus.publish(self.INTERFACE_NAME, self) def start(self): if not self._is_running: try: self._is_running = True self.loop.run() except KeyboardInterrupt: self.loop.quit() self._is_running = False def is_running(self): return self._is_running def stop(self): self.loop.quit() self._is_running = False def load_apps(self): old_length = len(self.managed_processes) new_scripts = self.check_for_new_scripts() self.load_new_scripts(new_scripts) new_length = len(self.managed_processes) return new_length - old_length @classmethod def bootstrap(cls): daemon = cls() daemon.start()