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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.8+ Windows x86-64

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

Uploaded CPython 3.8+ Windows x86

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

Uploaded CPython 3.8+ macOS 11.0+ ARM64

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

File metadata

  • Download URL: python_ripgrep-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 83714d0d60651662ad0ed9dcac6153757cc9ad250f12f9eb014881849af4f97a
MD5 6bdc687acd797c194a0a12aec1ddd50f
BLAKE2b-256 500497e769c7c361f4fd36296852d7e6fa4602e54315b1cd595a4b5f9db1b698

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 32ece73b2421c1016e86979a938c893197a0cec622e433bf54959abdd2b17906
MD5 1784ac14e9251b22a01259e45fbde7fd
BLAKE2b-256 7ce1c1ea8f8940d7f1d1c111088810f1e1969d50e062c2f003385d776d5f6057

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 1dbb1ca82b21d7be15a42e85d09780a030a730a92986e34ba4d2ffe0ca621255
MD5 af7a1a6d52cc5018dc610e7d5e282b61
BLAKE2b-256 db3a9725d0818af1cd90cf3c493b3923281a70c232391d5b09aed7ab480b3026

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 dacbac563eab70a37bd841975583f5c6b48ce947356d1d6c80044964d3bfbbaf
MD5 f195c08d17a3a6b3992b8d76e8771a3b
BLAKE2b-256 724df05a0d5af79b0de3f59174baa305290b89e87a31ec26f7f261c2aed64f5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 04d144bfe8762c5809e8a8780c7c5f300ed3cbb753f77330e242458b46aa4615
MD5 78401f25245ddcfded3b39c220232cc5
BLAKE2b-256 59be9cb5cf81e1b670a2e7e13cdc715bbf2720170a312010fa56a7ae5a91dfcb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 cdd4c5bd31bbe911d4954beea5b205ef155e61d2b26a8a81e2a8b393346f1109
MD5 c39f1e2ff2c3fdf9eec7360f07767c1f
BLAKE2b-256 808256577a640e184b8fc048d66b15aa8e277bfc77ea23bbcf03551f78717dd6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 c500efe81793f10c4100da65255629de9e1d82b0a3e3489d6f47a6ec9429122e
MD5 7a6aa20eaa8c2e4eb299599607aef041
BLAKE2b-256 091d4e11b14ecbd06f5a6ee4baf312dfabbe8c94a26ca7536d7d0547955c1643

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 87dccf41bf38daf6ad4e7a82fdbe8a99e0fb067438b7fd4a709302073bd785a0
MD5 0f4c51c9c72e65f99285d540cdddc120
BLAKE2b-256 c8bd80b569acf3555fe026094b7e4fd09d4b2c1e7aa1b9ba225633b11cf446ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 3c025ac97af399f79e32ca4804421a702b1e9b9a3dffdd00c6952a64f5d87697
MD5 3800badfb6a130d93b226a67be3e9073
BLAKE2b-256 3297c15f1f16b11a119d1501c4c72d490d81bde5370020320d992f25fed426e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 0f0720e9f014b4495e9485150b90ee969c97f94c78ae3e190bb0aa51cb6185b8
MD5 9ac4fb2cc83c577b178f6cfad56325ca
BLAKE2b-256 0ffd05ae4f5fbeffb2c1f463bccfa5b02f386edc4010f72e5329f6f123e1e0dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 2a00f91da9c24869367501522d0f36fae9dbcb6a0657e26aff19aab2e9f37ac6
MD5 d06dd9b736351b4f7cffbb2d0acf9279
BLAKE2b-256 06959f5716fec49663e3caba55ec73e4e338e8bdbb7f7fd792c3929ed81a9f0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 9b586f40d0a161749676e95a0b6d5a5f6eae08f3425d23a6376857129065ce92
MD5 db2c4f2790509c9241ea065f28fcbdb8
BLAKE2b-256 df65b92cc3771ba478ee9c3e74c7445ba96e3e5bb58b50fc44e52e0122ab675e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 65e448da97f5eeaae9e76793623bddcd1b3417f930143b2716b0ab1b352b4e3c
MD5 e19718642d1b024658a52d40c2b3afb4
BLAKE2b-256 9bee9c35e073a9a966803dc8f43dda78e4e4b178f1e28a57ccfce75784b60afb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 94d4caa566119f175a55b88b5a4e6fd0faf9bc7bbd866f5c5421efe19ac7b709
MD5 98df630db6a001a542a1061d563a1b7e
BLAKE2b-256 2c1b20d942ecbe6a56c410148d31be59156bacb72f3f6d16efdcd5b3a85c6bbb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_ripgrep-0.0.3-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ee4595df036b6fabb40f8215e659b43df8aca8e1baa559e27a4ac96d4c7b8edd
MD5 a10cb40ebd92fea4b4dfcd58b5b4a522
BLAKE2b-256 7dabd7137698b3ed7615aeb76e5ad1f48aec5f48c5af96b6deaad72622c4ae56

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