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
87483f68
Commit
87483f68
authored
3 years ago
by
Florian Spreckelsen
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Implement parsing of enums
parent
34da56d4
No related branches found
No related tags found
2 merge requests
!39
Release 0.4.0
,
!33
F json schema datamodel
Pipeline
#20433
passed
3 years ago
Stage: setup
Stage: cert
Stage: style
Stage: unittest
Stage: integrationtest
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caosadvancedtools/models/parser.py
+18
-4
18 additions, 4 deletions
src/caosadvancedtools/models/parser.py
unittests/test_json_schema_model_parser.py
+2
-1
2 additions, 1 deletion
unittests/test_json_schema_model_parser.py
with
20 additions
and
5 deletions
src/caosadvancedtools/models/parser.py
+
18
−
4
View file @
87483f68
...
@@ -580,9 +580,11 @@ class JsonSchemaParser(Parser):
...
@@ -580,9 +580,11 @@ class JsonSchemaParser(Parser):
for
ii
,
elt
in
enumerate
(
model_dict
):
for
ii
,
elt
in
enumerate
(
model_dict
):
if
"
title
"
not
in
elt
:
if
"
title
"
not
in
elt
:
raise
JsonSchemaDefinitionError
(
f
"
Object
{
ii
+
1
}
is lacking the `title` key word
"
)
raise
JsonSchemaDefinitionError
(
f
"
Object
{
ii
+
1
}
is lacking the `title` key word
"
)
if
"
type
"
not
in
elt
:
if
"
type
"
not
in
elt
:
raise
JsonSchemaDefinitionError
(
f
"
Object
{
ii
+
1
}
is lacking the `type` key word
"
)
raise
JsonSchemaDefinitionError
(
f
"
Object
{
ii
+
1
}
is lacking the `type` key word
"
)
# Check if this is a valid Json Schema
# Check if this is a valid Json Schema
try
:
try
:
jsonschema
.
Draft202012Validator
.
check_schema
(
elt
)
jsonschema
.
Draft202012Validator
.
check_schema
(
elt
)
...
@@ -597,7 +599,9 @@ class JsonSchemaParser(Parser):
...
@@ -597,7 +599,9 @@ class JsonSchemaParser(Parser):
def
_treat_element
(
self
,
elt
:
dict
,
name
:
str
):
def
_treat_element
(
self
,
elt
:
dict
,
name
:
str
):
if
name
in
self
.
model
:
if
name
in
self
.
model
:
return
self
.
model
[
name
]
return
self
.
model
[
name
]
if
elt
[
"
type
"
]
==
"
string
"
:
if
"
enum
"
in
elt
:
ent
=
self
.
_treat_enum
(
elt
,
name
)
elif
elt
[
"
type
"
]
==
"
string
"
:
if
"
format
"
in
elt
and
elt
[
"
format
"
]
==
"
date-time
"
:
if
"
format
"
in
elt
and
elt
[
"
format
"
]
==
"
date-time
"
:
# Treat datetime strings separately
# Treat datetime strings separately
ent
=
db
.
Property
(
name
=
name
,
datatype
=
db
.
DATETIME
)
ent
=
db
.
Property
(
name
=
name
,
datatype
=
db
.
DATETIME
)
...
@@ -612,7 +616,8 @@ class JsonSchemaParser(Parser):
...
@@ -612,7 +616,8 @@ class JsonSchemaParser(Parser):
elif
elt
[
"
type
"
]
==
"
object
"
:
elif
elt
[
"
type
"
]
==
"
object
"
:
ent
=
self
.
_treat_record_type
(
elt
,
name
)
ent
=
self
.
_treat_record_type
(
elt
,
name
)
else
:
else
:
raise
NotImplementedError
(
f
"
Cannot parse items of type
'
{
elt
[
'
type
'
]
}
'
(yet).
"
)
raise
NotImplementedError
(
f
"
Cannot parse items of type
'
{
elt
[
'
type
'
]
}
'
(yet).
"
)
if
"
description
"
in
elt
:
if
"
description
"
in
elt
:
ent
.
description
=
elt
[
"
description
"
]
ent
.
description
=
elt
[
"
description
"
]
...
@@ -637,6 +642,15 @@ class JsonSchemaParser(Parser):
...
@@ -637,6 +642,15 @@ class JsonSchemaParser(Parser):
return
rt
return
rt
def
_treat_enum
(
self
,
elt
:
dict
,
name
:
str
):
rt
=
db
.
RecordType
(
name
=
name
)
for
enum_elt
in
elt
[
"
enum
"
]:
rec
=
db
.
Record
(
name
=
enum_elt
)
rec
.
add_parent
(
rt
)
self
.
model
[
enum_elt
]
=
rec
return
rt
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
model
=
parse_model_from_yaml
(
'
data_model.yml
'
)
model
=
parse_model_from_yaml
(
'
data_model.yml
'
)
...
...
This diff is collapsed.
Click to expand it.
unittests/test_json_schema_model_parser.py
+
2
−
1
View file @
87483f68
...
@@ -141,7 +141,8 @@ def test_enum():
...
@@ -141,7 +141,8 @@ def test_enum():
assert
isinstance
(
model
[
"
Dataset
"
],
db
.
RecordType
)
assert
isinstance
(
model
[
"
Dataset
"
],
db
.
RecordType
)
assert
model
[
"
Dataset
"
].
get_property
(
"
license
"
)
is
not
None
assert
model
[
"
Dataset
"
].
get_property
(
"
license
"
)
is
not
None
assert
model
[
"
Dataset
"
].
get_property
(
"
license
"
).
datatype
==
"
license
"
assert
model
[
"
Dataset
"
].
get_property
(
"
license
"
).
is_reference
()
assert
model
[
"
Dataset
"
].
get_property
(
"
license
"
).
datatype
.
name
==
"
license
"
assert
isinstance
(
model
[
"
license
"
],
db
.
RecordType
)
assert
isinstance
(
model
[
"
license
"
],
db
.
RecordType
)
for
name
in
licenses
:
for
name
in
licenses
:
...
...
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