Skip to content

Commit

Permalink
Merge 389830f into 145b707
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthchirp committed Jan 24, 2019
2 parents 145b707 + 389830f commit 9b03cd2
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 45 deletions.
15 changes: 15 additions & 0 deletions ispyb/__init__.py
Expand Up @@ -38,3 +38,18 @@ def open(configuration_file):
raise AttributeError('No supported connection type found in %s' % configuration_file)

return conn


class ISPyBException(Exception):
'''Base class for all exceptions'''

class ConnectionError(ISPyBException):
'''Unable to connect or connection has been closed.'''

class NoResult(ISPyBException):
'''Query returned no result.'''

class ReadWriteError(ISPyBException):
'''Record could not be read, inserted, updated or deleted. This could be due to
illegal values, the wrong number of parameters, a violation of table or
index constraints, or a database failure.'''
25 changes: 12 additions & 13 deletions ispyb/connector/mysqlsp/main.py
Expand Up @@ -7,8 +7,7 @@

import ispyb.interface.connection
import mysql.connector
from ispyb.exception import (ISPyBConnectionException, ISPyBNoResultException,
ISPyBRetrieveFailed, ISPyBWriteFailed)
from ispyb import (ConnectionError, NoResult, ReadWriteError)
from mysql.connector.errors import DatabaseError, DataError, Error, InterfaceError

class ISPyBMySQLSPConnector(ispyb.interface.connection.IF):
Expand All @@ -23,7 +22,7 @@ def __enter__(self):
if hasattr(self, 'conn') and self.conn is not None:
return self
else:
raise ISPyBConnectionException
raise ConnectionError

def __exit__(self, type, value, traceback):
self.disconnect()
Expand All @@ -37,7 +36,7 @@ def connect(self, user=None, pw=None, host='localhost', db=None, port=3306, conn
database=db,
port=int(port))
if not self.conn:
raise ISPyBConnectionException('Could not connect to database')
raise ConnectionError('Could not connect to database')
self.conn.autocommit = True
self.reconn_attempts = reconn_attempts
self.reconn_delay = reconn_delay
Expand All @@ -56,14 +55,14 @@ def get_data_area_package(self):

def create_cursor(self, dictionary=False):
if not self.conn:
raise ISPyBConnectionException('Not connected to database')
raise ConnectionError('Not connected to database')
try:
self.conn.ping(reconnect=True, attempts=self.reconn_attempts, delay=self.reconn_delay)
except InterfaceError:
raise ISPyBConnectionException('Failed to ping connection')
raise ConnectionError('Failed to ping connection')
cursor = self.conn.cursor(dictionary=dictionary)
if not cursor:
raise ISPyBConnectionException('Could not create database cursor')
raise ConnectionError('Could not create database cursor')
return cursor

def call_sp_write(self, procname, args):
Expand All @@ -72,7 +71,7 @@ def call_sp_write(self, procname, args):
try:
result_args = cursor.callproc(procname=procname, args=args)
except DataError as e:
raise ISPyBWriteFailed("DataError({0}): {1}".format(e.errno, traceback.format_exc()))
raise ReadWriteError("DataError({0}): {1}".format(e.errno, traceback.format_exc()))
finally:
cursor.close()
if result_args is not None and len(result_args) > 0:
Expand All @@ -84,7 +83,7 @@ def call_sp_retrieve(self, procname, args):
try:
cursor.callproc(procname=procname, args=args)
except DataError as e:
raise ISPyBRetrieveFailed("DataError({0}): {1}".format(e.errno, traceback.format_exc()))
raise ReadWriteError("DataError({0}): {1}".format(e.errno, traceback.format_exc()))

result = []
for recordset in cursor.stored_results():
Expand All @@ -96,7 +95,7 @@ def call_sp_retrieve(self, procname, args):

cursor.close()
if result == []:
raise ISPyBNoResultException
raise NoResult
return result

