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, PyArgs

# Perform a simple search, returning a
# list of string results grouped by file.
results = search(
    PyArgs(
        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)
  • PyArgs: A class for configuring search parameters
  • 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.2.tar.gz (46.7 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.8+ Windows x86-64

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

Uploaded CPython 3.8+ Windows x86

python_ripgrep-0.0.2-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.2-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.2-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.2-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.2-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.2-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.2-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.2-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.2-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.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.8 MB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

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

Uploaded CPython 3.8+ macOS 11.0+ ARM64

python_ripgrep-0.0.2-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.2.tar.gz.

File metadata

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

File hashes

Hashes for python_ripgrep-0.0.2.tar.gz
Algorithm Hash digest
SHA256 3c73458a54206aad331d67c0aa7a283c9e943de8937051e63ef0a1120a14cda7
MD5 cb704382bc7f0662f6fb6950c485b1df
BLAKE2b-256 4e749502b3b7e6fa97013f3f4bdcbf329a8e9356d8bae3380bf3969df3096760

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f4a8b8fb5b551335c8d19a66a346fd14481b91442a46e52f3283ef3a5369fcc4
MD5 7fb030ddd9217fdd78190ea26c9489a1
BLAKE2b-256 9f38702eadec6cab3eab9f19b8d69d001ae7218b38ae31845873957294e3487f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 8558f0d86049ea125fccc197f92e9d8e1423be63511bcbdb91a8b087c7059326
MD5 7dc6d30a1d9a57070d8d55818b83a775
BLAKE2b-256 abffe604c2fe79372b34b013df50758e784e0e94e0d7aaa1ca6608b06ac5ff64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c53433ea6165356dd380dda3fadd43d42f4a4fa197966fd59cb2bfa65dedc751
MD5 ff64426c8dfc15fa5850aec1adbe0832
BLAKE2b-256 608ad44ca8368b3dde2e9b4a68f90c710e3fb4d3915bd04dbb5d63e880b69551

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 3f1366e0ec08cdc3495b52326aadf0715b20a11a6a11b986059597d45396a733
MD5 94189466eb531e5c8ee35f0807467d25
BLAKE2b-256 3dab73af24a0b8fc27150d0c1db42193494ac65fc357dc76561ce22670f589b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 710ef46dad84dd06526c26a3fa06122c4d86a748732575fa903c02dce78a4bad
MD5 dd3b74910217f32975236ced02f4d898
BLAKE2b-256 28a3675457b3db2fecab75fbd27c9f946fdbe04e95ea57726554917b675c738d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 2ad1565c31eeba7fa5e3713addd19da60b3267a51e256bd3523d6db08a63de08
MD5 676ad86770cf4258d9db6fa93fedfe4f
BLAKE2b-256 70bcbafdcef40f0b4db22810bf86b9567949de51ba34f58f0cf02cd8d5bd19cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2271dcae193ff1d14cc9ecd0c9838a8c7672750d4bfe403e21ce95427c47e8ee
MD5 ae1adb899922c62bba9f812e0e3f13a2
BLAKE2b-256 1a47cbd2c9f19339a73aaf301540530f9876274e2d7d7b025fad47a5d86f921a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 1ed6288a5024729cef16bc52217b0dfbfe1af65f2d19329fe1aec43fcfb0dce1
MD5 da1a28f7e5d801099236c2b748481e87
BLAKE2b-256 543392ba80ff509ebf2b21359ce9fb407454dd5505aae505b1275f9fa3fb6c59

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 088d8097d2a4e012dead8f6e397598bc4f1d321f619573ddc1ef436c832e76c2
MD5 28c671fc127e567558ee276b38f2bbaf
BLAKE2b-256 a1b917b36613665c1e20815c94e53e631c5b5d2e057cfb0e079666f7cda17884

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 55ab87962871cf45ac92715ef2db0dde332ce4df35730717b89782501cf26837
MD5 c212f3d74a2ddfc49816a3125bd08fd3
BLAKE2b-256 4c58ef1cb4d53032ec387e51fd110ed3748739999a3b0f8feb27309c2bd4decc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 4acfa7409c0f7176acf9eee8a5ee4295622b8712c16b92d27494b19b63afd241
MD5 00884d76ce8dcff1916f2ddcbef217eb
BLAKE2b-256 0270efc538f1917f0f5ed65a630a196f2ccdf8d76f018d9d88ec078bd4510cd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ab5068ed244e7352a5a18ea17ecddacad136d27a28a36cbec766c0c319768228
MD5 3e7f688ed5b8c01c3c6d14e076985819
BLAKE2b-256 099ebeecbf1ffebbee4d562307d2c113adece50a40112d97257db435481afabd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 79bd3087cbbf3d0cb97e5398f87bbe393a518936c8b5d97186947f842c00ad62
MD5 97f4ad1c4fa8095f64676970e1a0a680
BLAKE2b-256 04b9abd45bcbbcec80a0a1382a9192085d1bf62b75fc368a855f22cdfd927f69

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.2-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f728dec24850332f0c79868fe6ea4f94a2b0d10a4d923e6e48c728897b282559
MD5 bd3df113dc5f495bc9ab32b8c446e249
BLAKE2b-256 bf633fadcae1e46ceae38163c13f8787f02a51684167ca0edd6a4f99e0c093b5

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