|
| 1 | +"""Shared NDEF data models.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from enum import Enum, unique |
| 6 | + |
| 7 | + |
| 8 | +@unique |
| 9 | +class NdefUriPrefix(Enum): |
| 10 | + """NDEF URI Identifier Code prefix table.""" |
| 11 | + |
| 12 | + NO_PREFIX = (0x00, "") |
| 13 | + HTTP_WWW = (0x01, "http://www.") |
| 14 | + HTTPS_WWW = (0x02, "https://www.") |
| 15 | + HTTP = (0x03, "http://") |
| 16 | + HTTPS = (0x04, "https://") |
| 17 | + TEL = (0x05, "tel:") |
| 18 | + MAILTO = (0x06, "mailto:") |
| 19 | + FTP_ANONYMOUS = (0x07, "ftp://anonymous:anonymous@") |
| 20 | + FTP_FTP = (0x08, "ftp://ftp.") |
| 21 | + FTPS = (0x09, "ftps://") |
| 22 | + SFTP = (0x0A, "sftp://") |
| 23 | + SMB = (0x0B, "smb://") |
| 24 | + NFS = (0x0C, "nfs://") |
| 25 | + FTP = (0x0D, "ftp://") |
| 26 | + DAV = (0x0E, "dav://") |
| 27 | + NEWS = (0x0F, "news:") |
| 28 | + TELNET = (0x10, "telnet://") |
| 29 | + IMAP = (0x11, "imap:") |
| 30 | + RTSP = (0x12, "rtsp://") |
| 31 | + URN = (0x13, "urn:") |
| 32 | + POP = (0x14, "pop:") |
| 33 | + SIP = (0x15, "sip:") |
| 34 | + SIPS = (0x16, "sips:") |
| 35 | + TFTP = (0x17, "tftp:") |
| 36 | + BTSPP = (0x18, "btspp://") |
| 37 | + BTL2CAP = (0x19, "btl2cap://") |
| 38 | + BTGOEP = (0x1A, "btgoep://") |
| 39 | + TCPOBEX = (0x1B, "tcpobex://") |
| 40 | + IRDAOBEX = (0x1C, "irdaobex://") |
| 41 | + FILE = (0x1D, "file://") |
| 42 | + URN_EPC_ID = (0x1E, "urn:epc:id:") |
| 43 | + URN_EPC_TAG = (0x1F, "urn:epc:tag:") |
| 44 | + URN_EPC_PAT = (0x20, "urn:epc:pat:") |
| 45 | + URN_EPC_RAW = (0x21, "urn:epc:raw:") |
| 46 | + URN_EPC = (0x22, "urn:epc:") |
| 47 | + URN_NFC = (0x23, "urn:nfc:") |
| 48 | + |
| 49 | + def __init__(self, code: int, expanded_text: str) -> None: |
| 50 | + self._code = code |
| 51 | + self._expanded_text = expanded_text |
| 52 | + |
| 53 | + @property |
| 54 | + def code(self) -> int: |
| 55 | + """Return the numeric URI Identifier Code.""" |
| 56 | + return self._code |
| 57 | + |
| 58 | + @property |
| 59 | + def expanded_text(self) -> str: |
| 60 | + """Return the expanded text represented by the prefix code.""" |
| 61 | + return self._expanded_text |
| 62 | + |
| 63 | + @classmethod |
| 64 | + def from_code(cls, code: int) -> NdefUriPrefix: |
| 65 | + """Return the URI prefix for a numeric URI Identifier Code.""" |
| 66 | + try: |
| 67 | + return _NDEF_URI_PREFIXES_BY_CODE[code] |
| 68 | + except KeyError: |
| 69 | + msg = f"Unsupported URI identifier code: {code:#x}" |
| 70 | + raise ValueError(msg) from None |
| 71 | + |
| 72 | + |
| 73 | +_NDEF_URI_PREFIXES_BY_CODE: dict[int, NdefUriPrefix] = { |
| 74 | + prefix.code: prefix for prefix in NdefUriPrefix |
| 75 | +} |
0 commit comments