A simple sqlalchemy connection configuration manager
Project description
db_hooks
Extremely beta sqlalchemy and toml based sql connection manager.
config file
db_hooks looks for a local config file at ./.databases.toml in your working directory and then looks for a global config file using the appdirs library.
You can edit or create this file in your $EDITOR by running db_hooks edit. For me, this is vim. If no config exists, it will create one at the default location. In general, that location is one of the following:
| os | location |
|---|---|
| linux | ~/.local/share/db_hooks/databases.toml |
| osx | ~/Library/Application Support/db_hooks/databases.toml |
| windows 10 | ~\AppData\local\jfhbrook\db_hooks |
The format of this file is currently poorly-documented. The best place to get an idea of the full api is the source code. Defaults for most things are sensible.
connections
Keys in the TOML file under the connections namespace are connection names.
Connections names are nested under the connections namespace. Under those are key/value pairs for the various parameters in a connection. Connections support the following parameters:
protocol- a supported protocol. Currently:postgres,mysqlandsqlite.username- the username to log into the database with.has_password- set this flag tofalsein order to disable automatic password prompting. Defaults totruefor all protocols where passwords are supported.host- the database host.port- the database port.database- the database name.password_cmd- a connection-specific override for thepassword_cmd. By default, a connection will use the globally-setpassword_cmd.
For a very simple example, a local postgres connection might looks like this:
[connections.pg_example]
protocol = "postgres"
username = "josh"
host = "localhost"
port = "5432"
database = "josh"
passwords
db_hooks includes general purpose hooks for fetching passwords for connections, designed to be adaptable to multiple systems.
The top-level config supports two relevant parameters:
password_cmd- a shell snippet that will be ran in order to fetch the password. The command should print a string password to stdout.password_loader- configure the shell loader/parser used for running the command. defaults toshlex, but can be set tobashorpowershellin order to take advantage of a full shell language.
These two parameters, plus the password_cmd optional override, can be used to embed a small snippet that fetches the password.
This is designed to allow the most flexibility and therefore will depend on your needs.
For example, you may want to read passwords using zenity:
password_cmd = "zenity --password"
In Windows, you may want to use PowerShell to accomplish something similar:
password_loader = "powershell"
password_cmd = "(Get-Credential -Credential {name}).GetNetworkCredential().Password"
caching
Connections in code are cached by db_hooks using cachetools. This means that if you call get_engine with the same connection name twice that it will in most cases reuse an already-initialized engine, meaning that you will only be prompted for a password once.
Caching supports four cache types: LRUCache, RRCache, TTLCache and LFUCache. Each of these takes a collection of keyword args, which are passed to the corresponding contructors directly.
The default is LRUCache with a maxsize of 512. This means that all connection info is cached indefinitely until over 512 distinct engines have been created, at which the least recently used engine is evicted.
This API is relatively unstable and may change as the db_hooks system is implemented for other languages.
in your python code
Once you have a connection configured, you can load a sqlalchemy engine using
the get_engine API:
from db_hooks import get_engine
engine = get_engine('pg_example')
This will pull the connection, prompt for your password, and give you a configured sqlalchemy engine.
the cli
db_hooks exposes a CLI that will launch either psql, mysql or sqlite in a manner appropriate to the underlying connection. You can connect to this database by running db_hooks connect {your_connection_name_here}.
bash complete
db_hooks has support for shell tab-completion. You can enable it by running eval $(_DB_HOOKS_COMPLETE=source db_hooks) for bash (the default shell for most Linux distributions and older versions of OSX) or eval $(_DB_HOOKS_COMPLETE=source_zsh db_hooks) (the default shell in new versions of OSX). PowerShell is unfortunately not supported. For more information, you can read Click's docs for autocompltion.
licensing
This library is licensed under the Apache Software License. See the LICENSE and NOTICE files for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file db_hooks-0.4.0.tar.gz.
File metadata
- Download URL: db_hooks-0.4.0.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0.post20200127 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85749a1e535a76bbf9dd7fdf77efd0882ae86de89140d6c2ea23549636f27794
|
|
| MD5 |
39c34b7595c9bc662b879f6f819d1c40
|
|
| BLAKE2b-256 |
e550313b19f5aeaea73285be7c3740d43abfa336a6c53daf21dfb4f20095266f
|
File details
Details for the file db_hooks-0.4.0-py2.py3-none-any.whl.
File metadata
- Download URL: db_hooks-0.4.0-py2.py3-none-any.whl
- Upload date:
- Size: 19.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0.post20200127 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e34203f9ede6365d325627fa5842a0453fa744d06c8737568a6f36d921498624
|
|
| MD5 |
b7499a418217e0b8738a38a926c3ab2f
|
|
| BLAKE2b-256 |
ff730cb1801289ebffd81a37bbddf70b2a8f9b2cfccf6fbc3841e369c932e66b
|