Parse a proxy mode specification and return the corresponding `ProxyMode` instance.
(cls, spec: str)
| 97 | @classmethod |
| 98 | @cache |
| 99 | def parse(cls, spec: str) -> Self: |
| 100 | """ |
| 101 | Parse a proxy mode specification and return the corresponding `ProxyMode` instance. |
| 102 | """ |
| 103 | head, _, listen_at = spec.rpartition("@") |
| 104 | if not head: |
| 105 | head = listen_at |
| 106 | listen_at = "" |
| 107 | |
| 108 | mode, _, data = head.partition(":") |
| 109 | |
| 110 | if listen_at: |
| 111 | if ":" in listen_at: |
| 112 | host, _, port_str = listen_at.rpartition(":") |
| 113 | else: |
| 114 | host = None |
| 115 | port_str = listen_at |
| 116 | try: |
| 117 | port = int(port_str) |
| 118 | if port < 0 or 65535 < port: |
| 119 | raise ValueError |
| 120 | except ValueError: |
| 121 | raise ValueError(f"invalid port: {port_str}") |
| 122 | else: |
| 123 | host = None |
| 124 | port = None |
| 125 | |
| 126 | try: |
| 127 | mode_cls = ProxyMode.__types[mode.lower()] |
| 128 | except KeyError: |
| 129 | raise ValueError(f"unknown mode") |
| 130 | |
| 131 | if not issubclass(mode_cls, cls): |
| 132 | raise ValueError(f"{mode!r} is not a spec for a {cls.type_name} mode") |
| 133 | |
| 134 | return mode_cls( |
| 135 | full_spec=spec, data=data, custom_listen_host=host, custom_listen_port=port |
| 136 | ) |
| 137 | |
| 138 | def listen_host(self, default: str | None = None) -> str: |
| 139 | """ |
no outgoing calls
no test coverage detected