Skip to main content

Stubble

A native, inline code-generation tool using the comment templating language

The Problem

One of the most difficult aspects of code generation is integrating generated code into existing codebases. Templating systems typically require full ownership of files or whole directories.

The solution

Stubble aims to solve this by allowing you to provide comment-based "stubs" in your existing codebase, to allow for injection of additional code. Your templates can be written in your native language and will be valid code on their own in their native language.

Example

Let's say you have a python file. You can easily add a stub that stubble will find and replace:

... before your code
def main():
	do_something()
	do_something_else()
	# {{replace_this}}

Or maybe you have a C++ file, and you want to fill in the argument inside a function call. stubbles can handle that, too.

void myFunction(int arg1) {
	// Serial.println({{SOMETHING_GOES_HERE}});
	Serial.println("hello");
}

Getting Started

The easiest way to install stubble is via pip:

pip install stubble

From the command-line

And then it can be invoked as a command-line tool:

python3 -m stubble --template my_file.cpp --replacements replacements.json

By default, stubble produces code in a generated folder in the same location as it is in invoked, but this can be overriden using the --output-dir option.

Using as a python package

If you want to use stubbles as a python package, you can do that too. If you want to provide input files, you can invoke the main() method directly:

from stubbles.__main__ import main

main(
	template=your_template, 
	replacements_file=your_replacements_file, 
	output_dir=your_output_dir
)

Or, if you already have replacements as a dictionary, the template as a string, and you know your target language:

from stubbles import populate, Language

your_template = "# {{replacement1}}"
your_replacement_dict = {"replacement1": "value1"}

populated_template = populate(
	template=your_template,
	replacements=your_replacement_dict,
	lang=Language.PYTHON
)

stubbles also provides the helper functions read_dict_from_file, which reads a dict from an arbitrary key-value pair file, and language, which takes a filename and figures out, from its extension, what language that file is using.

Fetaures

  • All stubble template files are valid code in their native language
  • Simple, lightweight, logicless template replacement
  • Allows for inline replacements and whole-line replacements
  • Support for individual input files and input directories
  • Respects indentation level of comment, auto-indents inserted code.

Supported replacements file types

Currently, the following replacements file types are supported:

  • JSON
  • YAML
  • TOML
  • XML
  • .ini
  • CSV

If you want an additional replacements file type supported, please submit an issue, it's very easy to add.

Supported Languages

  • C++
  • C
  • Python
  • Ruby
  • TypeScript
  • Javascript
  • CSS / SASS / SCSS
  • Java
  • Golang
  • Rust

In principle, any language that supports comments can be supported by stubbles. If you want to add a language, just submit an issue. It's extremely easy to add.

TODO:

  • Support input directories rather than just individual files
  • Complete command-line interface
  • Add tests for command-line interface

Release files for stubbles 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stubbles 1.0.0
File Size Uploaded
stubbles-1.0.0.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stubbles 1.0.0
File Interpreter ABI Platform
stubbles-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.2 kB

Release files / stubbles-1.0.0.tar.gz

Download URL stubbles-1.0.0.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8734f7450c6c414464306b4345154dcd641eb43b0dafc20e1453c5f5dea06117
BLAKE2b-256 checksum
How to use checksums
4ae8ad0c92b2b1f7ac936d796c86b97d6a4e0da8676bfb8c476d1760d29ec870
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.12 Linux/6.2.0-26-generic

Release files / stubbles-1.0.0-py3-none-any.whl

Download URL stubbles-1.0.0-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4e88ce60304c33b113288cfbf6272abda400d5a18d9820c414688c45bc732e17
BLAKE2b-256 checksum
How to use checksums
26029fb71fa5b6cf154aff08ed8ff3d364308983ea17a864ba28798d69e98212
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.12 Linux/6.2.0-26-generic

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page