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
aecc3406
Commit
aecc3406
authored
Mar 16, 2022
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
ENH: entities can automatically be retrieved on resolve
parent
ba58a7c0
No related branches found
No related tags found
2 merge requests
!57
RELEASE 0.7.3
,
!52
F refactor high level api
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caosdb/high_level_api.py
+40
-9
40 additions, 9 deletions
src/caosdb/high_level_api.py
unittests/test_high_level_api.py
+10
-1
10 additions, 1 deletion
unittests/test_high_level_api.py
with
50 additions
and
10 deletions
src/caosdb/high_level_api.py
+
40
−
9
View file @
aecc3406
...
...
@@ -93,15 +93,18 @@ def high_level_type_for_role(role: str):
def
high_level_type_for_standard_type
(
standard_record
:
db
.
Entity
):
if
type
(
standard_record
)
==
db
.
Record
:
if
not
isinstance
(
standard_record
,
db
.
Entity
):
raise
ValueError
()
role
=
standard_record
.
role
if
role
==
"
Record
"
or
type
(
standard_record
)
==
db
.
Record
:
return
CaosDBPythonRecord
elif
type
(
standard_record
)
==
db
.
File
:
elif
role
==
"
File
"
or
type
(
standard_record
)
==
db
.
File
:
return
CaosDBPythonFile
elif
type
(
standard_record
)
==
db
.
Property
:
elif
role
==
"
Property
"
or
type
(
standard_record
)
==
db
.
Property
:
return
CaosDBPythonProperty
elif
type
(
standard_record
)
==
db
.
RecordType
:
elif
role
==
"
RecordType
"
or
type
(
standard_record
)
==
db
.
RecordType
:
return
CaosDBPythonRecordType
elif
type
(
standard_record
)
==
db
.
Entity
:
elif
role
==
"
Entity
"
or
type
(
standard_record
)
==
db
.
Entity
:
return
CaosDBPythonEntity
raise
RuntimeError
(
"
Incompatible type.
"
)
...
...
@@ -531,6 +534,14 @@ class CaosDBPythonEntity(object):
# don't do the lookup in the references container
return
visited
[
propval
.
id
]
if
references
is
None
:
ent
=
db
.
Entity
(
id
=
propval
.
id
).
retrieve
()
obj
=
convert_to_python_object
(
ent
,
references
)
visited
[
propval
.
id
]
=
obj
if
deep
:
obj
.
resolve_references
(
deep
,
references
,
visited
)
return
obj
# lookup in container:
for
ent
in
references
:
# Entities in container without an ID will be skipped:
...
...
@@ -556,6 +567,8 @@ class CaosDBPythonEntity(object):
references: Optional[db.Container]
A container with references that might be resolved.
If None is passed as the container, this function tries to resolve entities from a running
CaosDB instance directly.
"""
# This parameter is used in the recursion to keep track of already visited
...
...
@@ -613,8 +626,14 @@ class CaosDBPythonEntity(object):
for
parent
in
serialization
[
"
parents
"
]:
if
"
unresolved
"
in
parent
:
id
=
None
name
=
None
if
"
id
"
in
parent
:
id
=
parent
[
"
id
"
]
if
"
name
"
in
parent
:
name
=
parent
[
"
name
"
]
entity
.
add_parent
(
CaosDBPythonUnresolvedParent
(
id
=
parent
[
"
id
"
],
name
=
parent
[
"
name
"
]
))
id
=
id
,
name
=
name
))
else
:
raise
NotImplementedError
()
...
...
@@ -756,9 +775,11 @@ class CaosDBMultiProperty:
class
CaosDBPythonFile
(
CaosDBPythonEntity
):
def
get_File
(
self
,
target
=
None
):
f
=
db
.
File
(
id
=
self
.
_id
).
retrieve
()
self
.
_file
=
f
.
download
(
target
)
def
download
(
self
,
target
=
None
):
if
self
.
id
is
None
:
raise
RuntimeError
(
"
Cannot download file when id is missing.
"
)
f
=
db
.
File
(
id
=
self
.
id
).
retrieve
()
return
f
.
download
(
target
)
BASE_ATTRIBUTES
=
(
...
...
@@ -974,3 +995,13 @@ def create_entity_container(record: CaosDBPythonEntity):
return
db
.
Container
().
extend
(
lse
)
def
query
(
query
:
str
,
resolve_references
:
bool
=
True
,
references
:
db
.
Container
=
None
):
"""
"""
res
=
db
.
execute_query
(
query
)
objects
=
convert_to_python_object
(
res
)
if
resolve_references
:
for
obj
in
objects
:
obj
.
resolve_references
(
True
,
references
)
return
objects
This diff is collapsed.
Click to expand it.
unittests/test_high_level_api.py
+
10
−
1
View file @
aecc3406
...
...
@@ -532,9 +532,14 @@ def test_type_conversion():
with
pytest
.
raises
(
RuntimeError
,
match
=
"
Incompatible type.
"
):
standard_type_for_high_level_type
(
42
,
True
)
with
pytest
.
raises
(
RuntimeError
,
match
=
"
Incompatible type.
"
):
with
pytest
.
raises
(
ValueError
):
high_level_type_for_standard_type
(
"
ajsdkfjasfkj
"
)
with
pytest
.
raises
(
RuntimeError
,
match
=
"
Incompatible type.
"
):
class
IncompatibleType
(
db
.
Entity
):
pass
high_level_type_for_standard_type
(
IncompatibleType
())
def
test_deserialization
():
r
=
db
.
Record
(
id
=
17
,
name
=
"
test
"
)
...
...
@@ -585,3 +590,7 @@ def test_deserialization():
serial
=
obj
.
serialize
()
obj_des
=
CaosDBPythonEntity
.
deserialize
(
serial
)
assert
obj
.
serialize
()
==
obj_des
.
serialize
()
def
test_recursion
():
pass
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