import re
import subprocess


class WifiWidget(object):
    def __init__(self, interface='wlan0'):
        self.interface = interface

    def output(self):
        try:
            out = subprocess.check_output(['iwgetid'])
            out = out.decode('utf-8')
        except subprocess.CalledProcessError:
            return

        m = re.match(r'{interface}\s+ESSID:"(.+?)"\n'.format(
            interface=self.interface), out)

        if m:
            return {
                'name': "wifi",
                'instance': self.interface,
                'full_text': m.group(1),
            }

@@ -4,18 +4,21 @@
 ort 
-re
+array
 %0Aimport 
 subp
@@ -17,46 +17,197 @@
 ort 
-subprocess%0A%0A%0Aclass WifiWidget(object):
+fcntl%0Aimport socket%0Aimport struct%0A%0A%0Aclass WifiWidget(object):%0A    INTERFACE_MAXLEN = 16%0A%0A    # these values come from linux/wireless.h (V22)%0A    ESSID_MAXLEN = 32%0A    SIOCGIWESSID = 0x8B1B%0A
 %0A   
@@ -290,22 +290,21 @@
     def 
-output
+essid
 (self):%0A
@@ -315,92 +315,466 @@
     
-try:%0A            out = subprocess.check_output(%5B'iwgetid'%5D)%0A            out = out.de
+# danger: Python that is written like C :)%0A        # http://stackoverflow.com/a/14142016%0A%0A        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)%0A%0A        # allocate a buffer for the essid%0A        essid = array.array('b', b'%5C0' * self.ESSID_MAXLEN)%0A        eptr, elen = essid.buffer_info()%0A%0A        # allocate and fill buffer for ioctl call%0A        request = array.array(%0A            'b',%0A            self.interface.ljust(self.INTERFACE_MAXLEN, '%5C0').en
 code
@@ -782,25 +782,29 @@
 'utf-8')
-%0A
+ +%0A  
         
 except s
@@ -799,171 +799,407 @@
     
-except subprocess.CalledProcessError:%0A            return%0A%0A        m = re.match(r'%7Binterface%7D%5Cs+ESSID:%22(.+?)%22%5Cn'.format(%0A            interface=self.interface), out)
+  struct.pack('PHH', eptr, elen, 0))%0A%0A        # make ioctl call; essid will be placed into essid buffer%0A        fcntl.ioctl(s.fileno(), self.SIOCGIWESSID, request)%0A        essid = essid.tobytes().rstrip(b'%5C0')%0A%0A        if essid:%0A            return essid.decode('utf-8')%0A        return None%0A%0A    def output(self):%0A        try:%0A            essid = self.essid()%0A        except:%0A            essid = None
 %0A%0A  
@@ -1207,17 +1207,21 @@
      if 
-m
+essid
 :%0A      
@@ -1344,18 +1344,13 @@
 t': 
-m.group(1)
+essid
 ,%0A  

switch to ioctl instead of iwgetid calls