Skip to content

Commit

Permalink
Major code cleanup and file reorg; bump to v0.0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
alnvdl committed Aug 27, 2023
1 parent 5f88c76 commit d7df5d6
Show file tree
Hide file tree
Showing 10 changed files with 811 additions and 748 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ just one possible use case. It can take any dictionary-equivalent model
rudimentary support for cross-references. Generators then transform this model
in the desired output.

A `Model` is simply a Python dictionary (which may be manually iniatialized or
sourced from a YAML or JSON file) representing a hierarchy, typically with deep
nesting. The `select` operation is run on `Models`s to select a new sub-`Model`
based on a path selection mechanism.
A `Model` is simply a Python dictionary or enumerable (which may be manually
iniatialized or sourced from a YAML or JSON file) representing a hierarchy,
typically with deep nesting. The `select` operation is run on `Models`s to
select a new sub-`Model` based on a path selection mechanism.

Generators are functions annotated with the `@gen()` decorator, which gives
some extra powers to special f-strings expressions in them: automagic
Expand All @@ -40,6 +40,7 @@ file in this project):
```py
from fstringen import gen, Model

PREAMBLE = "// File generated by fstringen. DO NOT EDIT.\n\n"
model = Model.fromDict({
"structs": {
"mystruct": {
Expand All @@ -66,7 +67,7 @@ def gen_struct(struct):
*"""


@gen(model=model, fname="myfile.go", comment="//")
@gen(model=model, fname="myfile.go", preamble=PREAMBLE)
def gen_myfile(model):
return f"""*
package main
Expand All @@ -77,6 +78,7 @@ def gen_myfile(model):
```

All generator functions using fstringstars must be decorated with `@gen()`.

When no parameters are passed to `gen()`, the generator is considerate a
subordinate generator (i.e., they need to be called explicitly from other
generators).
Expand Down Expand Up @@ -145,8 +147,8 @@ supported (i.e., `f'''*...*'''` is not a valid fstringstar).
## Caveats
fstringen does dangerous things with scope and function re-declaration. This
means you should only use it in scripts that are code generators, and code
generators alone (i.e., they don't do anything else). We sacrificed correctness
for neatness and ease-to-use.
generators alone (i.e., they don't do anything else). Correctness and safety
were sacrificed for neatness and ease-to-use.

Since fstringen tramples over all common sense, pretty much all exceptions are
intercepted and transformed into custom error messages. Otherwise, because of
Expand Down
3 changes: 2 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fstringen import gen, Model

PREAMBLE = "// File generated by fstringen. DO NOT EDIT.\n\n"
model = Model.fromDict({
"structs": {
"mystruct": {
Expand All @@ -26,7 +27,7 @@ def gen_struct(struct):
*"""


@gen(model=model, fname="myfile.go", comment="//")
@gen(model=model, fname="myfile.go", preamble=PREAMBLE)
def gen_myfile(model):
return f"""*
package main
Expand Down
9 changes: 8 additions & 1 deletion fstringen/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
from fstringen.fstringen import gen, Model, ModelError, FStringenError # noqa
# flake8: noqa
# type: ignore

from .model import *
from .generator import *


__all__ = model.__all__ + generator.__all__
Loading

0 comments on commit d7df5d6

Please sign in to comment.