Skip to content

Commit

Permalink
storage: namespacing fixes (#13)
Browse files Browse the repository at this point in the history
* storage: pluginstorage layer namespacing fix

`PluginStorage` abstraction layer should not re-namespace a key
if it is already namespaced. this is to support using keys directly from
`find_keys` with `PluginStorage.get`, et al.

* tests: update expect mock; only test on py38

* tests: create_redis_pool should return redis_client fixture

* storage: support str and bytes in key namespacing
  • Loading branch information
pirogoeth authored Sep 11, 2020
1 parent 1950b1e commit ebb24fd
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 61 deletions.
12 changes: 1 addition & 11 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ workflows:
workflow:
jobs:
- lint-flake8
- test-python37
- test-python38

commands:
Expand All @@ -14,7 +13,7 @@ commands:
parameters:
env:
type: "string"
default: "py37"
default: "py38"
steps:
- restore_cache:
keys:
Expand Down Expand Up @@ -53,15 +52,6 @@ executors:
PYTHON_VERSION: "<< parameters.version >>"

jobs:
test-python37:
executor:
name: "python"
version: "3.7"
steps:
- checkout
- tox:
env: py37

test-python38:
executor:
name: "python"
Expand Down
24 changes: 14 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
default_language_version:
python: python3.7
python: python3.8

fail_fast: true

repos:
- repo: https://github.com/ambv/black
rev: 19.3b0
rev: 19.10b0
hooks:
- id: black

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.3
rev: v3.2.0
hooks:
- id: check-added-large-files
- id: check-ast
Expand All @@ -23,11 +23,6 @@ repos:
- id: debug-statements
- id: end-of-file-fixer
- id: fix-encoding-pragma
- id: flake8
args:
- --exclude=docs/*,tests/*
- --max-line-length=131
- --ignore=E203
- id: forbid-new-submodules
- id: mixed-line-ending
args:
Expand All @@ -38,11 +33,20 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.1.6
rev: v1.1.9
hooks:
- id: remove-tabs

- repo: https://github.com/Lucas-C/pre-commit-hooks-safety
rev: v1.1.0
rev: v1.1.3
hooks:
- id: python-safety-dependencies-check

- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.3
hooks:
- id: flake8
args:
- --exclude=docs/*,tests/*
- --max-line-length=131
- --ignore=E203
2 changes: 1 addition & 1 deletion machine/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
__description__ = "A sexy, simple, yet powerful and extendable Slack bot"
__uri__ = "https://github.com/DandyDev/slack-machine"

__version_info__ = (0, 21, 1)
__version_info__ = (0, 21, 2)
__version__ = ".".join(map(str, __version_info__))

__author__ = "Daan Debie"
Expand Down
11 changes: 10 additions & 1 deletion machine/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ def __init__(self, fq_plugin_name):
self._fq_plugin_name = fq_plugin_name

def _gen_unique_key(self, key):
return "{}:{}".format(self._fq_plugin_name, key)
separator = ":"
namespace = self._fq_plugin_name
if isinstance(key, bytes):
separator = separator.encode("utf-8")
namespace = namespace.encode("utf-8")

if key.startswith(namespace):
return key

return namespace + separator + key

def _namespace_key(self, key, shared):
return key if shared else self._gen_unique_key(key)
Expand Down
10 changes: 8 additions & 2 deletions machine/storage/backends/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ def _ensure_connected(self):
raise NotConnectedError()

def _prefix(self, key):
if key.startswith(self._key_prefix):
separator = ":"
prefix = self._key_prefix
if isinstance(key, bytes):
separator = separator.encode("utf-8")
prefix = prefix.encode("utf-8")

if key.startswith(prefix):
return key

return "{}:{}".format(self._key_prefix, key)
return prefix + separator + key

async def has(self, key):
self._ensure_connected()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def run(self):
"coverage",
],
install_requires=dependencies,
python_requires="~=3.3",
python_requires="~=3.8",
extras_require={"redis": ["aioredis"]},
classifiers=[
"Development Status :: 3 - Alpha",
Expand Down
Loading

0 comments on commit ebb24fd

Please sign in to comment.