Skip to content

Commit

Permalink
Add ispyb.open() function, replacing factory.create_connection in a b…
Browse files Browse the repository at this point in the history
…ackward-compatible manner.
  • Loading branch information
Anthchirp committed Nov 14, 2017
1 parent 6a6dc4f commit 082774c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
28 changes: 28 additions & 0 deletions ispyb/__init__.py
@@ -1,7 +1,35 @@
from __future__ import absolute_import, division, print_function

try:
import configparser
except ImportError:
import ConfigParser as configparser
import logging

__version__ = '3.2.0'

_log = logging.getLogger('ispyb')

def open(configuration_file):
'''Create an ISPyB connection using settings from a configuration file.'''
config = configparser.RawConfigParser(allow_no_value=True)
if not config.read(configuration_file):
raise AttributeError('No configuration found at %s' % configuration_file)

if config.has_section('ispyb_mysql_sp'):
from ispyb.connector.mysqlsp.main import ISPyBMySQLSPConnector as Connector
credentials = dict(config.items('ispyb_mysql_sp'))
_log.debug('Creating MySQL Stored Procedure connection from %s', configuration_file)
return Connector(**credentials)

if config.has_section('ispyb_ws'):
from ispyb.connector.ws.main import ISPyBWSConnector as Connector
credentials = dict(config.items('ispyb_ws'))
_log.debug('Creating Webservices connection from %s', configuration_file)
return Connector(**credentials)

raise AttributeError('No supported connection type found in %s' % configuration_file)

# add legacy imports to top level name space
import ispyb.legacy.common.driver
legacy_get_driver = ispyb.legacy.common.driver.get_driver
Expand Down
33 changes: 6 additions & 27 deletions ispyb/factory.py
@@ -1,12 +1,9 @@
import codecs
from __future__ import absolute_import, division, print_function

import importlib
import ispyb
from enum import Enum

try:
import configparser as ConfigParser
except ImportError:
import ConfigParser

class DataAreaType(Enum):
CORE = ('Core part of the database schema', 'core', 'Core')
MXACQUISITION = ('MX acquisition tables', 'mxacquisition', 'MXAcquisition')
Expand All @@ -22,27 +19,9 @@ def __init__(self, description, module, classname):
self.classname = classname

def create_connection(conf_file):
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.readfp(codecs.open(conf_file, "r", "utf8"))

section = None
module_str = None
class_str = None
if config.has_section('ispyb_mysql_sp'):
section = 'ispyb_mysql_sp'
module_str = 'ispyb.connector.mysqlsp.main'
class_str = 'ISPyBMySQLSPConnector'
elif config.has_section('ispyb_ws'):
section = 'ispyb_ws'
module_str = 'ispyb.connector.ws.main'
class_str = 'ISPyBWSConnector'
else:
raise AttributeError('No supported connection type found in %s' % conf_file)

conn_mod = importlib.import_module(module_str)
ConnClass = getattr(conn_mod, class_str)
credentials = dict(config.items(section))
return ConnClass(**credentials)
import warnings
warnings.warn("deprecated, use ispyb.open()", DeprecationWarning)
return ispyb.open(conf_file)

def create_data_area(data_area_type, conn):
'''Factory function. Given a DataArea type and a Connection object imports the relevant data area module and
Expand Down

0 comments on commit 082774c

Please sign in to comment.