def _run_worker_thread(self): """Runs the worker thread that waits for network events.""" from Foundation import ( CFRunLoopAddSource, CFRunLoopGetCurrent, kCFRunLoopCommonModes, CFRunLoopRun, ) from SystemConfiguration import ( SCDynamicStoreCreate, SCDynamicStoreSetNotificationKeys, SCDynamicStoreCreateRunLoopSource, ) store = SCDynamicStoreCreate(None, "global-network-watcher", self._on_network_changed, None) SCDynamicStoreSetNotificationKeys( store, None, ["State:/Network/Global/IPv4", "State:/Network/Global/IPv6"]) CFRunLoopAddSource( CFRunLoopGetCurrent(), SCDynamicStoreCreateRunLoopSource(None, store, 0), kCFRunLoopCommonModes, ) CFRunLoopRun()
def watchForProxyChanges(self): """ install a watcher for proxy changes """ SCDynamicStoreSetNotificationKeys(self.store, None, ['State:/Network/Global/Proxies']) source = SCDynamicStoreCreateRunLoopSource(None, self.store, 0) loop = NSRunLoop.currentRunLoop().getCFRunLoop() CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes)
def main(): # Setup a dynamic store controller that monitors all keys: store = SCDynamicStoreCreate(None, "demo.controller", dynamicStoreChanged, None) SCDynamicStoreSetNotificationKeys(store, None, [".*"]) source = SCDynamicStoreCreateRunLoopSource(None, store, 0) # Setup a preferences controller prefs = SCPreferencesCreate(None, "demo.prefs", None) SCPreferencesSetCallback(prefs, prefsChanged, None) # Set up a run loop and add all controllers to that. loop = CFRunLoopGetCurrent() CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes) SCPreferencesScheduleWithRunLoop(prefs, loop, kCFRunLoopCommonModes) signal.signal(signal.SIGINT, sigint) CFRunLoopRun()
####################### timer_interval = 2.0 if __name__ == '__main__': # log("Initializing") run_at = None if connected(): run_prey() sc_keys = ['State:/Network/Global/IPv4', 'State:/Network/Global/IPv6'] store = SCDynamicStoreCreate(None, "global-network-change", network_state_changed, None) SCDynamicStoreSetNotificationKeys(store, None, sc_keys) CFRunLoopAddSource( # NSRunLoop.currentRunLoop().getCFRunLoop(), CFRunLoopGetCurrent(), SCDynamicStoreCreateRunLoopSource(None, store, 0), kCFRunLoopCommonModes) # signal.signal(signal.SIGHUP, partial(quit, "SIGHUP received")) # NOTE: This timer is basically a kludge around the fact that we can't reliably get # signals or Control-C inside a runloop. This wakes us up often enough to # appear tolerably responsive: CFRunLoopAddTimer( NSRunLoop.currentRunLoop().getCFRunLoop(), CFRunLoopTimerCreate(None, CFAbsoluteTimeGetCurrent(), timer_interval,
key, sc_config[key], context="SystemConfiguration: %s" % key) if key.startswith("regexp:"): # strip "regexp:" re_key = key[7:] REGEXP_SC_HANDLERS[re.compile(re_key)] = handler regexp_sc_keys.append(re_key) else: EXPLICIT_SC_HANDLERS[key] = handler except AttributeError, exc: print >> sys.stderr, "Error configuring SystemConfiguration events: %s" % exc sys.exit(1) store = get_sc_store() SCDynamicStoreSetNotificationKeys(store, EXPLICIT_SC_HANDLERS.keys(), regexp_sc_keys) # Get a CFRunLoopSource for our store session and add it to the application's runloop: CFRunLoopAddSource(NSRunLoop.currentRunLoop().getCFRunLoop(), SCDynamicStoreCreateRunLoopSource(None, store, 0), kCFRunLoopCommonModes) log_list("Listening for these SystemConfiguration events: %s", keys) def add_mdns_notifications(mdns_config): for type_ in mdns_config: browser = MDNSBrowser.new() browser.callable = get_callable_for_event( type_, mdns_config[type_],