1 # -*- coding: utf-8 -*- |
1 # -*- coding: utf-8 -*- |
2 |
2 |
3 # Copyright (c) 2013 - 2014 Detlev Offenbach <detlev@die-offenbachs.de> |
3 # Copyright (c) 2013 - 2014 Detlev Offenbach <detlev@die-offenbachs.de> |
4 # |
4 # |
|
5 # pylint: disable=C0103 |
5 |
6 |
6 """ |
7 """ |
7 Module implementing a Qt free version of a background client for the various |
8 Module implementing a Qt free version of a background client for the various |
8 checkers and other python interpreter dependent functions. |
9 checkers and other python interpreter dependent functions. |
9 """ |
10 """ |
13 bytes = unicode #__IGNORE_WARNING__ |
14 bytes = unicode #__IGNORE_WARNING__ |
14 except NameError: |
15 except NameError: |
15 pass |
16 pass |
16 |
17 |
17 import json |
18 import json |
18 import os |
|
19 import socket |
19 import socket |
20 import struct |
20 import struct |
21 import sys |
21 import sys |
22 from zlib import adler32 |
22 from zlib import adler32 |
23 |
|
24 if __name__ == '__main__': |
|
25 # Add Eric basepath to sys.path to be able to import modules which are |
|
26 # laying not only below Utilities |
|
27 path = os.path.dirname(sys.argv[0]) |
|
28 path = os.path.dirname(path) |
|
29 sys.path.append(path) |
|
30 |
|
31 from Plugins.CheckerPlugins.SyntaxChecker import SyntaxCheck |
|
32 |
23 |
33 |
24 |
34 class BackgroundClient(object): |
25 class BackgroundClient(object): |
35 """ |
26 """ |
36 Class implementing the main part of the background client. |
27 Class implementing the main part of the background client. |
40 Constructor of the BackgroundClient class. |
31 Constructor of the BackgroundClient class. |
41 |
32 |
42 @param host ip address the background service is listening |
33 @param host ip address the background service is listening |
43 @param port port of the background service |
34 @param port port of the background service |
44 """ |
35 """ |
|
36 self.services = {} |
|
37 |
45 self.connection = socket.create_connection((host, port)) |
38 self.connection = socket.create_connection((host, port)) |
46 ver = b'2' if sys.version_info[0] == 2 else b'3' |
39 ver = b'2' if sys.version_info[0] == 2 else b'3' |
47 self.connection.sendall(ver) |
40 self.connection.sendall(ver) |
48 self.connection.settimeout(0.25) |
41 self.connection.settimeout(0.25) |
|
42 |
|
43 def __initClientService(self, fn, path, module): |
|
44 """ |
|
45 Import the given module and register it as service. |
|
46 |
|
47 @param fn service name to register (str) |
|
48 @param path contains the path to the module (str) |
|
49 @param module name to import (str) |
|
50 @return text result of the import action (str) |
|
51 """ |
|
52 sys.path.append(path) |
|
53 try: |
|
54 importedModule = __import__(module, globals(), locals(), [], 0) |
|
55 self.services[fn] = importedModule.initService() |
|
56 return 'ok' |
|
57 except ImportError: |
|
58 return 'Import Error' |
49 |
59 |
50 def __send(self, fx, fn, data): |
60 def __send(self, fx, fn, data): |
51 """ |
61 """ |
52 Private method to send a job response back to the BackgroundService. |
62 Private method to send a job response back to the BackgroundService. |
53 |
63 |
78 # Leave main loop if connection was closed. |
88 # Leave main loop if connection was closed. |
79 if not header: |
89 if not header: |
80 break |
90 break |
81 |
91 |
82 length, datahash = struct.unpack(b'!II', header) |
92 length, datahash = struct.unpack(b'!II', header) |
83 |
|
84 packedData = b'' |
93 packedData = b'' |
85 while len(packedData) < length: |
94 while len(packedData) < length: |
86 packedData += self.connection.recv(length - len(packedData)) |
95 packedData += self.connection.recv(length - len(packedData)) |
87 |
96 |
88 assert adler32(packedData) & 0xffffffff == datahash, \ |
97 assert adler32(packedData) & 0xffffffff == datahash, \ |
89 'Hashes not equal' |
98 'Hashes not equal' |
90 if sys.version_info[0] == 3: |
99 if sys.version_info[0] == 3: |
91 packedData = packedData.decode('utf-8') |
100 packedData = packedData.decode('utf-8') |
|
101 |
92 fx, fn, data = json.loads(packedData) |
102 fx, fn, data = json.loads(packedData) |
93 if fx == 'syntax': |
103 if fx == 'INIT': |
94 ret = SyntaxCheck.syntaxAndPyflakesCheck(fn, *data) |
104 ret = self.__initClientService(fn, *data) |
95 elif fx == 'style': |
|
96 print(data) |
|
97 elif fx == 'indent': |
|
98 pass |
|
99 else: |
105 else: |
100 continue |
106 callback = self.services.get(fx) |
|
107 if callback: |
|
108 ret = callback(fn, *data) |
|
109 else: |
|
110 ret = 'Unknown service.' |
101 |
111 |
102 self.__send(fx, fn, ret) |
112 self.__send(fx, fn, ret) |
103 |
113 |
104 self.connection.close() |
114 self.connection.close() |
105 sys.exit() |
115 sys.exit() |