Skip to main content

A Python wrapper for ripgrep

Project description

python-ripgrep

A Python wrapper for ripgrep, providing fast and efficient text searching capabilities.

Description

python-ripgrep is a Python package that wraps the functionality of ripgrep, a line-oriented search tool that recursively searches directories for a regex pattern. This package allows you to harness the power and speed of ripgrep directly from your Python code.

Features

  • Fast text searching using ripgrep's algorithms
  • Recursive directory searching
  • Regular expression support
  • Customizable search parameters

Installation

You can install python-ripgrep using pip:

pip install python-ripgrep

Usage

Here's a basic example of how to use python-ripgrep:

from python_ripgrep import search

# Perform a simple search, returning a
# list of string results grouped by file.
results = search(
    patterns=["pattern"],
    paths=["path/to/search"],
    globs=["*.py"],
)

# Process the results
for result in results:
    print(result)

API Reference

The main components of python-ripgrep are:

  • search: The primary function for performing searches
  • files: A function for listing files that would be searched (--files equivalent)
  • PySortMode and PySortModeKind: Enums for specifying sort modes

For detailed API documentation, please refer to the source code comments.

Implementation Details

Direct Rust Integration

Unlike many other ripgrep bindings for Python, python-ripgrep doesn't shell out to the ripgrep command-line tool. Instead, it reimplements core ripgrep logic in Rust and provides a direct interface to Python. This approach offers several advantages:

  1. Performance: By avoiding the overhead of creating a new process and parsing stdout, this implementation can be more efficient, especially for large-scale searches or when called frequently.

  2. Fine-grained control: The library can expose more detailed control over the search process and return structured data directly to Python.

  3. Better integration: It allows for tighter integration with Python code, making it easier to incorporate into larger Python applications.

Current Limitations

As of now, the library implements a subset of ripgrep's functionality. The main search options currently supported are:

  1. patterns: The search patterns to use
  2. paths: The paths to search in
  3. globs: File patterns to include or exclude
  4. sort: Sort mode for search results
  5. max_count: Maximum number of matches to show
  6. case_sensitive: Control case sensitivity
  7. smart_case: Enable smart case matching
  8. no_ignore: Disable gitignore/ignore file handling
  9. hidden: Search hidden files and directories

Implemented Flags

The following is a checklist of ripgrep flags that have been implemented in this Python wrapper:

  • patterns: Search patterns
  • paths: Paths to search (default: current directory)
  • globs: File patterns to include or exclude (default: all non-ignored files)
  • heading: (Optional) Whether to show file names above matching lines
  • sort: (Optional) Sort mode for search results
  • max_count: (Optional) Maximum number of matches to show per file
  • after_context: (Optional) Number of lines to show after each match
  • before_context: (Optional) Number of lines to show before each match
  • separator_field_context: (Optional) Separator between fields in context lines
  • separator_field_match: (Optional) Separator between fields in matching lines
  • separator_context: (Optional) Separator between context lines
  • -U, --multiline: Enable matching across multiple lines
  • -i, --ignore-case: Case insensitive search (via case_sensitive=False)
  • -s, --case-sensitive: Case sensitive search (via case_sensitive=True)
  • -S, --smart-case: Smart case search (via smart_case=True)
  • --no-ignore: Don't respect ignore files (via no_ignore=True)
  • --hidden: Search hidden files and directories (via hidden=True)
  • --json: Output results in JSON Lines format (via json=True)

The following flags from ripgrep are not yet implemented in this wrapper:

  • -C, --context: Show lines before and after each match
  • --color: Controls when to use color in output
  • -c, --count: Only show the count of matching lines
  • --debug: Show debug messages
  • --dfa-size-limit: Limit for regex DFA size
  • -E, --encoding: Specify the text encoding of files to search
  • -F, --fixed-strings: Treat patterns as literal strings
  • -v, --invert-match: Invert matching
  • -n, --line-number: Show line numbers
  • -x, --line-regexp: Only show matches surrounded by line boundaries
  • -M, --max-columns: Don't print lines longer than this limit
  • --mmap: Memory map searched files when possible
  • --no-unicode: Disable Unicode-aware search
  • -0, --null: Print NUL byte after file names
  • -o, --only-matching: Print only matched parts of a line
  • --passthru: Print both matching and non-matching lines
  • -P, --pcre2: Use the PCRE2 regex engine
  • -p, --pretty: Alias for --color=always --heading -n
  • -r, --replace: Replace matches with the given text
  • --stats: Print statistics about the search
  • -a, --text: Search binary files as if they were text
  • -t, --type: Only search files matching TYPE
  • -T, --type-not: Do not search files matching TYPE
  • -u, --unrestricted: Reduce the level of "smart" searching
  • -V, --version: Print version information
  • -w, --word-regexp: Only show matches surrounded by word boundaries
  • -z, --search-zip: Search in compressed files

Note that this list may not be exhaustive and some flags might have partial implementations or behave differently from the original ripgrep. Refer to the source code for the most up-to-date information on implemented features.

