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
42590e2c
Commit
42590e2c
authored
3 years ago
by
Florian Spreckelsen
Browse files
Options
Downloads
Patches
Plain Diff
Revert "Merge branch 'f-extend-yaml-model' into 'dev'"
This reverts commit
751a0d04
, reversing changes made to
cb9b86f6
.
parent
751a0d04
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!39
Release 0.4.0
Pipeline
#20110
passed
3 years ago
Stage: setup
Stage: cert
Stage: style
Stage: unittest
Stage: integrationtest
Changes
3
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
CHANGELOG.md
+0
-3
0 additions, 3 deletions
CHANGELOG.md
src/caosadvancedtools/models/parser.py
+10
-48
10 additions, 48 deletions
src/caosadvancedtools/models/parser.py
unittests/test_parser.py
+4
-75
4 additions, 75 deletions
unittests/test_parser.py
with
14 additions
and
126 deletions
CHANGELOG.md
+
0
−
3
View file @
42590e2c
...
@@ -11,9 +11,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
...
@@ -11,9 +11,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
-
CFood that creates a Record for each line in a csv file
-
CFood that creates a Record for each line in a csv file
-
`generic_analysis.py`
allows to easily call scripts to perform analyses in
-
`generic_analysis.py`
allows to easily call scripts to perform analyses in
server side scripting [EXPERIMENTAL]
server side scripting [EXPERIMENTAL]
-
New keyword "role" in yaml data model that allows creation of Records and Files.
-
It is now possible to set values of properties and default values of properties
directly in the yaml model.
### Changed ###
### Changed ###
...
...
This diff is collapsed.
Click to expand it.
src/caosadvancedtools/models/parser.py
+
10
−
48
View file @
42590e2c
...
@@ -25,7 +25,7 @@ import yaml
...
@@ -25,7 +25,7 @@ import yaml
from
.data_model
import
DataModel
from
.data_model
import
DataModel
# Keywords which are allowed in data model descriptions.
# Keywords which are allowed in data model descriptions.
KEYWORDS
=
[
"
parent
"
,
# TODO: can we remove that, see: #36
KEYWORDS
=
[
"
parent
"
,
"
importance
"
,
"
importance
"
,
"
datatype
"
,
# for example TEXT, INTEGER or REFERENCE
"
datatype
"
,
# for example TEXT, INTEGER or REFERENCE
"
unit
"
,
"
unit
"
,
...
@@ -35,11 +35,8 @@ KEYWORDS = ["parent", # TODO: can we remove that, see: #36
...
@@ -35,11 +35,8 @@ KEYWORDS = ["parent", # TODO: can we remove that, see: #36
"
suggested_properties
"
,
"
suggested_properties
"
,
"
inherit_from_recommended
"
,
"
inherit_from_recommended
"
,
"
inherit_from_suggested
"
,
"
inherit_from_suggested
"
,
"
inherit_from_obligatory
"
,
"
inherit_from_obligatory
"
,
]
"
role
"
,
"
value
"
,
]
# 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.
KEYWORDS_IGNORED
=
[
KEYWORDS_IGNORED
=
[
"
unit
"
,
"
unit
"
,
...
@@ -112,10 +109,6 @@ def parse_model_from_string(string):
...
@@ -112,10 +109,6 @@ def parse_model_from_string(string):
class
Parser
(
object
):
class
Parser
(
object
):
def
__init__
(
self
):
def
__init__
(
self
):
"""
Initialize an empty parer object and initialize
the dictionary of entities and the list of treated elements.
"""
self
.
model
=
{}
self
.
model
=
{}
self
.
treated
=
[]
self
.
treated
=
[]
...
@@ -184,11 +177,13 @@ class Parser(object):
...
@@ -184,11 +177,13 @@ class Parser(object):
ymlmodel
[
"
extern
"
]
=
[]
ymlmodel
[
"
extern
"
]
=
[]
for
name
in
ymlmodel
[
"
extern
"
]:
for
name
in
ymlmodel
[
"
extern
"
]:
for
role
in
(
"
Property
"
,
"
RecordType
"
,
"
Record
"
,
"
File
"
):
if
db
.
execute_query
(
"
COUNT Property {}
"
.
format
(
name
))
>
0
:
if
db
.
execute_query
(
"
COUNT {} {}
"
.
format
(
role
,
name
))
>
0
:
self
.
model
[
name
]
=
db
.
execute_query
(
"
FIND Property WITH name={}
"
.
format
(
name
),
unique
=
True
)
elif
db
.
execute_query
(
"
COUNT RecordType {}
"
.
format
(
name
))
>
0
:
self
.
model
[
name
]
=
db
.
execute_query
(
self
.
model
[
name
]
=
db
.
execute_query
(
"
FIND {} WITH name={}
"
.
format
(
role
,
name
),
unique
=
True
)
"
FIND RecordType WITH name={}
"
.
format
(
name
),
unique
=
True
)
break
else
:
else
:
raise
Exception
(
"
Did not find {}
"
.
format
(
name
))
raise
Exception
(
"
Did not find {}
"
.
format
(
name
))
...
@@ -240,8 +235,6 @@ class Parser(object):
...
@@ -240,8 +235,6 @@ class Parser(object):
"""
adds names of Properties and RecordTypes to the model dictionary
"""
adds names of Properties and RecordTypes to the model dictionary
Properties are also initialized.
Properties are also initialized.
name is the key of the yaml element and definition the value.
"""
"""
if
name
==
"
__line__
"
:
if
name
==
"
__line__
"
:
...
@@ -265,25 +258,9 @@ class Parser(object):
...
@@ -265,25 +258,9 @@ class Parser(object):
# and create the new property
# and create the new property
self
.
model
[
name
]
=
db
.
Property
(
name
=
name
,
self
.
model
[
name
]
=
db
.
Property
(
name
=
name
,
datatype
=
definition
[
"
datatype
"
])
datatype
=
definition
[
"
datatype
"
])
elif
(
self
.
model
[
name
]
is
None
and
isinstance
(
definition
,
dict
)
and
"
role
"
in
definition
):
if
definition
[
"
role
"
]
==
"
RecordType
"
:
self
.
model
[
name
]
=
db
.
RecordType
(
name
=
name
)
elif
definition
[
"
role
"
]
==
"
Record
"
:
self
.
model
[
name
]
=
db
.
Record
(
name
=
name
)
elif
definition
[
"
role
"
]
==
"
File
"
:
self
.
model
[
name
]
=
db
.
File
(
name
=
name
)
elif
definition
[
"
role
"
]
==
"
Property
"
:
self
.
model
[
name
]
=
db
.
Property
(
name
=
name
)
else
:
raise
RuntimeError
(
"
Unknown role {} in definition of entity.
"
.
format
(
definition
[
"
role
"
]))
# for setting values of properties directly:
if
not
isinstance
(
definition
,
dict
):
return
# add other definitions recursively
# add other definitions recursively
for
prop_type
in
[
"
recommended_properties
"
,
for
prop_type
in
[
"
recommended_properties
"
,
"
suggested_properties
"
,
"
obligatory_properties
"
]:
"
suggested_properties
"
,
"
obligatory_properties
"
]:
...
@@ -326,12 +303,8 @@ class Parser(object):
...
@@ -326,12 +303,8 @@ 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
):
...
@@ -355,10 +328,6 @@ class Parser(object):
...
@@ -355,10 +328,6 @@ 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
"
)):
...
@@ -375,9 +344,6 @@ class Parser(object):
...
@@ -375,9 +344,6 @@ 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
...
@@ -406,10 +372,6 @@ class Parser(object):
...
@@ -406,10 +372,6 @@ 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
+
4
−
75
View file @
42590e2c
...
@@ -15,8 +15,6 @@ def to_file(string):
...
@@ -15,8 +15,6 @@ 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
))
...
@@ -70,8 +68,7 @@ RT2:
...
@@ -70,8 +68,7 @@ RT2:
a:
a:
"""
"""
self
.
assertRaises
(
TwiceDefinedException
,
self
.
assertRaises
(
TwiceDefinedException
,
lambda
:
parse_model_from_yaml
(
to_file
(
string
)))
lambda
:
parse_model_from_yaml
(
to_file
(
string
)))
def
test_typical_case
(
self
):
def
test_typical_case
(
self
):
string
=
"""
string
=
"""
...
@@ -106,8 +103,7 @@ RT5:
...
@@ -106,8 +103,7 @@ RT5:
- RT1:
- RT1:
- RT2:
- RT2:
"""
"""
self
.
assertRaises
(
self
.
assertRaises
(
ValueError
,
lambda
:
parse_model_from_yaml
(
to_file
(
string
)))
ValueError
,
lambda
:
parse_model_from_yaml
(
to_file
(
string
)))
def
test_unknown_kwarg
(
self
):
def
test_unknown_kwarg
(
self
):
string
=
"""
string
=
"""
...
@@ -115,8 +111,7 @@ RT1:
...
@@ -115,8 +111,7 @@ RT1:
datetime:
datetime:
p1:
p1:
"""
"""
self
.
assertRaises
(
self
.
assertRaises
(
ValueError
,
lambda
:
parse_model_from_yaml
(
to_file
(
string
)))
ValueError
,
lambda
:
parse_model_from_yaml
(
to_file
(
string
)))
def
test_definition_in_inheritance
(
self
):
def
test_definition_in_inheritance
(
self
):
string
=
"""
string
=
"""
...
@@ -126,8 +121,7 @@ RT2:
...
@@ -126,8 +121,7 @@ RT2:
- RT1:
- RT1:
description:
"
tach
"
description:
"
tach
"
"""
"""
self
.
assertRaises
(
self
.
assertRaises
(
ValueError
,
lambda
:
parse_model_from_yaml
(
to_file
(
string
)))
ValueError
,
lambda
:
parse_model_from_yaml
(
to_file
(
string
)))
def
test_inheritance
(
self
):
def
test_inheritance
(
self
):
string
=
"""
string
=
"""
...
@@ -307,8 +301,6 @@ class ExternTest(unittest.TestCase):
...
@@ -307,8 +301,6 @@ class ExternTest(unittest.TestCase):
class
ErrorMessageTest
(
unittest
.
TestCase
):
class
ErrorMessageTest
(
unittest
.
TestCase
):
"""
Tests for understandable error messages.
"""
"""
Tests for understandable error messages.
"""
# Note: This was changed with implementation of role keyword
@unittest.expectedFailure
def
test_non_dict
(
self
):
def
test_non_dict
(
self
):
"""
When a value is given, where a list or mapping is expected.
"""
"""
When a value is given, where a list or mapping is expected.
"""
recordtype_value
=
"""
recordtype_value
=
"""
...
@@ -336,66 +328,3 @@ A:
...
@@ -336,66 +328,3 @@ 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.
Florian Spreckelsen
@florian
mentioned in commit
87dd3224
·
3 years ago
mentioned in commit
87dd3224
mentioned in commit 87dd322424578d19103e02785734c11165cfae88
Toggle commit list
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