Skip to main content

A Python wrapper for ripgrep

Project description

python-mripgrep

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

Description

python-mripgrep 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.

Modify by maming, to support python3.14

Features

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

Installation

You can install python-mripgrep using pip:

pip install python-mripgrep

Usage

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

from python_mripgrep 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-mripgrep 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-mripgrep 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

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

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
  • -i, --ignore-case: Case insensitive search
  • -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-ignore: Don't respect ignore files
  • --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
  • -S, --smart-case: Smart case search
  • -s, --case-sensitive: Case sensitive search
  • --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

maturin publish


This project is maintained by mm-bupt.

Project details


Download files

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

Source Distribution

python_mripgrep-0.0.11.tar.gz (50.1 kB view details)

Uploaded Source

Built Distributions

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

python_mripgrep-0.0.11-cp38-abi3-win_amd64.whl (1.4 MB view details)

Uploaded CPython 3.8+Windows x86-64

python_mripgrep-0.0.11-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

python_mripgrep-0.0.11-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.8 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

python_mripgrep-0.0.11-cp38-abi3-macosx_11_0_arm64.whl (1.6 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

python_mripgrep-0.0.11-cp38-abi3-macosx_10_12_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file python_mripgrep-0.0.11.tar.gz.

File metadata

  • Download URL: python_mripgrep-0.0.11.tar.gz
  • Upload date:
  • Size: 50.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for python_mripgrep-0.0.11.tar.gz
Algorithm Hash digest
SHA256 570983abfef424e630e422fd711d83696320778b781a57d77d6e1463c81261df
MD5 ef5b5f67f807b7aae2bd4712c676f43e
BLAKE2b-256 b4a8cc57d93ec95731edeebacba2331cc40d6bb132d51c13fd7d588381bc8aa6

See more details on using hashes here.

File details

Details for the file python_mripgrep-0.0.11-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for python_mripgrep-0.0.11-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 06ad98b85174189ed9618da7d7f054b40c5c0cf89552457566a57e001dc2417e
MD5 d427ff545b74e3b253da731ba721c7f1
BLAKE2b-256 a45a3ea28ae3c13a637d080a171fbbc2256806f50e5863820fe837c2f78e99ce

See more details on using hashes here.

File details

Details for the file python_mripgrep-0.0.11-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for python_mripgrep-0.0.11-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3d58fb055617a0d3fd13b1d71799857ecb70b3045de883e906a7ace8036674f5
MD5 0fb4435485a5749a16bff14c9338ae0d
BLAKE2b-256 dcbd3af987576a9bbfde77ffcaefa3cd54fd293a25a3a43b427ff4524b57ae6b

See more details on using hashes here.

File details

Details for the file python_mripgrep-0.0.11-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for python_mripgrep-0.0.11-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fb26e4bf0f9734988668963a53a231d0a93a7a9feaf109060f54281e52bdfb19
MD5 1bbdf85a363ef6343de80a784524826d
BLAKE2b-256 ff5d7de9510ff8fc062058c6daa0da04c345d7e73efe69665511400d424c8ffc

See more details on using hashes here.

File details

Details for the file python_mripgrep-0.0.11-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for python_mripgrep-0.0.11-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b78f40f69a1290b6b2f9f7df978cd66e425dcb9e6e64d7b8ff9a7c9b13314a02
MD5 c05813d1e04bc49468fa2134b6b58858
BLAKE2b-256 0191214c11e95611e6742db9e2b18add23088182c45bffe6d576a4cc84e78b85

See more details on using hashes here.

File details

Details for the file python_mripgrep-0.0.11-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for python_mripgrep-0.0.11-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 67124500fb20af99399e697a9a66751511896f28f63bbe4d6a5fa3d75c78a19e
MD5 9f7b03362437713992d36976eca4e23e
BLAKE2b-256 058c38f33777ba2cdbca2a5d72a5fec1dc18ef4bd6113698b95f9cd45fe5c43a

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