Placeholder value in playbooks, so that objects (flows in particular) can be referenced before they are known. Example: f = Placeholder(TCPFlow) assert ( playbook(tcp.TCPLayer(tctx)) << TcpStartHook(f) # the flow object returned here is generated by the layer.
| 363 | |
| 364 | |
| 365 | class _Placeholder(Generic[T]): |
| 366 | """ |
| 367 | Placeholder value in playbooks, so that objects (flows in particular) can be referenced before |
| 368 | they are known. Example: |
| 369 | |
| 370 | f = Placeholder(TCPFlow) |
| 371 | assert ( |
| 372 | playbook(tcp.TCPLayer(tctx)) |
| 373 | << TcpStartHook(f) # the flow object returned here is generated by the layer. |
| 374 | ) |
| 375 | |
| 376 | # We can obtain the flow object now using f(): |
| 377 | assert f().messages == 0 |
| 378 | """ |
| 379 | |
| 380 | def __init__(self, cls: type[T]): |
| 381 | self._obj = None |
| 382 | self._cls = cls |
| 383 | |
| 384 | def __call__(self) -> T: |
| 385 | """Get the actual object""" |
| 386 | return self._obj |
| 387 | |
| 388 | def setdefault(self, value: T) -> T: |
| 389 | if self._obj is None: |
| 390 | if self._cls is not Any and not isinstance(value, self._cls): |
| 391 | raise TypeError( |
| 392 | f"expected {self._cls.__name__}, got {type(value).__name__}." |
| 393 | ) |
| 394 | self._obj = value |
| 395 | return self._obj |
| 396 | |
| 397 | def __repr__(self): |
| 398 | return f"Placeholder:{self._obj!r}" |
| 399 | |
| 400 | def __str__(self): |
| 401 | return f"Placeholder:{self._obj}" |
| 402 | |
| 403 | |
| 404 | # noinspection PyPep8Naming |
no outgoing calls
no test coverage detected
searching dependent graphs…