CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-interrogate

Interrogate a codebase for docstring coverage.

Pending
Overview
Eval results
Files

utilities.mddocs/

Utilities

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.

Capabilities

Constants

Platform and configuration constants used across the package.

IS_WINDOWS = sys.platform == "win32"  # Windows platform detection

Regex Utilities

Click 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
    """

File Handling

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
    """

Path Utilities

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
    """

Output Formatting

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
        """

Usage Examples

Regex Pattern Compilation

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")

File Output Handling

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")

Path Utilities

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"

Output Formatting

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)

Platform-Specific Handling

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")

Advanced Formatting with Markup

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% threshold

Install with Tessl CLI

npx tessl i tessl/pypi-interrogate

docs

ast-visitor.md

badge-generation.md

cli-interface.md

configuration.md

coverage-analysis.md

index.md

utilities.md

tile.json