Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 53 additions & 150 deletions src/omero/gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3377,7 +3377,7 @@ def buildQuery(self, obj_type, ids=None, params=None, attributes=None,
wrapper = KNOWN_WRAPPERS.get(obj_type.lower(), None)
if wrapper is None:
raise KeyError(
"obj_type of %s not supported by getOjbects(). "
"obj_type of %s not supported by getObjects(). "
"E.g. use 'Image' etc" % obj_type)
else:
raise AttributeError(
Expand Down Expand Up @@ -5134,6 +5134,7 @@ class AnnotationWrapper (BlitzObjectWrapper):
# E.g. DoubleAnnotationI : DoubleAnnotationWrapper
registry = {}
OMERO_TYPE = None
OMERO_CLASS = "Annotation"

def __init__(self, *args, **kwargs):
"""
Expand Down Expand Up @@ -5164,13 +5165,49 @@ def _getQueryString(cls, opts=None):
Returns a tuple of (query, clauses, params).

:param opts: Dictionary of optional parameters.
NB: No options supported for this class.
parent_type: (optional) "Project", "Dataset", "Image" etc
parent_ids: (optional) list of IDs for the parent type
ns: (optional) namespace string to filter by
:return: Tuple of string, list, ParametersI
"""
query = ("select obj from Annotation obj "

query, clauses, params = super(
AnnotationWrapper, cls)._getQueryString(opts)
if opts is None:
opts = {}

fetch_file = ""
if cls.OMERO_CLASS in ("Annotation", "FileAnnotation"):
fetch_file = "left outer join fetch obj.file as file"

query = (f"select obj from {cls.OMERO_CLASS} obj "
f"{fetch_file} "
"join fetch obj.details.owner as owner "
"join fetch obj.details.creationEvent")
return query, [], omero.sys.ParametersI()

# We want to make parent_type case-insensitive...
if 'parent_type' in opts:
# Title case works for most objects...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this utility to a separate utility method for transforming lower-case strings into case-sensitive object types with associated unit tests ?

I see a few places in the gateway that can use it. There might be use cases elsewhere in OMERO.py.

obj_type = opts['parent_type'].title()
# Handle special cases for certain annotatable object types
for otype in ["ExperimenterGroup", "LightPath", "LightSource",
"OriginalFile", "PlaneInfo", "PlateAcquisition"]:
if obj_type == otype.title():
obj_type = otype
ids_clause = ""
if 'parent_ids' in opts:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the real-world use case associated with specifying parent_type but no parent_ids?
I think it should be easy to enforce that parent_ids must be supplied if parent_type is specified. And that can always be relaxed later on if there is a real need.

ids_clause = f"and link.parent.id in (:parent_ids)"
params.add("parent_ids", rlist([rlong(i) for i in opts['parent_ids']]))

clause = f"""exists (from {obj_type}AnnotationLink as link
where link.child.id = obj.id {ids_clause})"""
clauses.append(clause)

if 'ns' in opts:
clauses.append("obj.ns=:ns")
params.add("ns", rstring(opts['ns']))

return (query, clauses, params)

@classmethod
def _register(cls, regklass):
Expand Down Expand Up @@ -5315,29 +5352,14 @@ class FileAnnotationWrapper (AnnotationWrapper, OmeroRestrictionWrapper):
"""

OMERO_TYPE = FileAnnotationI
OMERO_CLASS = "FileAnnotation"

def __init__(self, *args, **kwargs):
super(FileAnnotationWrapper, self).__init__(*args, **kwargs)
self._file = None

_attrs = ('file|OriginalFileWrapper',)

@classmethod
def _getQueryString(cls, opts=None):
"""
Used for building queries in generic methods such as
getObjects("FileAnnotation").
Returns a tuple of (query, clauses, params).

:param opts: Dictionary of optional parameters.
NB: No options supported for this class.
:return: Tuple of string, list, ParametersI
"""
query = ("select obj from FileAnnotation obj "
"join fetch obj.details.owner as owner "
"join fetch obj.details.creationEvent join fetch obj.file")
return query, [], omero.sys.ParametersI()

def getValue(self):
""" Not implemented """
pass
Expand Down Expand Up @@ -5519,22 +5541,7 @@ class TimestampAnnotationWrapper (AnnotationWrapper):
"""

OMERO_TYPE = TimestampAnnotationI

@classmethod
def _getQueryString(cls, opts=None):
"""
Used for building queries in generic methods such as
getObjects("TimestampAnnotation").
Returns a tuple of (query, clauses, params).

:param opts: Dictionary of optional parameters.
NB: No options supported for this class.
:return: Tuple of string, list, ParametersI
"""
query = ("select obj from TimestampAnnotation obj "
"join fetch obj.details.owner as owner "
"join fetch obj.details.creationEvent")
return query, [], omero.sys.ParametersI()
OMERO_CLASS = "TimestampAnnotation"

def getValue(self):
"""
Expand Down Expand Up @@ -5574,22 +5581,7 @@ class BooleanAnnotationWrapper (AnnotationWrapper):
"""

OMERO_TYPE = BooleanAnnotationI

@classmethod
def _getQueryString(cls, opts=None):
"""
Used for building queries in generic methods such as
getObjects("BooleanAnnotation").
Returns a tuple of (query, clauses, params).

:param opts: Dictionary of optional parameters.
NB: No options supported for this class.
:return: Tuple of string, list, ParametersI
"""
query = ("select obj from BooleanAnnotation obj "
"join fetch obj.details.owner as owner "
"join fetch obj.details.creationEvent")
return query, [], omero.sys.ParametersI()
OMERO_CLASS = "BooleanAnnotation"

def getValue(self):
"""
Expand Down Expand Up @@ -5617,10 +5609,11 @@ def setValue(self, val):

class TagAnnotationWrapper (AnnotationWrapper):
"""
omero_model_BooleanAnnotationI class wrapper extends AnnotationWrapper.
omero_model_TagAnnotationI class wrapper extends AnnotationWrapper.
"""

OMERO_TYPE = TagAnnotationI
OMERO_CLASS = "TagAnnotation"

def countTagsInTagset(self):
# temp solution waiting for #5785
Expand Down Expand Up @@ -5667,22 +5660,6 @@ def listParents(self, withlinks=True):
self._conn, l.parent, l))
return rv

@classmethod
def _getQueryString(cls, opts=None):
"""
Used for building queries in generic methods such as
getObjects("TagAnnotation").
Returns a tuple of (query, clauses, params).

:param opts: Dictionary of optional parameters.
NB: No options supported for this class.
:return: Tuple of string, list, ParametersI
"""
query = ("select obj from TagAnnotation obj "
"join fetch obj.details.owner as owner "
"join fetch obj.details.creationEvent")
return query, [], omero.sys.ParametersI()

def getValue(self):
"""
Gets the value of the Tag
Expand Down Expand Up @@ -5714,22 +5691,7 @@ class CommentAnnotationWrapper (AnnotationWrapper):
"""

OMERO_TYPE = CommentAnnotationI

@classmethod
def _getQueryString(cls, opts=None):
"""
Used for building queries in generic methods such as
getObjects("CommentAnnotation").
Returns a tuple of (query, clauses, params).

:param opts: Dictionary of optional parameters.
NB: No options supported for this class.
:return: Tuple of string, list, ParametersI
"""
query = ("select obj from CommentAnnotation obj "
"join fetch obj.details.owner as owner "
"join fetch obj.details.creationEvent")
return query, [], omero.sys.ParametersI()
OMERO_CLASS = "CommentAnnotation"

def getValue(self):
"""
Expand Down Expand Up @@ -5760,22 +5722,7 @@ class LongAnnotationWrapper (AnnotationWrapper):
omero_model_LongAnnotationI class wrapper extends AnnotationWrapper.
"""
OMERO_TYPE = LongAnnotationI

@classmethod
def _getQueryString(cls, opts=None):
"""
Used for building queries in generic methods such as
getObjects("LongAnnotation").
Returns a tuple of (query, clauses, params).

:param opts: Dictionary of optional parameters.
NB: No options supported for this class.
:return: Tuple of string, list, ParametersI
"""
query = ("select obj from LongAnnotation obj "
"join fetch obj.details.owner as owner "
"join fetch obj.details.creationEvent")
return query, [], omero.sys.ParametersI()
OMERO_CLASS = "LongAnnotation"

def getValue(self):
"""
Expand Down Expand Up @@ -5807,22 +5754,7 @@ class DoubleAnnotationWrapper (AnnotationWrapper):
omero_model_DoubleAnnotationI class wrapper extends AnnotationWrapper.
"""
OMERO_TYPE = DoubleAnnotationI

@classmethod
def _getQueryString(cls, opts=None):
"""
Used for building queries in generic methods such as
getObjects("DoubleAnnotation").
Returns a tuple of (query, clauses, params).

:param opts: Dictionary of optional parameters.
NB: No options supported for this class.
:return: Tuple of string, list, ParametersI
"""
query = ("select obj from DoubleAnnotation obj "
"join fetch obj.details.owner as owner "
"join fetch obj.details.creationEvent")
return query, [], omero.sys.ParametersI()
OMERO_CLASS = "DoubleAnnotation"

def getValue(self):
"""
Expand Down Expand Up @@ -5855,22 +5787,7 @@ class TermAnnotationWrapper (AnnotationWrapper):
only in 4.2+
"""
OMERO_TYPE = TermAnnotationI

@classmethod
def _getQueryString(cls, opts=None):
"""
Used for building queries in generic methods such as
getObjects("TermAnnotation").
Returns a tuple of (query, clauses, params).

:param opts: Dictionary of optional parameters.
NB: No options supported for this class.
:return: Tuple of string, list, ParametersI
"""
query = ("select obj from TermAnnotation obj "
"join fetch obj.details.owner as owner "
"join fetch obj.details.creationEvent")
return query, [], omero.sys.ParametersI()
OMERO_CLASS = "TermAnnotation"

def getValue(self):
"""
Expand Down Expand Up @@ -5902,6 +5819,7 @@ class XmlAnnotationWrapper (CommentAnnotationWrapper):
omero_model_XmlAnnotationI class wrapper extends CommentAnnotationWrapper.
"""
OMERO_TYPE = XmlAnnotationI
OMERO_CLASS = "XmlAnnotation"

AnnotationWrapper._register(XmlAnnotationWrapper)

Expand All @@ -5914,23 +5832,7 @@ class MapAnnotationWrapper (AnnotationWrapper):
omero_model_MapAnnotationI class wrapper.
"""
OMERO_TYPE = MapAnnotationI

@classmethod
def _getQueryString(cls, opts=None):
"""
Used for building queries in generic methods such as
getObjects("MapAnnotation").
Returns a tuple of (query, clauses, params).

:param opts: Dictionary of optional parameters.
NB: No options supported for this class.
:return: Tuple of string, list, ParametersI
"""

query = ("select obj from MapAnnotation obj "
"join fetch obj.details.owner as owner "
"join fetch obj.details.creationEvent")
return query, [], omero.sys.ParametersI()
OMERO_CLASS = "MapAnnotation"

def getValue(self):
"""
Expand Down Expand Up @@ -11139,6 +11041,7 @@ def refreshWrappers():
"termannotation": TermAnnotationWrapper,
"timestampannotation": TimestampAnnotationWrapper,
"mapannotation": MapAnnotationWrapper,
"xmlannotation": XmlAnnotationWrapper,
# allows for getObjects("Annotation", ids)
"annotation": AnnotationWrapper._wrap})

Expand Down