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.6.tar.gz (47.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_ripgrep-0.0.6-cp38-abi3-win_amd64.whl (1.4 MB view details)

Uploaded CPython 3.8+Windows x86-64

python_ripgrep-0.0.6-cp38-abi3-win32.whl (1.4 MB view details)

Uploaded CPython 3.8+Windows x86

python_ripgrep-0.0.6-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.6-cp38-abi3-musllinux_1_2_i686.whl (2.0 MB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

python_ripgrep-0.0.6-cp38-abi3-musllinux_1_2_armv7l.whl (2.0 MB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

python_ripgrep-0.0.6-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.6-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.6-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (2.1 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

python_ripgrep-0.0.6-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.6-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.6-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.6-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.6-cp38-abi3-macosx_11_0_arm64.whl (1.6 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

python_ripgrep-0.0.6-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.6.tar.gz.

File metadata

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

File hashes

Hashes for python_ripgrep-0.0.6.tar.gz
Algorithm Hash digest
SHA256 87d7bf4ab07cc9767febeaffcf661b5170eb75a3c5fa55c6f86c8e40f028d006
MD5 2ba534065cf5a7270d17a4912f64e5fa
BLAKE2b-256 c04c631b52f5a8896065d525cbfb48b0cff259b518d7aeb1b0130bc0b7890638

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 db74c753d560236d2579b54c1593eb688349a7166aa7c54718ae6e16be68e351
MD5 ea39116a6e13f645c007f384359d9d20
BLAKE2b-256 a1093de12205422fe1e18bdd1eb7ecfbdd6e7d1abd2a5544848a82c535d0dba3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 3e1b9d776a07ea452d9910448925c253d5df331ee179276a7cf9ef584a8e8acb
MD5 2a086198d6cf52b384978cf2df16ae7f
BLAKE2b-256 9acfa74309c1f8f4e94be24e50776c4c104f067900d9bd83d48494ff275b3a54

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1fdc9b5dcd107c0c36888b0956ec40e4b7c08398106b998186f5537cd7332d96
MD5 87d3d3d951a221e44da677eeb0f6f7ca
BLAKE2b-256 d356a692c701aa91679d01b724f858676563bbf6418d6a20734e859f6c1bf139

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 fc72b9f96634bed3534a253707fcbe5beaf8e403c823893edc4bd7f459c5018f
MD5 a196e3848b6e37c4f1621a636c737a24
BLAKE2b-256 381e6f235717e58e6dba9cec147786c66f2e01e95f399b1180e03a1931f90f10

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 b4c45a40c584daf2ccd1a0396dcf46201e38f56208c0498daff41fa037edea3a
MD5 efd4df5a1e83031809cf9deb45734048
BLAKE2b-256 d166695ed94b562a1d15d23698bf0d3bfbb077a509639a17459d33b4495f0d08

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 7d5b00e954c8a052e7ecfa577f52dc836ea2fa72060aba90780235c36635e8aa
MD5 a65c6b4e2eb8047e04b3a57a98a0183a
BLAKE2b-256 9290c0be2acb09d191a87bf9a37a251ab85a22e24d3ff3073138728c562f9635

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a2024e4cb16654b09a37f4e3ab0e4add24bd4c86e528f438c4e1a7ea801ad911
MD5 7fac12dcb075a6df6527d2d5a33ce2c2
BLAKE2b-256 93a8fea4a21ae0aaa80404a54ed2799c127ad9222cacff539cd273b2759b4215

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 2351360ea9987804071097bcd27ab9f0817a201fa0c2918a5d871e8aa1d59369
MD5 2530748b4c6561c45170578e42eafd1f
BLAKE2b-256 0ade7ffd37a6d93fdc322049c6fc74553012c1b87efbce99665fcb3e1f9c7f90

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 e75466a2a2d799f9284fcfbdd27b3b96adeb5da9525da32496c12ef7cae8232c
MD5 5c61a3e15610ef4b88f822bd16759f32
BLAKE2b-256 aab4097e8164eaa4b7d29a537c4e23fc10e48b949e630bb16afa1242822a3e9e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 71dc6823d743ecf5d2c5b80e8e24e0e4fcdcfb74134238c6bfd3460535977d78
MD5 f0a971c8a12169f87a3c62ab4b1d128f
BLAKE2b-256 099ddfa8ae8ca0125aff0dae31db508acb3e3d46ee297c4af4203024da47e3e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 35e2d4fe7f7c7cdbcd84dbccac58e0a2306c478cf5a1066aa7e72e03a6fa115f
MD5 dca761aaf96102ce5e09bdd367322b0e
BLAKE2b-256 d808548f775254b9d1c46dd86fbf1afd152ce8c36e28081f32f7f6d0e601f646

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5fe2ae787b93449fa705a3ccc7fa45a64a25f7e18b3ad560185dafa65f1638b1
MD5 4c6a12392abef04f4b693a0528e7d6b6
BLAKE2b-256 8920b3101e5818c8fc0bcc21e0ec4e9260847346b9f58ddf073fbba06706d75c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6ecf2b7f2b3612bb90d6cf298db84e617905f20aa2073f4a015f3be3e032d10f
MD5 0c8413bb9f099161277b80442a6b6b32
BLAKE2b-256 48421c2c0b53bdddf8107c360d97f5eef318a657bb2a1fcf509f0065a2025361

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.6-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1af17c0472f29d036668e8cf1f38bed2c0738693781a27499b0e45a3a2d03d1b
MD5 cbeb9a761bb6c088490585530f7b545e
BLAKE2b-256 bfa161c2045a1b763da601f02c6813d3ecdcd6aae02f3fe2d4c90fde259e12e8

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