Skip to content
Snippets Groups Projects
Commit b8b423c5 authored by Frederik Hennig's avatar Frederik Hennig
Browse files

minor fixes to documentation and clang-format integration

parent af0e3408
Branches
Tags
No related merge requests found
Pipeline #58403 passed
......@@ -18,19 +18,18 @@ The code generation process in a generator script is controlled by the
It configures the code generator by combining configuration options from the
environment (e.g. a CMake build system) with options specified in the script,
and infers the names of the output files from the script's name.
It then prepares a code generation [context][pystencilssfg.SfgContext] and a
[composer][pystencilssfg.SfgComposer].
The latter is returned when entering the context manager, and provides a convenient
interface to constructing the source files.
It then prepares and returns a code generation [context][pystencilssfg.SfgContext].
This context may then be passed to a [composer][pystencilssfg.SfgComposer],
which provides a convenient interface for constructing the source files.
To start, place the following code in a Python script, e.g. `kernels.py`:
```Python
from pystencilssfg import SourceFileGenerator, SfgConfiguration
from pystencilssfg import SourceFileGenerator, SfgConfiguration, SfgComposer
sfg_config = SfgConfiguration()
with SourceFileGenerator(sfg_config) as sfg:
pass
with SourceFileGenerator(sfg_config) as ctx:
sfg = SfgComposer(ctx)
```
......@@ -56,9 +55,11 @@ A few notes on configuration:
## Using the Composer
The object `sfg` returned by the context manager is an [SfgComposer](pystencilssfg.SfgComposer).
It is a stateless builder object which provides a convenient interface to construct the source
file's contents. Here's an overview:
The object `sfg` constructed in above snippet is an instance of [SfgComposer][pystencilssfg.SfgComposer].
The composer is the central part of the user front-end of *pystencils-sfg*.
It provides an interface for constructing source files that attempts to closely mimic
C++ syntactic structures within Python.
Here is an overview of its various functions:
### Includes and Definitions
......
......@@ -85,15 +85,6 @@ class SfgCodeStyle:
prefix = " " * self.indent_width
return indent(s, prefix)
def __post__init__(self):
if self.force_clang_format:
import shutil
if not shutil.which(self.clang_format_binary):
raise SfgException(
"`force_clang_format` set to true in code style, but clang-format binary not found."
)
@dataclass
class SfgOutputSpec:
......
......@@ -9,7 +9,13 @@ def invoke_clang_format(code: str, codestyle: SfgCodeStyle) -> str:
args = [codestyle.clang_format_binary, f"--style={codestyle.code_style}"]
if not shutil.which(codestyle.clang_format_binary):
return code
if codestyle.force_clang_format:
raise SfgException(
"`force_clang_format` was set to true in code style, "
"but clang-format binary could not be found."
)
else:
return code
result = subprocess.run(args, input=code, capture_output=True, text=True)
......
......@@ -4,7 +4,6 @@ from ..configuration import SfgOutputSpec
from ..context import SfgContext
from .prepare import prepare_context
from .printers import SfgHeaderPrinter, SfgImplPrinter
from .clang_format import invoke_clang_format
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment