askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
object_ids.py · 780 bytes · 35 lines
| 1 | """Shared ID helpers for DBBASIC-created resources.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from uuid import UUID, uuid4 |
| 6 | |
| 7 | |
| 8 | def new_uuid4() -> str: |
| 9 | """Return a canonical UUIDv4 string.""" |
| 10 | return str(uuid4()) |
| 11 | |
| 12 | |
| 13 | def normalize_uuid4(value: object) -> str | None: |
| 14 | """Return a canonical UUIDv4 string, or ``None`` for non-UUIDv4 input.""" |
| 15 | if value is None: |
| 16 | return None |
| 17 | |
| 18 | text = str(value).strip() |
| 19 | if not text: |
| 20 | return None |
| 21 | |
| 22 | try: |
| 23 | parsed = UUID(text) |
| 24 | except (TypeError, ValueError, AttributeError): |
| 25 | return None |
| 26 | |
| 27 | if parsed.version != 4: |
| 28 | return None |
| 29 | |
| 30 | return str(parsed) |
| 31 | |
| 32 | |
| 33 | def is_uuid4(value: object) -> bool: |
| 34 | """Return True when ``value`` is a UUIDv4 string.""" |
| 35 | return normalize_uuid4(value) is not None |