Prospector is a tool to analyse Python code by aggregating the result of other tools.
—
Core functionality for running code analysis with the main Prospector class. This handles tool orchestration, configuration management, and result aggregation.
Main orchestrator class that coordinates the execution of multiple static analysis tools and aggregates their results.
class Prospector:
def __init__(self, config: ProspectorConfig) -> NoneCreates a new Prospector instance with the provided configuration.
Parameters:
config: ProspectorConfig - Configuration object containing analysis settingsProperties:
config: ProspectorConfig - The configuration objectsummary: Optional[dict[str, Any]] - Analysis summary informationmessages: list[Message] - Collection of analysis messagesdef execute(self) -> NoneExecutes the analysis by running all configured tools and processing their results. This method:
def get_messages(self) -> list[Message]Returns all analysis messages found during execution.
Returns:
list[Message] - List of all messages from analysis toolsdef get_summary(self) -> Optional[dict[str, Any]]Returns summary information about the analysis run.
Returns:
Optional[dict[str, Any]] - Dictionary containing:
started: datetime - When analysis startedcompleted: datetime - When analysis completedtime_taken: str - Duration in secondsmessage_count: int - Total number of messageslibraries: list[str] - Detected librariesstrictness: Optional[str] - Strictness level usedprofiles: str - Comma-separated list of profilestools: list[str] - Tools that were runformatter: str - Output formatter usedexternal_config: str - External config files used (if any)def print_messages(self) -> NonePrints analysis results using the configured output formatters. Handles multiple output formats and files as specified in configuration.
def write_to(self, formatter: Formatter, target: TextIO) -> NoneWrites formatted output to a specific target.
Parameters:
formatter: Formatter - The formatter to use for outputtarget: TextIO - The target stream to write todef process_messages(self, found_files: FileFinder, messages: list[Message],
tools: dict[str, ToolBase]) -> list[Message]Processes raw messages from tools by applying blending and filtering.
Parameters:
found_files: FileFinder - File discovery objectmessages: list[Message] - Raw messages from toolstools: dict[str, ToolBase] - Dictionary of tools that were runReturns:
list[Message] - Processed and filtered messagesdef main() -> NoneMain entry point function for the prospector command-line tool. This function:
Exit Codes:
0 - Success (no issues found or --zero-exit flag used)1 - Analysis completed but issues were found2 - Fatal error or invalid argumentsdef get_parser() -> argparse.ArgumentParserReturns the argparse parser used by prospector. This is primarily used for documentation generation with Sphinx.
Returns:
argparse.ArgumentParser - The argument parser with all prospector optionsfrom prospector.config import ProspectorConfig
from prospector.run import Prospector
# Create default configuration
config = ProspectorConfig()
# Run analysis
prospector = Prospector(config)
prospector.execute()
# Process results
messages = prospector.get_messages()
for message in messages:
print(f"{message.source}: {message.message}")
# Get summary
summary = prospector.get_summary()
print(f"Analysis took {summary['time_taken']} seconds")
print(f"Found {summary['message_count']} issues")from pathlib import Path
from prospector.config import ProspectorConfig
from prospector.run import Prospector
# Create configuration with custom working directory
workdir = Path("/path/to/project")
config = ProspectorConfig(workdir=workdir)
# Run analysis
prospector = Prospector(config)
prospector.execute()
# Print results using configured formatters
prospector.print_messages()from prospector.config import ProspectorConfig
from prospector.run import Prospector
from prospector.exceptions import FatalProspectorException
try:
config = ProspectorConfig()
prospector = Prospector(config)
prospector.execute()
messages = prospector.get_messages()
if messages:
print(f"Found {len(messages)} issues")
else:
print("No issues found!")
except FatalProspectorException as e:
print(f"Fatal error: {e.message}")
except Exception as e:
print(f"Unexpected error: {e}")Install with Tessl CLI
npx tessl i tessl/pypi-prospector