Skip to main content

A command line tool to fetch code from github for AI purposes.

Project description

codegrab

PyPI Changelog License

codegrab

codegrab is a command-line tool that allows you to extract Python module or function code from local files or directly from a GitHub repository. It concatenates all files fetched to prepare for feeding into LLMs.

Features

  • Retrieve an entire Python module locally or from a GitHub repository.
  • Extract specific function definitions, including decorators.
  • Supports specifying a GitHub repository and branch to fetch files remotely.
  • Concats all results.

Why this is useful

By fetching code and concatting it into a string, you can feed it into tools like Simon Willison's command-line tool llm.

The following will take one function from the specified code base, compare it to another, and use OpenAI to give feedback on why one works and not the other by piping it into the llm command.

uv run codegrab module.location:function --repo [private repo] \
 | uv run codegrab other_module.location:function --repo [private repo] \
 | llm --system "The first function does not work, the second one does. The first one fails when I supply stats=['mean'] whereas the second one successfully includes stats. Can you see what the difference could be? Can you suggest changes to the first implementation so that it accepts stats in the same way the second one does?"

Fetching code as a string could also be used to load a prompt with lots of examples to show it how to write domain specific code and so on.

Installation

pip install codegrab

Alternatively, you can run it without installing using uv:

uvx codegrab

Usage

Extract a Local Python Module

To retrieve the full code of a module from your local file system:

codegrab module.submodule

Fetch from a GitHub Repository

If the --repo option is provided, CodeGrab fetches the file from a GitHub repository instead of the local system:

codegrab module.submodule --repo https://github.com/user/repo

By default, CodeGrab fetches from the repository's default branch. You can specify a different branch using the --branch option:

codegrab module.submodule --repo https://github.com/user/repo --branch dev

Example:

Download and concat the LLM module from Simon Willison's command-line llm tool.

codegrab llm --repo https://github.com/simonw/llm

Extract a Specific Function

To retrieve only the code for a specific function within a module:

codegrab module.submodule:function_name

Example:

codegrab llm.cli:prompt --repo https://github.com/simonw/llm

This will retreive the prompt method from the llm repository.

[!NOTE]
codegrab doesn't support scoping the function being retreived to a class, it currently fethces the first function that matches the name in the file.

Environment Variables

To avoid GitHub rate limiting, you can set a personal access token:

export GITHUB_TOKEN=your_personal_token

Error Handling

  • If the file or function is not found, an error message is displayed.
  • If the GitHub request fails, an HTTP error code is shown.

License

MIT License

Contributions

Feel free to open issues and submit pull requests to improve codegrab!

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

codegrab-0.1.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

codegrab-0.1.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: codegrab-0.1.2.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.1

File hashes

Hashes for codegrab-0.1.2.tar.gz
Algorithm Hash digest
SHA256 258bda8770e8d107d5f86e007085fc46c07129f71fbeb199971df9a2918d9a30
MD5 ddfce464b0fa26c6267c099211b2deb5
BLAKE2b-256 86687e5ad7bcdbd6691a67e0a614f01454ae6cf51978205fa190847b6489f195

See more details on using hashes here.

File details

Details for the file codegrab-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: codegrab-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.1

File hashes

Hashes for codegrab-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5a1c6a344ef783f7bfe6cf26b0326e21b9a236b05f8281a1773e51ea10260da6
MD5 3f8306d167eecaa6d6721fe51a90f957
BLAKE2b-256 7cfb51c3fb390f8b9f2158bc17cad30d4aa7f4fe2cf4feffdf4b28c4e3408d37

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