Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-pylib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
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-pylib
Commits
6a39b734
Verified
Commit
6a39b734
authored
1 year ago
by
Daniel Hornung
Browse files
Options
Downloads
Patches
Plain Diff
WIP: Plain json serialization for high level api.
parent
25a6b054
No related branches found
No related tags found
2 merge requests
!189
ENH: add convenience functions
,
!185
High level API serialization
Pipeline
#50554
passed with warnings
1 year ago
Stage: code_style
Stage: linting
Stage: test
Stage: deploy
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/linkahead/high_level_api.py
+56
-35
56 additions, 35 deletions
src/linkahead/high_level_api.py
with
56 additions
and
35 deletions
src/linkahead/high_level_api.py
+
56
−
35
View file @
6a39b734
...
@@ -471,7 +471,6 @@ class CaosDBPythonEntity(object):
...
@@ -471,7 +471,6 @@ class CaosDBPythonEntity(object):
if
isinstance
(
att
,
list
):
if
isinstance
(
att
,
list
):
return
att
return
att
else
:
return
[
att
]
return
[
att
]
def
add_parent
(
self
,
parent
:
Union
[
def
add_parent
(
self
,
parent
:
Union
[
...
@@ -679,53 +678,68 @@ class CaosDBPythonEntity(object):
...
@@ -679,53 +678,68 @@ class CaosDBPythonEntity(object):
return
entity
return
entity
def
serialize
(
self
,
without_metadata
:
bool
=
False
,
visited
:
dict
=
None
):
def
serialize
(
self
,
without_metadata
:
bool
=
None
,
plain_json
:
bool
=
False
,
"""
visited
:
dict
=
None
):
Serialize necessary information into a dict.
"""
Serialize necessary information into a dict.
without_metadata: bool
Parameters
----------
without_metadata: bool, optional
If True don
'
t set the metadata field in order to increase
If True don
'
t set the metadata field in order to increase
readability. Not recommended if deserialization is needed.
readability. Not recommended if deserialization is needed.
"""
plain_json: bool, optional
If True, serialize to a plain dict without any additional information besides the property values,
name and id. This should conform to the format as specified by the json schema generated by the
advanced user tools. This implies ``without_metadata = True``.
"""
if
plain_json
:
if
without_metadata
is
None
:
without_metadata
=
True
if
not
without_metadata
:
raise
ValueError
(
"
`plain_json` implies `without_metadata`.
"
)
if
without_metadata
is
None
:
without_metadata
=
False
if
visited
is
None
:
if
visited
is
None
:
visited
=
dict
()
visited
=
{}
if
self
in
visited
:
if
self
in
visited
:
return
visited
[
self
]
return
visited
[
self
]
metadata
:
Dict
[
str
,
Any
]
=
dict
()
metadata
:
Dict
[
str
,
Any
]
=
{}
properties
=
dict
()
properties
=
{}
parents
=
list
()
parents
=
[]
# The full information to be returned:
# The full information to be returned:
fulldict
=
dict
()
fulldict
=
{}
visited
[
self
]
=
fulldict
visited
[
self
]
=
fulldict
# Add CaosDB role:
fulldict
[
"
role
"
]
=
standard_type_for_high_level_type
(
self
,
True
)
for
parent
in
self
.
_parents
:
for
parent
in
self
.
_parents
:
if
isinstance
(
parent
,
CaosDBPythonEntity
):
if
isinstance
(
parent
,
CaosDBPythonEntity
):
parents
.
append
(
parent
.
serialize
(
without_metadata
,
visited
))
parents
.
append
(
parent
.
serialize
(
without_metadata
=
without_metadata
,
plain_json
=
plain_json
,
visited
=
visited
))
elif
isinstance
(
parent
,
CaosDBPythonUnresolvedParent
):
elif
isinstance
(
parent
,
CaosDBPythonUnresolvedParent
):
parents
.
append
({
"
name
"
:
parent
.
name
,
"
id
"
:
parent
.
id
,
parents
.
append
({
"
name
"
:
parent
.
name
,
"
id
"
:
parent
.
id
,
"
unresolved
"
:
True
})
"
unresolved
"
:
True
})
else
:
else
:
raise
RuntimeError
(
"
Incompatible class used as parent.
"
)
raise
RuntimeError
(
"
Incompatible class used as parent.
"
)
if
not
plain_json
:
# Add LinkAhead role:
fulldict
[
"
role
"
]
=
standard_type_for_high_level_type
(
self
,
True
)
for
baseprop
in
(
"
name
"
,
"
id
"
,
"
description
"
,
"
version
"
):
for
baseprop
in
(
"
name
"
,
"
id
"
,
"
description
"
,
"
version
"
):
val
=
self
.
__getattribute__
(
baseprop
)
val
=
self
.
__getattribute__
(
baseprop
)
if
val
is
not
None
:
if
val
is
not
None
:
fulldict
[
baseprop
]
=
val
fulldict
[
baseprop
]
=
val
if
typ
e
(
self
)
==
CaosDBPythonFile
:
if
isinstanc
e
(
self
,
CaosDBPythonFile
)
:
fulldict
[
"
file
"
]
=
self
.
file
fulldict
[
"
file
"
]
=
self
.
file
fulldict
[
"
path
"
]
=
self
.
path
fulldict
[
"
path
"
]
=
self
.
path
for
p
in
self
.
get_properties
():
for
p
in
self
.
get_properties
():
m
=
self
.
get_property_metadata
(
p
)
m
=
self
.
get_property_metadata
(
p
)
metadata
[
p
]
=
dict
()
metadata
[
p
]
=
{}
for
f
in
fields
(
m
):
for
f
in
fields
(
m
):
val
=
m
.
__getattribute__
(
f
.
name
)
val
=
m
.
__getattribute__
(
f
.
name
)
if
val
is
not
None
:
if
val
is
not
None
:
...
@@ -735,28 +749,35 @@ class CaosDBPythonEntity(object):
...
@@ -735,28 +749,35 @@ class CaosDBPythonEntity(object):
if
isinstance
(
val
,
CaosDBPythonUnresolvedReference
):
if
isinstance
(
val
,
CaosDBPythonUnresolvedReference
):
properties
[
p
]
=
{
"
id
"
:
val
.
id
,
"
unresolved
"
:
True
}
properties
[
p
]
=
{
"
id
"
:
val
.
id
,
"
unresolved
"
:
True
}
elif
isinstance
(
val
,
CaosDBPythonEntity
):
elif
isinstance
(
val
,
CaosDBPythonEntity
):
properties
[
p
]
=
val
.
serialize
(
without_metadata
,
visited
)
properties
[
p
]
=
val
.
serialize
(
without_metadata
=
without_metadata
,
plain_json
=
plain_json
,
visited
=
visited
)
elif
isinstance
(
val
,
list
):
elif
isinstance
(
val
,
list
):
serializedelements
=
[]
serializedelements
=
[]
for
element
in
val
:
for
element
in
val
:
if
isinstance
(
element
,
CaosDBPythonUnresolvedReference
):
if
isinstance
(
element
,
CaosDBPythonUnresolvedReference
):
elm
=
dict
()
elm
=
{}
elm
[
"
id
"
]
=
element
.
id
elm
[
"
id
"
]
=
element
.
id
elm
[
"
unresolved
"
]
=
True
elm
[
"
unresolved
"
]
=
True
serializedelements
.
append
(
elm
)
serializedelements
.
append
(
elm
)
elif
isinstance
(
element
,
CaosDBPythonEntity
):
elif
isinstance
(
element
,
CaosDBPythonEntity
):
serializedelements
.
append
(
serializedelements
.
append
(
element
.
serialize
(
without_metadata
,
element
.
serialize
(
without_metadata
=
without_metadata
,
visited
))
plain_json
=
plain_json
,
visited
=
visited
))
else
:
else
:
serializedelements
.
append
(
element
)
serializedelements
.
append
(
element
)
properties
[
p
]
=
serializedelements
properties
[
p
]
=
serializedelements
else
:
else
:
properties
[
p
]
=
val
properties
[
p
]
=
val
if
plain_json
:
fulldict
[
"
id
"
]
=
getattr
(
self
,
"
id
"
)
fulldict
[
"
name
"
]
=
getattr
(
self
,
"
name
"
)
fulldict
.
update
(
properties
)
else
:
fulldict
[
"
properties
"
]
=
properties
fulldict
[
"
properties
"
]
=
properties
fulldict
[
"
parents
"
]
=
parents
fulldict
[
"
parents
"
]
=
parents
if
not
without_metadata
:
if
not
without_metadata
:
fulldict
[
"
metadata
"
]
=
metadata
fulldict
[
"
metadata
"
]
=
metadata
return
fulldict
return
fulldict
...
...
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