Skip to main content

Inline seamless code generation

Project description

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

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

stubbles-1.0.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

stubbles-1.0.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file stubbles-1.0.0.tar.gz.

File metadata

  • Download URL: stubbles-1.0.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/6.2.0-26-generic

File hashes

Hashes for stubbles-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8734f7450c6c414464306b4345154dcd641eb43b0dafc20e1453c5f5dea06117
MD5 91cadd90f2d37446de1f388bcf97886d
BLAKE2b-256 4ae8ad0c92b2b1f7ac936d796c86b97d6a4e0da8676bfb8c476d1760d29ec870

See more details on using hashes here.

File details

Details for the file stubbles-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: stubbles-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/6.2.0-26-generic

File hashes

Hashes for stubbles-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e88ce60304c33b113288cfbf6272abda400d5a18d9820c414688c45bc732e17
MD5 71ba62877d02b1c8ff73cd5a5149fcf4
BLAKE2b-256 26029fb71fa5b6cf154aff08ed8ff3d364308983ea17a864ba28798d69e98212

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page