def call_sf_retrieve(self, funcname, args):
Expand All @@ -105,14 +104,14 @@ def call_sf_retrieve(self, funcname, args):
try:
cursor.execute(('select %s' % funcname) + ' (%s)' % ','.join(['%s'] * len(args)), args)
except DataError as e:
raise ISPyBRetrieveFailed("DataError({0}): {1}".format(e.errno, traceback.format_exc()))
raise ReadWriteError("DataError({0}): {1}".format(e.errno, traceback.format_exc()))
result = None
rs = cursor.fetchone()
if len(rs) > 0:
result = next(iter(rs.items()))[1] #iter(rs.items()).next()[1]
cursor.close()
if result is None:
raise ISPyBNoResultException
raise NoResult
return result

def call_sf_write(self, funcname, args):
Expand All @@ -121,7 +120,7 @@ def call_sf_write(self, funcname, args):
try:
cursor.execute(('select %s' % funcname) + ' (%s)' % ','.join(['%s'] * len(args)), args)
except DataError as e:
raise ISPyBWriteFailed("DataError({0}): {1}".format(e.errno, traceback.format_exc()))
raise ReadWriteError("DataError({0}): {1}".format(e.errno, traceback.format_exc()))
result = None
rs = cursor.fetchone()
if len(rs) > 0:
Expand Down
26 changes: 10 additions & 16 deletions ispyb/exception.py
@@ -1,20 +1,14 @@
class ISPyBException(Exception):
'''Base class for all exceptions'''
from __future__ import absolute_import, division, print_function

class ISPyBConnectionException(ISPyBException):
'''Unable to connect or connection has been closed.'''
import warnings

class ISPyBNoResultException(ISPyBException):
'''Query returned no result.'''
import ispyb

class ISPyBWriteFailed(ISPyBException):
'''Record could not be inserted, updated or deleted. This could be due to
illegal values, the wrong number of parameters, a violation of table or
index constraints, or a database failure.'''
warnings.warn("ispyb.exceptions is deprecated and will be removed in the next release. Use the exceptions underneath ispyb. instead.", DeprecationWarning)

class ISPyBRetrieveFailed(ISPyBException):
'''Record(s) could not be retrieved. This could be due to invalid argument
values, or a database failure. '''

class ISPyBKeyProblem(ISPyBException):
'''A mandatory key is missing or its value is None.'''
ISPyBException = ispyb.ISPyBException
ISPyBConnectionException = ispyb.ConnectionError
ISPyBNoResultException = ispyb.NoResult
ISPyBWriteFailed = ispyb.ReadWriteError
ISPyBRetrieveFailed = ispyb.ReadWriteError
ISPyBKeyProblem = KeyError
4 changes: 2 additions & 2 deletions ispyb/model/gridinfo.py
@@ -1,6 +1,6 @@
from __future__ import absolute_import, division, print_function

import ispyb.exception
import ispyb
import ispyb.model

class GridInfo(ispyb.model.DBCache):
Expand All @@ -27,7 +27,7 @@ def reload(self):
'''Load/update information from the database.'''
try:
self._data = self._db.mx_acquisition.retrieve_dcg_grid(self._dcgid)[0]
except ispyb.exception.ISPyBNoResultException:
except ispyb.NoResult:
self._data = None

@property
Expand Down
6 changes: 3 additions & 3 deletions ispyb/model/processingjob.py
Expand Up @@ -164,7 +164,7 @@ def reload(self):
p['parameterKey'], p['parameterValue'], p['parameterId']))
for p in self._db.retrieve_job_parameters(self._jobid)
]
except ispyb.exception.ISPyBNoResultException:
except ispyb.NoResult:
self._data = []
self._data_dict = collections.OrderedDict(self._data)

Expand Down Expand Up @@ -261,7 +261,7 @@ def reload(self):
ProcessingJobImageSweep(p['dataCollectionId'], p['startImage'], p['endImage'], p['sweepId'], self._db)
for p in self._db.retrieve_job_image_sweeps(self._jobid)
]
except ispyb.exception.ISPyBNoResultException:
except ispyb.NoResult:
self._data = []

