flagdock 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flagdock-0.3.1/.gitignore +12 -0
- flagdock-0.3.1/LICENSE +21 -0
- flagdock-0.3.1/PKG-INFO +94 -0
- flagdock-0.3.1/README.md +75 -0
- flagdock-0.3.1/pyproject.toml +48 -0
- flagdock-0.3.1/src/flagdock/__init__.py +7 -0
- flagdock-0.3.1/src/flagdock/_usage.py +81 -0
- flagdock-0.3.1/src/flagdock/engine.py +673 -0
- flagdock-0.3.1/src/flagdock/provider.py +568 -0
- flagdock-0.3.1/src/flagdock/py.typed +0 -0
flagdock-0.3.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Flagdock
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
flagdock-0.3.1/PKG-INFO
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: flagdock
|
|
3
|
+
Version: 0.3.1
|
|
4
|
+
Summary: Flagdock feature flags for Python: an OpenFeature provider that downloads the rules once and evaluates in memory
|
|
5
|
+
Project-URL: Homepage, https://flagdock.com
|
|
6
|
+
Author-email: Flagdock <info@flagdock.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: feature-flags,feature-toggles,flagdock,openfeature,openfeature-provider
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Typing :: Typed
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Requires-Dist: openfeature-sdk<1,>=0.8.0
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# flagdock
|
|
21
|
+
|
|
22
|
+
[Flagdock](https://flagdock.com) feature flags for Python services.
|
|
23
|
+
|
|
24
|
+
It is a provider for the [OpenFeature](https://openfeature.dev) Python SDK. It downloads your
|
|
25
|
+
environment's rules once, answers every flag check from memory with no network call, follows
|
|
26
|
+
changes over a live stream, and keeps serving the last rules it had if Flagdock can't be reached.
|
|
27
|
+
It gives exactly the same answers as the Node, Go and browser SDKs: the same tests run in all of them.
|
|
28
|
+
|
|
29
|
+
The only dependency is `openfeature-sdk`; HTTP uses the standard library. Python 3.10 or newer.
|
|
30
|
+
|
|
31
|
+
## Quickstart
|
|
32
|
+
|
|
33
|
+
1. Install:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
pip install flagdock
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
2. Create a **server** SDK key in the Flagdock console (SDK keys page) and put it in your app's
|
|
40
|
+
environment as `FLAGDOCK_KEY`.
|
|
41
|
+
|
|
42
|
+
3. Check a flag:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
import os
|
|
46
|
+
from openfeature import api
|
|
47
|
+
from openfeature.evaluation_context import EvaluationContext
|
|
48
|
+
from flagdock import FlagdockProvider
|
|
49
|
+
|
|
50
|
+
api.set_provider_and_wait(FlagdockProvider(os.environ["FLAGDOCK_KEY"], app_name="checkout-api"))
|
|
51
|
+
flags = api.get_client()
|
|
52
|
+
|
|
53
|
+
if flags.get_boolean_value("new-checkout", False, EvaluationContext(user.id, {"plan": user.plan})):
|
|
54
|
+
... # the new code path
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Call `api.shutdown()` when the process stops, so the last usage counts are sent.
|
|
58
|
+
|
|
59
|
+
Server keys can read your targeting rules, so keep them on the server.
|
|
60
|
+
|
|
61
|
+
## Options
|
|
62
|
+
|
|
63
|
+
| Option | Default | What it does |
|
|
64
|
+
| --- | --- | --- |
|
|
65
|
+
| `key` | (required) | A server SDK key (`hbf_srv_…`) for one environment. |
|
|
66
|
+
| `app_name` | `python-app` | Name shown under "Connected apps" in the console. |
|
|
67
|
+
| `snapshot_file` | | Keeps the last rules on disk (mode 600), so a restart during an outage still has flags. |
|
|
68
|
+
| `bootstrap_file` | | A ruleset JSON file to start from when nothing else is available. |
|
|
69
|
+
| `poll_interval` | `15` | Seconds between checks for changes while the live stream is down. |
|
|
70
|
+
| `stale_after` | `300` | Seconds without a sync before the provider reports itself stale. |
|
|
71
|
+
| `start_timeout` | `10` | Seconds `initialize` waits for the first download. |
|
|
72
|
+
| `usage_interval` | `60` | Seconds between usage reports; `0` turns reporting off. |
|
|
73
|
+
| `stream` | `True` | Follow changes live; `False` only polls. |
|
|
74
|
+
| `url`, `fallback_urls` | Flagdock edge, then control plane | Where the rules come from. |
|
|
75
|
+
|
|
76
|
+
Number flags can be read with `get_integer_value` (whole numbers only) or `get_float_value`, and
|
|
77
|
+
JSON flags with `get_object_value`. While Flagdock can't be reached, answers carry the reason
|
|
78
|
+
`STALE` and the provider emits `PROVIDER_STALE`, then `PROVIDER_READY` when it is back.
|
|
79
|
+
|
|
80
|
+
Date conditions read ISO 8601 text (`2026-12-01`, `2026-12-01T10:00:00Z`; no offset means UTC),
|
|
81
|
+
epoch milliseconds, `datetime` and `date` values.
|
|
82
|
+
|
|
83
|
+
## More
|
|
84
|
+
|
|
85
|
+
- `provider.answers_for(context)` returns every flag's answer for one person (answers only, no
|
|
86
|
+
rules). Put it in the page as JSON and pass it as `bootstrap` to `@flagdock/web` or
|
|
87
|
+
`@flagdock/react`, so the browser shows the right values in its first render.
|
|
88
|
+
- `provider.rotate_key(new_key)` switches keys without a restart.
|
|
89
|
+
- `client.track("order-completed", EvaluationContext(user.id), TrackingEventDetails(value=order.total))`
|
|
90
|
+
reports a goal event, which the console uses to compare the values of a flag.
|
|
91
|
+
- The provider is thread safe; with forking servers (gunicorn, uWSGI) create it in each worker
|
|
92
|
+
after the fork.
|
|
93
|
+
|
|
94
|
+
MIT licensed. Questions: info@flagdock.com
|
flagdock-0.3.1/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# flagdock
|
|
2
|
+
|
|
3
|
+
[Flagdock](https://flagdock.com) feature flags for Python services.
|
|
4
|
+
|
|
5
|
+
It is a provider for the [OpenFeature](https://openfeature.dev) Python SDK. It downloads your
|
|
6
|
+
environment's rules once, answers every flag check from memory with no network call, follows
|
|
7
|
+
changes over a live stream, and keeps serving the last rules it had if Flagdock can't be reached.
|
|
8
|
+
It gives exactly the same answers as the Node, Go and browser SDKs: the same tests run in all of them.
|
|
9
|
+
|
|
10
|
+
The only dependency is `openfeature-sdk`; HTTP uses the standard library. Python 3.10 or newer.
|
|
11
|
+
|
|
12
|
+
## Quickstart
|
|
13
|
+
|
|
14
|
+
1. Install:
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
pip install flagdock
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
2. Create a **server** SDK key in the Flagdock console (SDK keys page) and put it in your app's
|
|
21
|
+
environment as `FLAGDOCK_KEY`.
|
|
22
|
+
|
|
23
|
+
3. Check a flag:
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
import os
|
|
27
|
+
from openfeature import api
|
|
28
|
+
from openfeature.evaluation_context import EvaluationContext
|
|
29
|
+
from flagdock import FlagdockProvider
|
|
30
|
+
|
|
31
|
+
api.set_provider_and_wait(FlagdockProvider(os.environ["FLAGDOCK_KEY"], app_name="checkout-api"))
|
|
32
|
+
flags = api.get_client()
|
|
33
|
+
|
|
34
|
+
if flags.get_boolean_value("new-checkout", False, EvaluationContext(user.id, {"plan": user.plan})):
|
|
35
|
+
... # the new code path
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Call `api.shutdown()` when the process stops, so the last usage counts are sent.
|
|
39
|
+
|
|
40
|
+
Server keys can read your targeting rules, so keep them on the server.
|
|
41
|
+
|
|
42
|
+
## Options
|
|
43
|
+
|
|
44
|
+
| Option | Default | What it does |
|
|
45
|
+
| --- | --- | --- |
|
|
46
|
+
| `key` | (required) | A server SDK key (`hbf_srv_…`) for one environment. |
|
|
47
|
+
| `app_name` | `python-app` | Name shown under "Connected apps" in the console. |
|
|
48
|
+
| `snapshot_file` | | Keeps the last rules on disk (mode 600), so a restart during an outage still has flags. |
|
|
49
|
+
| `bootstrap_file` | | A ruleset JSON file to start from when nothing else is available. |
|
|
50
|
+
| `poll_interval` | `15` | Seconds between checks for changes while the live stream is down. |
|
|
51
|
+
| `stale_after` | `300` | Seconds without a sync before the provider reports itself stale. |
|
|
52
|
+
| `start_timeout` | `10` | Seconds `initialize` waits for the first download. |
|
|
53
|
+
| `usage_interval` | `60` | Seconds between usage reports; `0` turns reporting off. |
|
|
54
|
+
| `stream` | `True` | Follow changes live; `False` only polls. |
|
|
55
|
+
| `url`, `fallback_urls` | Flagdock edge, then control plane | Where the rules come from. |
|
|
56
|
+
|
|
57
|
+
Number flags can be read with `get_integer_value` (whole numbers only) or `get_float_value`, and
|
|
58
|
+
JSON flags with `get_object_value`. While Flagdock can't be reached, answers carry the reason
|
|
59
|
+
`STALE` and the provider emits `PROVIDER_STALE`, then `PROVIDER_READY` when it is back.
|
|
60
|
+
|
|
61
|
+
Date conditions read ISO 8601 text (`2026-12-01`, `2026-12-01T10:00:00Z`; no offset means UTC),
|
|
62
|
+
epoch milliseconds, `datetime` and `date` values.
|
|
63
|
+
|
|
64
|
+
## More
|
|
65
|
+
|
|
66
|
+
- `provider.answers_for(context)` returns every flag's answer for one person (answers only, no
|
|
67
|
+
rules). Put it in the page as JSON and pass it as `bootstrap` to `@flagdock/web` or
|
|
68
|
+
`@flagdock/react`, so the browser shows the right values in its first render.
|
|
69
|
+
- `provider.rotate_key(new_key)` switches keys without a restart.
|
|
70
|
+
- `client.track("order-completed", EvaluationContext(user.id), TrackingEventDetails(value=order.total))`
|
|
71
|
+
reports a goal event, which the console uses to compare the values of a flag.
|
|
72
|
+
- The provider is thread safe; with forking servers (gunicorn, uWSGI) create it in each worker
|
|
73
|
+
after the fork.
|
|
74
|
+
|
|
75
|
+
MIT licensed. Questions: info@flagdock.com
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "flagdock"
|
|
7
|
+
version = "0.3.1"
|
|
8
|
+
description = "Flagdock feature flags for Python: an OpenFeature provider that downloads the rules once and evaluates in memory"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
authors = [{ name = "Flagdock", email = "info@flagdock.com" }]
|
|
13
|
+
requires-python = ">=3.10"
|
|
14
|
+
dependencies = ["openfeature-sdk>=0.8.0,<1"]
|
|
15
|
+
keywords = ["feature-flags", "feature-toggles", "openfeature", "openfeature-provider", "flagdock"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
22
|
+
"Typing :: Typed",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://flagdock.com"
|
|
27
|
+
|
|
28
|
+
[tool.hatch.build.targets.wheel]
|
|
29
|
+
packages = ["src/flagdock"]
|
|
30
|
+
|
|
31
|
+
[tool.hatch.build.targets.sdist]
|
|
32
|
+
include = ["src/flagdock", "README.md", "LICENSE"]
|
|
33
|
+
|
|
34
|
+
[tool.pytest.ini_options]
|
|
35
|
+
testpaths = ["tests"]
|
|
36
|
+
pythonpath = ["src", "tests"]
|
|
37
|
+
|
|
38
|
+
[tool.ruff]
|
|
39
|
+
line-length = 140
|
|
40
|
+
target-version = "py310"
|
|
41
|
+
|
|
42
|
+
[tool.ruff.lint]
|
|
43
|
+
select = ["E", "F", "W", "B", "UP", "S", "BLE"]
|
|
44
|
+
ignore = ["S101", "E501", "UP045", "UP007"]
|
|
45
|
+
|
|
46
|
+
[tool.mypy]
|
|
47
|
+
strict = true
|
|
48
|
+
files = ["src"]
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""Flagdock feature flags for Python: an OpenFeature provider that evaluates in memory."""
|
|
2
|
+
|
|
3
|
+
from .engine import Resolution, evaluate
|
|
4
|
+
from .provider import SDK_VERSION, FlagdockProvider
|
|
5
|
+
|
|
6
|
+
__all__ = ["FlagdockProvider", "Resolution", "SDK_VERSION", "evaluate"]
|
|
7
|
+
__version__ = SDK_VERSION
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Counts flag checks and goal events in memory until they are sent. Keeps no personal data beyond the person's ID."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import threading
|
|
6
|
+
from typing import Any, Optional
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class UsageCounter:
|
|
10
|
+
"""Flag key, value key and a number: nothing else."""
|
|
11
|
+
|
|
12
|
+
def __init__(self) -> None:
|
|
13
|
+
self._lock = threading.Lock()
|
|
14
|
+
self._counts: dict[tuple[str, str], dict[str, Any]] = {}
|
|
15
|
+
|
|
16
|
+
def record(self, flag: str, value_key: Optional[str]) -> None:
|
|
17
|
+
key = (flag, value_key or "")
|
|
18
|
+
with self._lock:
|
|
19
|
+
entry = self._counts.get(key)
|
|
20
|
+
if entry is not None:
|
|
21
|
+
entry["count"] += 1
|
|
22
|
+
# A cap, so a bug that checks endless made-up flag names cannot eat memory.
|
|
23
|
+
elif len(self._counts) < 5000:
|
|
24
|
+
self._counts[key] = {"flag": flag, **({"value": value_key} if value_key is not None else {}), "count": 1}
|
|
25
|
+
|
|
26
|
+
def drain(self) -> list[dict[str, Any]]:
|
|
27
|
+
with self._lock:
|
|
28
|
+
out = list(self._counts.values())
|
|
29
|
+
self._counts = {}
|
|
30
|
+
return out
|
|
31
|
+
|
|
32
|
+
def restore(self, counts: list[dict[str, Any]]) -> None:
|
|
33
|
+
with self._lock:
|
|
34
|
+
for c in counts:
|
|
35
|
+
key = (c["flag"], c.get("value") or "")
|
|
36
|
+
entry = self._counts.get(key)
|
|
37
|
+
if entry is not None:
|
|
38
|
+
entry["count"] += c["count"]
|
|
39
|
+
elif len(self._counts) < 5000:
|
|
40
|
+
self._counts[key] = dict(c)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class ActivityLog:
|
|
44
|
+
"""Who got which value, and what they did afterwards, between two reports."""
|
|
45
|
+
|
|
46
|
+
def __init__(self) -> None:
|
|
47
|
+
self._lock = threading.Lock()
|
|
48
|
+
self._seen: set[tuple[str, str, str]] = set()
|
|
49
|
+
self._exposures: list[dict[str, str]] = []
|
|
50
|
+
self._events: list[dict[str, Any]] = []
|
|
51
|
+
|
|
52
|
+
def expose(self, flag: str, value: str, user: str) -> None:
|
|
53
|
+
key = (flag, value, user)
|
|
54
|
+
with self._lock:
|
|
55
|
+
if key in self._seen:
|
|
56
|
+
return
|
|
57
|
+
# A cap on memory. Starting over only means some people are reported twice, which the server ignores.
|
|
58
|
+
if len(self._seen) >= 100_000:
|
|
59
|
+
self._seen.clear()
|
|
60
|
+
self._seen.add(key)
|
|
61
|
+
if len(self._exposures) < 10_000:
|
|
62
|
+
self._exposures.append({"flag": flag, "value": value, "user": user})
|
|
63
|
+
|
|
64
|
+
def track(self, event: str, user: str, amount: Optional[float] = None) -> None:
|
|
65
|
+
with self._lock:
|
|
66
|
+
if len(self._events) < 10_000:
|
|
67
|
+
entry: dict[str, Any] = {"event": event, "user": user}
|
|
68
|
+
if amount is not None and amount == amount and abs(amount) != float("inf"):
|
|
69
|
+
entry["amount"] = amount
|
|
70
|
+
self._events.append(entry)
|
|
71
|
+
|
|
72
|
+
def drain(self) -> tuple[list[dict[str, str]], list[dict[str, Any]]]:
|
|
73
|
+
with self._lock:
|
|
74
|
+
out = (self._exposures, self._events)
|
|
75
|
+
self._exposures, self._events = [], []
|
|
76
|
+
return out
|
|
77
|
+
|
|
78
|
+
def restore(self, exposures: list[dict[str, str]], events: list[dict[str, Any]]) -> None:
|
|
79
|
+
with self._lock:
|
|
80
|
+
self._exposures = (exposures + self._exposures)[:10_000]
|
|
81
|
+
self._events = (events + self._events)[:10_000]
|