configuration.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. # flake8: noqa
  2. import pkg_resources
  3. import setoptconf as soc
  4. from prospector.config.datatype import OutputChoice
  5. from prospector.formatters import FORMATTERS
  6. from prospector.tools import DEFAULT_TOOLS, TOOLS
  7. __all__ = ("build_manager",)
  8. _VERSION = pkg_resources.get_distribution("prospector").version
  9. def build_manager():
  10. manager = soc.ConfigurationManager("prospector")
  11. manager.add(soc.BooleanSetting("zero_exit", default=False))
  12. manager.add(soc.BooleanSetting("autodetect", default=True))
  13. manager.add(soc.ListSetting("uses", soc.String, default=[]))
  14. manager.add(soc.BooleanSetting("blending", default=True))
  15. manager.add(soc.BooleanSetting("doc_warnings", default=None))
  16. manager.add(soc.BooleanSetting("test_warnings", default=None))
  17. manager.add(soc.BooleanSetting("no_style_warnings", default=None))
  18. manager.add(soc.BooleanSetting("member_warnings", default=None))
  19. manager.add(soc.BooleanSetting("full_pep8", default=None))
  20. manager.add(soc.IntegerSetting("max_line_length", default=None))
  21. manager.add(soc.BooleanSetting("messages_only", default=False))
  22. manager.add(soc.BooleanSetting("summary_only", default=False))
  23. manager.add(soc.BooleanSetting("quiet", default=False))
  24. manager.add(
  25. soc.ListSetting(
  26. "output_format",
  27. OutputChoice(sorted(FORMATTERS.keys())),
  28. default=None,
  29. )
  30. )
  31. manager.add(soc.BooleanSetting("absolute_paths", default=False))
  32. manager.add(
  33. soc.ListSetting(
  34. "tools",
  35. soc.Choice(sorted(TOOLS.keys())),
  36. default=None,
  37. )
  38. )
  39. manager.add(soc.ListSetting("with_tools", soc.String, default=[]))
  40. manager.add(soc.ListSetting("without_tools", soc.String, default=[]))
  41. manager.add(soc.ListSetting("profiles", soc.String, default=[]))
  42. manager.add(soc.ListSetting("profile_path", soc.String, default=[]))
  43. manager.add(
  44. soc.ChoiceSetting(
  45. "strictness",
  46. ["veryhigh", "high", "medium", "low", "verylow"],
  47. default=None,
  48. )
  49. )
  50. manager.add(soc.BooleanSetting("show_profile", default=False))
  51. manager.add(soc.BooleanSetting("no_external_config", default=False))
  52. manager.add(soc.BooleanSetting("legacy_tool_names", default=False))
  53. manager.add(soc.StringSetting("pylint_config_file", default=None))
  54. manager.add(soc.StringSetting("path", default=None))
  55. manager.add(soc.ListSetting("ignore_patterns", soc.String, default=[]))
  56. manager.add(soc.ListSetting("ignore_paths", soc.String, default=[]))
  57. manager.add(soc.BooleanSetting("die_on_tool_error", default=False))
  58. manager.add(soc.BooleanSetting("include_tool_stdout", default=False))
  59. manager.add(soc.BooleanSetting("direct_tool_stdout", default=False))
  60. return manager
  61. def build_default_sources():
  62. sources = [
  63. build_command_line_source(),
  64. soc.EnvironmentVariableSource(),
  65. soc.ConfigFileSource(
  66. (
  67. ".prospectorrc",
  68. "setup.cfg",
  69. "tox.ini",
  70. )
  71. ),
  72. soc.ConfigFileSource(
  73. (
  74. soc.ConfigDirectory(".prospectorrc"),
  75. soc.HomeDirectory(".prospectorrc"),
  76. )
  77. ),
  78. ]
  79. return sources
  80. def build_command_line_source(prog=None, description="Performs static analysis of Python code"):
  81. parser_options = {}
  82. if prog is not None:
  83. parser_options["prog"] = prog
  84. if description is not None:
  85. parser_options["description"] = description
  86. options = {
  87. "zero_exit": {
  88. "flags": ["-0", "--zero-exit"],
  89. "help": "Prospector will exit with a code of 1 (one) if any messages"
  90. " are found. This makes automation easier; if there are any"
  91. " problems at all, the exit code is non-zero. However this behaviour"
  92. " is not always desirable, so if this flag is set, prospector will"
  93. " exit with a code of 0 if it ran successfully, and non-zero if"
  94. " it failed to run.",
  95. },
  96. "autodetect": {
  97. "flags": ["-A", "--no-autodetect"],
  98. "help": "Turn off auto-detection of frameworks and libraries used."
  99. " By default, autodetection will be used. To specify"
  100. " manually, see the --uses option.",
  101. },
  102. "uses": {
  103. "flags": ["-u", "--uses"],
  104. "help": "A list of one or more libraries or frameworks that the"
  105. " project uses. Possible values are: django, celery, flask. This will be"
  106. " autodetected by default, but if autodetection doesn't"
  107. " work, manually specify them using this flag.",
  108. },
  109. "blending": {
  110. "flags": ["-B", "--no-blending"],
  111. "help": "Turn off blending of messages. Prospector will merge"
  112. " together messages from different tools if they represent"
  113. " the same error. Use this option to see all unmerged"
  114. " messages.",
  115. },
  116. "doc_warnings": {
  117. "flags": ["-D", "--doc-warnings"],
  118. "help": "Include warnings about documentation.",
  119. },
  120. "test_warnings": {
  121. "flags": ["-T", "--test-warnings"],
  122. "help": "Also check test modules and packages.",
  123. },
  124. "legacy_tool_names": {
  125. "flags": ["--legacy-tool-names"],
  126. "help": "Output deprecated names for tools (pep8, pep257) "
  127. "instead of updated names (pycodestyle, pydocstyle)",
  128. },
  129. "no_style_warnings": {
  130. "flags": ["-8", "--no-style-warnings"],
  131. "help": "Don't create any warnings about style. This disables the"
  132. " PEP8 tool and similar checks for formatting.",
  133. },
  134. "member_warnings": {
  135. "flags": ["-m", "--member-warnings"],
  136. "help": "Attempt to warn when code tries to access an attribute of a "
  137. "class or member of a module which does not exist. This is disabled "
  138. "by default as it tends to be quite inaccurate.",
  139. },
  140. "quiet": {
  141. "flags": ["-q", "--quiet"],
  142. "help": "Run but do not output anything to stdout. Useful to suppress "
  143. "output in scripts without sending to a file (via -o)",
  144. },
  145. "full_pep8": {
  146. "flags": ["-F", "--full-pep8"],
  147. "help": "Enables every PEP8 warning, so that all PEP8 style violation will be reported.",
  148. },
  149. "max_line_length": {
  150. "flags": ["--max-line-length"],
  151. "help": "The maximum line length allowed. This will be set by the strictness if no"
  152. " value is explicitly specified",
  153. },
  154. "messages_only": {
  155. "flags": ["-M", "--messages-only"],
  156. "help": "Only output message information (don't output summary" " information about the checks)",
  157. },
  158. "summary_only": {
  159. "flags": ["-S", "--summary-only"],
  160. "help": "Only output summary information about the checks (don't" "output message information)",
  161. },
  162. "output_format": {
  163. "flags": ["-o", "--output-format"],
  164. "help": "The output format. Valid values are: %s. This will output to stdout by default, "
  165. "however a target file can be used instead by adding :path-to-output-file, eg, -o json:output.json"
  166. % (", ".join(sorted(FORMATTERS.keys())),),
  167. },
  168. "absolute_paths": {
  169. "help": "Whether to output absolute paths when referencing files "
  170. "in messages. By default, paths will be relative to the "
  171. "project path",
  172. },
  173. "tools": {
  174. "flags": ["-t", "--tool"],
  175. "help": "A list of tools to run. This lets you set exactly which "
  176. "tools to run. To add extra tools to the defaults, see "
  177. "--with-tool. Possible values are: %s. By "
  178. "default, the following tools will be run: %s"
  179. % (
  180. ", ".join(sorted(TOOLS.keys())),
  181. ", ".join(sorted(DEFAULT_TOOLS)),
  182. ),
  183. },
  184. "with_tools": {
  185. "flags": ["-w", "--with-tool"],
  186. "help": "A list of tools to run in addition to the default tools. "
  187. "To specify all tools explicitly, use the --tool argument. "
  188. "Possible values are %s." % (", ".join(sorted(TOOLS.keys()))),
  189. },
  190. "without_tools": {
  191. "flags": ["-W", "--without-tool"],
  192. "help": "A list of tools that should not be run. Useful to turn off "
  193. "only a single tool from the defaults. "
  194. "To specify all tools explicitly, use the --tool argument. "
  195. "Possible values are %s." % (", ".join(sorted(TOOLS.keys()))),
  196. },
  197. "profiles": {
  198. "flags": ["-P", "--profile"],
  199. "help": "The list of profiles to load. A profile is a certain"
  200. " 'type' of behaviour for prospector, and is represented"
  201. " by a YAML configuration file. Either a full path to the YAML"
  202. " file describing the profile must be provided, or it must be"
  203. " on the profile path (see --profile-path)",
  204. },
  205. "profile_path": {
  206. "flags": ["--profile-path"],
  207. "help": "Additional paths to search for profile files. By default this"
  208. " is the path that prospector will check, and a directory "
  209. ' called ".prospector" in the path that prospector will check.',
  210. },
  211. "show_profile": {
  212. "flags": ["--show-profile"],
  213. "help": "Include the computed profile in the summary. This will show what"
  214. " prospector has decided the overall profile is once all profiles"
  215. " have been combined and inherited from. This will produce a large"
  216. " output in most cases so is only useful when trying to debug why"
  217. " prospector is not behaving like you expect.",
  218. },
  219. "strictness": {
  220. "flags": ["-s", "--strictness"],
  221. "help": "How strict the checker should be. This affects how"
  222. " harshly the checker will enforce coding guidelines. The"
  223. ' default value is "medium", possible values are'
  224. ' "veryhigh", "high", "medium", "low" and "verylow".',
  225. },
  226. "no_external_config": {
  227. "flags": ["-E", "--no-external-config"],
  228. "help": "Determines how prospector should behave when"
  229. " configuration already exists for a tool. By default,"
  230. " prospector will use existing configuration. This flag"
  231. " will cause prospector to ignore existing configuration"
  232. " and use its own settings for every tool. Note that"
  233. " prospector will always use its own config for tools which"
  234. " do not have custom configuration.",
  235. },
  236. "pylint_config_file": {
  237. "flags": ["--pylint-config-file"],
  238. "help": "The path to a pylintrc file to use to configure pylint. Prospector will find"
  239. " .pylintrc files in the root of the project, but you can use this option to "
  240. "specify manually where it is.",
  241. },
  242. "ignore_patterns": {
  243. "flags": ["-I", "--ignore-patterns"],
  244. "help": "A list of paths to ignore, as a list of regular"
  245. " expressions. Files and folders will be ignored if their"
  246. " full path contains any of these patterns.",
  247. },
  248. "ignore_paths": {
  249. "flags": ["-i", "--ignore-paths"],
  250. "help": "A list of file or directory names to ignore. If the"
  251. " complete name matches any of the items in this list, the"
  252. " file or directory (and all subdirectories) will be"
  253. " ignored.",
  254. },
  255. "die_on_tool_error": {
  256. "flags": ["-X", "--die-on-tool-error"],
  257. "help": "If a tool fails to run, prospector will try to carry on."
  258. " Use this flag to cause prospector to die and raise the"
  259. " exception the tool generated. Mostly useful for"
  260. " development on prospector.",
  261. },
  262. "include-tool-stdout": {
  263. "flags": ["--include-tool-stdout"],
  264. "help": "There are various places where tools will output warnings to "
  265. "stdout/stderr, which breaks parsing of JSON output. Therefore while tols "
  266. "is running, this is suppressed. For developing, it is sometimes useful to "
  267. "see this. This flag will cause stdout/stderr from a tool to be shown as "
  268. "a normal message amongst other warnings. See also --direct-tool-stdout",
  269. },
  270. "direct-tool-stdout": {
  271. "flags": ["--direct-tool-stdout"],
  272. "help": "Same as --include-tool-stdout, except the output will be printed "
  273. "directly rather than shown as a message.",
  274. },
  275. "path": {
  276. "flags": ["-p", "--path"],
  277. "help": "The path to a Python project to inspect. Defaults to PWD"
  278. " if not specified. Note: This command line argument is"
  279. " deprecated and will be removed in a future update. Please"
  280. " use the positional PATH argument instead.",
  281. },
  282. }
  283. positional = (
  284. (
  285. "checkpath",
  286. {
  287. "help": "The path to a Python project to inspect. Defaults to PWD"
  288. " if not specified. If multiple paths are specified,"
  289. " they must all be files (no directories).",
  290. "metavar": "PATH",
  291. "nargs": "*",
  292. },
  293. ),
  294. )
  295. return soc.CommandLineSource(
  296. options=options,
  297. version=_VERSION,
  298. parser_options=parser_options,
  299. positional=positional,
  300. )