Interrogate a codebase for docstring coverage.
—
Collection of general helper functions, constants, and formatting utilities used throughout interrogate. These utilities provide file handling, path manipulation, regex compilation, and output formatting capabilities.
Platform and configuration constants used across the package.
IS_WINDOWS = sys.platform == "win32" # Windows platform detectionClick callback function for compiling regular expression patterns from CLI options.
def parse_regex(ctx, param, values):
"""
Compile regular expressions from CLI input.
Args:
ctx: Click command context
param: Click command parameter (ignore_regex or whitelist_regex)
values: List of regular expression strings to compile
Returns:
List of compiled regular expression objects, or None if no values
"""Context manager for unified file and stdout handling.
@contextlib.contextmanager
def smart_open(filename=None, fmode=None):
"""
Context manager to handle both stdout and files uniformly.
Args:
filename: Filename to open, or None/"-" for stdout
fmode: File mode for opening (e.g., "w", "r")
Yields:
File handle or sys.stdout
"""Functions for path manipulation and common base directory calculation.
def get_common_base(files):
"""
Find the common parent base path for a list of files.
Args:
files: Iterable of file paths
Returns:
Common parent path as string
"""Complete output formatting class for terminal display with color support.
class OutputFormatter:
"""Interrogate results formatter with terminal-aware output."""
# Class constants
TERMINAL_WIDTH: int # Computed terminal width
TABLE_SEPARATOR = ["---"]
def __init__(self, config, file=None):
"""
Initialize formatter with configuration.
Args:
config: InterrogateConfig instance
file: Optional file handle for output
"""
def should_markup(self):
"""
Determine if color markup should be applied to output.
Returns:
bool: True if colors should be used
"""
def set_detailed_markup(self, padded_cells):
"""
Add color markup to detailed output rows.
Args:
padded_cells: List of formatted table cells
Returns:
List of cells with color markup applied
"""
def set_summary_markup(self, padded_cells):
"""
Add color markup to summary output rows.
Args:
padded_cells: List of formatted table cells
Returns:
List of cells with color markup applied
"""
def get_table_formatter(self, table_type):
"""
Get tabulate table formatter for specified output type.
Args:
table_type: "detailed" or "summary"
Returns:
tabulate.TableFormat instance
"""import click
from interrogate.utils import parse_regex
# Simulating CLI callback usage
@click.command()
@click.option('--ignore-regex', multiple=True, callback=parse_regex)
def analyze(ignore_regex):
# ignore_regex is now a list of compiled regex objects
for pattern in ignore_regex or []:
if pattern.match("some_function_name"):
print("Function matches ignore pattern")from interrogate.utils import smart_open
# Write to file or stdout uniformly
with smart_open("output.txt", "w") as f:
f.write("Coverage results\\n")
# Output to stdout when filename is None or "-"
with smart_open(None) as f:
f.write("Results to console\\n")
# stdout when using "-"
with smart_open("-") as f:
f.write("Also to console\\n")from interrogate.utils import get_common_base
# Find common base path
files = [
"/usr/src/myproject/main.py",
"/usr/src/myproject/utils.py",
"/usr/src/myproject/submodule/helper.py"
]
base_path = get_common_base(files)
print(base_path) # "/usr/src/myproject"
# Works with different depths
mixed_files = ["/usr/local/bin/script", "/usr/local/lib/module.py"]
base = get_common_base(mixed_files)
print(base) # "/usr/local"from interrogate.utils import OutputFormatter
from interrogate.config import InterrogateConfig
import sys
# Create formatter with configuration
config = InterrogateConfig(color=True, fail_under=80.0)
formatter = OutputFormatter(config, file=sys.stdout)
# Check if colors should be used
if formatter.should_markup():
print("Terminal supports color output")
# Get table formatter for different output types
detailed_formatter = formatter.get_table_formatter("detailed")
summary_formatter = formatter.get_table_formatter("summary")
# Use with tabulate for terminal-aware table formatting
import tabulate
data = [["file.py", "50", "25", "50%"]]
headers = ["File", "Total", "Missing", "Coverage"]
formatted_table = tabulate.tabulate(
data,
headers=headers,
tablefmt=detailed_formatter
)
print(formatted_table)from interrogate.utils import IS_WINDOWS
if IS_WINDOWS:
# Handle Windows-specific terminal behavior
print("Running on Windows - adjusting output formatting")
else:
print("Running on Unix-like system")from interrogate.utils import OutputFormatter
from interrogate.config import InterrogateConfig
config = InterrogateConfig(color=True, fail_under=75.0)
formatter = OutputFormatter(config)
# Example detailed row (filename, line, type, name, status)
detailed_row = ["src/main.py", "42", "function", "calculate", "MISSED"]
marked_row = formatter.set_detailed_markup(detailed_row)
# Row will be colored red for missed docstring
# Example summary row (file, total, missing, covered, percentage)
summary_row = ["src/main.py", "10", "3", "7", "70%"]
marked_summary = formatter.set_summary_markup(summary_row)
# Row will be colored red since 70% < 75% thresholdInstall with Tessl CLI
npx tessl i tessl/pypi-interrogate