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

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

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
  • -U, --multiline: Enable matching across multiple lines
  • --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

[License information to be added]

Acknowledgements

This project is based on ripgrep by Andrew Gallant.


This project is built and maintained by the Exponent team.

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_ripgrep-0.0.8.tar.gz (47.8 kB view details)

Uploaded Source

Built Distributions

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

python_ripgrep-0.0.8-cp38-abi3-win_amd64.whl (1.4 MB view details)

Uploaded CPython 3.8+Windows x86-64

python_ripgrep-0.0.8-cp38-abi3-win32.whl (1.3 MB view details)

Uploaded CPython 3.8+Windows x86

python_ripgrep-0.0.8-cp38-abi3-musllinux_1_2_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ x86-64

python_ripgrep-0.0.8-cp38-abi3-musllinux_1_2_i686.whl (1.9 MB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

python_ripgrep-0.0.8-cp38-abi3-musllinux_1_2_armv7l.whl (1.9 MB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

python_ripgrep-0.0.8-cp38-abi3-musllinux_1_2_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

python_ripgrep-0.0.8-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_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (2.0 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.9 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (1.8 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ i686

python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.7 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.7 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

python_ripgrep-0.0.8-cp38-abi3-macosx_11_0_arm64.whl (1.5 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

python_ripgrep-0.0.8-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_ripgrep-0.0.8.tar.gz.

File metadata

  • Download URL: python_ripgrep-0.0.8.tar.gz
  • Upload date:
  • Size: 47.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.3

File hashes

Hashes for python_ripgrep-0.0.8.tar.gz
Algorithm Hash digest
SHA256 ebb0f4618c4a15baad524beab636e2b59e87b4ca283ceed0a1026384118d466b
MD5 d60caf854faa4833603ffff6362b2cbb
BLAKE2b-256 b4afc7000c2076169b2420790792bed5a9699280c6ba9865b4e07d725801cf5f

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 45ca2f4e7f104aee98eaddd9fa58487b7b4c092f61424fa8890bee3a8048efcb
MD5 0afee1f09a6b5f18d6ae85b77e02c2fe
BLAKE2b-256 5e4ecc99fd63b00ea6e197bfbaaea9e55bb5a3ce0d3ab34ba2ce9ce50b6c0da0

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-win32.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 d52fffe9a779ae0cabadd24a891d9ba336628291b52812b03fb0fac31f7dd10b
MD5 6e75c2a29ae4f6ffe74c9f9cd4caa2f3
BLAKE2b-256 a60ddade61e0617cf36e31f40f4fc8eff7f6715785de60ea68e1493ee4b2606e

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 48be1bff7df234fed96a839022075c75e9c03f0e8c247a55d990a7d448639041
MD5 1c522e8d2611435ae8cb36ebb65c6d41
BLAKE2b-256 fc6a2b2889cbe36a593ac8bba3f2466d345a61f0d5938109b006b58fa1351d2f

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 f46fdd232716c371b57977da5753038a95fcc0e0b45a882910987be1802e555f
MD5 058581889dfc5dcde9611192732d60f6
BLAKE2b-256 7d915cf70b5143d90c529bde900d689dc502f088a7efdb0cce253433e7c6fe92

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 18a4dad0122213c01cdb19f8bfb9cd0535c9cd4fecc9a444be77a58088dcb6a6
MD5 d88e2198cc33a6ea6b916ff0a1d3f519
BLAKE2b-256 bde5242aef8a2ff28726b4e17dff6b568e94c3c7670e0e14d084334045732ad6

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 ea68a80096aaefde6dea0054c0e94d868e87f22fe095a69801e86e92c5e6a689
MD5 01f1eb20263dbf9e2bd6e47a6678f574
BLAKE2b-256 36ce7345bd5709a846f4bee27a3977474c2bb2252dd891296714636c50c4080a

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 55e214264de03497ff80e768d7c277383c4fba9c2b000a102b132a7430a8453a
MD5 488853baa2f5796ef4234064372554a1
BLAKE2b-256 416264f1aac8ff9aa9032da090eb888abe88808e6fb26a04528bd5fe02ae4b2e

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 1a49a13680bb3a8400fb9711983c801c019e33fdd0c93a797baf435309eda7b7
MD5 10c5851f19df31c6015ad5fa5995d294
BLAKE2b-256 1a4ba21970322be689c783aacb11ef8116ae9ad7c3d6d0c0db7db2ae8203b976

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 8b9cace3e6425442c85426d239249b7a5c7694ee84d5318a607b64de5b2de080
MD5 3685264fb4b7fa31d6fe1adbd8998fe8
BLAKE2b-256 76060159f1cb044b0cac6c3b040bd4a756f5c04dc0d9e8baccf60f4fcd0e5035

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 db6e7226e12cf3fce2439bbc7a1483ee30a037c07d1c044d87e420fe93be7614
MD5 6a4bb725f82a811814f201f5f8feb726
BLAKE2b-256 2b8d450921470e0a92609027ab9dd2b303d02df20a83b53bff963c50b1f2856f

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 aac02ab81bd582e3fbc8f7069cd7ebc271c164f38b26c375b1a05af291abc1e0
MD5 0951cdfa67bfb7d0549865a1bb758483
BLAKE2b-256 5b18ed444b7ff296f76a764d81eb4785a31c3caad18932d255cceac6f16e6866

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 727d5066a8dc2c3b60b1b0825727185d1204685c0dc061ceaf489d9255a8ffcf
MD5 880c14689b8c90cf64fe44727ab09dd4
BLAKE2b-256 4193ec68cd3eed220919dec1b26cd659360c52d9a83483c6a5c55d8aeaaa7026

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2129ea8a9be97122a437518d3b856ca01b2c83c29b107ffa6dea0f30678ef442
MD5 97da9ccf8e203513ebf5cd3dfcf5b5f1
BLAKE2b-256 cfedf7a0fbc74484ba51d5db4ed3ed1d92668f336fcda63d4ad37a962522410a

See more details on using hashes here.

File details

Details for the file python_ripgrep-0.0.8-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for python_ripgrep-0.0.8-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1a5ba9063d4f6c15bd2c77cb3f5d94af0c550a4530179340dbeaea0a5d55e0dd
MD5 c384256a20cace68b4f7932c305c51f9
BLAKE2b-256 421cbb4d91556f7dfe61f3d4b9e11b788ccbba10f50644d51add1ddc2ee9a5fc

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