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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
CaosDB Crawler
Commits
73d4d222
Commit
73d4d222
authored
3 years ago
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
ENH: enhanced macro format
parent
74f4e7ef
No related branches found
No related tags found
2 merge requests
!53
Release 0.1
,
!25
F macros
Pipeline
#23203
passed with warnings
3 years ago
Stage: info
Stage: setup
Stage: cert
Stage: style
Stage: test
Changes
3
Pipelines
1
Hide 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
+31
-20
31 additions, 20 deletions
src/caoscrawler/macros/macro_yaml_object.py
unittests/test_macros.py
+11
-16
11 additions, 16 deletions
unittests/test_macros.py
with
43 additions
and
37 deletions
src/caoscrawler/macros/__init__.py
+
1
−
1
View file @
73d4d222
from
.macro_yaml_object
import
defmacro_constructor
,
macro_constructor
,
multimacro_constructor
from
.macro_yaml_object
import
defmacro_constructor
,
macro_constructor
This diff is collapsed.
Click to expand it.
src/caoscrawler/macros/macro_yaml_object.py
+
31
−
20
View file @
73d4d222
...
@@ -117,26 +117,37 @@ def macro_constructor(loader, node):
...
@@ -117,26 +117,37 @@ def macro_constructor(loader, node):
It can be registered in pyaml using:
It can be registered in pyaml using:
yaml.SafeLoader.add_constructor(
"
!macro
"
, macro_constructor)
yaml.SafeLoader.add_constructor(
"
!macro
"
, macro_constructor)
"""
"""
res
=
dict
()
value
=
loader
.
construct_mapping
(
node
,
deep
=
True
)
value
=
loader
.
construct_mapping
(
node
,
deep
=
True
)
name
=
value
[
"
name
"
]
for
name
,
params_setter
in
value
.
items
():
macro
=
macro_store
[
name
]
if
name
in
macro_store
:
params
=
deepcopy
(
macro
.
params
)
# If params_setter is a list, run this for every element:
if
"
params
"
in
value
:
if
params_setter
is
not
None
and
isinstance
(
params_setter
,
list
):
params
.
update
(
value
[
"
params
"
])
for
el
in
params_setter
:
definition
=
deepcopy
(
macro
.
definition
)
macro
=
macro_store
[
name
]
params
=
deepcopy
(
macro
.
params
)
if
el
is
not
None
:
if
isinstance
(
el
,
dict
):
params
.
update
(
el
)
else
:
raise
RuntimeError
(
"
params type not supported
"
)
else
:
raise
RuntimeError
(
"
params type must not be None
"
)
definition
=
substitute_dict
(
macro
.
definition
,
params
)
res
.
update
(
definition
)
else
:
# This is just a single macro:
macro
=
macro_store
[
name
]
params
=
deepcopy
(
macro
.
params
)
if
params_setter
is
not
None
:
if
isinstance
(
params_setter
,
dict
):
params
.
update
(
params_setter
)
else
:
raise
RuntimeError
(
"
params type not supported
"
)
definition
=
substitute_dict
(
macro
.
definition
,
params
)
res
.
update
(
definition
)
else
:
# If there is no macro with that name, just keep that node:
res
[
name
]
=
params_setter
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)
"""
res
=
dict
()
for
val
in
node
.
value
:
res
.
update
(
macro_constructor
(
loader
,
val
))
return
res
return
res
This diff is collapsed.
Click to expand it.
unittests/test_macros.py
+
11
−
16
View file @
73d4d222
...
@@ -22,9 +22,7 @@
...
@@ -22,9 +22,7 @@
# ** end header
# ** end header
#
#
from
caoscrawler.macros
import
(
defmacro_constructor
,
from
caoscrawler.macros
import
defmacro_constructor
,
macro_constructor
macro_constructor
,
multimacro_constructor
)
from
caoscrawler.macros.macro_yaml_object
import
macro_store
from
caoscrawler.macros.macro_yaml_object
import
macro_store
import
yaml
import
yaml
import
pytest
import
pytest
...
@@ -33,7 +31,6 @@ import pytest
...
@@ -33,7 +31,6 @@ import pytest
def
register_macros
():
def
register_macros
():
yaml
.
SafeLoader
.
add_constructor
(
"
!defmacro
"
,
defmacro_constructor
)
yaml
.
SafeLoader
.
add_constructor
(
"
!defmacro
"
,
defmacro_constructor
)
yaml
.
SafeLoader
.
add_constructor
(
"
!macro
"
,
macro_constructor
)
yaml
.
SafeLoader
.
add_constructor
(
"
!macro
"
,
macro_constructor
)
yaml
.
SafeLoader
.
add_constructor
(
"
!multimacro
"
,
multimacro_constructor
)
@pytest.fixture
@pytest.fixture
def
macro_store_reset
():
def
macro_store_reset
():
...
@@ -55,8 +52,7 @@ defs:
...
@@ -55,8 +52,7 @@ defs:
testnode:
testnode:
obl: !macro
obl: !macro
name: test
test:
params:
a: 4
a: 4
b: yea
b: yea
"""
,
Loader
=
yaml
.
SafeLoader
)
"""
,
Loader
=
yaml
.
SafeLoader
)
...
@@ -82,8 +78,7 @@ defs:
...
@@ -82,8 +78,7 @@ defs:
testnode:
testnode:
obl: !macro
obl: !macro
name: test
test:
params:
a: 4
a: 4
b: yea
b: yea
"""
,
Loader
=
yaml
.
SafeLoader
)
"""
,
Loader
=
yaml
.
SafeLoader
)
...
@@ -108,9 +103,9 @@ defs:
...
@@ -108,9 +103,9 @@ defs:
replaced3: ok
replaced3: ok
testnode:
testnode:
obl: !
multi
macro
obl: !macro
- name:
test_one
test_one
:
- name:
test_two
test_two
:
"""
,
Loader
=
yaml
.
SafeLoader
)
"""
,
Loader
=
yaml
.
SafeLoader
)
print
(
yaml
.
dump
(
dat
))
print
(
yaml
.
dump
(
dat
))
assert
dat
[
"
testnode
"
][
"
obl
"
][
"
replaced1
"
]
==
"
ok
"
assert
dat
[
"
testnode
"
][
"
obl
"
][
"
replaced1
"
]
==
"
ok
"
...
@@ -136,9 +131,9 @@ defs:
...
@@ -136,9 +131,9 @@ defs:
replaced2: ok
replaced2: ok
replaced3: ok
replaced3: ok
testnode: !
multi
macro
testnode: !macro
- name:
test_one
test_one
:
- name:
test_two
test_two
:
"""
,
Loader
=
yaml
.
SafeLoader
)
"""
,
Loader
=
yaml
.
SafeLoader
)
assert
dat
[
"
testnode
"
][
"
replaced1
"
]
==
"
ok
"
assert
dat
[
"
testnode
"
][
"
replaced1
"
]
==
"
ok
"
assert
dat
[
"
testnode
"
][
"
replaced2
"
]
==
"
ok
"
assert
dat
[
"
testnode
"
][
"
replaced2
"
]
==
"
ok
"
...
@@ -169,8 +164,8 @@ defs:
...
@@ -169,8 +164,8 @@ defs:
d: $testvar_list
d: $testvar_list
testnode:
testnode:
obl: !macro
obl: !macro
name:
test
test
:
"""
,
Loader
=
yaml
.
SafeLoader
)
"""
,
Loader
=
yaml
.
SafeLoader
)
print
(
yaml
.
dump
(
dat
))
print
(
yaml
.
dump
(
dat
))
assert
dat
[
"
testnode
"
][
"
obl
"
][
"
replaced1
"
][
"
c
"
][
"
t1
"
]
==
"
a
"
assert
dat
[
"
testnode
"
][
"
obl
"
][
"
replaced1
"
][
"
c
"
][
"
t1
"
]
==
"
a
"
...
...
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