RadonMetrics/CodeMetricsCalculator.py

changeset 3
7150ed890fd5
child 4
9ac53bf21182
equal deleted inserted replaced
2:1ad320a50a01 3:7150ed890fd5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 from __future__ import unicode_literals
7
8 import multiprocessing
9
10
11 def initService():
12 """
13 Initialize the service and return the entry point.
14
15 @return the entry point for the background client (function)
16 """
17 return codeMetrics
18
19
20 def initBatchService():
21 """
22 Initialize the batch service and return the entry point.
23
24 @return the entry point for the background client (function)
25 """
26 return batchCodeMetrics
27
28
29 def codeMetrics(file, text="", type_=""):
30 """
31 Private function to calculate selected code metrics of one file.
32
33 @param file source filename
34 @type str
35 @param text source text
36 @param str
37 @return tuple containing the filename and the result list
38 @rtype (str, list)
39 """
40 if type_ == "raw":
41 return __rawCodeMetrics(file, text)
42
43 # TODO: Return error indication
44
45
46 def batchCodeMetrics(argumentsList, send, fx, cancelled):
47 """
48 Module function to calculate selected code metrics for a batch of files.
49
50 @param argumentsList list of arguments tuples as given for check
51 @type list
52 @param send reference to send function
53 @type function
54 @param fx registered service name
55 @type str
56 @param cancelled reference to function checking for a cancellation
57 @type function
58 """
59 try:
60 NumberOfProcesses = multiprocessing.cpu_count()
61 if NumberOfProcesses >= 1:
62 NumberOfProcesses -= 1
63 except NotImplementedError:
64 NumberOfProcesses = 1
65
66 # Create queues
67 taskQueue = multiprocessing.Queue()
68 doneQueue = multiprocessing.Queue()
69
70 # Submit tasks (initially two time number of processes
71 initialTasks = 2 * NumberOfProcesses
72 for task in argumentsList[:initialTasks]:
73 taskQueue.put(task)
74
75 # Start worker processes
76 for i in range(NumberOfProcesses):
77 multiprocessing.Process(target=worker, args=(taskQueue, doneQueue))\
78 .start()
79
80 # Get and send results
81 endIndex = len(argumentsList) - initialTasks
82 for i in range(len(argumentsList)):
83 filename, result = doneQueue.get()
84 send(fx, filename, result)
85 if cancelled():
86 # just exit the loop ignoring the results of queued tasks
87 break
88 if i < endIndex:
89 taskQueue.put(argumentsList[i + initialTasks])
90
91 # Tell child processes to stop
92 for i in range(NumberOfProcesses):
93 taskQueue.put('STOP')
94
95
96 def worker(input, output):
97 """
98 Module function acting as the parallel worker for the style check.
99
100 @param input input queue
101 @type multiprocessing.Queue
102 @param output output queue
103 @type multiprocessing.Queue
104 """
105 for filename, source, type_ in iter(input.get, 'STOP'):
106 if type_ == "raw":
107 result = __rawCodeMetrics(filename, source)
108 else:
109 result = []
110 output.put((filename, result))
111
112
113 def __rawCodeMetrics(file, text=""):
114 """
115 Private function to calculate the raw code metrics for one Python file.
116
117 @param file source filename
118 @type str
119 @param text source text
120 @type str
121 @return tuple containing the result list
122 @rtype (list)
123 """
124 from radon.raw import analyze
125 res = analyze(text)
126 return (res, )

eric ide

mercurial