Make multi-wizard definition override the previous by default
[odoo/odoo.git] / bin / tiny_socket.py
1 import socket
2 import cPickle
3 import marshal
4
5 class Myexception(Exception):
6         def __init__(self, faultCode, faultString):
7                 self.faultCode = faultCode
8                 self.faultString = faultString
9                 self.args = (faultCode, faultString)
10
11 class mysocket:
12         def __init__(self, sock=None):
13                 if sock is None:
14                         self.sock = socket.socket(
15                         socket.AF_INET, socket.SOCK_STREAM)
16                 else:
17                         self.sock = sock
18                 self.sock.settimeout(120)
19         def connect(self, host, port=False):
20                 if not port:
21                         protocol, buf = host.split('//')
22                         host, port = buf.split(':')
23                 self.sock.connect((host, int(port)))
24         def disconnect(self):
25                 self.sock.shutdown(socket.SHUT_RDWR)
26                 self.sock.close()
27         def mysend(self, msg, exception=False, traceback=None):
28                 msg = cPickle.dumps([msg,traceback])
29                 size = len(msg)
30                 self.sock.send('%8d' % size)
31                 self.sock.send(exception and "1" or "0")
32                 totalsent = 0
33                 while totalsent < size:
34                         sent = self.sock.send(msg[totalsent:])
35                         if sent == 0:
36                                 raise RuntimeError, "socket connection broken"
37                         totalsent = totalsent + sent
38         def myreceive(self):
39                 buf=''
40                 while len(buf) < 8:
41                         chunk = self.sock.recv(8 - len(buf))
42                         if chunk == '':
43                                 raise RuntimeError, "socket connection broken"
44                         buf += chunk
45                 size = int(buf)
46                 buf = self.sock.recv(1)
47                 if buf != "0":
48                         exception = buf
49                 else:
50                         exception = False
51                 msg = ''
52                 while len(msg) < size:
53                         chunk = self.sock.recv(size-len(msg))
54                         if chunk == '':
55                                 raise RuntimeError, "socket connection broken"
56                         msg = msg + chunk
57                 res = cPickle.loads(msg)
58                 if isinstance(res[0],Exception):
59                         if exception:
60                                 raise Myexception(str(res[0]), str(res[1]))
61                         raise res[0]
62                 else:
63                         return res[0]