-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
499 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
|
||
from dunamai import Style, Version | ||
|
||
version = Version.from_git().serialize(style=Style.SemVer) | ||
|
||
with open("distributed_lock/__init__.py") as f: | ||
c = f.read() | ||
|
||
lines = [] | ||
for line in c.splitlines(): | ||
if line.startswith("VERSION = "): | ||
lines.append(f'VERSION = "{version}"') | ||
else: | ||
lines.append(line) | ||
|
||
with open("distributed_lock/__init__.py", "w") as g: | ||
g.write("\n".join(lines)) | ||
|
||
print(f"Setting version={version}") | ||
os.system(f"poetry version {version}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
from __future__ import annotations | ||
|
||
from distributed_lock.const import DEFAULT_CLUSTER, DEFAULT_LIFETIME, DEFAULT_WAIT | ||
from distributed_lock.common import AcquiredRessource | ||
from distributed_lock.const import ( | ||
DEFAULT_CLUSTER, | ||
DEFAULT_LIFETIME, | ||
DEFAULT_SERVER_SIDE_WAIT, | ||
) | ||
from distributed_lock.exception import ( | ||
BadConfigurationError, | ||
DistributedLockError, | ||
DistributedLockException, | ||
NotAcquiredError, | ||
NotAcquiredException, | ||
NotReleasedError, | ||
NotReleasedException, | ||
) | ||
from distributed_lock.sync import AcquiredRessource, DistributedLockClient | ||
from distributed_lock.sync import DistributedLockClient | ||
|
||
__all__ = [ | ||
"DEFAULT_CLUSTER", | ||
"DEFAULT_LIFETIME", | ||
"DEFAULT_WAIT", | ||
"DEFAULT_SERVER_SIDE_WAIT", | ||
"AcquiredRessource", | ||
"DistributedLockClient", | ||
"DistributedlockException", | ||
"NotAcquiredError", | ||
"NotReleasedException", | ||
"NotReleasedError", | ||
"NotAcquiredException", | ||
"BadConfigurationError", | ||
"DistributedLockError", | ||
"DistributedLockException", | ||
] | ||
|
||
__pdoc__ = {"sync": False, "exception": False, "common": False} | ||
|
||
VERSION = "v0.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from __future__ import annotations | ||
|
||
import datetime | ||
import os | ||
from dataclasses import asdict, dataclass, field | ||
from typing import Any | ||
|
||
from distributed_lock.const import DEFAULT_CLUSTER | ||
from distributed_lock.exception import BadConfigurationError, DistributedLockError | ||
|
||
|
||
@dataclass | ||
class AcquiredRessource: | ||
"""This dataclass holds an acquired ressource.""" | ||
|
||
resource: str | ||
"""The resource name.""" | ||
|
||
lock_id: str | ||
"""The lock unique identifier (you will need it to unlock).""" | ||
|
||
tenant_id: str | ||
"""The tenant identifier.""" | ||
|
||
created: datetime.datetime = field(default_factory=datetime.datetime.utcnow) | ||
"""The lock creation datetime.""" | ||
|
||
expires: datetime.datetime = field(default_factory=datetime.datetime.utcnow) | ||
"""The lock expiration datetime.""" | ||
|
||
user_agent: str = "" | ||
"""Your user-agent (warning: not supported in all plans).""" | ||
|
||
user_data: Any = "" | ||
"""User data stored with the lock (warning: not supported in all plans).""" | ||
|
||
@classmethod | ||
def from_dict(cls, d: dict) -> AcquiredRessource: | ||
"""Create an AcquiredRessource from a dict.""" | ||
for f in ( | ||
"lock_id", | ||
"resource", | ||
"tenant_id", | ||
"created", | ||
"expires", | ||
"user_agent", | ||
"user_data", | ||
): | ||
if f not in d: | ||
raise DistributedLockError(f"bad reply from service, missing {f}") | ||
d2 = dict(d) | ||
for f in ("created", "expires"): | ||
if isinstance(d2[f], str): | ||
d2[f] = datetime.datetime.fromisoformat(d2[f]) | ||
return cls(**d2) | ||
|
||
def to_dict(self) -> dict: | ||
"""Convert an AcquiredRessource to a dict.""" | ||
d = asdict(self) | ||
for f in ("created", "expires"): | ||
d[f] = d[f].isoformat()[0:19] + "Z" | ||
return d | ||
|
||
|
||
def get_cluster() -> str: | ||
"""Get the target cluster from env or from default.""" | ||
if os.environ.get("DLOCK_CLUSTER"): | ||
return os.environ["DLOCK_CLUSTER"].lower().strip() | ||
return DEFAULT_CLUSTER | ||
|
||
|
||
def get_token() -> str: | ||
"""Get the service token from env (raise an exception if not set). | ||
Raises: | ||
BadConfigurationError: if the token is not set. | ||
""" | ||
if os.environ.get("DLOCK_TOKEN"): | ||
return os.environ["DLOCK_TOKEN"].lower().strip() | ||
raise BadConfigurationError("You must provide a token (or set DLOCK_TOKEN env var)") | ||
|
||
|
||
def get_tenant_id() -> str: | ||
"""Get the "tenant id" from env (raise an exception if not set). | ||
Raises: | ||
BadConfigurationError: if the "tenant id" is not set. | ||
""" | ||
if os.environ.get("DLOCK_TENANT_ID"): | ||
return os.environ["DLOCK_TENANT_ID"].lower().strip() | ||
raise BadConfigurationError( | ||
"You must provide a tenant_id (or set DLOCK_TENANT_ID env var)" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
from __future__ import annotations | ||
|
||
DEFAULT_CLUSTER = "europe-free" | ||
"""Default cluster to request.""" | ||
|
||
DEFAULT_LIFETIME = 3600 | ||
DEFAULT_WAIT = 10 | ||
"""Default lock lifetime (in seconds).""" | ||
|
||
DEFAULT_SERVER_SIDE_WAIT = 60 | ||
"""Default server side wait (in seconds).""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.