Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
CaosDB Crawler
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
CaosDB Crawler
Commits
cfa296d4
Commit
cfa296d4
authored
3 years ago
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
ENH: more tests and replacements of sub dictionaries in lists
parent
9985cde5
No related branches found
No related tags found
2 merge requests
!53
Release 0.1
,
!25
F macros
Pipeline
#23200
passed with warnings
3 years ago
Stage: info
Stage: setup
Stage: cert
Stage: style
Stage: test
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/caoscrawler/macros/__init__.py
+1
-1
1 addition, 1 deletion
src/caoscrawler/macros/__init__.py
src/caoscrawler/macros/macro_yaml_object.py
+11
-0
11 additions, 0 deletions
src/caoscrawler/macros/macro_yaml_object.py
unittests/test_macros.py
+34
-2
34 additions, 2 deletions
unittests/test_macros.py
with
46 additions
and
3 deletions
src/caoscrawler/macros/__init__.py
+
1
−
1
View file @
cfa296d4
from
.macro_yaml_object
import
defmacro_constructor
,
macro_constructor
from
.macro_yaml_object
import
defmacro_constructor
,
macro_constructor
,
multimacro_constructor
This diff is collapsed.
Click to expand it.
src/caoscrawler/macros/macro_yaml_object.py
+
11
−
0
View file @
cfa296d4
...
...
@@ -79,6 +79,8 @@ def substitute_dict(sourced: dict[str, Any], values: dict[str, Any]):
for
i
in
d
[
k
]:
if
isinstance
(
i
,
str
):
subst_list
.
append
(
substitute
(
i
,
values
))
elif
isinstance
(
i
,
dict
):
subst_list
.
append
(
substitute_dict
(
i
,
values
))
else
:
subst_list
.
append
(
i
)
d
[
k
]
=
subst_list
...
...
@@ -119,3 +121,12 @@ def macro_constructor(loader, node):
definition
=
deepcopy
(
macro
.
definition
)
return
substitute_dict
(
definition
,
params
)
def
multimacro_constructor
(
loader
,
node
):
"""
Function that can be used to chain macros for complex definitions of dictionaries.
It can be registered in pyaml using:
yaml.SafeLoader.add_constructor(
"
!multimacro
"
, multimacro_constructor)
"""
pass
This diff is collapsed.
Click to expand it.
unittests/test_macros.py
+
34
−
2
View file @
cfa296d4
...
...
@@ -22,12 +22,17 @@
# ** end header
#
from
caoscrawler.macros
import
defmacro_constructor
,
macro_constructor
from
caoscrawler.macros
import
defmacro_constructor
,
macro_constructor
,
multimacro_constructor
import
yaml
import
pytest
def
test_macros
():
@pytest.fixture
def
register_macros
():
yaml
.
SafeLoader
.
add_constructor
(
"
!defmacro
"
,
defmacro_constructor
)
yaml
.
SafeLoader
.
add_constructor
(
"
!macro
"
,
macro_constructor
)
yaml
.
SafeLoader
.
add_constructor
(
"
!multimacro
"
,
multimacro_constructor
)
def
test_macros
(
register_macros
):
dat
=
yaml
.
load
(
"""
defs:
- !defmacro
...
...
@@ -52,3 +57,30 @@ testnode:
assert
dat
[
"
testnode
"
][
"
obl
"
][
"
expanded_yea
"
][
"
yea
"
]
==
"
$variable
"
assert
"
expanded_bla
"
not
in
dat
[
"
testnode
"
][
"
obl
"
]
assert
"
bla
"
not
in
dat
[
"
testnode
"
][
"
obl
"
][
"
expanded_yea
"
]
def
test_macro_list_replacment
(
register_macros
):
dat
=
yaml
.
load
(
"""
defs:
- !defmacro
name: test
params:
a: 2
b: bla
c: $variable
definition:
expanded_$b:
blubb:
- ok$a
- $b: $c
testnode:
obl: !macro
name: test
params:
a: 4
b: yea
"""
,
Loader
=
yaml
.
SafeLoader
)
assert
isinstance
(
dat
[
"
testnode
"
][
"
obl
"
][
"
expanded_yea
"
][
"
blubb
"
],
list
)
assert
len
(
dat
[
"
testnode
"
][
"
obl
"
][
"
expanded_yea
"
][
"
blubb
"
])
==
2
assert
dat
[
"
testnode
"
][
"
obl
"
][
"
expanded_yea
"
][
"
blubb
"
][
0
]
==
"
ok4
"
assert
dat
[
"
testnode
"
][
"
obl
"
][
"
expanded_yea
"
][
"
blubb
"
][
1
][
"
yea
"
]
==
"
$variable
"
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