source: trunk/LocationDo/action.py@ 196

Last change on this file since 196 was 196, checked in by Nicholas Riley, 19 years ago

LocationDo

File size: 2.9 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 terminalApp = app(id='com.apple.Terminal')
63 terminalApp.do_script(command + '; exit')
64
65_auth = None
66
67def authorizationDo(command, *args):
68 global _auth
69 if not _auth:
70 _auth = Authorization(destroyflags=(kAuthorizationFlagDestroyRights,))
71 return _auth.executeWithPrivileges(command, *args)
72
73def stopVPNC():
74 authorizationDo('/usr/bin/killall', 'vpnc')
75
76def 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))
Note: See TracBrowser for help on using the repository browser.