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 searchesfiles
: A function for listing files that would be searched (--files equivalent)PySortMode
andPySortModeKind
: 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:
-
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.
-
Fine-grained control: The library can expose more detailed control over the search process and return structured data directly to Python.
-
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:
patterns
: The search patterns to usepaths
: The paths to search inglobs
: File patterns to include or excludesort
: Sort mode for search resultsmax_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:
- Update the
PyArgs
struct insrc/ripgrep_core.rs
to include the new option. - Modify the
pyargs_to_hiargs
function in the same file to convert the new Python argument to the corresponding ripgrep argument. - Update the Python wrapper code to expose the new option to Python users.
For example, to add a new option case_sensitive
:
-
Add to
PyArgs
:pub case_sensitive: Option<bool>,
-
In
pyargs_to_hiargs
, add:if let Some(case_sensitive) = py_args.case_sensitive { low_args.case_sensitive = case_sensitive; }
-
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:
- Ensure you have Rust and Python installed
- Install maturin:
pip install maturin
- Clone the repository
- 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
Built Distributions
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | d041ab14f1ec4af55e162cfb9a8f034103b1021650cd527433a6b937b57141d8 |
|
MD5 | e0c4a00d48fec1ac3682cd4cccc91d36 |
|
BLAKE2b-256 | ba8eb6db370d3fa4bbfdb3ef125d30f21cbaed16b31aaf2edaee6bba21840541 |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-win_amd64.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8d02d3adf926e53fd34f77b72f6959af0a971d311819ea4f8cd883f2cac3b67 |
|
MD5 | a9df9bcbccb6663417eab2a061e41171 |
|
BLAKE2b-256 | 12501f00d8922f618fee4d2204d444fd9ba7cc0e3240c2d00c80bc00cabb2a24 |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-win32.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-win32.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.8+, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc0cc162812e1c818cfa0f34635085a6050bfb109efaf2f6488364a1be885aad |
|
MD5 | c896ffa67b7930c655f7267e08ecbf4a |
|
BLAKE2b-256 | a79ddc4f87b55884ac078bf0696f6380574ddf6bc06e60a1299dacdc523c6e44 |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_x86_64.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.8+, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 364932d951d24d51957429f574d357bc101a476ec733fc3084175e3721c5f944 |
|
MD5 | 1afd5ddba6104cddeec85078d2f9d5a7 |
|
BLAKE2b-256 | f391ca788c6056e88021196ed488246663bcec0d443cf2c58b0cb7b864f4a7f2 |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_i686.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_i686.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.8+, musllinux: musl 1.2+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66a22f9949e6305d48f07162feff869e97ae02c30a7c784b9ee0b2dbb50dc9ca |
|
MD5 | 8c21623dfc89f7929e5d6c9a9a90056f |
|
BLAKE2b-256 | 8c27b6493be77e1dcef4a4b1d9a8fa5dcd9646716e4238870c05f9fbb5f94354 |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_armv7l.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_armv7l.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.8+, musllinux: musl 1.2+ ARMv7l
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3066f027cb80a9c8f47ae75103e5cce1d6c6b879c80d4103da27b08de0d53b17 |
|
MD5 | 146c1da304290bb6cb4bd97c739f590f |
|
BLAKE2b-256 | d73b90eb5433b60a02e57bcaf5ccd5dd8e0b1a670c5d721505d232af1eddd26e |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_aarch64.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-musllinux_1_2_aarch64.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.8+, musllinux: musl 1.2+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1f87e5631d687a9585a7fe91e40f6165d91e5f58344ba882dbf8dcda2805532 |
|
MD5 | a66385126e76abb633cc75a11c880d81 |
|
BLAKE2b-256 | 0e6326bee8b18a2eea99305f3e8eda31a3db0276f0eccef912f3754bcdc881a4 |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b7b008026a0c94ee8fdde53f50eb367e4e9ed8ac717733fa74105b10ad4757d |
|
MD5 | a4a65e5d756019ead22ee87b23df5969 |
|
BLAKE2b-256 | 574b39b273bfbad3cb3b722b500101acc82b785df639856d9b5cb4191fdb9674 |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ s390x
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed9892a4c52b92730b8be85667621b72d791db104b8bdccdc4b068e402314d61 |
|
MD5 | 18ed208f233a49ca00d5a986e4454968 |
|
BLAKE2b-256 | 391fc6b905741a7a85250f70bb30998c2dccfc28f2709959d8e56e87afe4fd73 |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ppc64le
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8d4a57e5ae2563734c02f776904aa398985148eeb10b6342a95aae6a11be696 |
|
MD5 | 9aae15fcaa2b8ef7f7ff6f22ebab0c7f |
|
BLAKE2b-256 | ef203ac5100cfe95c2497ec1c22c15c94b337afd1a8bfde902b8290fc4ec4244 |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91306f0ac97fc04c1a8898a2c899b4abe02fad94578282879b6a6daf581d055d |
|
MD5 | 315aa5c393e308e540d73ec144f73886 |
|
BLAKE2b-256 | 561a8fa450432b97b657e19c1914d244b8e0b138c5cde6d0de8aebe386190330 |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ARMv7l
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a9ab20047bb39bd12905ca436766f68a5e764781988e3719a952059d71ada59 |
|
MD5 | 6b9ec797906c81f2a0687b0526395ecd |
|
BLAKE2b-256 | 92fbce0ed8509a3cdf8de2d1d1e7f2b0f820d14cd6b3fa2189bd904a901d38d2 |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c22da98c341081e8da327e1ec4ca79be18131ff29bbd28e7fa8156ebfd6239a |
|
MD5 | 28bb37923a42a5bd754a32affbf04929 |
|
BLAKE2b-256 | 35a82e54fe6b2ecf796b514ad0447eddcf35037577df25234f1151afb7c0da76 |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-macosx_11_0_arm64.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8423d674a38e08b143e3a042f063954b2d14b53731ae8fb6896749a864483b05 |
|
MD5 | 5712bddd5686d2448eb2a41311677a3c |
|
BLAKE2b-256 | 15816b01801bd64391a11e262968481dffc0a6a7e18ecd15fa19123e2a4e984e |
File details
Details for the file python_ripgrep-0.0.5-cp38-abi3-macosx_10_12_x86_64.whl
.
File metadata
- Download URL: python_ripgrep-0.0.5-cp38-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 1.6 MB
- Tags: CPython 3.8+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd051b01722a7be7687fae2c699055e7d6eaefab9ce8884726e7367ecdc7d6e8 |
|
MD5 | 5d1fc180090a803e5338d248390a76b3 |
|
BLAKE2b-256 | badddb248e4f3c65308d197a17149d0f663135f3985722a831cb76f77b2474a5 |