Skip to content

Commit

Permalink
hive-common: Add hive.common.parse_uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
gbenson committed Nov 26, 2024
1 parent 6d64ee5 commit 2e16b9a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
12 changes: 4 additions & 8 deletions libs/chat/hive/chat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from datetime import datetime, timezone
from types import NoneType
from typing import Any, Optional
from uuid import RFC_4122, UUID, uuid4
from uuid import UUID, uuid4

from hive.common import parse_uuid

from .matrix import ClientEvent as MatrixEvent

Expand Down Expand Up @@ -34,13 +36,7 @@ def __post_init__(self):
if not isinstance(self.timestamp, datetime):
self.timestamp = datetime.fromisoformat(self.timestamp)

if not isinstance(self.uuid, UUID):
self.uuid = UUID(self.uuid)

if self.uuid.variant != RFC_4122:
raise ValueError(self.uuid)
if self.uuid.version != 4:
raise ValueError(self.uuid)
self.uuid = parse_uuid(self.uuid)

@classmethod
def json_keys(cls) -> list[str]:
Expand Down
1 change: 1 addition & 0 deletions libs/common/hive/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .argument_parser import HiveArgumentParser as ArgumentParser
from .config import read as read_config
from .resource import read_resource
from .uuid import parse_uuid
13 changes: 13 additions & 0 deletions libs/common/hive/common/uuid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from uuid import RFC_4122, UUID


def parse_uuid(uuid: str | UUID) -> UUID:
if not isinstance(uuid, UUID):
uuid = UUID(uuid)

if uuid.variant != RFC_4122:
raise ValueError(uuid)
if uuid.version != 4:
raise ValueError(uuid)

return uuid
8 changes: 2 additions & 6 deletions services/service-monitor/hive/service_monitor/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
from functools import cached_property
from html import escape
from typing import Optional
from uuid import RFC_4122, UUID

from pika import BasicProperties
from pika.spec import Basic

from valkey import Valkey

from hive.chat import tell_user
from hive.common import parse_uuid
from hive.messaging import Channel
from hive.service import HiveService, RestartMonitor

Expand Down Expand Up @@ -43,11 +43,7 @@ def on_service_status_event(
if report["meta"]["type"] != "service_status_report":
raise ValueError(body)

uuid = UUID(report["meta"]["uuid"])
if uuid.variant != RFC_4122:
raise ValueError(body)
if uuid.version != 4:
raise ValueError(body)
uuid = parse_uuid(report["meta"]["uuid"])

timestamp = datetime.fromisoformat(report["meta"]["timestamp"])

Expand Down

0 comments on commit 2e16b9a

Please sign in to comment.