1 | from Authorization import Authorization, kAuthorizationFlagDestroyRights
|
---|
2 | from Foundation import NSBundle, NSNotificationCenter, NSObject, NSURL
|
---|
3 | from AppKit import NSWorkspace
|
---|
4 | from appscript import app, k, its
|
---|
5 | import os, osax, subprocess, tempfile, SCNetworkReachability
|
---|
6 |
|
---|
7 | appBundle = NSBundle.mainBundle()
|
---|
8 |
|
---|
9 | def 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 |
|
---|
18 | def setVolumePercent(percent):
|
---|
19 | # XXX should use CoreAudio: see Pester/Source/NJRSoundManager.m
|
---|
20 | osax.setvolume(int(7 * percent))
|
---|
21 |
|
---|
22 | def _openURL(url):
|
---|
23 | ws = NSWorkspace.sharedWorkspace()
|
---|
24 | url = NSURL.URLWithString_(url)
|
---|
25 | return ws.openURL_(url)
|
---|
26 |
|
---|
27 | class _LDURLWatcher(NSObject):
|
---|
28 | def URLIsReachable_(self, notification):
|
---|
29 | _openURL(notification.object())
|
---|
30 |
|
---|
31 | _urlWatcher = _LDURLWatcher.alloc().init()
|
---|
32 | NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(
|
---|
33 | _urlWatcher, 'URLIsReachable:', SCNetworkReachability.SCNetworkIsReachable,
|
---|
34 | None)
|
---|
35 |
|
---|
36 | def openURL(url):
|
---|
37 | hostname = NSURL.URLWithString_(url).host()
|
---|
38 | if not SCNetworkReachability.notify_when_reachable(hostname, url):
|
---|
39 | _openURL(url)
|
---|
40 |
|
---|
41 | def openInBackground(path):
|
---|
42 | ws = NSWorkspace.sharedWorkspace()
|
---|
43 | path = os.path.expanduser(path)
|
---|
44 | return ws.openFile_withApplication_andDeactivate_(path, None, False)
|
---|
45 |
|
---|
46 | def 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 |
|
---|
52 | def 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 |
|
---|
61 | def terminalDo(command):
|
---|
62 | terminalApp = app(id='com.apple.Terminal')
|
---|
63 | terminalApp.do_script(command + '; exit')
|
---|
64 |
|
---|
65 | _auth = None
|
---|
66 |
|
---|
67 | def authorizationDo(command, *args):
|
---|
68 | global _auth
|
---|
69 | if not _auth:
|
---|
70 | _auth = Authorization(destroyflags=(kAuthorizationFlagDestroyRights,))
|
---|
71 | return _auth.executeWithPrivileges(command, *args)
|
---|
72 |
|
---|
73 | def stopVPNC():
|
---|
74 | authorizationDo('/usr/bin/killall', 'vpnc')
|
---|
75 |
|
---|
76 | def startVPNC(vpncProfile=None):
|
---|
77 | stopVPNC()
|
---|
78 | args = ['--kernel-ipsec']
|
---|
79 | if vpncProfile:
|
---|
80 | args.append('/etc/vpnc/%s.conf' % vpncProfile)
|
---|
81 | # XXX get password from keychain, then use:
|
---|
82 | # authorizationDo('/usr/local/sbin/vpnc', *args)
|
---|
83 | terminalDo('sudo /usr/local/sbin/vpnc %s' % ' '.join(args))
|
---|