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

Uploaded CPython 3.8+Windows x86-64

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

Uploaded CPython 3.8+Windows x86

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

Uploaded CPython 3.8+macOS 11.0+ ARM64

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

File metadata

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

File hashes

Hashes for python_ripgrep-0.0.7.tar.gz
Algorithm Hash digest
SHA256 48b8cb3507b97e22f007315fe08817f23642212e29c964bc32a58330cf6328c2
MD5 5add44f14654e4f91e9500ec810e9327
BLAKE2b-256 69746e074082665d55e940fd772b6d7346e5142b8c828ef5ff489651eb6d87d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 430b5770280a1d7554ae6ad24fc58f287ac918f9fd8a3f0eded4e2460976b5d2
MD5 bd3c53e5ffdef2654062a51b7c718f9d
BLAKE2b-256 03dc8175d8bce00daacba4525a7cc4fab95bbb39fc571b041730dfa35e236c37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 dfbe601dc3f02f28382d28ae8a74170434899bcae39d1b34d5b63b14bab75404
MD5 631b54a58ac57ae92d390152a5f3fc4a
BLAKE2b-256 e766810015a7c82663154306a730f24c28778908485aa73c00c2207bbfb93fe1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a4e080354f90c6ae0a1424067a55e8641d3834802055d02d464e7125dd829077
MD5 03fd007f2c604c1d8a806d54206844c6
BLAKE2b-256 2424f124659f0575b7b2f8f951717ac6ce22183c126283704fd55bf17e756822

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 32ff380ccbb0f3ef53690226804791c9dee0192450778f334db115260aaad688
MD5 f0920f2fc610842a39575dab56b86d32
BLAKE2b-256 1fcbeb076f73d117275a5cf97658f19e3565bbcfa1f74e332bda280c7d254c60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 d1657a348729cd94adbe1335fcee52370141300d848e796f80120d7e83daf851
MD5 d6a7664cea17dfb18e9c3649e650c565
BLAKE2b-256 a1ecb746c0963f2c4845f8cf6b5885c776da39d2df280fe3f76a6c11af9349e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 5b5bf1dc8254f542c7ca73a4909cff1c8cf4fc21d6222755c288e8e33eff0f90
MD5 aab51e2e7aec1ac5e24ebda3f6693f47
BLAKE2b-256 ea901fc0ed22451f805e15bda8c24389a4230b5d051b4f42d80c32f2bbe8a457

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 766094f2de7f2f1808d7d85a6e63261a0d6452ef3a8e4174c3d4f4122370d89a
MD5 05474ec230a76b2f8fdb48e79dd3fc74
BLAKE2b-256 eda82997eb8e8d1fd5157bdac7d4817070307ebc202a78d533355490358583ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 e77477ebd1b9ed71baa21130cb4d5a73c37a764877abfb4934b42422ecbbcf0e
MD5 656924d0404ac1bb09b681bdfc04712e
BLAKE2b-256 7ff9acb8186529eac3a6df2829e2bd9b168b6384fb4498d19905d8302140fa07

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 3611e125f9823e39a9ed0912632eda779b0d522e94799871a0804b59b4dddf2f
MD5 2775772b55133d010e3b31c153dec22e
BLAKE2b-256 61d4d00b781d973a8589b4d544744c1d1c9fcf930f42cafa6eed1324af271b36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 86b77bd06320c5a4142e573d11d892dbc52acdac9d0f254e88b4a2b84ee0ce41
MD5 3cc36322c3872d6812229b24094fb07d
BLAKE2b-256 bd740d13d54d55f88b1051bccfb9afb2eb58abe40ee48e3ee9b9e82d2b487768

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 316782e9e8554cf056e4c6ea2452c9dd8437a7adf3ce508ab0000c512f93085e
MD5 4d8795657c78aad1acfeca4dac7e6ebf
BLAKE2b-256 3bd742553583dfd06c36ba3e7fdbb038ef044794e3104e4f960458527d465026

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 85e61c1404c2a44f42a07624e19f47c1581949572f1c1eecd15b9182a952bd08
MD5 fde26b69c2b62ff7aba3f54ae3521c9d
BLAKE2b-256 652765a4bf8e0fa492e731290f67cc0b780e2cbc59ab5a79225f76086b2c38fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e11fb2746af51a9c74ded32fa284a6d27947200e1cd747d0108761d3d364cbe9
MD5 58d8f675db882bb4490e29e61861cce3
BLAKE2b-256 205c4e591c997b8053408163562ec81d4628f4fc7ded6e433174a0958b1aa69f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.7-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 42182a3b1cf2a12c561f9091916eb579d2e5b90103a5cdd9fb2ce6cf9bc4801d
MD5 229d3ca74d679ab7fb913333ef8cbbbb
BLAKE2b-256 7498766901ba7d3e7d40f1233f64bc88495f91545a48f06613768388ff71a6c1

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