class ConnectedDeviceModel: """ Keeps track of the first device connected, until it becomes disconnected. The client is responsible for calling update() regularly. The connected attribute is (port, USBDevice) or None when no connected device. """ def __init__(self, view, known_devices=Devices.all): """ :param view: called with update((port,device)) when a device is connected and update(None) when disconnected """ self.view = view self.connected = None self.scan = USBScanner(known_devices) self.mgr = SetManager() self.mgr.events += lambda item, added: self.device_changed(item, added) def device_changed(self, item, added): if added != self.connected: # when added, we don't have a connection, and when removed we do self.connected = (item if added else None) self.view.update(self.connected) def update(self): current = self.scan.scan() self.mgr.update(current)
def __init__(self, view, known_devices=Devices.all): """ :param view: called with update((port,device)) when a device is connected and update(None) when disconnected """ self.view = view self.connected = None self.scan = USBScanner(known_devices) self.mgr = SetManager() self.mgr.events += lambda item, added: self.device_changed(item, added)
def main(): parser = argparse.ArgumentParser(description="Manage nftables sets") parser.add_argument("--debug", action='store_true', help="Enable debugging") parser.add_argument("--quiet", action='store_true', help="Silence output except for errors") parser.add_argument("--berserk", action='store_true', help="Add all berserker_ips config to the resolver") parser.add_argument( "--sets", action='store', metavar="SET", type=str, nargs='+', help= "Sets to update. Default is to update all sets in the configuration file" ) parser.add_argument("--config-file", type=str, metavar="FILE", default=DEFAULT_CONFIG_FILE, help="Configuration filepath, default: %s" % DEFAULT_CONFIG_FILE) parser.add_argument("--plugin-dir", type=str, metavar="FILE", default=DEFAULT_PLUGIN_DIR, help="Directory that contains plugins, default: %s" % DEFAULT_PLUGIN_DIR) args = parser.parse_args() with open(args.config_file, 'r') as stream: try: config = yaml.safe_load(stream) except yaml.YAMLError as err: raise RuntimeError("Could not load config file %s: %s" % (args.config_file, err)) manager = SetManager(args, config) manager.update_sets()