Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-advanced-user-tools
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-advanced-user-tools
Commits
4ff4cca5
Commit
4ff4cca5
authored
3 years ago
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
ENH: completed implementation of role keyword and added several tests
parent
d452e955
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!39
Release 0.4.0
,
!30
F extend yaml model
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caosadvancedtools/models/parser.py
+21
-2
21 additions, 2 deletions
src/caosadvancedtools/models/parser.py
unittests/test_parser.py
+66
-1
66 additions, 1 deletion
unittests/test_parser.py
with
87 additions
and
3 deletions
src/caosadvancedtools/models/parser.py
+
21
−
2
View file @
4ff4cca5
...
@@ -36,7 +36,8 @@ KEYWORDS = ["parent", # TODO: can we remove that, see: #36
...
@@ -36,7 +36,8 @@ KEYWORDS = ["parent", # TODO: can we remove that, see: #36
"
inherit_from_recommended
"
,
"
inherit_from_recommended
"
,
"
inherit_from_suggested
"
,
"
inherit_from_suggested
"
,
"
inherit_from_obligatory
"
,
"
inherit_from_obligatory
"
,
"
role
"
,
]
"
role
"
,
"
value
"
,
]
# TODO: check whether it's really ignored
# TODO: check whether it's really ignored
# These KEYWORDS are not forbidden as properties, but merely ignored.
# These KEYWORDS are not forbidden as properties, but merely ignored.
...
@@ -278,8 +279,11 @@ class Parser(object):
...
@@ -278,8 +279,11 @@ class Parser(object):
raise
RuntimeError
(
"
Unknown role {} in definition of entity.
"
.
format
(
raise
RuntimeError
(
"
Unknown role {} in definition of entity.
"
.
format
(
definition
[
"
role
"
]))
definition
[
"
role
"
]))
# add other definitions recursively
# for setting values of properties directly:
if
not
isinstance
(
definition
,
dict
):
return
# add other definitions recursively
for
prop_type
in
[
"
recommended_properties
"
,
for
prop_type
in
[
"
recommended_properties
"
,
"
suggested_properties
"
,
"
obligatory_properties
"
]:
"
suggested_properties
"
,
"
obligatory_properties
"
]:
...
@@ -322,8 +326,12 @@ class Parser(object):
...
@@ -322,8 +326,12 @@ class Parser(object):
name
=
n
,
name
=
n
,
importance
=
importance
,
importance
=
importance
,
datatype
=
db
.
LIST
(
_get_listdatatype
(
e
[
"
datatype
"
])))
datatype
=
db
.
LIST
(
_get_listdatatype
(
e
[
"
datatype
"
])))
elif
e
is
None
:
self
.
model
[
ent_name
].
add_property
(
name
=
n
,
importance
=
importance
)
else
:
else
:
self
.
model
[
ent_name
].
add_property
(
name
=
n
,
self
.
model
[
ent_name
].
add_property
(
name
=
n
,
value
=
e
,
importance
=
importance
)
importance
=
importance
)
def
_inherit
(
self
,
name
,
prop
,
inheritance
):
def
_inherit
(
self
,
name
,
prop
,
inheritance
):
...
@@ -347,6 +355,10 @@ class Parser(object):
...
@@ -347,6 +355,10 @@ class Parser(object):
if
definition
is
None
:
if
definition
is
None
:
return
return
# for setting values of properties directly:
if
not
isinstance
(
definition
,
dict
):
return
if
(
"
datatype
"
in
definition
if
(
"
datatype
"
in
definition
and
definition
[
"
datatype
"
].
startswith
(
"
LIST
"
)):
and
definition
[
"
datatype
"
].
startswith
(
"
LIST
"
)):
...
@@ -363,6 +375,9 @@ class Parser(object):
...
@@ -363,6 +375,9 @@ class Parser(object):
if
prop_name
==
"
unit
"
:
if
prop_name
==
"
unit
"
:
self
.
model
[
name
].
unit
=
prop
self
.
model
[
name
].
unit
=
prop
elif
prop_name
==
"
value
"
:
self
.
model
[
name
].
value
=
prop
elif
prop_name
==
"
description
"
:
elif
prop_name
==
"
description
"
:
self
.
model
[
name
].
description
=
prop
self
.
model
[
name
].
description
=
prop
...
@@ -391,6 +406,10 @@ class Parser(object):
...
@@ -391,6 +406,10 @@ class Parser(object):
elif
prop_name
==
"
datatype
"
:
elif
prop_name
==
"
datatype
"
:
continue
continue
# role has already been used
elif
prop_name
==
"
role
"
:
continue
elif
prop_name
==
"
inherit_from_obligatory
"
:
elif
prop_name
==
"
inherit_from_obligatory
"
:
self
.
_inherit
(
name
,
prop
,
db
.
OBLIGATORY
)
self
.
_inherit
(
name
,
prop
,
db
.
OBLIGATORY
)
elif
prop_name
==
"
inherit_from_recommended
"
:
elif
prop_name
==
"
inherit_from_recommended
"
:
...
...
This diff is collapsed.
Click to expand it.
unittests/test_parser.py
+
66
−
1
View file @
4ff4cca5
...
@@ -15,7 +15,7 @@ def to_file(string):
...
@@ -15,7 +15,7 @@ def to_file(string):
return
f
.
name
return
f
.
name
# TODO: check purpose of this function... add documentation
def
parse_str
(
string
):
def
parse_str
(
string
):
parse_model_from_yaml
(
to_file
(
string
))
parse_model_from_yaml
(
to_file
(
string
))
...
@@ -328,3 +328,68 @@ A:
...
@@ -328,3 +328,68 @@ A:
with
self
.
assertRaises
(
YamlDefinitionError
)
as
yde
:
with
self
.
assertRaises
(
YamlDefinitionError
)
as
yde
:
parse_str
(
string
)
parse_str
(
string
)
assert
(
"
line {}
"
.
format
(
line
)
in
yde
.
exception
.
args
[
0
])
assert
(
"
line {}
"
.
format
(
line
)
in
yde
.
exception
.
args
[
0
])
def
test_define_role
():
model
=
"""
A:
role: Record
"""
entities
=
parse_model_from_string
(
model
)
assert
"
A
"
in
entities
assert
isinstance
(
entities
[
"
A
"
],
db
.
Record
)
assert
entities
[
"
A
"
].
role
==
"
Record
"
model
=
"""
A:
role: Record
inherit_from_obligatory:
- C
obligatory_properties:
b:
b:
datatype: INTEGER
C:
obligatory_properties:
b:
D:
role: RecordType
"""
entities
=
parse_model_from_string
(
model
)
for
l
,
ent
in
((
"
A
"
,
"
Record
"
),
(
"
b
"
,
"
Property
"
),
(
"
C
"
,
"
RecordType
"
),
(
"
D
"
,
"
RecordType
"
)):
assert
l
in
entities
assert
isinstance
(
entities
[
l
],
getattr
(
db
,
ent
))
assert
entities
[
l
].
role
==
ent
assert
entities
[
"
A
"
].
parents
[
0
].
name
==
"
C
"
assert
entities
[
"
A
"
].
name
==
"
A
"
assert
entities
[
"
A
"
].
properties
[
0
].
name
==
"
b
"
assert
entities
[
"
A
"
].
properties
[
0
].
value
is
None
assert
entities
[
"
C
"
].
properties
[
0
].
name
==
"
b
"
assert
entities
[
"
C
"
].
properties
[
0
].
value
is
None
model
=
"""
A:
role: Record
obligatory_properties:
b: 42
b:
datatype: INTEGER
"""
entities
=
parse_model_from_string
(
model
)
assert
entities
[
"
A
"
].
get_property
(
"
b
"
).
value
==
42
assert
entities
[
"
b
"
].
value
is
None
model
=
"""
b:
datatype: INTEGER
value: 18
"""
entities
=
parse_model_from_string
(
model
)
assert
entities
[
"
b
"
].
value
==
18
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