Globally unique identifier structure.
| 17 | |
| 18 | |
| 19 | class GUID(Structure): |
| 20 | """Globally unique identifier structure.""" |
| 21 | |
| 22 | _fields_ = [("Data1", DWORD), ("Data2", WORD), ("Data3", WORD), ("Data4", BYTE * 8)] |
| 23 | |
| 24 | def __init__(self, name=None): |
| 25 | if name is not None: |
| 26 | _CLSIDFromString(str(name), byref(self)) |
| 27 | |
| 28 | def __repr__(self): |
| 29 | return f'GUID("{str(self)}")' |
| 30 | |
| 31 | def __str__(self) -> str: |
| 32 | p = c_wchar_p() |
| 33 | _StringFromCLSID(byref(self), byref(p)) |
| 34 | result = p.value |
| 35 | _CoTaskMemFree(p) |
| 36 | # stringified `GUID_null` would be '{00000000-0000-0000-0000-000000000000}' |
| 37 | # Should we do `assert result is not None`? |
| 38 | return result # type: ignore |
| 39 | |
| 40 | def __bool__(self) -> bool: |
| 41 | return self != GUID_null |
| 42 | |
| 43 | def __eq__(self, other) -> bool: |
| 44 | return isinstance(other, GUID) and binary(self) == binary(other) |
| 45 | |
| 46 | def __hash__(self) -> int: |
| 47 | # We make GUID instances hashable, although they are mutable. |
| 48 | return hash(binary(self)) |
| 49 | |
| 50 | def copy(self) -> "GUID": |
| 51 | return GUID(str(self)) |
| 52 | |
| 53 | @classmethod |
| 54 | def from_progid(cls, progid: Any) -> "hints.Self": |
| 55 | """Get guid from progid, ...""" |
| 56 | if hasattr(progid, "_reg_clsid_"): |
| 57 | progid = progid._reg_clsid_ |
| 58 | if isinstance(progid, cls): |
| 59 | return progid |
| 60 | elif isinstance(progid, str): |
| 61 | if progid.startswith("{"): |
| 62 | return cls(progid) |
| 63 | inst = cls() |
| 64 | _CLSIDFromProgID(str(progid), byref(inst)) |
| 65 | return inst |
| 66 | else: |
| 67 | raise TypeError(f"Cannot construct guid from {progid!r}") |
| 68 | |
| 69 | def as_progid(self) -> str: |
| 70 | """Convert a GUID into a progid""" |
| 71 | progid = c_wchar_p() |
| 72 | _ProgIDFromCLSID(byref(self), byref(progid)) |
| 73 | result = progid.value |
| 74 | _CoTaskMemFree(progid) |
| 75 | # Should we do `assert result is not None`? |
| 76 | return result # type: ignore |
no outgoing calls
searching dependent graphs…