Sun, 18 Dec 2022 19:33:46 +0100
Refactored the Utilities and Globals modules in order to enhance the maintainability.
7335 | 1 | # -*- coding: utf-8 -*- |
2 | ||
8881
54e42bc2437a
Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8650
diff
changeset
|
3 | # Copyright (c) 2019 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
7335 | 4 | # |
5 | ||
6 | """ | |
7 | Module implementing the syntax check for JSON. | |
8 | """ | |
9 | ||
9482
a2bc06a54d9d
Corrected/acknowledged some bad import style and removed some obsolete code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
10 | import json |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9292
diff
changeset
|
11 | import multiprocessing |
7335 | 12 | import queue |
13 | ||
14 | ||
15 | def initService(): | |
16 | """ | |
17 | Initialize the service and return the entry point. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
18 | |
7335 | 19 | @return the entry point for the background client |
20 | @rtype func | |
21 | """ | |
22 | return jsonSyntaxCheck | |
23 | ||
24 | ||
25 | def initBatchService(): | |
26 | """ | |
27 | Initialize the batch service and return the entry point. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
28 | |
7335 | 29 | @return the entry point for the background client |
30 | @rtype func | |
31 | """ | |
32 | return jsonSyntaxBatchCheck | |
33 | ||
34 | ||
35 | def jsonSyntaxCheck(file, codestring): | |
36 | """ | |
37 | Function to check a JSON source file for syntax errors. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
38 | |
7335 | 39 | @param file source filename |
40 | @type str | |
41 | @param codestring string containing the code to check | |
42 | @type str | |
43 | @return dictionary with the keys 'error' and 'warnings' which | |
44 | hold a list containing details about the error/ warnings | |
45 | (file name, line number, column, codestring (only at syntax | |
46 | errors), the message, a list with arguments for the message) | |
47 | @rtype dict | |
48 | """ | |
49 | return __jsonSyntaxCheck(file, codestring) | |
50 | ||
51 | ||
52 | def jsonSyntaxBatchCheck(argumentsList, send, fx, cancelled, maxProcesses=0): | |
53 | """ | |
54 | Module function to check syntax for a batch of files. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
55 | |
7335 | 56 | @param argumentsList list of arguments tuples as given for yamlSyntaxCheck |
57 | @type list | |
58 | @param send reference to send function | |
59 | @type func | |
60 | @param fx registered service name | |
61 | @type str | |
62 | @param cancelled reference to function checking for a cancellation | |
63 | @type func | |
64 | @param maxProcesses number of processes to be used | |
65 | @type int | |
66 | """ | |
67 | if maxProcesses == 0: | |
68 | # determine based on CPU count | |
69 | try: | |
70 | NumberOfProcesses = multiprocessing.cpu_count() | |
71 | if NumberOfProcesses >= 1: | |
72 | NumberOfProcesses -= 1 | |
73 | except NotImplementedError: | |
74 | NumberOfProcesses = 1 | |
75 | else: | |
76 | NumberOfProcesses = maxProcesses | |
77 | ||
78 | # Create queues | |
79 | taskQueue = multiprocessing.Queue() | |
80 | doneQueue = multiprocessing.Queue() | |
81 | ||
9289
ba49c41e8f63
Did some optimizations in the multiprocessing code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9284
diff
changeset
|
82 | # Submit tasks (initially two times the number of processes) |
ba49c41e8f63
Did some optimizations in the multiprocessing code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9284
diff
changeset
|
83 | tasks = len(argumentsList) |
9292
a5c8a0213fe3
Fixed an issue in the multiprocessing usage causing a traceback when then number of tasks is smaller than the number of worker processes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9289
diff
changeset
|
84 | initialTasks = min(2 * NumberOfProcesses, tasks) |
9289
ba49c41e8f63
Did some optimizations in the multiprocessing code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9284
diff
changeset
|
85 | for _ in range(initialTasks): |
ba49c41e8f63
Did some optimizations in the multiprocessing code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9284
diff
changeset
|
86 | taskQueue.put(argumentsList.pop(0)) |
7335 | 87 | |
88 | # Start worker processes | |
8650
100726f55a9a
Changed the 'multiprocessing.Process()' code of the background batch services to (hopefully) cure the slow down when used multiple times.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
89 | workers = [ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
90 | multiprocessing.Process(target=workerTask, args=(taskQueue, doneQueue)) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
91 | for _ in range(NumberOfProcesses) |
8650
100726f55a9a
Changed the 'multiprocessing.Process()' code of the background batch services to (hopefully) cure the slow down when used multiple times.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
92 | ] |
100726f55a9a
Changed the 'multiprocessing.Process()' code of the background batch services to (hopefully) cure the slow down when used multiple times.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
93 | for worker in workers: |
100726f55a9a
Changed the 'multiprocessing.Process()' code of the background batch services to (hopefully) cure the slow down when used multiple times.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
94 | worker.start() |
7335 | 95 | |
96 | # Get and send results | |
9289
ba49c41e8f63
Did some optimizations in the multiprocessing code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9284
diff
changeset
|
97 | for _ in range(tasks): |
7335 | 98 | resultSent = False |
99 | wasCancelled = False | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
100 | |
7335 | 101 | while not resultSent: |
102 | try: | |
103 | # get result (waiting max. 3 seconds and send it to frontend | |
104 | filename, result = doneQueue.get() | |
105 | send(fx, filename, result) | |
106 | resultSent = True | |
107 | except queue.Empty: | |
108 | # ignore empty queue, just carry on | |
109 | if cancelled(): | |
110 | wasCancelled = True | |
111 | break | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
112 | |
7335 | 113 | if wasCancelled or cancelled(): |
114 | # just exit the loop ignoring the results of queued tasks | |
115 | break | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
116 | |
9289
ba49c41e8f63
Did some optimizations in the multiprocessing code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9284
diff
changeset
|
117 | if argumentsList: |
ba49c41e8f63
Did some optimizations in the multiprocessing code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9284
diff
changeset
|
118 | taskQueue.put(argumentsList.pop(0)) |
7335 | 119 | |
120 | # Tell child processes to stop | |
121 | for _ in range(NumberOfProcesses): | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
122 | taskQueue.put("STOP") |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
123 | |
8650
100726f55a9a
Changed the 'multiprocessing.Process()' code of the background batch services to (hopefully) cure the slow down when used multiple times.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
124 | for worker in workers: |
100726f55a9a
Changed the 'multiprocessing.Process()' code of the background batch services to (hopefully) cure the slow down when used multiple times.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
125 | worker.join() |
100726f55a9a
Changed the 'multiprocessing.Process()' code of the background batch services to (hopefully) cure the slow down when used multiple times.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
126 | worker.close() |
9284
3b3a4f659782
"Blacked" the sources.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9279
diff
changeset
|
127 | |
9279
e252f827aaa7
Added code to explicitly close the queues to/from the workers at the end of a batch check.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
128 | taskQueue.close() |
e252f827aaa7
Added code to explicitly close the queues to/from the workers at the end of a batch check.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
129 | doneQueue.close() |
7335 | 130 | |
131 | ||
8650
100726f55a9a
Changed the 'multiprocessing.Process()' code of the background batch services to (hopefully) cure the slow down when used multiple times.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
132 | def workerTask(inputQueue, outputQueue): |
7335 | 133 | """ |
134 | Module function acting as the parallel worker for the syntax check. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
135 | |
7335 | 136 | @param inputQueue input queue |
137 | @type multiprocessing.Queue | |
138 | @param outputQueue output queue | |
139 | @type multiprocessing.Queue | |
140 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
141 | for filename, args in iter(inputQueue.get, "STOP"): |
7335 | 142 | source = args[0] |
143 | result = __jsonSyntaxCheck(filename, source) | |
144 | outputQueue.put((filename, result)) | |
145 | ||
146 | ||
147 | def __jsonSyntaxCheck(file, codestring): | |
148 | """ | |
149 | Function to check a YAML source file for syntax errors. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
150 | |
7335 | 151 | @param file source filename |
152 | @type str | |
153 | @param codestring string containing the code to check | |
154 | @type str | |
155 | @return dictionary with the keys 'error' and 'warnings' which | |
156 | hold a list containing details about the error/ warnings | |
157 | (file name, line number, column, codestring (only at syntax | |
158 | errors), the message, a list with arguments for the message) | |
159 | @rtype dict | |
160 | """ | |
161 | try: | |
162 | json.loads(codestring) | |
163 | except json.JSONDecodeError as exc: | |
164 | line = exc.lineno | |
165 | column = exc.colno | |
166 | error = exc.msg | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
167 | |
7335 | 168 | cline = min(len(codestring.splitlines()), int(line)) - 1 |
169 | code = codestring.splitlines()[cline] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
170 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
171 | return [{"error": (file, line, column, code, error)}] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
172 | |
7335 | 173 | return [{}] |