Skip to content

Commit

Permalink
feat: get postgresql uri
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon committed Sep 11, 2024
1 parent f13e145 commit da4b834
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 13 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ from bio_data_to_db.bindingdb.fix_tables import fix_assay_table
fix_assay_table("mysql://username:password@localhost/bind")
```


### PostgreSQL Helpers (CLI)

Get connection string (aka. URI) from the environment variables or `.env` file.

This is useful for the above `bio-data-to-db` commands that usually require a connection string.

You need to set:
- POSTGRESQL_HOST (default: localhost)
- POSTGRESQL_PORT (default: 5432)
- POSTGRESQL_USER
- POSTGRESQL_PASSWORD

```console
$ bio-data-to-db postgresql get-uri-from-env
postgresql://username:password@localhost:5432

$ bio-data-to-db postgresql get-uri-from-env uniprot
postgresql://username:password@localhost:5432/uniprot

$ export POSTGRESQL_PASSWORD='!@#$%^&*()'
$ bio-data-to-db postgresql get-uri-from-env uniprot
postgresql://username:%21%40%23%24%25%5E%26%2A%28%29@localhost:5432/uniprot
```


### PostgreSQL Helpers, SMILES, Polars utils and more

This package also provides some useful functions to work with PostgreSQL, SMILES, Polars and more.
Expand Down
2 changes: 1 addition & 1 deletion deps/lock/aarch64-apple-darwin/.requirements.in.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b2aeb764caf50f816850626567cb58bbe65e02c45df54e8d56b7f9e5702148c4 requirements.in
0f0639ee4488d0173fcd2fe6da6a991d614525066cbf04ea0932c593fed75d99 requirements.in
4 changes: 3 additions & 1 deletion deps/lock/aarch64-apple-darwin/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in -o /home/runner/work/bio-data-to-db/bio-data-to-db/deps/lock/aarch64-apple-darwin/requirements.txt --python-platform aarch64-apple-darwin --python-version 3.10
# uv pip compile requirements.in -o /Users/kiyoon/project/bio-data-to-db/deps/lock/aarch64-apple-darwin/requirements.txt --python-platform aarch64-apple-darwin --python-version 3.10
click==8.1.7
# via typer
connectorx==0.3.3
Expand Down Expand Up @@ -31,6 +31,8 @@ pygments==2.18.0
# via rich
python-dateutil==2.9.0.post0
# via pandas
python-dotenv==1.0.1
# via -r requirements.in
pytz==2024.1
# via pandas
rdkit==2024.3.3
Expand Down
4 changes: 3 additions & 1 deletion deps/lock/aarch64-apple-darwin/requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements_dev.in -o /home/runner/work/bio-data-to-db/bio-data-to-db/deps/lock/aarch64-apple-darwin/requirements_dev.txt --python-platform aarch64-apple-darwin --python-version 3.10
# uv pip compile requirements_dev.in -o /Users/kiyoon/project/bio-data-to-db/deps/lock/aarch64-apple-darwin/requirements_dev.txt --python-platform aarch64-apple-darwin --python-version 3.10
certifi==2024.7.4
# via requests
charset-normalizer==3.3.2
Expand Down Expand Up @@ -62,6 +62,8 @@ pytest==8.2.2
# via -r requirements_dev.in
python-dateutil==2.9.0.post0
# via pandas
python-dotenv==1.0.1
# via -r requirements.in
pytz==2024.1
# via pandas
pyyaml==6.0.1
Expand Down
2 changes: 1 addition & 1 deletion deps/lock/x86_64-apple-darwin/.requirements.in.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b2aeb764caf50f816850626567cb58bbe65e02c45df54e8d56b7f9e5702148c4 requirements.in
0f0639ee4488d0173fcd2fe6da6a991d614525066cbf04ea0932c593fed75d99 requirements.in
4 changes: 3 additions & 1 deletion deps/lock/x86_64-apple-darwin/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in -o /home/runner/work/bio-data-to-db/bio-data-to-db/deps/lock/x86_64-apple-darwin/requirements.txt --python-platform x86_64-apple-darwin --python-version 3.10
# uv pip compile requirements.in -o /Users/kiyoon/project/bio-data-to-db/deps/lock/x86_64-apple-darwin/requirements.txt --python-platform x86_64-apple-darwin --python-version 3.10
click==8.1.7
# via typer
connectorx==0.3.3
Expand Down Expand Up @@ -33,6 +33,8 @@ pygments==2.18.0
# via rich
python-dateutil==2.9.0.post0
# via pandas
python-dotenv==1.0.1
# via -r requirements.in
pytz==2024.1
# via pandas
rdkit==2024.3.3
Expand Down
4 changes: 3 additions & 1 deletion deps/lock/x86_64-apple-darwin/requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements_dev.in -o /home/runner/work/bio-data-to-db/bio-data-to-db/deps/lock/x86_64-apple-darwin/requirements_dev.txt --python-platform x86_64-apple-darwin --python-version 3.10
# uv pip compile requirements_dev.in -o /Users/kiyoon/project/bio-data-to-db/deps/lock/x86_64-apple-darwin/requirements_dev.txt --python-platform x86_64-apple-darwin --python-version 3.10
certifi==2024.7.4
# via requests
charset-normalizer==3.3.2
Expand Down Expand Up @@ -64,6 +64,8 @@ pytest==8.2.2
# via -r requirements_dev.in
python-dateutil==2.9.0.post0
# via pandas
python-dotenv==1.0.1
# via -r requirements.in
pytz==2024.1
# via pandas
pyyaml==6.0.1
Expand Down
2 changes: 1 addition & 1 deletion deps/lock/x86_64-manylinux_2_28/.requirements.in.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b2aeb764caf50f816850626567cb58bbe65e02c45df54e8d56b7f9e5702148c4 requirements.in
0f0639ee4488d0173fcd2fe6da6a991d614525066cbf04ea0932c593fed75d99 requirements.in
4 changes: 3 additions & 1 deletion deps/lock/x86_64-manylinux_2_28/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in -o /home/runner/work/bio-data-to-db/bio-data-to-db/deps/lock/x86_64-manylinux_2_28/requirements.txt --python-platform x86_64-manylinux_2_28 --python-version 3.10
# uv pip compile requirements.in -o /Users/kiyoon/project/bio-data-to-db/deps/lock/x86_64-manylinux_2_28/requirements.txt --python-platform x86_64-manylinux_2_28 --python-version 3.10
click==8.1.7
# via typer
connectorx==0.3.3
Expand Down Expand Up @@ -33,6 +33,8 @@ pygments==2.18.0
# via rich
python-dateutil==2.9.0.post0
# via pandas
python-dotenv==1.0.1
# via -r requirements.in
pytz==2024.1
# via pandas
rdkit==2024.3.3
Expand Down
4 changes: 3 additions & 1 deletion deps/lock/x86_64-manylinux_2_28/requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements_dev.in -o /home/runner/work/bio-data-to-db/bio-data-to-db/deps/lock/x86_64-manylinux_2_28/requirements_dev.txt --python-platform x86_64-manylinux_2_28 --python-version 3.10
# uv pip compile requirements_dev.in -o /Users/kiyoon/project/bio-data-to-db/deps/lock/x86_64-manylinux_2_28/requirements_dev.txt --python-platform x86_64-manylinux_2_28 --python-version 3.10
certifi==2024.7.4
# via requests
charset-normalizer==3.3.2
Expand Down Expand Up @@ -64,6 +64,8 @@ pytest==8.2.2
# via -r requirements_dev.in
python-dateutil==2.9.0.post0
# via pandas
python-dotenv==1.0.1
# via -r requirements.in
pytz==2024.1
# via pandas
pyyaml==6.0.1
Expand Down
2 changes: 1 addition & 1 deletion deps/lock/x86_64-pc-windows-msvc/.requirements.in.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b2aeb764caf50f816850626567cb58bbe65e02c45df54e8d56b7f9e5702148c4 requirements.in
0f0639ee4488d0173fcd2fe6da6a991d614525066cbf04ea0932c593fed75d99 requirements.in
4 changes: 3 additions & 1 deletion deps/lock/x86_64-pc-windows-msvc/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in -o /home/runner/work/bio-data-to-db/bio-data-to-db/deps/lock/x86_64-pc-windows-msvc/requirements.txt --python-platform x86_64-pc-windows-msvc --python-version 3.10
# uv pip compile requirements.in -o /Users/kiyoon/project/bio-data-to-db/deps/lock/x86_64-pc-windows-msvc/requirements.txt --python-platform x86_64-pc-windows-msvc --python-version 3.10
click==8.1.7
# via typer
colorama==0.4.6
Expand Down Expand Up @@ -37,6 +37,8 @@ pygments==2.18.0
# via rich
python-dateutil==2.9.0.post0
# via pandas
python-dotenv==1.0.1
# via -r requirements.in
pytz==2024.1
# via pandas
rdkit==2024.3.3
Expand Down
4 changes: 3 additions & 1 deletion deps/lock/x86_64-pc-windows-msvc/requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements_dev.in -o /home/runner/work/bio-data-to-db/bio-data-to-db/deps/lock/x86_64-pc-windows-msvc/requirements_dev.txt --python-platform x86_64-pc-windows-msvc --python-version 3.10
# uv pip compile requirements_dev.in -o /Users/kiyoon/project/bio-data-to-db/deps/lock/x86_64-pc-windows-msvc/requirements_dev.txt --python-platform x86_64-pc-windows-msvc --python-version 3.10
certifi==2024.7.4
# via requests
charset-normalizer==3.3.2
Expand Down Expand Up @@ -69,6 +69,8 @@ pytest==8.2.2
# via -r requirements_dev.in
python-dateutil==2.9.0.post0
# via pandas
python-dotenv==1.0.1
# via -r requirements.in
pytz==2024.1
# via pandas
pyyaml==6.0.1
Expand Down
1 change: 1 addition & 0 deletions deps/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ mysqlclient>=2.2.0
connectorx>=0.3.0
rdkit>=2024.3.1
tqdm>=4.62.0
python-dotenv>=1.0.1
3 changes: 2 additions & 1 deletion src/bio_data_to_db/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import typer

from . import bindingdb, uniprot
from . import bindingdb, postgresql, uniprot

app = typer.Typer(no_args_is_help=True)

app.add_typer(uniprot.app, name="uniprot")
app.add_typer(bindingdb.app, name="bindingdb")
app.add_typer(postgresql.app, name="postgresql")


def main():
Expand Down
62 changes: 62 additions & 0 deletions src/bio_data_to_db/cli/postgresql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# ruff: noqa: T201
import os
import urllib.parse
from typing import Annotated

import typer

app = typer.Typer(no_args_is_help=True)


@app.command()
def get_uri_from_env(
db_name: Annotated[str | None, typer.Argument(..., help="Database name")] = None,
):
"""
Get connection URI from environment variables for .env file.
The following environment variables are required:
- POSTGRESQL_HOST (default: localhost)
- POSTGRESQL_PORT (default: 5432)
- POSTGRESQL_USER
- POSTGRESQL_PASSWORD
"""
from dotenv import find_dotenv, load_dotenv

dotenv_path = find_dotenv(usecwd=True)
if dotenv_path != "":
load_dotenv(dotenv_path)

host = os.getenv("POSTGRESQL_HOST")
if host is None:
host = "localhost"
port = os.getenv("POSTGRESQL_PORT")
if port is None:
port = "5432"

user = os.getenv("POSTGRESQL_USER")
if user is None:
print("❌ POSTGRESQL_USER is required.")
typer.Exit(code=1)
return
password = os.getenv("POSTGRESQL_PASSWORD")
if password is None:
print("❌ POSTGRESQL_PASSWORD is required.")
typer.Exit(code=1)
return

if db_name is not None:
print(
f"postgresql://{user}:{urllib.parse.quote_plus(password)}@{host}:{port}/{db_name}"
)
else:
print(f"postgresql://{user}:{urllib.parse.quote_plus(password)}@{host}:{port}")


def main():
app()


if __name__ == "__main__":
main()

0 comments on commit da4b834

Please sign in to comment.