From ae0f589d43ffcd1edbbfadeb02a0a566d0d65d2e Mon Sep 17 00:00:00 2001 From: Dan O'Reilly Date: Fri, 27 Feb 2009 00:08:31 -0500 Subject: [PATCH] Fix issue where signal strength wouldn't be reported correctly if the first link quality regex didn't match. Add some helper methods that will eventually be used for encryption template parsing. Use more specific exception checking in a few places. --- .bzrignore | 1 + wicd/backends/be-ioctl.py | 26 +++++++----------- wicd/misc.py | 38 ++++++++++++++++++++++++--- wicd/wicd-client.py | 2 +- wicd/wicd-daemon.py | 2 +- wicd/wnettools.py | 55 ++++++++++++++++----------------------- 6 files changed, 70 insertions(+), 54 deletions(-) diff --git a/.bzrignore b/.bzrignore index 4521dc6..29a8142 100644 --- a/.bzrignore +++ b/.bzrignore @@ -23,3 +23,4 @@ scripts/wicd-client scripts/wicd-curses translations/* wicd/wpath.py +*tags diff --git a/wicd/backends/be-ioctl.py b/wicd/backends/be-ioctl.py index e421ced..b4681a0 100644 --- a/wicd/backends/be-ioctl.py +++ b/wicd/backends/be-ioctl.py @@ -33,8 +33,7 @@ from wicd import misc from wicd import wnettools from wicd import wpath from wicd.wnettools import * -from wicd.wnettools import strength_pattern, altstrength_pattern, wep_pattern, \ - signaldbm_pattern +from wicd.wnettools import wep_pattern, signaldbm_pattern import iwscan import wpactrl @@ -311,17 +310,10 @@ class WirelessInterface(Interface, wnettools.BaseWirelessInterface): # Link Quality ap['qual_found'] = True - try: - [(strength, max_strength)] = strength_pattern.findall(cell["stats"]) - if max_strength: - ap["quality"] = 100 * int(strength) // int(max_strength) - else: - ap["quality"] = int(strength) - except ValueError: - ap['quality'] = misc.RunRegex(altstrength_pattern,cell["stats"]) - if not ap['quality']: - ap['qual_found'] = False - ap['quality'] = -1 + ap['quality'] = self._get_link_quality(cell['stats']) + if ap['quality'] is None: + ap['qual_found'] = False + ap['quality'] = -1 # Signal Strength (only used if user doesn't want link # quality displayed or it isn't found) @@ -498,10 +490,12 @@ class WirelessInterface(Interface, wnettools.BaseWirelessInterface): buff = get_iw_ioctl_result(self.iface, SIOCGIWSTATS) strength = ord(buff[2]) max_strength = self._get_max_strength() - if strength and max_strength: + if strength not in ['', None] and max_strength: return 100 * int(strength) // int(max_strength) - - return strength + elif strength not in ['', None]: + return int(strength) + else: + return None def _get_max_strength(self): """ Gets the maximum possible strength from the wireless driver. """ diff --git a/wicd/misc.py b/wicd/misc.py index e0a69c6..feafbf7 100644 --- a/wicd/misc.py +++ b/wicd/misc.py @@ -26,6 +26,7 @@ import gobject from threading import Thread from subprocess import Popen, STDOUT, PIPE, call from commands import getoutput +from itertools import repeat, chain, izip # wicd imports import wpath @@ -243,9 +244,11 @@ def ParseEncryption(network): # Write the data to the files then chmod them so they can't be read # by normal users. - f = open(wpath.networks + network["bssid"].replace(":", "").lower(), "w") - os.chmod(wpath.networks + network["bssid"].replace(":", "").lower(), 0600) - os.chown(wpath.networks + network["bssid"].replace(":", "").lower(), 0, 0) + file_loc = os.path.join(wpath.networks, + network['bssid'].replace(":", "").lower()) + f = open(file_loc, "w") + os.chmod(file_loc, 0600) + os.chown(file_loc, 0, 0) # We could do this above, but we'd like to read protect # them before we write, so that it can't be read. f.write(config_file) @@ -690,4 +693,31 @@ def timeout_add(time, func, milli=False): else: if not milli: time = time * 1000 return gobject.timeout_add(time, func) - + +def izip_longest(*args, **kwds): + """ Implement the itertools.izip_longest method. + + We implement the method here because its new in Python 2.6. + + """ + # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- + fillvalue = kwds.get('fillvalue') + def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): + yield counter() # yields the fillvalue, or raises IndexError + fillers = repeat(fillvalue) + iters = [chain(it, sentinel(), fillers) for it in args] + try: + for tup in izip(*iters): + yield tup + except IndexError: + pass + +def grouper(n, iterable, fillvalue=None): + """ Iterate over several elements at once + + "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" + + """ + args = [iter(iterable)] * n + return izip_longest(fillvalue=fillvalue, *args) + diff --git a/wicd/wicd-client.py b/wicd/wicd-client.py index 36c8ba5..b1a0c91 100755 --- a/wicd/wicd-client.py +++ b/wicd/wicd-client.py @@ -625,7 +625,7 @@ class TrayIcon(object): def __init__(self, use_tray=True): TrayIcon.TrayIconGUI.__init__(self, use_tray) self.use_tray = use_tray - if not use_tray: + if not use_tray: self.toggle_wicd_gui() return diff --git a/wicd/wicd-daemon.py b/wicd/wicd-daemon.py index 96ae287..c4015ce 100644 --- a/wicd/wicd-daemon.py +++ b/wicd/wicd-daemon.py @@ -1053,7 +1053,7 @@ class WirelessDaemon(dbus.service.Object): """ Returns the current signal strength. """ try: strength = int(self.wifi.GetSignalStrength(iwconfig)) - except: + except TypeError: strength = 0 return strength diff --git a/wicd/wnettools.py b/wicd/wnettools.py index 2bfe775..774bb6d 100644 --- a/wicd/wnettools.py +++ b/wicd/wnettools.py @@ -47,7 +47,7 @@ essid_pattern = re.compile('.*ESSID:"?(.*?)"?\s*\n', __re_mode) ap_mac_pattern = re.compile('.*Address: (.*?)\n', __re_mode) channel_pattern = re.compile('.*Channel:? ?(\d\d?)', __re_mode) strength_pattern = re.compile('.*Quality:?=? ?(\d+)\s*/?\s*(\d*)', __re_mode) -altstrength_pattern = re.compile('.*Signal level:?=? ?(\d\d*)', __re_mode) +altstrength_pattern = re.compile('.*Signal level:?=? ?(\d+)\s*/?\s*(\d*)', __re_mode) signaldbm_pattern = re.compile('.*Signal level:?=? ?(-\d\d*)', __re_mode) mode_pattern = re.compile('.*Mode:(.*?)\n', __re_mode) freq_pattern = re.compile('.*Frequency:(.*?)\n', __re_mode) @@ -383,7 +383,7 @@ class BaseInterface(object): the connection attempt. Keyword arguments: - pipe -- stdout pipe to the dhcpcd process. + pipe -- stdout pipe to the dhclient process. Returns: 'success' if succesful', an error code string otherwise. @@ -408,7 +408,7 @@ class BaseInterface(object): """ Determines if obtaining an IP using pump succeeded. Keyword arguments: - pipe -- stdout pipe to the dhcpcd process. + pipe -- stdout pipe to the pump process. Returns: 'success' if succesful, an error code string otherwise. @@ -443,7 +443,7 @@ class BaseInterface(object): while not dhcpcd_complete: line = pipe.readline() - if line.startswith("Error"): + if "Error" in line or "timed out" in line: dhcpcd_success = False dhcpcd_complete = True elif line == '': @@ -1083,18 +1083,8 @@ class BaseWirelessInterface(BaseInterface): # Link Quality # Set strength to -1 if the quality is not found - - # more of the patch from - # https://bugs.launchpad.net/wicd/+bug/175104 - if (strength_pattern.match(cell)): - [(strength, max_strength)] = strength_pattern.findall(cell) - if max_strength: - ap["quality"] = 100 * int(strength) // int(max_strength) - else: - ap["quality"] = int(strength) - elif misc.RunRegex(altstrength_pattern,cell): - ap['quality'] = misc.RunRegex(altstrength_pattern, cell) - else: + ap['quality'] = self._get_link_quality(cell) + if ap['quality'] is None: ap['quality'] = -1 # Signal Strength (only used if user doesn't want link @@ -1190,6 +1180,22 @@ class BaseWirelessInterface(BaseInterface): bssid = misc.RunRegex(bssid_pattern, output) return bssid + def _get_link_quality(self, output): + """ Parse out the link quality from iwlist scan or iwconfig output. """ + try: + [(strength, max_strength)] = strength_pattern.findall(output) + except ValueError: + (strength, max_strength) = (None, None) + + if strength in ['', None]: + [(strength, max_strength)] = altstrength_pattern.findall(output) + if strength not in ['', None] and max_strength: + return (100 * int(strength) // int(max_strength)) + elif strength not in ["", None]: + return int(strength) + else: + return None + def GetSignalStrength(self, iwconfig=None): """ Get the signal strength of the current network. @@ -1203,22 +1209,7 @@ class BaseWirelessInterface(BaseInterface): output = misc.Run(cmd) else: output = iwconfig - - strength_pattern = re.compile('.*Quality:?=? ?(\d+)\s*/?\s*(\d*)', - re.I | re.M | re.S) - altstrength_pattern = re.compile('.*Signal level:?=? ?(\d\d*)', re.I | re.M | re.S) - [(strength, max_strength)] = strength_pattern.findall(output) - if max_strength and strength: - if int(max_strength) != 0: - return 100 * int(strength) // int(max_strength) - else: - # Prevent a divide by zero error. - strength = int(strength) - - if strength is None: - strength = misc.RunRegex(altstrength_pattern, output) - - return strength + return self._get_link_quality(output) def GetDBMStrength(self, iwconfig=None): """ Get the dBm signal strength of the current network.