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
0f463254
Verified
Commit
0f463254
authored
Feb 17, 2023
by
Daniel Hornung
Browse files
Options
Downloads
Patches
Plain Diff
FIX: Recursive parent getting now retrieves data from server.
parent
de6c562c
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!95
DOC: Added CITATION.cff to the list of files in the release guide where the...
,
!94
Get parents recursively
Pipeline
#33723
passed
Feb 17, 2023
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/caosdb/common/models.py
+54
-19
54 additions, 19 deletions
src/caosdb/common/models.py
with
54 additions
and
19 deletions
src/caosdb/common/models.py
+
54
−
19
View file @
0f463254
...
...
@@ -34,6 +34,7 @@ All additional classes are either important for the entities or the
transactions.
"""
from
__future__
import
print_function
,
unicode_literals
from
__future__
import
annotations
# Can be removed with 3.10.
import
re
import
sys
...
...
@@ -82,7 +83,7 @@ SPECIAL_ATTRIBUTES = ["name", "role", "datatype", "description",
"
id
"
,
"
path
"
,
"
checksum
"
,
"
size
"
]
class
Entity
(
object
)
:
class
Entity
:
"""
Entity is a generic CaosDB object.
...
...
@@ -621,24 +622,44 @@ class Entity(object):
return
self
def
has_parent
(
self
,
parent
,
recursive
=
True
,
check_name
=
True
,
check_id
=
False
):
"""
Check
s
if this entity has a given parent.
def
has_parent
(
self
,
parent
:
Entity
,
recursive
:
bool
=
True
,
retrieve
:
bool
=
True
,
check_name
:
bool
=
True
,
check_id
:
bool
=
False
):
"""
Check if this entity has a given parent.
If
'
check_name
'
and
'
check_id
'
are both False, test for identity
on the Python level. Otherwise use the name and/or ID for the
check. Note that, if checked, name or ID should not be None,
lest the check fail.
@param parent: Check for this parent.
@param recursive: Whether to check recursively.
@param check_name: Whether to use the name for ancestry check.
@param check_id: Whether to use the ID for ancestry check.
@return: True if
'
parent
'
is a true parent, False otherwise.
Parameters
----------
parent: Entity
Check for this parent.
recursive: bool, optional
Whether to check recursively.
check_name: bool, optional
Whether to use the name for ancestry check.
check_id: bool, optional
Whether to use the ID for ancestry check.
retrieve: bool, optional
If False, do not retrieve parents from the server.
Returns
-------
out: bool
True if ``parent`` is a true parent, False otherwise.
"""
if
recursive
:
parents
=
self
.
get_parents_recursively
()
parents
=
self
.
get_parents_recursively
(
retrieve
=
retrieve
)
else
:
if
retrieve
:
parents
=
[
pp
.
retrieve
().
_wrapped_entity
for
pp
in
self
.
parents
]
else
:
parents
=
[
pp
.
_wrapped_entity
for
pp
in
self
.
parents
]
...
...
@@ -664,18 +685,27 @@ class Entity(object):
return
self
.
parents
def
get_parents_recursively
(
self
):
def
get_parents_recursively
(
self
,
retrieve
:
bool
=
True
):
"""
Get all ancestors of this entity.
@return: list of Entities
Parameters
----------
retrieve: bool, optional
If False, do not retrieve parents from the server.
Returns
-------
out: List[Entity]
The parents of this Entity
"""
all_parents
=
_Parents
()
self
.
_get_parent_recursively
(
all_parents
)
self
.
_get_parent_recursively
(
all_parents
,
retrieve
=
retrieve
)
return
all_parents
def
_get_parent_recursively
(
self
,
all_parents
):
def
_get_parent_recursively
(
self
,
all_parents
,
retrieve
:
bool
=
True
):
"""
Get all ancestors with a little helper.
As a side effect of this method, the ancestors are added to
...
...
@@ -688,10 +718,15 @@ class Entity(object):
for
parent
in
self
.
parents
:
w_parent
=
parent
.
_wrapped_entity
if
retrieve
:
parent
.
retrieve
()
for
next_parent
in
parent
.
parents
:
w_parent
.
add_parent
(
next_parent
)
if
w_parent
not
in
all_parents
:
if
(
w_parent
.
id
,
w_parent
.
name
)
not
in
[
(
all_p
.
id
,
all_p
.
name
)
for
all_p
in
all_parents
]:
all_parents
.
append
(
w_parent
)
w_parent
.
_get_parent_recursively
(
all_parents
)
w_parent
.
_get_parent_recursively
(
all_parents
,
retrieve
=
retrieve
)
def
get_parent
(
self
,
key
):
"""
Return the first parent matching the key or None if no match exists.
...
...
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