Package pywingo
[frames] | no frames]

Source Code for Package pywingo

 1  import os 
 2  import os.path 
 3  import socket 
 4   
 5  from pywingo.commands import WingoCommands 
 6   
 7  _bool_cmds = ['True', 'False', 'Not', 'And', 'Or'] 
 8   
9 -class WingoError(Exception):
10 - def __init__(self, message):
11 self.message = message
12
13 - def __str__(self):
14 return self.message
15
16 -class Wingo(WingoCommands):
17 - def __init__(self):
18 19 # Open socket 20 self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) 21 f = os.path.join(os.getenv('XDG_RUNTIME_DIR'), 'wingo', 22 os.getenv('DISPLAY')) 23 self.sock.connect(f)
24
25 - def __del__(self):
26 # Close socket 27 self.sock.close()
28
29 - def __recv(self, sock):
30 data = '' 31 while chr(0) not in data: 32 data += sock.recv(4096) 33 if chr(0) in data: 34 data = data[0:data.index(chr(0))] 35 return data
36
37 - def gribble(self, cmd):
38 self.sock.send("%s%s" % (cmd, chr(0))) 39 return self.__recv(self.sock)
40
41 - def _assert_arg_type(self, name, val, types):
42 for t in types: 43 if isinstance(val, t): 44 return 45 assert False, '%s has invalid type %s' % (name, type(val))
46
47 - def _gribble_arg_str(self, vals):
48 args = [] 49 for v in vals: 50 if isinstance(v, int) or isinstance(v, float): 51 args.append(repr(v)) 52 elif isinstance(v, basestring): 53 args.append('"%s"' % self._escape_str(v)) 54 else: 55 assert False, 'bug' 56 return ' '.join(args)
57
58 - def _escape_str(self, s):
59 return s.replace('"', '\\"')
60
61 - def _from_str(self, cmd_name, s):
62 if cmd_name in _bool_cmds or cmd_name.startswith('Match'): 63 return bool(int(s)) 64 65 try: 66 return int(s) 67 except ValueError: 68 try: 69 return float(s) 70 except ValueError: 71 if s.startswith('ERROR:'): 72 raise WingoError(s) 73 elif '\n' in s: 74 return map(lambda item: self._from_str(cmd_name, item), 75 s.strip().split('\n')) 76 else: 77 return s 78 79 assert False, 'bug'
80