Skip to main content

Python API for ripgrep-style file walking and searching

Project description

rgapi

rgapi is a Python API for ripgrep-style walking and search. It is meant for Python code that wants fd-style file discovery or rg-style searching without shelling out.

It uses the same ignore, grep-regex, and grep-searcher crates that ripgrep uses for walking, regex matching, and file scanning. Walking and searching run in parallel by default. Most expensive work stays in Rust.

Overview

For common file discovery and search:

from rgapi import fd, rg, rg_iter

fd(".", ext="py", exclude="test_*.py")
for row in rg_iter("TODO", ".", include="*.py", context=2): print(row.asdict())
rg("TODO", ".", ext="py", skip_dir=".venv", paths=True)

For direct access to the regex, search, and walk pieces:

from rgapi import compile, search_path, search_text, walk

matcher = compile("TODO")
matcher.is_match("TODO")
matcher.finditer("TODO TODO")

walk(".")
search_text(matcher, "alpha\nTODO\nomega\n", path="memory.txt", context=1)
search_path(matcher, "src/lib.rs", display_path="src/lib.rs")

Install

pip install rgapi

Semantics

fd and walk return slash-separated paths relative to root. They use the ignore crate, so .gitignore and the usual ripgrep filters apply by default. Hidden files are skipped unless hidden=True. Pass ignore=False to disable ignore filtering. Symlinks are not followed unless follow_links=True; same_file_system=True avoids crossing filesystem boundaries. Traversal is parallel, and result order is not guaranteed; use sorted(...) if order matters. root arguments accept str or pathlib.Path and expand ~; search_path also accepts path-like file paths. Display labels such as display_path are stringified without expansion.

fd adds fd-like filtering on top of walk: pattern is a substring match on the relative path, and include/exclude use glob syntax. glob= is accepted as an alias for include=. A basename glob such as *.py also matches recursively, so it finds src/app.py. Use ext="py" or ext=["py", "rs"] for extension filters, min_depth=/max_depth= to bound recursion, and max_filesize= to skip files above a byte limit.

path_re and skip_path_re are regex filters on slash-separated relative paths. They filter returned paths or searched files, but do not control traversal. skip_dir uses glob syntax to prune matching directory subtrees, and skip_dir_re does the same with regex.

rg and rg_iter return structured rows rather than raw CLI text. They accept the same include, exclude, glob, ext, path_re, skip_path_re, skip_dir, skip_dir_re, min_depth, max_depth, max_filesize, follow_links, and same_file_system filters as fd. Each row is a SearchLine with:

kind         'match', 'before', 'after', or 'context'
path         path relative to root
line_number  1-based line number
line         line text without the trailing newline
matches      list of (start, end) byte offsets for match rows

rg, search_text, and search_path return SearchResults by default, a list subclass whose str() and notebook pretty display are rg-style multiline text. rg_iter yields rows lazily.

SearchLine has a structured repr, an rg-style str, and SearchLine.asdict() returns row fields as a plain Python dict. rg(..., paths=True) returns unique matched paths, and rg(..., count=True) returns the total number of match spans. paths and count cannot both be set.

before_context, after_context, and context are like rg -B, rg -A, and rg -C. Files containing NUL bytes or invalid UTF-8 are skipped.

Search is case-sensitive by default, matching rg. Use smart_case=True for rg --smart-case behavior, or case_sensitive=False to force case-insensitive matching.

Benchmarks

tools/bench.py compares the rg CLI with in-process rgapi. Run it against a release build. One run on this machine, using best time from seven repeats:

fixture rg rgapi
6 x 2 MB files, 2 matches 6.54 ms 1.44 ms
800 x 1.5 KB files, 2 matches 13.90 ms 10.94 ms
tiny dir, repeated 30x 5.92 ms 2.14 ms

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

