Skip to main content

package for database cofiguration and credential management

Project description

Credbl: database settings and credentials simplified

There are many moving parts when setting up programmatic access to a database. Credbl divides (and conquers) those bits and pieces the following way:

  • [connect-where] database server specific settings are read from a YAML configuration file provided by the database admin

  • [connect-who] user's credentials are requested and stored in keyring or winreg (on Windows).

    The first time the user connects to the database with connect_* functions, the credentials will be requested and stored. Subsequent times, they will be silently retrieved and used to authenticate. If authentication fails due to wrong credentials, the user will be asked to enter credentials again.

  • [connect-how] database drivers are chosen based on user's Operation system.

Currently credbl focuses on MS SQL Server and MongoDB connections. You are welcome to submit issues and pull requests for other database types.

Installation

Python package

from terminal shell (standard):

  • latest release: pip install credbl

  • bleeding edge: pip install git+git://github.com/BCHSI/credbl.git#egg=credbl

  • development mode

      git clone https://github.com/BCHSI/credbl
      cd credbl
      pip install -e .
    

R package

from R:

install.packages('devtools')
library(devtools)
install_github('https://github.com/BCHSI/credbl')

or terminal + R:

git clone https://github.com/BCHSI/credbl
cd credbl
R
> install.packages('devtools')
> library(devtools)
> install('.')

get help:

?credbl

Usage examples:

connecting to a MS SQL Server

from credbl import connect_mssql
conn = connect_mssql("tp-mssql-settings.yaml")

Contents of database configuration file

In a previous example, "tp-mssql-settings.yaml" must contain:

server:    12.34.56.78 (OR) mydatabase.mybusiness.com
port:      1234
database:  tp-inventory
driver:    FreeTDS (optional)

Low-level interface:

You might need to understand it if you would like to use SQL drivers or driver wrappers other than pyodbc, such as SQLAlchemy.

with pyodbc

import pyodbc
from credbl import get_mssql_connection_string 

# if called for the first time, will request credentials
# second time may ask for your _system_ credentials; mark "always allow"

connection_str = get_mssql_connection_string("tp-mssql-settings.yaml")
# you'll be requested to enter your credentials when running it for the first time
conn = pyodbc.connect(connection_str)

if you believe you've entered wrong credentials first time, call with reset=True

connection_str = get_mssql_connection_string("tp-mssql-settings.yaml", reset=True)

conn = pyodbc.connect(connection_str)

with SqlAlchemy

import sqlalchemy
from credbl import get_mssql_connection_string 

connection_str_encoded = get_mssql_connection_string('covid19_omop.yaml',
                                                 urlencode=True)
connection_uri = 'mssql+pyodbc:///?odbc_connect={}'.format(connection_str_encoded)
conn = sqlalchemy.create_engine(connection_uri)

connecting to mongodb

from credbl import connect_mongodb

# if called for the first time, will request database credentials
mdb = connect_mongodb("mongo-settings.yaml")

mdb.list_collection_names()

The "mongo-settings.yaml" file must contain following:

url: mongodb://10.20.30.40:27017
db: 'databasename'

Alternatively / optionally to URL, server or IP address and port can be provided:

server: xyz.company.org
ip: 10.20.30.40
port: 27017

storing credentials in keyring (Mac, Unix) or Windows key storage:

In [1]: import credbl

In [2]: credbl.get_credentials('something')
enter user name for 'something': []: myname
enter password for 'myname':
Out[2]: ('myname', 'xyz')

In [3]: credbl.get_credentials('something')
Out[3]: ('myname', 'xyz')

Files

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

credbl-0.0.4.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

credbl-0.0.4-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file credbl-0.0.4.tar.gz.

File metadata

  • Download URL: credbl-0.0.4.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

File hashes

Hashes for credbl-0.0.4.tar.gz
Algorithm Hash digest
SHA256 06e7444cd50835370b8726e05c9d50a69d628bda9be97e0fd6dcbf3c3725f6b8
MD5 e6b55962e2a0736c98ad12f101c49d7f
BLAKE2b-256 0f5fcbdfa895d3a1f6ecde0f4d9c5379192e4bc6f379112e5db2fc2a13f10383

See more details on using hashes here.

File details

Details for the file credbl-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: credbl-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

File hashes

Hashes for credbl-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c8bf28d7927a40db64328eb72decdb037e0aa42f727c1b386d354f5712096fe5
MD5 5908c53baef621ffce2136ae9cae9acf
BLAKE2b-256 bdff6d1e8bace634409e159c0fa297c6108a59a46708758047af0bb12975381b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page