Skip to content

Commit

Permalink
merge master -Dorg -Ssuccess-only: PR 207 (BlitzGateway.connect raise…
Browse files Browse the repository at this point in the history
… on error)
  • Loading branch information
snoopycrimecop committed Mar 24, 2022
2 parents 3032b97 + d68e639 commit b9440aa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/omero/gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1616,9 +1616,7 @@ def __enter__(self):
print list(conn.getObjects('Project'))
"""
if not self._connected:
r = self.connect()
if not r:
raise Exception("Connect failed")
self.connect(raiseOnError=True)
return self

def __exit__(self, *args):
Expand Down Expand Up @@ -2155,12 +2153,13 @@ def _resetOmeroClient(self):
if self.userip is not None:
self.c.setIP(self.userip)

def connect(self, sUuid=None):
def connect(self, sUuid=None, raiseOnError=False):
"""
Creates or retrieves connection for the given sessionUuid.
Returns True if connected.
:param sUuid: omero_model_SessionI
:param raiseOnError: Boolean
:return: Boolean
"""

Expand All @@ -2169,6 +2168,8 @@ def connect(self, sUuid=None):
if not self.c: # pragma: no cover
self._connected = False
logger.debug("Ooops. no self._c")
if raiseOnError:
raise Exception('No self.c')
return False
try:
if self._sessionUuid is None and sUuid:
Expand All @@ -2194,6 +2195,8 @@ def connect(self, sUuid=None):
raise
except Exception as x: # pragma: no cover
logger.debug("Error: " + str(x))
if raiseOnError:
raise
self._sessionUuid = None
if sUuid:
return False
Expand Down Expand Up @@ -2236,7 +2239,7 @@ def connect(self, sUuid=None):
self._closeSession()
self._sessionUuid = None
self._connected = True
return self.connect()
return self.connect(raiseOnError=raiseOnError)
else: # pragma: no cover
logger.debug(
"BlitzGateway.connect().createSession(): " +
Expand All @@ -2254,7 +2257,7 @@ def connect(self, sUuid=None):
"## User not in '%s' group" % self.group)
self.group = None
self._connected = True
return self.connect()
return self.connect(raiseOnError=raiseOnError)
else:
raise
except Ice.SyscallException: # pragma: no cover
Expand All @@ -2277,10 +2280,14 @@ def connect(self, sUuid=None):
raise
except Ice.LocalException as x: # pragma: no cover
logger.debug("connect(): " + traceback.format_exc())
if raiseOnError:
raise
self._last_error = x
return False
except Exception as x: # pragma: no cover
logger.debug("connect(): " + traceback.format_exc())
if raiseOnError:
raise
self._last_error = x
return False
logger.debug(".. connected!")
Expand Down
16 changes: 16 additions & 0 deletions test/unit/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,19 @@ def test_simple_marshal_not_tiled(self, wrapped_image):
data = wrapped_image.simpleMarshal(xtra={'tiled': True})
self.assert_data(data)
assert data['tiled'] is False


class TestBlitzGatewayConnect(object):

def test_connect_default(self):
conn = BlitzGateway(
host='this.host.does.not.exist.example.org',
username='username', passwd='passwd')
assert conn.connect() == False

def test_connect_raise(self):
conn = BlitzGateway(
host='this.host.does.not.exist.example.org',
username='username', passwd='passwd')
with pytest.raises(Ice.DNSException):
conn.connect(raiseOnError=True)

0 comments on commit b9440aa

Please sign in to comment.