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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.8+ Windows x86-64

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

Uploaded CPython 3.8+ Windows x86

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

Uploaded CPython 3.8+ macOS 11.0+ ARM64

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

File metadata

  • Download URL: python_ripgrep-0.0.4.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.4.tar.gz
Algorithm Hash digest
SHA256 cd010eaa4fcb5e303fdbdd6093000233cebd754b023a7e91896ee1844b33b20f
MD5 dffa42c71f7fcc562abf11cd384392ee
BLAKE2b-256 e48be75933fcabc668d5ad0cb7dde21f4adfba2b8cc94f1aaaeedf3d56477293

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 bfbc0f99eac24e20e18047bf01cfe3997c2c842c327ba7898056e3c98939a11f
MD5 4097410af1be07071d69f79cf6dc0ae2
BLAKE2b-256 71a72678cd2b406b6a6c4d294e79c373978bf33a97c4fabb10fa67299b1779d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 f19d94120d96d6fcacf427bdc37e8318dd2f4fac2b72506589217d531e77fda3
MD5 f70a612be30faea1677a00f593bee227
BLAKE2b-256 42065d50e236d3f2b0157e9bbd1fe984a9f9c52621382e3f20f5b513bd76a7e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 af12967cad2f40d1ac63225db4160929227f4d18ed81d50b5d188eb3b2d5e1e1
MD5 d23e919677fb773e6bcf0438d5c14ff5
BLAKE2b-256 2861674e50a553db4a88723b4a931dabef40bf4335982c11bfc00daca0a6733e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 349a32896db94436c34fecef62a4f5e716a595f426868863a44e7fe68c87e490
MD5 bed9fc5cc4021b979918ec18253789d5
BLAKE2b-256 19951a94b50a8c9337c38d14137096b4f316460db0f97c0b17ddb7433a99ae5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 31cb4bd72d170005a1d0cabec63ba4e7801a04cc7ebff2937ae53f23db234a06
MD5 ea915041242a6fe0ff8f857c70172ef9
BLAKE2b-256 b7189168f662858d78286a88715a3164d97f31ca6fdd42aab9e556c059bc0d05

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 0dc02aa8971583c4756bdb08a7e3d796973809fd47e912fa973a6ac462787e15
MD5 b0de5488dc3f9635a3594d501e41638a
BLAKE2b-256 421de5d01bf12a008a556a8fcf733c57b18b0e4ab8805fdf840b9a66efe274e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1486c017e781b4f2ca7fccb41aabb42a82c95a59fed2fa2ae0d3ccdd1e0c6fb1
MD5 9aa7b37b05408ddaadd7d16141e2f9a3
BLAKE2b-256 5c5494536592adf11371de1b8328bbc8632c911e2666e5025807f3175012543d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 53548eb9801a6becec04954e90e329d6e42c4af010242c788dc3711925465799
MD5 4d76cbb1e42fff25696a03050759a876
BLAKE2b-256 772df18ef26c0297a71d47a2d78c56379264088b1dbf2079e8ab1003d96a4c16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 a7ff30875ac122b3607baf4b7a6d87a606503e4417e6efcf6d5bff49d539b2d4
MD5 547a768bb7431a32d388010d11d3073e
BLAKE2b-256 fca1d3f6cb2fb9c6a0416fa7ce14748848afd3be969a21536538bbf8e8a3f1a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 870f1c217845f9ca010a601a4952dfe6994cebb94af4d04d2cabf80a337b70f4
MD5 2a9660842df6cd08d79f0fa294450ffd
BLAKE2b-256 0f15b4463ed206ce7e9b142da78b72876aa232825203f8566523a0424fec97bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 37eb5747e54bf090131b6313ba9e63ad409c39e900520a938b74d62ef3b09886
MD5 f04723dd86eada3d255e88ac8bac8cf3
BLAKE2b-256 2733143ea17c8d008f92b529aabedfb7a7831ba0d9b5607749c45850384ebb03

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e03a06ab0c0623e9ff6b6a69042e310a41f5e6c634bb3190fe8fb4e6e6f2d9e1
MD5 f5269c910f35dc4d564756e9db3cb297
BLAKE2b-256 df2c5733cb65a84480ec6227d4b8acaf5b2f20c360bf00cb291e521ef7a1a6a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b8284528d40e7ce3c8ebe0340598c8422ba16bc0d8bfcd349b60f75bacc5aaa1
MD5 76b16323a044c6ac5ba980b071aa6a3d
BLAKE2b-256 c4c2cdecc9d38a880fcab6f70244411581ed5957c26233a7512087bcf4e45d9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.4-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 46f13b5f663210792c316081748254b94da05b525600dcbafcdef0b3c4d8972d
MD5 10bfff1844560224faa1b827e2675787
BLAKE2b-256 a4486e35b8da2b47bed0d74d42779fb172d2ec3b200e0ddb30dc4ea8ab75fea6

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