From 49654eeede2bf2e3294542e2b0afb19a6d824f1d Mon Sep 17 00:00:00 2001 From: Andrew Brookins Date: Wed, 20 Oct 2021 14:29:31 -0700 Subject: [PATCH] Refine connection strings (dotenv), Makefile targets --- Makefile | 7 ++++++- docker-compose.yml | 2 +- poetry.lock | 17 ++++++++++++++++- pyproject.toml | 1 + redis_developer/connections.py | 25 +++++++++++++++++++++++-- redis_developer/model/model.py | 3 ++- tests/conftest.py | 1 - 7 files changed, 49 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 937438f..c7ce50b 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ help: @echo " format reformat code" @echo " test run all the tests" @echo " shell open a Poetry shell" + @echo " redis start a Redis instance with Docker" @echo "" @echo "Check the Makefile to know exactly what each target is doing." @@ -44,7 +45,7 @@ format: $(INSTALL_STAMP) .PHONY: test test: $(INSTALL_STAMP) #$(POETRY) run pytest ./tests/ --cov-report term-missing --cov-fail-under 100 --cov $(NAME) - $(POETRY) run pytest ./tests/ + $(POETRY) run pytest -s -vv ./tests/ .PHONY: shell shell: $(INSTALL_STAMP) @@ -53,3 +54,7 @@ shell: $(INSTALL_STAMP) .PHONY: redis redis: docker-compose up -d + + +.PHONY: all +all: redis $(INSTALL_STAMP) lint test \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index e719f54..87e406a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ version: "3.8" services: redis: image: "redislabs/redismod:edge" - entrypoint: ["redis-server", "--appendonly", "yes", "--loadmodule", "/usr/lib/redis/modules/rejson.so"] + entrypoint: ["redis-server", "--appendonly", "yes", "--loadmodule", "/usr/lib/redis/modules/rejson.so", "--loadmodule", "/usr/lib/redis/modules/redisearch.so"] restart: always ports: - "6380:6379" diff --git a/poetry.lock b/poetry.lock index 31f55eb..bba2c2b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -513,6 +513,17 @@ toml = "*" [package.extras] testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"] +[[package]] +name = "python-dotenv" +version = "0.19.1" +description = "Read key-value pairs from a .env file and set them as environment variables" +category = "main" +optional = false +python-versions = ">=3.5" + +[package.extras] +cli = ["click (>=5.0)"] + [[package]] name = "python-ulid" version = "1.0.3" @@ -645,7 +656,7 @@ python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "f1ccd73314f307ce41497d093ddce99cfb96ebf1814e854a94e37d5156647967" +content-hash = "1bc742aa620496b8823ab47f485c0e7a24c64ba1e54521d8485ec79d7e453016" [metadata.files] aioredis = [ @@ -883,6 +894,10 @@ pytest = [ {file = "pytest-6.2.5-py3-none-any.whl", hash = "sha256:7310f8d27bc79ced999e760ca304d69f6ba6c6649c0b60fb0e04a4a77cacc134"}, {file = "pytest-6.2.5.tar.gz", hash = "sha256:131b36680866a76e6781d13f101efb86cf674ebb9762eb70d3082b6f29889e89"}, ] +python-dotenv = [ + {file = "python-dotenv-0.19.1.tar.gz", hash = "sha256:14f8185cc8d494662683e6914addcb7e95374771e707601dfc70166946b4c4b8"}, + {file = "python_dotenv-0.19.1-py2.py3-none-any.whl", hash = "sha256:bbd3da593fc49c249397cbfbcc449cf36cb02e75afc8157fcc6a81df6fb7750a"}, +] python-ulid = [ {file = "python-ulid-1.0.3.tar.gz", hash = "sha256:5dd8b969312a40e2212cec9c1ad63f25d4b6eafd92ee3195883e0287b6e9d19e"}, {file = "python_ulid-1.0.3-py3-none-any.whl", hash = "sha256:8704dc20f547f531fe3a41d4369842d737a0f275403b909d0872e7ea0fe8d6f2"}, diff --git a/pyproject.toml b/pyproject.toml index 66c73e5..037170e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ pptree = "^3.1" types-redis = "^3.5.9" types-six = "^1.16.1" python-ulid = "^1.0.3" +python-dotenv = "^0.19.1" [tool.poetry.dev-dependencies] mypy = "^0.910" diff --git a/redis_developer/connections.py b/redis_developer/connections.py index f8ba77f..a49e8b9 100644 --- a/redis_developer/connections.py +++ b/redis_developer/connections.py @@ -1,5 +1,26 @@ +import os + +import dotenv import redis -def get_redis_connection() -> redis.Redis: - return redis.Redis(decode_responses=True) +dotenv.load_dotenv() + +URL = os.environ.get("REDIS_OM_URL", None) + + +def get_redis_connection(**kwargs) -> redis.Redis: + # If someone passed in a 'url' parameter, or specified a REDIS_OM_URL + # environment variable, we'll create the Redis client from the URL. + url = kwargs.pop("url", URL) + if not url: + import ipdb + + ipdb.set_trace() + if url: + return redis.from_url(url, **kwargs) + + # Decode from UTF-8 by default + if "decode_responses" not in kwargs: + kwargs["decode_responses"] = True + return redis.Redis(**kwargs) diff --git a/redis_developer/model/model.py b/redis_developer/model/model.py index bc4464e..08a38cd 100644 --- a/redis_developer/model/model.py +++ b/redis_developer/model/model.py @@ -37,6 +37,7 @@ from pydantic.utils import Representation from redis.client import Pipeline from ulid import ULID +from ..connections import get_redis_connection from .encoders import jsonable_encoder from .render_tree import render_tree from .token_escaper import TokenEscaper @@ -997,7 +998,7 @@ class ModelMeta(ModelMetaclass): ) if not getattr(new_class._meta, "database", None): new_class._meta.database = getattr( - base_meta, "database", redis.Redis(decode_responses=True) + base_meta, "database", get_redis_connection() ) if not getattr(new_class._meta, "primary_key_creator_cls", None): new_class._meta.primary_key_creator_cls = getattr( diff --git a/tests/conftest.py b/tests/conftest.py index 0b1b365..2e1fbd5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,7 +17,6 @@ def redis(): @pytest.fixture def key_prefix(): - # TODO yield "redis-developer"