Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BlitzGateway.connect raise on error #207

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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 @@ -2154,12 +2152,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 @@ -2168,6 +2167,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 @@ -2193,6 +2194,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 @@ -2235,7 +2238,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 @@ -2253,7 +2256,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 @@ -2276,10 +2279,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)