def open(configuration_file): """Create an ISPyB connection using settings from a configuration file. This can be used either as a function call or as a context manager. :param configuration_file: Full path to a file containing database credentials :return: ISPyB connection object """ config = configparser.RawConfigParser(allow_no_value=True) if not config.read(configuration_file): raise AttributeError("No configuration found at %s" % configuration_file) conn = None if config.has_section("ispyb_mariadb_sp"): from ispyb.connector.mysqlsp.main import ISPyBMySQLSPConnector as Connector credentials = dict(config.items("ispyb_mariadb_sp")) _log.debug("Creating MariaDB Stored Procedure connection from %s", configuration_file) conn = Connector(**credentials) elif 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) conn = Connector(**credentials) else: raise AttributeError( "No supported connection type found in %s. For an example of a valid config file, please see config.example.cfg." % configuration_file) return conn
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) conn = None 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) conn = Connector(**credentials) elif 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) conn = Connector(**credentials) else: raise AttributeError('No supported connection type found in %s' % configuration_file) return conn
def open(credentials=None, configuration_file=None): """Create an ISPyB connection. Args: credentials: a config file containing database credentials. If ``credentials=None`` then look for a credentials file in the "ISPYB_CREDENTIALS" environment variable. Example credentials file:: [ispyb_mariadb_sp] user = ispyb_api pw = password_1234 host = localhost port = 3306 db = ispybtest reconn_attempts = 6 reconn_delay = 1 Returns: The ISPyB connection object. """ if configuration_file: warnings.warn( "The parameter 'configuration_file' is deprecated and will be removed in a future version. " "Use positional arguments or 'credentials' instead", DeprecationWarning, stacklevel=2, ) credentials = credentials or configuration_file or os.getenv( "ISPYB_CREDENTIALS") if not credentials: raise AttributeError("No credentials file specified") config = configparser.RawConfigParser(allow_no_value=True) if not config.read(credentials): raise AttributeError(f"No configuration found at {credentials}") if config.has_section("ispyb_mariadb_sp"): from ispyb.connector.mysqlsp.main import ISPyBMySQLSPConnector as Connector _log.debug( f"Creating MariaDB Stored Procedure connection from {credentials}") credentials = dict(config.items("ispyb_mariadb_sp")) conn = Connector(**credentials) elif config.has_section("ispyb_ws"): from ispyb.connector.ws.main import ISPyBWSConnector as Connector _log.debug(f"Creating Webservices connection from {credentials}") credentials = dict(config.items("ispyb_ws")) conn = Connector(**credentials) else: raise AttributeError( f"No supported connection type found in {credentials}. For an example of a valid config file, please see config.example.cfg." ) return conn
def get_conn(): credentials = { "user": os.environ["ISPYB_USER"], "pw": os.environ["ISPYB_PASSWORD"], "host": os.environ["ISPYB_HOST"], "port": os.environ["ISPYB_PORT"], "db": "ispyb", } conn = Connector(**credentials) return conn