- 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
andParser
objects are returned from installed grammar modules.
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
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.
-
Python : Ensure you have Python 3.6 or higher installed.
-
Python venv - Optional, but highly recommended.
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 modulespydantic
- for grammar objects validationpydantic-settings
- for config validation and dynamic path fetching
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. 😊
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.
Contributions are always welcome!
See contributing.md
for ways to get started.
Distributed under the MIT License. See LICENSE.txt for more information.
Vlad Andreev - [email protected]
Use this section to mention useful resources and libraries that you have used in your projects.