-
Notifications
You must be signed in to change notification settings - Fork 171
/
__init__.py
269 lines (217 loc) · 9.03 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import os
import re
import sys
from prospector import tools
from prospector.autodetect import autodetect_libraries
from prospector.config import configuration as cfg
from prospector.profiles.profile import load_profiles, ProfileNotFound
from prospector.tools import DEFAULT_TOOLS
class ProspectorConfig(object):
# There are several methods on this class which could technically
# be functions (they don't use the 'self' argument) but that would
# make this module/class a bit ugly.
# Also the 'too many instance attributes' warning is ignored, as this
# is a config object and its sole purpose is to hold many properties!
# pylint:disable=no-self-use,too-many-instance-attributes
def __init__(self):
self.config, self.arguments = self._configure_prospector()
self.paths = self._get_work_path(self.config, self.arguments)
self.explicit_file_mode = all(map(os.path.isfile, self.paths))
if os.path.isdir(self.paths[0]):
self.workdir = self.paths[0]
else:
self.workdir = os.getcwd()
self.profile, self.profile_names, self.strictness = self._get_profile(self.workdir, self.config)
self.libraries = self._find_used_libraries(self.config)
self.tools_to_run = self._determine_tool_runners(self.config, self.profile)
self.ignores = self._determine_ignores(self.config, self.profile, self.libraries)
self.configured_by = {}
def get_tools(self, found_files):
self.configured_by = {}
runners = []
for tool_name in self.tools_to_run:
tool = tools.TOOLS[tool_name]()
self.configured_by[tool] = tool.configure(self, found_files)
runners.append(tool)
return runners
def get_output_format(self):
# Get the output formatter
if self.config.output_format is not None:
output_format = self.config.output_format
else:
output_format = self.profile.output_format
if output_format is None:
output_format = 'grouped'
return output_format
def _configure_prospector(self):
# first we will configure prospector as a whole
mgr = cfg.build_manager()
config = mgr.retrieve(*cfg.build_default_sources())
return config, mgr.arguments
def _get_work_path(self, config, arguments):
# Figure out what paths we're prospecting
if config['path']:
paths = [self.config['path']]
elif arguments['checkpath']:
paths = arguments['checkpath']
else:
paths = [os.getcwd()]
return paths
def _get_profile(self, path, config):
# Use other specialty profiles based on options
profile_names = []
if not config.doc_warnings:
profile_names.append('no_doc_warnings')
if not config.test_warnings:
profile_names.append('no_test_warnings')
if not config.style_warnings:
profile_names.append('no_pep8')
if config.full_pep8:
profile_names.append('full_pep8')
# Use the specified profiles
profile_provided = False
if len(config.profiles) > 0:
profile_provided = True
profile_names += config.profiles
# if there is a '.prospector.ya?ml' or a '.prospector/prospector.ya?ml'
# file then we'll include that
poss_profs = (
('.prospector.yaml',),
('.prospector.yml',),
('prospector', '.prospector.yaml'),
('prospector', '.prospector.yml'),
)
for possible_profile in poss_profs:
prospector_yaml = os.path.join(path, *possible_profile) # pylint:disable=star-args
if os.path.exists(prospector_yaml) and os.path.isfile(prospector_yaml):
profile_provided = True
profile_names.append(prospector_yaml)
strictness = None
if not profile_provided:
# Use the strictness profile only if no profile has been given
if config.strictness:
profile_names = ['strictness_%s' % config.strictness] + profile_names
strictness = config.strictness
else:
strictness = 'from profile'
# the profile path is
# * anything provided as an argument
# * a directory called .prospector in the check path
# * the check path
# * prospector provided profiles
profile_path = config.profile_path
prospector_dir = os.path.join(path, '.prospector')
if os.path.exists(prospector_dir) and os.path.isdir(prospector_dir):
profile_path.append(prospector_dir)
profile_path.append(path)
provided = os.path.join(os.path.dirname(__file__), '../profiles/profiles')
profile_path.append(provided)
try:
profile = load_profiles(profile_names, profile_path)
except ProfileNotFound as nfe:
sys.stderr.write("Failed to run:\nCould not find profile %s. Search path: %s\n" %
(nfe.name, ':'.join(nfe.profile_path)))
sys.exit(1)
else:
return profile, profile_names, strictness
def _find_used_libraries(self, config):
libraries = []
# Bring in adaptors that we automatically detect are needed
if config.autodetect:
map(libraries.append, autodetect_libraries(self.workdir))
# Bring in adaptors for the specified libraries
for name in config.uses:
if name not in libraries:
libraries.append(name)
return libraries
def _determine_tool_runners(self, config, profile):
if config.tools is None:
# we had no command line settings for an explicit list of
# tools, so we use the defaults
to_run = set(DEFAULT_TOOLS)
# we can also use any that the profiles dictate
for tool in tools.TOOLS.keys():
if profile.is_tool_enabled(tool):
to_run.add(tool)
else:
to_run = set(config.tools)
# profiles have no say in the list of tools run when
# a command line is specified
for tool in config.with_tools:
to_run.add(tool)
for tool in config.without_tools:
to_run.remove(tool)
if config.tools is None and len(config.with_tools) == 0 and len(config.without_tools) == 0:
for tool in tools.TOOLS.keys():
enabled = profile.is_tool_enabled(tool)
if enabled is None:
enabled = tool in DEFAULT_TOOLS
if tool in to_run and not enabled:
to_run.remove(tool)
return sorted(list(to_run))
def _determine_ignores(self, config, profile, libraries):
# Grab ignore patterns from the profile adapter
ignores = [
re.compile(ignore)
for ignore in profile.ignore
]
# Grab ignore patterns from the options
ignores += [
re.compile(patt)
for patt in config.ignore_patterns
]
# Grab ignore paths from the options
boundary = r"(^|/|\\)%s(/|\\|$)"
ignores += [
re.compile(boundary % re.escape(ignore_path))
for ignore_path in config.ignore_paths
]
# some libraries have further automatic ignores
if 'django' in libraries:
ignores += [
re.compile('(^|/)(south_)?migrations(/|$)')
]
return ignores
def get_summary_information(self):
return {
'libraries': self.libraries,
'strictness': self.strictness,
'profiles': self.profile_names,
'tools': self.tools_to_run,
}
def exit_with_zero_on_success(self):
return self.config.zero_exit
def get_disabled_messages(self, tool_name):
return self.profile.get_disabled_messages(tool_name)
def use_external_config(self, _):
# Currently there is only one single global setting for whether to use
# global config, but this could be extended in the future
return not self.config.no_external_config
def tool_options(self, tool_name):
tool = getattr(self.profile, tool_name, None)
if tool is None:
return {}
return tool.get('options', {})
def external_config_location(self, tool_name):
return getattr(self.config, '%s_config_file' % tool_name, None)
@property
def die_on_tool_error(self):
return self.config.die_on_tool_error
@property
def summary_only(self):
return self.config.summary_only
@property
def messages_only(self):
return self.config.messages_only
@property
def blending(self):
return self.config.blending
@property
def absolute_paths(self):
return self.config.absolute_paths
@property
def max_line_length(self):
return self.arguments.get('max_line_length', None)
@property
def loquacious_pylint(self):
return self.config.loquacious_pylint