Skip to content

ukw2d/py-grove-watcher

Repository files navigation

logo

GroveWatcher

Tree-Sitter grammar fetcher for Python.

forks last update forks open issues license


Table of Contents

About the Project

Tech Stack

Features

  • Automated Grammar Installation: Easily install Tree-Sitter language grammars via pip by simply specifying the language name.
  • Virtual Environment Support: Optionally use a dedicated virtual environment to cache grammar installations, ensuring isolation and reproducibility.
  • Pydantic Validation: Ensure that only valid Tree-Sitter Language and Parser objects are returned from installed grammar modules.

Configuration

Environment Variables

You can use your .env to override path values required for the optional dedicated venv that is used for grammar (pip packages) caching.

  • GW_CACHE_DIR_WIN32 - Override Path to the optional dedicated venv directory for Windows platform

  • GW_CACHE_DIR_UNIX - Override Path to the optional dedicated venv directory for Unix platforms

  • GW_VENV_EXECUTABLE_WIN32 - Override Path to the optional dedicated venv Python executable for Windows platform

  • GW_VENV_EXECUTABLE_UNIX - Override Path to the optional dedicated venv Python executable for Unix platforms

  • GW_VENV_EXECUTABLE_DEFAULT_EXECUTABLE - Override Python executable name used for the optional dedicated venv

Config files

src/grove_watcher/config/config.json - json used for tree-sitter grammar pip packages prefixes and default venv path values.

src/grove_watcher/config/logging.json - json used for logging configuration.

Getting Started

Prerequisites

  • Python : Ensure you have Python 3.6 or higher installed.

  • Python venv - Optional, but highly recommended.

Installation

Package has been published to PyPI, so you can install it using pip:

  pip install grove-watcher

This package will install 3 dependencies:

  • tree-sitter - for creating grammar objects based on imported grammar modules
  • pydantic - for grammar objects validation
  • pydantic-settings - for config validation and dynamic path fetching

Usage

After installation, you can use it simply like this:

import grove_watcher

parser = grove_watcher.find("python")

If you prefer not to install grammar packages in your current environment, pass the use_venv parameter as True:

import grove_watcher

parser = grove_watcher.find("python", True)

This will create a dedicated virtual environment in the cache directory (the path can be configured in the .env file). This virtual environment is used for installing grammar modules from pip and retrieving the Tree-Sitter grammar object for your current environment. Subsequent calls with use_venv=True will reuse the existing virtual environment if it's available.

That's it! Hope this module saves you time and reduces your code complexity. 😊

Return Value

The find function in GroveWatcher returns a TSGrammarModel Pydantic object, which encapsulates the Tree-Sitter grammar and parser. This object is defined as follows:

from pydantic import BaseModel, ConfigDict
from tree_sitter import Language, Parser

class TSGrammarModel(BaseModel):
    lang: Language
    parser: Parser
    model_config = ConfigDict(
        arbitrary_types_allowed=True,
        extra='forbid',
        frozen=True
    )

lang : A Language object from the Tree-Sitter library, representing the installed grammar for the specified language.

parser : A Parser object configured with the corresponding Language, ready to parse source code in the specified language.

Contributing

Contributions are always welcome!

See contributing.md for ways to get started.

License

Distributed under the MIT License. See LICENSE.txt for more information.

Contact

Vlad Andreev - [email protected]

Acknowledgements

Use this section to mention useful resources and libraries that you have used in your projects.

About

Tree-sitter grammar fetcher for Python

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages