1
|
|
|
from coalib.bears.Bear import Bear |
2
|
|
|
from coalib.bears.BEAR_KIND import BEAR_KIND |
3
|
|
|
from coalib.settings.FunctionMetadata import FunctionMetadata |
4
|
|
|
|
5
|
|
|
|
6
|
|
View Code Duplication |
class FileWiseBear(Bear): |
|
|
|
|
7
|
|
|
""" |
8
|
|
|
A FileWiseBear is a Bear that analyzes only one file at once. It therefore |
9
|
|
|
can not analyze semantical facts over multiple files. |
10
|
|
|
|
11
|
|
|
This has the advantage that it can be highly parallelized. In addition, |
12
|
|
|
the results from multiple bears for one file can be shown together for that |
13
|
|
|
file, which is better to grasp for the user. coala takes care of all that. |
14
|
|
|
|
15
|
|
|
Examples for LocalBear's could be: |
16
|
|
|
|
17
|
|
|
- A SpaceConsistencyBear that checks every line for trailing whitespaces, |
18
|
|
|
tabs, etc. |
19
|
|
|
- A VariableNameBear that checks variable names and constant names for |
20
|
|
|
certain conditions |
21
|
|
|
""" |
22
|
|
|
|
23
|
|
|
def __init__(self, |
24
|
|
|
file_set, # A set of file_proxies |
25
|
|
|
section, |
26
|
|
|
message_queue, |
27
|
|
|
timeout=0): |
28
|
|
|
Bear.__init__(self, section, message_queue, timeout) |
29
|
|
|
self.file_set = file_set |
30
|
|
|
|
31
|
|
|
self.kwargs = self.get_metadata().create_params_from_section(section) |
32
|
|
|
|
33
|
|
|
@staticmethod |
34
|
|
|
def kind(): |
35
|
|
|
return BEAR_KIND.FILEWISE |
36
|
|
|
|
37
|
|
|
def execute_task(self, task, kwargs): |
38
|
|
|
""" |
39
|
|
|
This method is responsible for getting results from the |
40
|
|
|
analysis routines. |
41
|
|
|
""" |
42
|
|
|
return list(self.analyze(task, **kwargs)) |
43
|
|
|
|
44
|
|
|
def analyze(self, |
45
|
|
|
file_proxy, |
46
|
|
|
*args, |
47
|
|
|
dependency_results=None, |
48
|
|
|
**kwargs): |
49
|
|
|
""" |
50
|
|
|
Handles the given file. |
51
|
|
|
|
52
|
|
|
:param file_proxy: Object containing filename and contents. |
53
|
|
|
:return: A list of Result |
54
|
|
|
""" |
55
|
|
|
raise NotImplementedError("This function has to be implemented for a " |
56
|
|
|
"runnable bear.") |
57
|
|
|
|
58
|
|
|
def generate_tasks(self): |
59
|
|
|
""" |
60
|
|
|
This method is responsible for providing the files and arguments for |
61
|
|
|
the tasks the bear needs to perform. |
62
|
|
|
""" |
63
|
|
|
return ((file, self.kwargs) for file in self.file_set) |
64
|
|
|
|
65
|
|
|
@classmethod |
66
|
|
|
def get_metadata(cls): |
67
|
|
|
return FunctionMetadata.from_function( |
68
|
|
|
cls.run, |
69
|
|
|
omit={"self", "file_proxy", "dependency_results"}) |
|
|
|
|