Extending Functionality

To add more ripgrep options to the library, you'll need to modify both the Rust and Python sides of the codebase:

  1. Update the PyArgs struct in src/ripgrep_core.rs to include the new option.
  2. Modify the pyargs_to_hiargs function in the same file to convert the new Python argument to the corresponding ripgrep argument.
  3. Update the Python wrapper code to expose the new option to Python users.

For example, to add a new option case_sensitive:

  1. Add to PyArgs:

    pub case_sensitive: Option<bool>,
    
  2. In pyargs_to_hiargs, add:

    if let Some(case_sensitive) = py_args.case_sensitive {
        low_args.case_sensitive = case_sensitive;
    }
    
  3. Update the Python wrapper to include the new option.

Remember to handle any necessary type conversions between Python and Rust in the pyargs_to_hiargs function.

Development

This project uses maturin for building the Python package from Rust code. To set up a development environment:

  1. Ensure you have Rust and Python installed
  2. Install maturin: pip install maturin
  3. Clone the repository
  4. Run maturin develop to build and install the package locally

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details.

Acknowledgements

This project is based on ripgrep by Andrew Gallant.

Publishing to PyPI

This package uses GitHub Actions for automated publishing. When a new release is created on GitHub, wheels are automatically built for multiple platforms (Linux, macOS, Windows) and published to PyPI.

To publish a new version:

  1. Update version in pyproject.toml and Cargo.toml
  2. Commit and push changes
  3. Create a new GitHub release with a tag (e.g., v0.1.0)
  4. GitHub Actions will automatically build and publish to PyPI

This project is maintained by Indent.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

ripgrep_rs-0.3.0-cp314-cp314t-manylinux_2_39_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.39+ x86-64

ripgrep_rs-0.3.0-cp314-cp314-manylinux_2_39_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.39+ x86-64

ripgrep_rs-0.3.0-cp313-cp313t-manylinux_2_39_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.13tmanylinux: glibc 2.39+ x86-64

ripgrep_rs-0.3.0-cp313-cp313-manylinux_2_39_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.39+ x86-64

File details

Details for the file ripgrep_rs-0.3.0-cp314-cp314t-manylinux_2_39_x86_64.whl.

File metadata

  • Download URL: ripgrep_rs-0.3.0-cp314-cp314t-manylinux_2_39_x86_64.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: CPython 3.14t, manylinux: glibc 2.39+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ripgrep_rs-0.3.0-cp314-cp314t-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 8cba29b1b339a451b7867ff5cc332a55a6e1acfe30c75dc64d27bf15f4fb69a3
MD5 b4e3a1794d52812a7ff4bbd8fd2bea25
BLAKE2b-256 ae1263086ed8a263908febed4b330a0cb19283efd4257046ed8f64a4103912d4

See more details on using hashes here.

File details

Details for the file ripgrep_rs-0.3.0-cp314-cp314-manylinux_2_39_x86_64.whl.

File metadata

  • Download URL: ripgrep_rs-0.3.0-cp314-cp314-manylinux_2_39_x86_64.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: CPython 3.14, manylinux: glibc 2.39+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ripgrep_rs-0.3.0-cp314-cp314-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 26783816cfac7d19793b79c7f46bbad2fd54a791a55708b964194a510d1e9191
MD5 a816f1ff462a93ac57d54027dd2c8107
BLAKE2b-256 e40fcb6cff318df0df86c9e57e94c2e27ac35d5d97457cbc2ee8ef89a811e4fc

See more details on using hashes here.

File details

Details for the file ripgrep_rs-0.3.0-cp313-cp313t-manylinux_2_39_x86_64.whl.

File metadata

  • Download URL: ripgrep_rs-0.3.0-cp313-cp313t-manylinux_2_39_x86_64.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: CPython 3.13t, manylinux: glibc 2.39+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ripgrep_rs-0.3.0-cp313-cp313t-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 aeed696e19f3377f825bfbd7b86bb33aa140a2646275ee8232d4692c072281dd
MD5 0efb9ee0f6181c7b4056d8a07516c891
BLAKE2b-256 75de5408981fd05426b6749b52bded7e44083826defb3e8ff42d0af829d047ae

See more details on using hashes here.

File details

Details for the file ripgrep_rs-0.3.0-cp313-cp313-manylinux_2_39_x86_64.whl.

File metadata

  • Download URL: ripgrep_rs-0.3.0-cp313-cp313-manylinux_2_39_x86_64.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: CPython 3.13, manylinux: glibc 2.39+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ripgrep_rs-0.3.0-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 dfebefa8a2f880411d7b341969a7c98fcd689b37dc71bd7fe848a82b2a7ae9a8
MD5 f70c1b2c9e934e7dad0be6b6f4f0342d
BLAKE2b-256 3b8f6f708ed777b0bbea8f51e03760afba0a0077757b583f3377223a882e5e13

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page