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.5.tar.gz (47.1 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.8+ Windows x86-64

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

Uploaded CPython 3.8+ Windows x86

python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_x86_64.whl (1.9 MB view details)

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

python_ripgrep-0.0.5-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.5-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.5-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.5-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.5-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.5-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.5-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (1.9 MB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ i686

python_ripgrep-0.0.5-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.5-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.5-cp38-abi3-macosx_11_0_arm64.whl (1.5 MB view details)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

python_ripgrep-0.0.5-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.5.tar.gz.

File metadata

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

File hashes

Hashes for python_ripgrep-0.0.5.tar.gz
Algorithm Hash digest
SHA256 d041ab14f1ec4af55e162cfb9a8f034103b1021650cd527433a6b937b57141d8
MD5 e0c4a00d48fec1ac3682cd4cccc91d36
BLAKE2b-256 ba8eb6db370d3fa4bbfdb3ef125d30f21cbaed16b31aaf2edaee6bba21840541

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c8d02d3adf926e53fd34f77b72f6959af0a971d311819ea4f8cd883f2cac3b67
MD5 a9df9bcbccb6663417eab2a061e41171
BLAKE2b-256 12501f00d8922f618fee4d2204d444fd9ba7cc0e3240c2d00c80bc00cabb2a24

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 bc0cc162812e1c818cfa0f34635085a6050bfb109efaf2f6488364a1be885aad
MD5 c896ffa67b7930c655f7267e08ecbf4a
BLAKE2b-256 a79ddc4f87b55884ac078bf0696f6380574ddf6bc06e60a1299dacdc523c6e44

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 364932d951d24d51957429f574d357bc101a476ec733fc3084175e3721c5f944
MD5 1afd5ddba6104cddeec85078d2f9d5a7
BLAKE2b-256 f391ca788c6056e88021196ed488246663bcec0d443cf2c58b0cb7b864f4a7f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 66a22f9949e6305d48f07162feff869e97ae02c30a7c784b9ee0b2dbb50dc9ca
MD5 8c21623dfc89f7929e5d6c9a9a90056f
BLAKE2b-256 8c27b6493be77e1dcef4a4b1d9a8fa5dcd9646716e4238870c05f9fbb5f94354

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 3066f027cb80a9c8f47ae75103e5cce1d6c6b879c80d4103da27b08de0d53b17
MD5 146c1da304290bb6cb4bd97c739f590f
BLAKE2b-256 d73b90eb5433b60a02e57bcaf5ccd5dd8e0b1a670c5d721505d232af1eddd26e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 c1f87e5631d687a9585a7fe91e40f6165d91e5f58344ba882dbf8dcda2805532
MD5 a66385126e76abb633cc75a11c880d81
BLAKE2b-256 0e6326bee8b18a2eea99305f3e8eda31a3db0276f0eccef912f3754bcdc881a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3b7b008026a0c94ee8fdde53f50eb367e4e9ed8ac717733fa74105b10ad4757d
MD5 a4a65e5d756019ead22ee87b23df5969
BLAKE2b-256 574b39b273bfbad3cb3b722b500101acc82b785df639856d9b5cb4191fdb9674

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 ed9892a4c52b92730b8be85667621b72d791db104b8bdccdc4b068e402314d61
MD5 18ed208f233a49ca00d5a986e4454968
BLAKE2b-256 391fc6b905741a7a85250f70bb30998c2dccfc28f2709959d8e56e87afe4fd73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 b8d4a57e5ae2563734c02f776904aa398985148eeb10b6342a95aae6a11be696
MD5 9aae15fcaa2b8ef7f7ff6f22ebab0c7f
BLAKE2b-256 ef203ac5100cfe95c2497ec1c22c15c94b337afd1a8bfde902b8290fc4ec4244

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 91306f0ac97fc04c1a8898a2c899b4abe02fad94578282879b6a6daf581d055d
MD5 315aa5c393e308e540d73ec144f73886
BLAKE2b-256 561a8fa450432b97b657e19c1914d244b8e0b138c5cde6d0de8aebe386190330

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 4a9ab20047bb39bd12905ca436766f68a5e764781988e3719a952059d71ada59
MD5 6b9ec797906c81f2a0687b0526395ecd
BLAKE2b-256 92fbce0ed8509a3cdf8de2d1d1e7f2b0f820d14cd6b3fa2189bd904a901d38d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6c22da98c341081e8da327e1ec4ca79be18131ff29bbd28e7fa8156ebfd6239a
MD5 28bb37923a42a5bd754a32affbf04929
BLAKE2b-256 35a82e54fe6b2ecf796b514ad0447eddcf35037577df25234f1151afb7c0da76

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8423d674a38e08b143e3a042f063954b2d14b53731ae8fb6896749a864483b05
MD5 5712bddd5686d2448eb2a41311677a3c
BLAKE2b-256 15816b01801bd64391a11e262968481dffc0a6a7e18ecd15fa19123e2a4e984e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.5-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 fd051b01722a7be7687fae2c699055e7d6eaefab9ce8884726e7367ecdc7d6e8
MD5 5d1fc180090a803e5338d248390a76b3
BLAKE2b-256 badddb248e4f3c65308d197a17149d0f663135f3985722a831cb76f77b2474a5

See more details on using hashes here.

Supported by

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