|
3 | 3 |
|
4 | 4 | from uuid import UUID
|
5 | 5 |
|
6 |
| -import ulid |
| 6 | +try: # pragma: no cover |
| 7 | + from ulid import ULID |
| 8 | +except ImportError: # pragma: no cover |
| 9 | + err_msg = ( |
| 10 | + "The ulid package is not installed. " |
| 11 | + "Install it with `pip install jobflow[ulid]` or `pip install python-ulid`." |
| 12 | + ) |
| 13 | + |
| 14 | + class ULID: # type: ignore |
| 15 | + """Fake ULID class for raising import error.""" |
| 16 | + |
| 17 | + def __init__(self, *args, **kwargs): |
| 18 | + raise ImportError(err_msg) |
| 19 | + |
| 20 | + def from_str(self, *args, **kwargs): |
| 21 | + """Raise import error.""" |
| 22 | + raise ImportError(err_msg) |
7 | 23 |
|
8 | 24 |
|
9 | 25 | def suid(id_type: str | None = None) -> str:
|
@@ -33,7 +49,7 @@ def suid(id_type: str | None = None) -> str:
|
33 | 49 | funcs = {
|
34 | 50 | "uuid1": uuid.uuid1,
|
35 | 51 | "uuid4": uuid.uuid4,
|
36 |
| - "ulid": ulid.ULID, |
| 52 | + "ulid": ULID, |
37 | 53 | }
|
38 | 54 | if id_type not in funcs:
|
39 | 55 | raise ValueError(f"UUID type {id_type} not supported.")
|
@@ -61,7 +77,7 @@ def get_timestamp_from_uid(uid: str) -> float:
|
61 | 77 | )
|
62 | 78 | funcs = {
|
63 | 79 | "uuid1": lambda uuid: (UUID(uuid).time - 0x01B21DD213814000) / 1e7,
|
64 |
| - "ulid": lambda uuid: ulid.ULID.from_str(uuid).timestamp, |
| 80 | + "ulid": lambda uuid: ULID.from_str(uuid).timestamp, |
65 | 81 | }
|
66 | 82 | return funcs[id_type](uid)
|
67 | 83 |
|
@@ -90,7 +106,7 @@ def _get_id_type(uid: str) -> str:
|
90 | 106 | pass
|
91 | 107 |
|
92 | 108 | try:
|
93 |
| - ulid.ULID.from_str(uid) |
| 109 | + ULID.from_str(uid) |
94 | 110 | return "ulid"
|
95 | 111 | except ValueError:
|
96 | 112 | pass
|
|
0 commit comments