diff --git a/docs/usage/generator_scripts.md b/docs/usage/generator_scripts.md index 429eb4f7b9571253b423c5f41ddca4e729e48103..da34bada9b8672a48271064b20ee2acda18a3cac 100644 --- a/docs/usage/generator_scripts.md +++ b/docs/usage/generator_scripts.md @@ -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 diff --git a/src/pystencilssfg/configuration.py b/src/pystencilssfg/configuration.py index dc0aa214b438d82c11530b437ecff34a6933b26f..a7bced43629ed8bdfa4e551d562ef9fe264a3d85 100644 --- a/src/pystencilssfg/configuration.py +++ b/src/pystencilssfg/configuration.py @@ -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: diff --git a/src/pystencilssfg/emission/clang_format.py b/src/pystencilssfg/emission/clang_format.py index e663fec9ea1da6ff9709c58d56fb558b14711c85..f0970ae057bedf42f115a7ddf8aff3f068dae3d2 100644 --- a/src/pystencilssfg/emission/clang_format.py +++ b/src/pystencilssfg/emission/clang_format.py @@ -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) diff --git a/src/pystencilssfg/emission/header_source_pair.py b/src/pystencilssfg/emission/header_source_pair.py index 20e590393a97eb567d025a14cfa884d1b83a7d9a..8eaef4e5b8cb92702e1339ddb3107cf82ad7719c 100644 --- a/src/pystencilssfg/emission/header_source_pair.py +++ b/src/pystencilssfg/emission/header_source_pair.py @@ -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