From b8b423c5cfe69cbed4913b985841f911c8a61019 Mon Sep 17 00:00:00 2001 From: Frederik Hennig <frederik.hennig@fau.de> Date: Fri, 15 Dec 2023 17:04:09 +0100 Subject: [PATCH] minor fixes to documentation and clang-format integration --- docs/usage/generator_scripts.md | 21 ++++++++++--------- src/pystencilssfg/configuration.py | 9 -------- src/pystencilssfg/emission/clang_format.py | 8 ++++++- .../emission/header_source_pair.py | 1 - 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/docs/usage/generator_scripts.md b/docs/usage/generator_scripts.md index 429eb4f..da34bad 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 dc0aa21..a7bced4 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 e663fec..f0970ae 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 20e5903..8eaef4e 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 -- GitLab