source: trunk/LocationDo/action.py@ 205

Last change on this file since 205 was 205, checked in by Nicholas Riley, 18 years ago

LocationDo.py: Add a bunch of TODOs, and some more locations and
actions.

action.py: Add a TODO.

SCNetworkReachabilitymodule.c: Add a TODO.

File size: 3.2 KB
Line 
1from Authorization import Authorization, kAuthorizationFlagDestroyRights
2from Foundation import NSBundle, NSNotificationCenter, NSObject, NSURL
3from AppKit import NSWorkspace
4from appscript import app, k, its
5import os, osax, subprocess, tempfile, SCNetworkReachability
6
7appBundle = NSBundle.mainBundle()
8
9def ensureKerberosPrincipalsValid(principals):
10 kerberosApp = app(id='edu.mit.Kerberos.KerberosApp')
11 validPrincipals = kerberosApp.caches.filter(its.time_remaining != 'Expired').principal.get()
12 for principal in principals:
13 if principal not in validPrincipals:
14 # XXX make these async
15 kerberosApp.renew_tickets(for_principal=principal)
16 # kerberosApp.get_tickets(for_principal=principal)
17
18def setVolumePercent(percent):
19 # XXX should use CoreAudio: see Pester/Source/NJRSoundManager.m
20 osax.setvolume(int(7 * percent))
21
22def _openURL(url):
23 ws = NSWorkspace.sharedWorkspace()
24 url = NSURL.URLWithString_(url)
25 return ws.openURL_(url)
26
27class _LDURLWatcher(NSObject):
28 def URLIsReachable_(self, notification):
29 _openURL(notification.object())
30
31_urlWatcher = _LDURLWatcher.alloc().init()
32NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(
33 _urlWatcher, 'URLIsReachable:', SCNetworkReachability.SCNetworkIsReachable,
34 None)
35
36def openURL(url):
37 hostname = NSURL.URLWithString_(url).host()
38 if not SCNetworkReachability.notify_when_reachable(hostname, url):
39 _openURL(url)
40
41def openInBackground(path):
42 ws = NSWorkspace.sharedWorkspace()
43 path = os.path.expanduser(path)
44 return ws.openFile_withApplication_andDeactivate_(path, None, False)
45
46def setDisplayBrightnessPercent(percent):
47 # XXX create brightness module
48 pathToBrightness = appBundle.pathForResource_ofType_('brightness', None)
49 subprocess.call([pathToBrightness, str(percent)], stderr=subprocess.STDOUT,
50 stdout=subprocess.PIPE)
51
52def setAdiumStatus(status):
53 # XXX doesn't match preset status if available
54 adiumApp = app(id='com.adiumX.adiumX')
55 if adiumApp.my_status_type() == k.offline:
56 adiumApp.my_status.status_message_string.set(status)
57 else:
58 adiumApp.my_status_message.set(status)
59 adiumApp.my_status_type.set(k.available)
60
61def terminalDo(command):
62 # XXX if this launches Terminal, it inherits our PYTHONPATH
63 terminalApp = app(id='com.apple.Terminal')
64 # XXX this does not create a new Terminal window; fix
65 terminalApp.do_script(command + '; exit')
66
67_auth = None
68
69def authorizationDo(command, *args):
70 global _auth
71 if not _auth:
72 _auth = Authorization(destroyflags=(kAuthorizationFlagDestroyRights,))
73 return _auth.executeWithPrivileges(command, *args)
74
75def stopVPNC():
76 # killall uses your uid, not your euid to determine which user's
77 # processes to kill; without '-u root', this fails
78 authorizationDo('/usr/bin/killall', '-u', 'root', 'vpnc')
79
80def startVPNC(vpncProfile=None):
81 stopVPNC()
82 args = ['--kernel-ipsec']
83 if vpncProfile:
84 args.append('/etc/vpnc/%s.conf' % vpncProfile)
85 # XXX get password from keychain, then use:
86 # authorizationDo('/usr/local/sbin/vpnc', *args)
87 terminalDo('sudo /usr/local/sbin/vpnc %s' % ' '.join(args))
Note: See TracBrowser for help on using the repository browser.