Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sed-i committed Dec 17, 2024
1 parent 06e3944 commit 226030f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/cosl/grafana_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import json
import logging
import lzma
from typing import Any, Dict, Union
import warnings
from typing import Any, Dict, Tuple, Union

logger = logging.getLogger(__name__)

Expand All @@ -23,11 +23,17 @@ class GrafanaDashboard(str):

@staticmethod
def _serialize(raw_json: Union[str, bytes]) -> "GrafanaDashboard":
warnings.warn("GrafanaDashboard._serialize is deprecated; use LZMABase64.compress(json.dumps(...)) instead.", category=DeprecationWarning)
warnings.warn(
"GrafanaDashboard._serialize is deprecated; use LZMABase64.compress(json.dumps(...)) instead.",
category=DeprecationWarning,
)
return GrafanaDashboard(LZMABase64.compress(raw_json))

def _deserialize(self) -> Dict[str, Any]:
warnings.warn("GrafanaDashboard._deserialize is deprecated; use json.loads(LZMABase64.decompress(...)) instead.", category=DeprecationWarning)
warnings.warn(
"GrafanaDashboard._deserialize is deprecated; use json.loads(LZMABase64.decompress(...)) instead.",
category=DeprecationWarning,
)
try:
return json.loads(LZMABase64.decompress(self))
except json.decoder.JSONDecodeError as e:
Expand Down Expand Up @@ -58,7 +64,7 @@ def decompress(cls, compressed: str) -> str:
return lzma.decompress(base64.b64decode(compressed.encode("utf-8"))).decode()


def _hash(components: tuple, length: int) -> str:
def _hash(components: Tuple[str, ...], length: int) -> str:
return hashlib.shake_256("-".join(components).encode("utf-8")).hexdigest(length)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_grafana_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TestGenerateUID(unittest.TestCase):
"""Spec for the UID generation logic."""

def test_uid_length_is_40(self):
self.assertEqual(40, len(generate_dashboard_uid("whatever")))
self.assertEqual(40, len(generate_dashboard_uid("my-charm", "my-dash.json")))

def test_collisions(self):
"""A very naive and primitive collision check that is meant to catch trivial errors."""
Expand All @@ -41,6 +41,6 @@ def test_collisions(self):
)

self.assertNotEqual(
generate_dashboard_uid("some-charm"),
generate_dashboard_uid("some-charm", "dashboard.json"),
generate_dashboard_uid("diff-charm", "dashboard.json"),
)

0 comments on commit 226030f

Please sign in to comment.