def __getitem__(self, item):
Expand Down Expand Up @@ -307,7 +307,7 @@ def reload(self):
ispyb.model.processingprogram.ProcessingProgram(p['id'], self._db, preload=p)
for p in self._db.retrieve_programs_for_job_id(self._jobid)
]
except ispyb.exception.ISPyBNoResultException:
except ispyb.NoResult:
self._data = []

def __getitem__(self, item):
Expand Down
14 changes: 7 additions & 7 deletions ispyb/xmltools.py
@@ -1,10 +1,10 @@
from __future__ import absolute_import, division, print_function

# XML-to-dict code from here:
# http://code.activestate.com/recipes/410469-xml-as-dictionary/

from xml.etree import ElementTree

from ispyb.exception import ISPyBKeyProblem

class XmlListConfig(list):
def __init__(self, aList):
for element in aList:
Expand Down Expand Up @@ -100,11 +100,11 @@ def mx_data_reduction_to_ispyb(xmldict, dc_id = None, mxprocessing = None):
scaling = xmldict['AutoProcScalingContainer']['AutoProcScaling']

if proc == None:
raise ISPyBKeyProblem("Missing key 'AutoProc'")
raise KeyError("Missing key 'AutoProc'")
if scaling == None:
raise ISPyBKeyProblem("Missing key 'AutoProcScaling'")
raise KeyError("Missing key 'AutoProcScaling'")
if int_containers == None:
raise ISPyBKeyProblem("Missing key 'AutoProcIntegrationContainer'")
raise KeyError("Missing key 'AutoProcIntegrationContainer'")

s = [None, None, None]
for i in range(0,3):
Expand All @@ -117,15 +117,15 @@ def mx_data_reduction_to_ispyb(xmldict, dc_id = None, mxprocessing = None):
s[2] = stats

if s[0] == None or s[1] == None or s[2] == None:
raise ISPyBKeyProblem("Need 3 'AutoProcScalingStatistics' keys in 'AutoProcScalingContainer'")
raise KeyError("Need 3 'AutoProcScalingStatistics' keys in 'AutoProcScalingContainer'")

for int_container in int_containers:
integration = int_container['AutoProcIntegration']
if 'dataCollectionId' not in integration:
if dc_id is not None:
integration['dataCollectionId'] = dc_id
else:
raise ISPyBKeyProblem("Missing key 'dataCollectionId'")
raise KeyError("Missing key 'dataCollectionId'")

# Store results from MX data reduction pipelines
# ...first the program info
Expand Down
4 changes: 2 additions & 2 deletions tests/test_misc.py
Expand Up @@ -2,7 +2,7 @@

import threading

import ispyb.exception
import ispyb
import ispyb.model.__future__
import mysql.connector.errors
import pytest
Expand Down Expand Up @@ -39,7 +39,7 @@ def test_multi_threads_upsert(testdb):
worker.join()

def test_retrieve_failure(testdb):
with pytest.raises(ispyb.exception.ISPyBNoResultException):
with pytest.raises(ispyb.NoResult):
rs = testdb.mx_acquisition.retrieve_data_collection_main(0)

def test_database_reconnects_on_connection_failure(testconfig, testdb):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_mxacquisition.py
Expand Up @@ -2,7 +2,7 @@

from datetime import datetime

import ispyb.exception
import ispyb
import pytest

def test_mxacquisition_methods(testdb):
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_mxacquisition_methods(testdb):
params['comments'] = 'Forgot to comment!'
iid = mxacquisition.upsert_image(list(params.values()))

with pytest.raises(ispyb.exception.ISPyBNoResultException):
with pytest.raises(ispyb.NoResult):
gridinfo = mxacquisition.retrieve_dcg_grid(dcgid)

params = mxacquisition.get_dcg_grid_params()
Expand Down

0 comments on commit 9b03cd2

Please sign in to comment.