rgapi-0.1.2.tar.gz (20.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

rgapi-0.1.2-cp313-cp313-manylinux_2_34_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

rgapi-0.1.2-cp313-cp313-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

rgapi-0.1.2-cp312-cp312-manylinux_2_34_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

rgapi-0.1.2-cp312-cp312-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

rgapi-0.1.2-cp311-cp311-manylinux_2_34_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

rgapi-0.1.2-cp311-cp311-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

rgapi-0.1.2-cp310-cp310-manylinux_2_34_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

rgapi-0.1.2-cp310-cp310-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file rgapi-0.1.2.tar.gz.

File metadata

  • Download URL: rgapi-0.1.2.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rgapi-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b89b7e8f1cf61a92b3723d3bba705b7e465fc5bd58e865a20939a0c91bda8d89
MD5 6948f01e16a4c8e29f883edd8db6b3fd
BLAKE2b-256 e2f4802b9fc96ad2b6341efed1fb8d9707dabfcf9ed78cc553f6fb1e01fe97c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for rgapi-0.1.2.tar.gz:

Publisher: ci.yml on AnswerDotAI/rgapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rgapi-0.1.2-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rgapi-0.1.2-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 8ce5c71161e768e100b2ddcfc74da569bba2b49bf58502bb895946623936e10e
MD5 70d952aa96c78a5b480f6c5db76ec3f3
BLAKE2b-256 8073cc5e35c3b217fa19178924f82c2a93f686f33031344a7c76ee51d03e89b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for rgapi-0.1.2-cp313-cp313-manylinux_2_34_x86_64.whl:

Publisher: ci.yml on AnswerDotAI/rgapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rgapi-0.1.2-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rgapi-0.1.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9aafd021cd0ba4d94d5ce65da5d2bddbee41326f59ae173f4de4abe1e0b93ec9
MD5 d69baf11e3d468dc82ce520885c6f882
BLAKE2b-256 721e216fe09282a05ddc7041e6bd86aab49c7efbf0b78b5593ee0a4b2e5cf5dc

See more details on using hashes here.

Provenance

The following attestation bundles were made for rgapi-0.1.2-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: ci.yml on AnswerDotAI/rgapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rgapi-0.1.2-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rgapi-0.1.2-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 d2a35f817d30b2152b98ae53524e8c56a359866a0801cffbb321004fe0df4156
MD5 b1929f94e573eab1f0805fde59e50b64
BLAKE2b-256 11458c9d1125f335b92c82b5f25c129585991fe03ea7015a273a1294643f305d

See more details on using hashes here.

Provenance

The following attestation bundles were made for rgapi-0.1.2-cp312-cp312-manylinux_2_34_x86_64.whl:

Publisher: ci.yml on AnswerDotAI/rgapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rgapi-0.1.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rgapi-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 afc308fdf29b75c5b6ae41579182598e7e3b8383be87b1cffdd9af16d231637b
MD5 21bc88f23fd6f0bd34d1ad1b81a9a380
BLAKE2b-256 ea5e9b15b6c380ea5a6eb747b55ff20574c4fd12657a9420e1ef96baa32412e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for rgapi-0.1.2-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: ci.yml on AnswerDotAI/rgapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rgapi-0.1.2-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rgapi-0.1.2-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 91ae8f8974f809d5cc44028b0417bf536f9a6f010bf92313afa849f367313831
MD5 452b955f34ea5e7ec612c6827a67924d
BLAKE2b-256 f843783e944bdb7209e29a61c29b4fded13b4e0aae0425805ff312cd09721669

See more details on using hashes here.

Provenance

The following attestation bundles were made for rgapi-0.1.2-cp311-cp311-manylinux_2_34_x86_64.whl:

Publisher: ci.yml on AnswerDotAI/rgapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rgapi-0.1.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rgapi-0.1.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5f67b6192f7715c8619df19dece8e5bb229378e8b899a97c8fbe87f4f94f2894
MD5 190b6936ab81f115c1c19a6cf652703b
BLAKE2b-256 abb0e942cb3c0e3cac1203c866dd88ade5cecbd8d4cbe75964ddd00faf950561

See more details on using hashes here.

Provenance

The following attestation bundles were made for rgapi-0.1.2-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: ci.yml on AnswerDotAI/rgapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rgapi-0.1.2-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rgapi-0.1.2-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 45fcce24db935b622b7418ba80c036a80ce9b3e59c6030d88a5622e45930923b
MD5 ad6cc52fc3536c96404b6e425d609f10
BLAKE2b-256 e5e774524ed49bcf859d40b65b6220f87ac7aa85245426ee7763be1a765b105b

See more details on using hashes here.

Provenance

The following attestation bundles were made for rgapi-0.1.2-cp310-cp310-manylinux_2_34_x86_64.whl:

Publisher: ci.yml on AnswerDotAI/rgapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rgapi-0.1.2-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rgapi-0.1.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1e0eae9ca2dd609cb05d844d976c477d48ca6b22fe03ce56902e8d68d9c5c42c
MD5 8f4a14f6e62a11a8c08345b67d9b56f0
BLAKE2b-256 4f818e6029dc56938e73f9e6aabe1b508168008f36e9e21a1d5503ec669c47b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for rgapi-0.1.2-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: ci.yml on AnswerDotAI/rgapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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