| 266 | |
| 267 | |
| 268 | class ASN1F_enum_INTEGER(ASN1F_INTEGER): |
| 269 | def __init__(self, |
| 270 | name, # type: str |
| 271 | default, # type: ASN1_INTEGER |
| 272 | enum, # type: Dict[int, str] |
| 273 | context=None, # type: Optional[Any] |
| 274 | implicit_tag=None, # type: Optional[Any] |
| 275 | explicit_tag=None, # type: Optional[Any] |
| 276 | ): |
| 277 | # type: (...) -> None |
| 278 | super(ASN1F_enum_INTEGER, self).__init__( |
| 279 | name, default, context=context, |
| 280 | implicit_tag=implicit_tag, |
| 281 | explicit_tag=explicit_tag |
| 282 | ) |
| 283 | i2s = self.i2s = {} # type: Dict[int, str] |
| 284 | s2i = self.s2i = {} # type: Dict[str, int] |
| 285 | if isinstance(enum, list): |
| 286 | keys = range(len(enum)) |
| 287 | else: |
| 288 | keys = list(enum) |
| 289 | if any(isinstance(x, str) for x in keys): |
| 290 | i2s, s2i = s2i, i2s # type: ignore |
| 291 | for k in keys: |
| 292 | i2s[k] = enum[k] |
| 293 | s2i[enum[k]] = k |
| 294 | |
| 295 | def i2m(self, |
| 296 | pkt, # type: ASN1_Packet |
| 297 | s, # type: Union[bytes, str, int, ASN1_INTEGER] |
| 298 | ): |
| 299 | # type: (...) -> bytes |
| 300 | if not isinstance(s, str): |
| 301 | vs = s |
| 302 | else: |
| 303 | vs = self.s2i[s] |
| 304 | return super(ASN1F_enum_INTEGER, self).i2m(pkt, vs) |
| 305 | |
| 306 | def i2repr(self, |
| 307 | pkt, # type: ASN1_Packet |
| 308 | x, # type: Union[str, int] |
| 309 | ): |
| 310 | # type: (...) -> str |
| 311 | if x is not None and isinstance(x, ASN1_INTEGER): |
| 312 | r = self.i2s.get(x.val) |
| 313 | if r: |
| 314 | return "'%s' %s" % (r, repr(x)) |
| 315 | return repr(x) |
| 316 | |
| 317 | |
| 318 | class ASN1F_BIT_STRING(ASN1F_field[str, ASN1_BIT_STRING]): |
no outgoing calls
no test coverage detected