# `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](https://pypi.org/project/appdirs/) library. Right now there isn't an obvious way to find this path, but running `db_hooks list` will bomb with a useful error message if the file isn't there. In general, try these locations:
| os | location |
|------------|---------------------------------------------------------|
| linux | `~/.local/share/db_hooks/databases.toml` |
| osx | `~/Library/Application Support/db_hooks/databases.toml` |
| windows 10 | `~\AppData\local\jfhbrook\db_hooks` |
Keys in the TOML file are connection names. Under those are key/value pairs for the various parameters in a connection. Connections support the following parameters:
* `connection` - a sqlalchemy-compatible connstring. May have a `{password}` f-string parameter.
* `password_cmd` - a bash command that will be executed in order to fetch the password, which will be applied to `connection` as an f-string.
In general, one would use `password_cmd` to write 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 have a local postgres account and want to read the password using [`zenity`](https://help.gnome.org/users/zenity/):
```toml
[pg_example]
connection = "postgresql+psycopg2://josh:{password}@localhost:5432/josh"
password_cmd = "zenity --password"
```
## in your code
Once you have a connection configured, you can load a sqlalchemy engine using
the `get_engine` API:
```py
from db_hooks import get_engine
engine = get_engine('pg_example')
```
This will pull the connection, run your `password_cmd` and give you a configured sqlalchemy engine.
Note that `get_engine` is cached with a `cachetools.TTLCache` configured with a ttl of 10 minutes.
## the cli
`db_hooks` exposes a CLI that will launch either `psql` or `mysql` in a manner appropriate to the underlying connection. You can run `db_hooks --help` for hopefully useful help output.
## licensing
This library is licensed under the Apache Software License. See the LICENSE and NOTICE files for details.
PyPI 官网下载 | db_hooks-0.2.0.tar.gz
版权申诉
66 浏览量
2022-02-10
07:18:12
上传
评论
收藏 8KB GZ 举报
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
最新资源
- 演讲稿.txt
- 基于Python的爬虫案例-软科中国大学TOP200
- 碳排放权交易明细数据(2024年5月更新).xlsx
- 特殊文件属性命令chattr和lsattr
- HTML、CSS 和 JavaScript动态、交互式的网页 .txt
- b0cd8f9b23d4e5e381b6a8fd8ee0e907.JPG
- ff45d61c5900e45634cf4cac6cff61a1.JPG
- springboot.springboot.springboot.springboot.txt
- linux-进程与服务管理
- 毕业设计基于Django+MySQL+Redis实现简单的天气预报系统python源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