Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pystencils-sfg
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pycodegen
pystencils-sfg
Commits
1d98cef0
Commit
1d98cef0
authored
3 months ago
by
Frederik Hennig
Browse files
Options
Downloads
Patches
Plain Diff
use config system and descriptors from pystencils.codegen.config
parent
86ea72ea
No related branches found
No related tags found
1 merge request
!15
Use config descriptor system from pystencils
Pipeline
#73015
failed
3 months ago
Stage: Code Quality
Stage: Tests
Stage: Documentation
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/pystencilssfg/config.py
+18
-92
18 additions, 92 deletions
src/pystencilssfg/config.py
with
18 additions
and
92 deletions
src/pystencilssfg/config.py
+
18
−
92
View file @
1d98cef0
...
...
@@ -3,101 +3,27 @@ from __future__ import annotations
from
argparse
import
ArgumentParser
from
types
import
ModuleType
from
typing
import
Generic
,
TypeVar
,
Callable
,
Any
,
Sequence
from
abc
import
ABC
from
dataclasses
import
dataclass
,
fields
,
field
from
typing
import
Any
,
Sequence
from
dataclasses
import
dataclass
from
enum
import
Enum
,
auto
from
os
import
path
from
importlib
import
util
as
iutil
class
SfgConfigException
(
Exception
):
...
# noqa: E701
Option_T
=
TypeVar
(
"
Option_T
"
)
class
Option
(
Generic
[
Option_T
]):
"""
Option descriptor.
This descriptor is used to model configuration options.
It maintains a default value for the option that is used when no value
was specified by the user.
from
pystencils.codegen.config
import
ConfigBase
,
BasicOption
,
Category
In configuration options, the value `None` stands for `unset`.
It can therefore not be used to set an option to the meaning
"
not any
"
, or
"
empty
"
- for these, special values need to be used.
"""
def
__init__
(
self
,
default
:
Option_T
|
None
=
None
,
validator
:
Callable
[[
Any
,
Option_T
|
None
],
Option_T
|
None
]
|
None
=
None
,
)
->
None
:
self
.
_default
=
default
self
.
_validator
=
validator
self
.
_name
:
str
self
.
_lookup
:
str
def
validate
(
self
,
validator
:
Callable
[[
Any
,
Any
],
Any
]
|
None
):
self
.
_validator
=
validator
return
validator
@property
def
default
(
self
)
->
Option_T
|
None
:
return
self
.
_default
def
get
(
self
,
obj
)
->
Option_T
|
None
:
val
=
getattr
(
obj
,
self
.
_lookup
,
None
)
if
val
is
None
:
return
self
.
_default
else
:
return
val
def
__set_name__
(
self
,
owner
,
name
:
str
):
self
.
_name
=
name
self
.
_lookup
=
f
"
_
{
name
}
"
def
__get__
(
self
,
obj
,
objtype
=
None
)
->
Option_T
|
None
:
if
obj
is
None
:
return
None
return
getattr
(
obj
,
self
.
_lookup
,
None
)
def
__set__
(
self
,
obj
,
value
:
Option_T
|
None
):
if
self
.
_validator
is
not
None
:
value
=
self
.
_validator
(
obj
,
value
)
setattr
(
obj
,
self
.
_lookup
,
value
)
def
__delete__
(
self
,
obj
):
delattr
(
obj
,
self
.
_lookup
)
class
ConfigBase
(
ABC
):
def
get_option
(
self
,
name
:
str
)
->
Any
:
"""
Get the value set for the specified option, or the option
'
s default value if none has been set.
"""
descr
:
Option
=
type
(
self
).
__dict__
[
name
]
return
descr
.
get
(
self
)
def
override
(
self
,
other
:
ConfigBase
):
for
f
in
fields
(
self
):
# type: ignore
fvalue
=
getattr
(
self
,
f
.
name
)
if
isinstance
(
fvalue
,
ConfigBase
):
# type: ignore
fvalue
.
override
(
getattr
(
other
,
f
.
name
))
else
:
new_val
=
getattr
(
other
,
f
.
name
)
if
new_val
is
not
None
:
setattr
(
self
,
f
.
name
,
new_val
)
class
SfgConfigException
(
Exception
):
...
# noqa: E701
@dataclass
class
FileExtensions
(
ConfigBase
):
"""
Option category containing output file extensions.
"""
"""
Basic
Option category containing output file extensions.
"""
header
:
Option
[
str
]
=
Option
(
"
hpp
"
)
header
:
Basic
Option
[
str
]
=
Basic
Option
(
"
hpp
"
)
"""
File extension for generated header file.
"""
impl
:
Option
[
str
]
=
Option
()
impl
:
Basic
Option
[
str
]
=
Basic
Option
()
"""
File extension for generated implementation file.
"""
@header.validate
...
...
@@ -132,7 +58,7 @@ class OutputMode(Enum):
class
CodeStyle
(
ConfigBase
):
"""
Options affecting the code style used by the source file generator.
"""
indent_width
:
Option
[
int
]
=
Option
(
2
)
indent_width
:
Basic
Option
[
int
]
=
Basic
Option
(
2
)
"""
The number of spaces successively nested blocks should be indented with
"""
# TODO possible future options:
...
...
@@ -150,20 +76,20 @@ class CodeStyle(ConfigBase):
class
ClangFormatOptions
(
ConfigBase
):
"""
Options affecting the invocation of ``clang-format`` for automatic code formatting.
"""
code_style
:
Option
[
str
]
=
Option
(
"
file
"
)
code_style
:
Basic
Option
[
str
]
=
Basic
Option
(
"
file
"
)
"""
Code style to be used by clang-format. Passed verbatim to `--style` argument of the clang-format CLI.
Similar to clang-format itself, the default value is `file`, such that a `.clang-format` file found in the build
tree will automatically be used.
"""
force
:
Option
[
bool
]
=
Option
(
False
)
force
:
Basic
Option
[
bool
]
=
Basic
Option
(
False
)
"""
If set to ``True``, abort code generation if ``clang-format`` binary cannot be found.
"""
skip
:
Option
[
bool
]
=
Option
(
False
)
skip
:
Basic
Option
[
bool
]
=
Basic
Option
(
False
)
"""
If set to ``True``, skip formatting using ``clang-format``.
"""
binary
:
Option
[
str
]
=
Option
(
"
clang-format
"
)
binary
:
Basic
Option
[
str
]
=
Basic
Option
(
"
clang-format
"
)
"""
Path to the clang-format executable
"""
@force.validate
...
...
@@ -194,7 +120,7 @@ GLOBAL_NAMESPACE = _GlobalNamespace()
class
SfgConfig
(
ConfigBase
):
"""
Configuration options for the `SourceFileGenerator`.
"""
extensions
:
FileExtensions
=
field
(
default_fact
ory
=
FileExtensions
)
extensions
:
Category
[
FileExtensions
]
=
Categ
ory
(
FileExtensions
()
)
"""
File extensions of the generated files
Options in this category:
...
...
@@ -203,7 +129,7 @@ class SfgConfig(ConfigBase):
FileExtensions.impl
"""
output_mode
:
Option
[
OutputMode
]
=
Option
(
OutputMode
.
STANDALONE
)
output_mode
:
Basic
Option
[
OutputMode
]
=
Basic
Option
(
OutputMode
.
STANDALONE
)
"""
The generator
'
s output mode; defines which files to generate, and the set of legal file extensions.
Possible parameters:
...
...
@@ -213,7 +139,7 @@ class SfgConfig(ConfigBase):
OutputMode.HEADER_ONLY
"""
outer_namespace
:
Option
[
str
|
_GlobalNamespace
]
=
Option
(
GLOBAL_NAMESPACE
)
outer_namespace
:
Basic
Option
[
str
|
_GlobalNamespace
]
=
Basic
Option
(
GLOBAL_NAMESPACE
)
"""
The outermost namespace in the generated file. May be a valid C++ nested namespace qualifier
(like ``a::b::c``) or `GLOBAL_NAMESPACE` if no outer namespace should be generated.
...
...
@@ -221,7 +147,7 @@ class SfgConfig(ConfigBase):
GLOBAL_NAMESPACE
"""
codestyle
:
CodeStyle
=
field
(
default_fact
ory
=
CodeStyle
)
codestyle
:
Category
[
CodeStyle
]
=
Categ
ory
(
CodeStyle
()
)
"""
Options affecting the code style emitted by pystencils-sfg.
Options in this category:
...
...
@@ -229,7 +155,7 @@ class SfgConfig(ConfigBase):
CodeStyle.indent_width
"""
clang_format
:
ClangFormatOptions
=
field
(
default_fact
ory
=
ClangFormatOptions
)
clang_format
:
Category
[
ClangFormatOptions
]
=
Categ
ory
(
ClangFormatOptions
()
)
"""
Options governing the code style used by the code generator
Options in this category:
...
...
@@ -240,7 +166,7 @@ class SfgConfig(ConfigBase):
ClangFormatOptions.binary
"""
output_directory
:
Option
[
str
]
=
Option
(
"
.
"
)
output_directory
:
Basic
Option
[
str
]
=
Basic
Option
(
"
.
"
)
"""
Directory to which the generated files should be written.
"""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment