1
0
mirror of https://github.com/gryf/wicd.git synced 2025-12-22 14:07:59 +01:00

merge in experimental branch

This commit is contained in:
Dan O'Reilly
2009-01-15 00:59:52 -05:00

View File

@@ -45,6 +45,7 @@ import re
import time import time
import threading import threading
import thread import thread
import re
# wicd imports # wicd imports
import misc import misc
@@ -178,7 +179,10 @@ class Controller(object):
iface = self.iface iface = self.iface
if self.disconnect_script != None: if self.disconnect_script != None:
print 'Running wired disconnect script' print 'Running wired disconnect script'
misc.Run(self.disconnect_script) iwconfig = self.GetIwconfig()
misc.ExecuteScript(expand_script_macros(self.disconnect_script, 'disconnection',
self.wiface.GetBSSID( iwconfig ),
self.wiface.GetCurrentNetwork( iwconfig ) ))
iface.ReleaseDHCP() iface.ReleaseDHCP()
iface.SetAddress('0.0.0.0') iface.SetAddress('0.0.0.0')
@@ -212,6 +216,31 @@ class Controller(object):
""" """
return self.iface.Down() return self.iface.Down()
def expand_script_macros(script, msg, bssid, essid):
"""Expands any supported macros in a script.
Keyword arguments:
script -- the script to execute.
msg -- the name of the script, %{script} will be expanded to this.
bssid -- the bssid of the network we connect to, defaults to 'wired'.
essid -- the essid of the network we connect to, defaults to 'wired'."""
macro_dict = { 'script' : msg,
'bssid' : bssid,
'essid' : essid }
# Define a replacement function, this is done here so that %{script} will be substituted correctly
def repl(match):
macro = match.group( 1 ).lower()
if macro_dict.has_key( macro ):
return macro_dict[macro]
print 'Warning: found illegal macro %s in %s script' % (macro, msg)
return match.group()
regex = re.compile( r'%\{([a-zA-Z0-9]+)\}' )
expanded = regex.sub( repl, script )
print "Expanded '%s' to '%s'" % (script, expanded)
return expanded
class ConnectThread(threading.Thread): class ConnectThread(threading.Thread):
""" A class to perform network connections in a multi-threaded way. """ A class to perform network connections in a multi-threaded way.
@@ -326,7 +355,7 @@ class ConnectThread(threading.Thread):
iface.Down() iface.Down()
@abortable @abortable
def run_script_if_needed(self, script, msg): def run_script_if_needed(self, script, msg, bssid='wired', essid='wired'):
""" Execute a given script if needed. """ Execute a given script if needed.
Keyword arguments: Keyword arguments:
@@ -336,7 +365,7 @@ class ConnectThread(threading.Thread):
""" """
if script: if script:
print 'Executing %s script' % (msg) print 'Executing %s script' % (msg)
misc.ExecuteScript(script) misc.ExecuteScript(expand_script_macros(script, msg, bssid, essid))
@abortable @abortable
def flush_routes(self, iface): def flush_routes(self, iface):
@@ -711,7 +740,8 @@ class WirelessConnectThread(ConnectThread):
self.is_connecting = True self.is_connecting = True
# Run pre-connection script. # Run pre-connection script.
self.run_script_if_needed(self.before_script, 'pre-connection') self.run_script_if_needed(self.before_script, 'pre-connection', self.network['bssid'], self.network['essid'])
# Take down interface and clean up previous connections. # Take down interface and clean up previous connections.
self.put_iface_down(wiface) self.put_iface_down(wiface)
@@ -752,7 +782,7 @@ class WirelessConnectThread(ConnectThread):
self.set_dns_addresses() self.set_dns_addresses()
# Run post-connection script. # Run post-connection script.
self.run_script_if_needed(self.after_script, 'post-connection') self.run_script_if_needed(self.after_script, 'post-connection', self.network['bssid'], self.network['essid'])
self.SetStatus('done') self.SetStatus('done')
print 'Connecting thread exiting.' print 'Connecting thread exiting.'
@@ -916,7 +946,7 @@ class WiredConnectThread(ConnectThread):
self.is_connecting = True self.is_connecting = True
# Run pre-connection script. # Run pre-connection script.
self.run_script_if_needed(self.before_script, 'pre-connection') self.run_script_if_needed(self.before_script, 'pre-connection', 'wired', 'wired')
# Take down interface and clean up previous connections. # Take down interface and clean up previous connections.
self.put_iface_down(liface) self.put_iface_down(liface)
@@ -934,7 +964,7 @@ class WiredConnectThread(ConnectThread):
self.set_dns_addresses() self.set_dns_addresses()
# Run post-connection script. # Run post-connection script.
self.run_script_if_needed(self.after_script, 'post-connection') self.run_script_if_needed(self.after_script, 'post-connection', 'wired', 'wired')
self.SetStatus('done') self.SetStatus('done')
print 'Connecting thread exiting.' print 'Connecting thread exiting.'