Skip to content

Commit

Permalink
Revert "clipstick test"
Browse files Browse the repository at this point in the history
This reverts commit 15f523b.
  • Loading branch information
sbrunner committed Oct 25, 2024
1 parent 15f523b commit fef672f
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 54 deletions.
10 changes: 10 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,13 @@ to specify the format of the file.

Notes that the YAML files should not provide the ``prospector`` global section, and the ``pyproject.toml``
should not provide the ``tool.prospector`` section.

.. _full_options:

All Options
'''''''''''

.. argparse::
:module: prospector.run
:func: get_parser
:prog: prospector
6 changes: 4 additions & 2 deletions prospector/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ def _configure_prospector(self) -> cfg.ProspectorConfiguration:

def _get_work_path(self) -> list[Path]:
# Figure out what paths we're prospecting

paths = [Path(self.config.path)] if self.config.path else [Path.cwd()]
if self.config.path:
paths = [Path(self.config.path)]
else:
paths = [Path.cwd()]
return [p.resolve() for p in paths]

def _get_profile(self) -> tuple[ProspectorProfile, cfg.Strictness]:
Expand Down
244 changes: 194 additions & 50 deletions prospector/config/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
from enum import Enum
from typing import Any, List, Optional, Union, get_args, get_origin
import clipstick._tokens

import toml # type: ignore[import-untyped]
import yaml
from pydantic import BaseModel
Expand All @@ -27,83 +27,40 @@ class Strictness(Enum):

class ProspectorConfiguration(BaseModel):
"""
Performs static analysis of Python code.
The configuration for Prospector.
"""

zero_exit: bool = False
"""Prospector will exit with a code of 1 (one) if any messages are found. This makes automation easier; if there are any problems at all, the exit code is non-zero. However this behavior is not always desirable, so if this flag is set, prospector will exit with a code of 0 if it ran successfully, and non-zero if it failed to run."""
autodetect: bool = True
"""Turn off auto-detection of frameworks and libraries used. By default, autodetection will be used. To specify manually, see the --uses option."""
uses: list[str] = []
"""A list of one or more libraries or frameworks that the project uses. Possible values are: django, celery, flask. This will be autodetected by default, but if autodetection doesn't work, manually specify them using this flag."""
blending: bool = True
"""Turn off blending of messages. Prospector will merge together messages from different tools if they represent the same error. Use this option to see all unmerged messages."""
doc_warnings: Optional[bool] = None
"""Include warnings about documentation."""
test_warnings: Optional[bool] = None
"""Also check test modules and packages."""
no_style_warnings: Optional[bool] = None
"""Don't create any warnings about style. This disables the PEP8 tool and similar checks for formatting."""
member_warnings: Optional[bool] = None
"""Attempt to warn when code tries to access an attribute of a class or member of a module which does not exist. This is disabled by default as it tends to be quite inaccurate."""
full_pep8: Optional[bool] = None
"""Enables every PEP8 warning, so that all PEP8 style violation will be reported."""
max_line_length: Optional[int] = None
"""The maximum line length allowed. This will be set by the strictness if no value is explicitly specified"""
messages_only: bool = False
"""Only output message information (don't output summary information about the checks)"""
summary_only: bool = False
"""Only output summary information about the checks (don't output message information)"""
quiet: bool = False
"""Run but do not output anything to stdout. Useful to suppress output in scripts without sending to a file (via -o)"""
output_format: Optional[list[tuple[str, list[str]]]] = None
"""The output format. Valid values are: {}. This will output to stdout by default, however a target file can be used instead by adding :path-to-output-file, eg, -o json:output.json"""
# .format(
# ", ".join(sorted(FORMATTERS.keys()))
# )
absolute_paths: bool = False
"""Whether to output absolute paths when referencing files in messages. By default, paths will be relative to the project path"""
tools: Optional[list[str]] = None
"""A list of tools to run. This lets you set exactly which tools to run. To add extra tools to the defaults, see --with-tool. Possible values are: {}. By default, the following tools will be run: {}"""
# .format(
# ", ".join(sorted(TOOLS.keys())), ", ".join(sorted(DEFAULT_TOOLS))
# )
with_tools: list[str] = []
"""A list of tools to run in addition to the default tools. To specify all tools explicitly, use the --tool argument. Possible values are {}."""
# .format(
# ", ".join(sorted(TOOLS.keys()))
# )
without_tools: list[str] = []
"""A list of tools that should not be run. Useful to turn off only a single tool from the defaults. To specify all tools explicitly, use the --tool argument. Possible values are {}."""
# .format(
# ", ".join(sorted(TOOLS.keys()))
# )
profiles: list[str] = []
"""The list of profiles to load. A profile is a certain 'type' of behavior for prospector, and is represented by a YAML configuration file. Either a full path to the YAML file describing the profile must be provided, or it must be on the profile path (see --profile-path)"""
profile_path: list[str] = []
"""Additional paths to search for profile files. By default this is the path that prospector will check, and a directory called '.prospector' in the path that prospector will check."""
strictness: Optional[Strictness] = None
"""How strict the checker should be. This affects how harshly the checker will enforce coding guidelines. The default value is "medium", possible values are "veryhigh", "high", "medium", "low" and "verylow"."""
show_profile: bool = False
"""Include the computed profile in the summary. This will show what prospector has decided the overall profile is once all profiles have been combined and inherited from. This will produce a large output in most cases so is only useful when trying to debug why prospector is not behaving like you expect."""
no_external_config: bool = False
"""Determines how prospector should behave when configuration already exists for a tool. By default, prospector will use existing configuration. This flag will cause prospector to ignore existing configuration and use its own settings for every tool. Note that prospector will always use its own config for tools which do not have custom configuration."""
legacy_tool_names: bool = False
"""Output deprecated names for tools (pep8, pep257) instead of updated names (pycodestyle, pydocstyle)"""
pylint_config_file: Optional[str] = None
"""The path to a pylintrc file to use to configure pylint. Prospector will find .pylintrc files in the root of the project, but you can use this option to specify manually where it is."""
path: Optional[str]
"""The path to a Python project to inspect. Defaults to PWD if not specified. Note: This command line argument is deprecated and will be removed in a future update. Please use the positional PATH argument instead."""
path: Optional[str] = None
ignore_patterns: list[str] = []
"""A list of paths to ignore, as a list of regular expressions. Files and folders will be ignored if their full path contains any of these patterns."""
ignore_paths: list[str] = []
"""A list of file or directory names to ignore. If the complete name matches any of the items in this list, the file or directory (and all subdirectories) will be ignored."""
die_on_tool_error: bool = False
"""If a tool fails to run, prospector will try to carry on. Use this flag to cause prospector to die and raise the exception the tool generated. Mostly useful for development on prospector."""
include_tool_stdout: bool = False
"""There are various places where tools will output warnings to stdout/stderr, which breaks parsing of JSON output. Therefore while tols is running, this is suppressed. For developing, it is sometimes useful to see this. This flag will cause stdout/stderr from a tool to be shown as a normal message amongst other warnings. See also --direct-tool-stdout"""
direct_tool_stdout: bool = False
"""Same as --include-tool-stdout, except the output will be printed directly rather than shown as a message."""


def _parse_value(
Expand Down Expand Up @@ -198,7 +155,7 @@ def get_config() -> ProspectorConfiguration:
if value is not None:
config[name] = value

args = build_command_line_parser()
args = build_command_line_parser().parse_args()
for name, conf in ProspectorConfiguration.model_fields.items():
if hasattr(args, name):
value = getattr(args, name)
Expand All @@ -222,6 +179,193 @@ def get_config() -> ProspectorConfiguration:

# flake8: noqa
def build_command_line_parser(
args: Optional[List[str]] = None,
) -> clipstick._tokens.TPydanticModel:
return clipstick.parse(ProspectorConfiguration, args=args)
prog: str = "prospector", description: str = "Performs static analysis of Python code"
) -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(prog=prog, description=description)

parser.add_argument(
"-0",
"--zero-exit",
action="store_true",
help="Prospector will exit with a code of 1 (one) if any messages are found. This makes automation easier; if there are any problems at all, the exit code is non-zero. However this behavior is not always desirable, so if this flag is set, prospector will exit with a code of 0 if it ran successfully, and non-zero if it failed to run.",
)
parser.add_argument(
"-A",
"--no-autodetect",
action="store_true",
help="Turn off auto-detection of frameworks and libraries used. By default, autodetection will be used. To specify manually, see the --uses option.",
)
parser.add_argument(
"-u",
"--uses",
action="append",
help="A list of one or more libraries or frameworks that the project uses. Possible values are: django, celery, flask. This will be autodetected by default, but if autodetection doesn't work, manually specify them using this flag.",
)
parser.add_argument(
"-B",
"--no-blending",
action="store_true",
help="Turn off blending of messages. Prospector will merge together messages from different tools if they represent the same error. Use this option to see all unmerged messages.",
)
parser.add_argument("-D", "--doc-warnings", action="store_true", help="Include warnings about documentation.")
parser.add_argument("-T", "--test-warnings", action="store_true", help="Also check test modules and packages.")
parser.add_argument(
"--legacy-tool-names",
action="store_true",
help="Output deprecated names for tools (pep8, pep257) instead of updated names (pycodestyle, pydocstyle)",
)
parser.add_argument(
"-8",
"--no-style-warnings",
action="store_true",
help="Don't create any warnings about style. This disables the PEP8 tool and similar checks for formatting.",
)
parser.add_argument(
"-m",
"--member-warnings",
action="store_true",
help="Attempt to warn when code tries to access an attribute of a class or member of a module which does not exist. This is disabled by default as it tends to be quite inaccurate.",
)
parser.add_argument(
"-q",
"--quiet",
action="store_true",
help="Run but do not output anything to stdout. Useful to suppress output in scripts without sending to a file (via -o)",
)
parser.add_argument(
"-F",
"--full-pep8",
action="store_true",
help="Enables every PEP8 warning, so that all PEP8 style violation will be reported.",
)
parser.add_argument(
"--max-line-length",
type=int,
help="The maximum line length allowed. This will be set by the strictness if no value is explicitly specified",
)
parser.add_argument(
"-M",
"--messages-only",
action="store_true",
help="Only output message information (don't output summary information about the checks)",
)
parser.add_argument(
"-S",
"--summary-only",
action="store_true",
help="Only output summary information about the checks (don't output message information)",
)
parser.add_argument(
"-o",
"--output-format",
action="append",
help="The output format. Valid values are: {}. This will output to stdout by default, however a target file can be used instead by adding :path-to-output-file, eg, -o json:output.json".format(
", ".join(sorted(FORMATTERS.keys()))
),
)
parser.add_argument(
"--absolute-paths",
action="store_true",
help="Whether to output absolute paths when referencing files in messages. By default, paths will be relative to the project path",
)
parser.add_argument(
"-t",
"--tool",
dest="tools",
action="append",
help="A list of tools to run. This lets you set exactly which tools to run. To add extra tools to the defaults, see --with-tool. Possible values are: {}. By default, the following tools will be run: {}".format(
", ".join(sorted(TOOLS.keys())), ", ".join(sorted(DEFAULT_TOOLS))
),
)
parser.add_argument(
"-w",
"--with-tool",
dest="with_tools",
action="append",
help="A list of tools to run in addition to the default tools. To specify all tools explicitly, use the --tool argument. Possible values are {}.".format(
", ".join(sorted(TOOLS.keys()))
),
)
parser.add_argument(
"-W",
"--without-tool",
dest="without_tools",
action="append",
help="A list of tools that should not be run. Useful to turn off only a single tool from the defaults. To specify all tools explicitly, use the --tool argument. Possible values are {}.".format(
", ".join(sorted(TOOLS.keys()))
),
)
parser.add_argument(
"-P",
"--profile",
dest="profiles",
action="append",
help="The list of profiles to load. A profile is a certain 'type' of behavior for prospector, and is represented by a YAML configuration file. Either a full path to the YAML file describing the profile must be provided, or it must be on the profile path (see --profile-path)",
)
parser.add_argument(
"--profile-path",
action="append",
help="Additional paths to search for profile files. By default this is the path that prospector will check, and a directory called '.prospector' in the path that prospector will check.",
)
parser.add_argument(
"--show-profile",
action="store_true",
help="Include the computed profile in the summary. This will show what prospector has decided the overall profile is once all profiles have been combined and inherited from. This will produce a large output in most cases so is only useful when trying to debug why prospector is not behaving like you expect.",
)
parser.add_argument(
"-s",
"--strictness",
help='How strict the checker should be. This affects how harshly the checker will enforce coding guidelines. The default value is "medium", possible values are "veryhigh", "high", "medium", "low" and "verylow".',
)
parser.add_argument(
"-E",
"--no-external-config",
action="store_true",
help="Determines how prospector should behave when configuration already exists for a tool. By default, prospector will use existing configuration. This flag will cause prospector to ignore existing configuration and use its own settings for every tool. Note that prospector will always use its own config for tools which do not have custom configuration.",
)
parser.add_argument(
"--pylint-config-file",
help="The path to a pylintrc file to use to configure pylint. Prospector will find .pylintrc files in the root of the project, but you can use this option to specify manually where it is.",
)
parser.add_argument(
"-I",
"--ignore-patterns",
action="append",
help="A list of paths to ignore, as a list of regular expressions. Files and folders will be ignored if their full path contains any of these patterns.",
)
parser.add_argument(
"-i",
"--ignore-paths",
action="append",
help="A list of file or directory names to ignore. If the complete name matches any of the items in this list, the file or directory (and all subdirectories) will be ignored.",
)
parser.add_argument(
"-X",
"--die-on-tool-error",
action="store_true",
help="If a tool fails to run, prospector will try to carry on. Use this flag to cause prospector to die and raise the exception the tool generated. Mostly useful for development on prospector.",
)
parser.add_argument(
"--include-tool-stdout",
action="store_true",
help="There are various places where tools will output warnings to stdout/stderr, which breaks parsing of JSON output. Therefore while tols is running, this is suppressed. For developing, it is sometimes useful to see this. This flag will cause stdout/stderr from a tool to be shown as a normal message amongst other warnings. See also --direct-tool-stdout",
)
parser.add_argument(
"--direct-tool-stdout",
action="store_true",
help="Same as --include-tool-stdout, except the output will be printed directly rather than shown as a message.",
)
parser.add_argument(
"-p",
"--path",
help="The path to a Python project to inspect. Defaults to PWD if not specified. Note: This command line argument is deprecated and will be removed in a future update. Please use the positional PATH argument instead.",
)

parser.add_argument(
"path",
help="The path to a Python project to inspect. Defaults to PWD if not specified. If multiple paths are specified, they must all be files (no directories).",
metavar="PATH",
nargs="?",
)

return parser
2 changes: 1 addition & 1 deletion prospector/profiles/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, name: str, profile_dict: dict[str, Any], inherit_order: list[
profile_dict.get("ignore-patterns", []) + profile_dict.get("ignore", [])
)

self.output_format: Optional[str] = profile_dict.get("output-format")
self.output_format: Optional[str] = profile_dict["output-format"] if "output-format" in profile_dict else None
self.output_target: Optional[str] = profile_dict.get("output-target")
self.autodetect: bool = profile_dict.get("autodetect", True)
self.uses: list[str] = [
Expand Down
11 changes: 10 additions & 1 deletion prospector/run.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import argparse
import codecs
import os.path
import sys
Expand Down Expand Up @@ -174,14 +175,22 @@ def write_to(self, formatter: Formatter, target: TextIO) -> None:
target.write("\n")


def get_parser() -> argparse.ArgumentParser:
"""
This is a helper method to return an argparse parser, to
be used with the Sphinx argparse plugin for documentation.
"""
return cfg.build_command_line_parser()


def main() -> None:
# Get our configuration
config = ProspectorConfig()

paths = config.paths
if len(paths) > 1 and not all(os.path.isfile(path) for path in paths):
sys.stderr.write("\nIn multi-path mode, all inputs must be files, not directories.\n\n")
cfg.build_command_line_parser(args=["--help"])
get_parser().print_usage()
sys.exit(2)

# Make it so
Expand Down

0 comments on commit fef672f

Please sign in to comment.