Initialize the method cache for the given method type. Args: method_type: method type to initialize the cache for. Allowed values are: ``ip4`` | ``ip6`` | ``iface`` | ``default_iface`` network_request: if methods that make network requests should be included
(
method_type, network_request=True
)
| 1395 | |
| 1396 | |
| 1397 | def initialize_method_cache( |
| 1398 | method_type, network_request=True |
| 1399 | ): # type: (str, bool) -> bool |
| 1400 | """ |
| 1401 | Initialize the method cache for the given method type. |
| 1402 | |
| 1403 | Args: |
| 1404 | method_type: method type to initialize the cache for. |
| 1405 | Allowed values are: ``ip4`` | ``ip6`` | ``iface`` | ``default_iface`` |
| 1406 | network_request: if methods that make network requests should be included |
| 1407 | (those methods that have the attribute ``network_request`` set to ``True``) |
| 1408 | """ |
| 1409 | if METHOD_CACHE.get(method_type): |
| 1410 | if DEBUG: |
| 1411 | log.debug( |
| 1412 | "Method cache already initialized for method type '%s'", method_type |
| 1413 | ) |
| 1414 | return True |
| 1415 | |
| 1416 | log.debug("Initializing '%s' method cache (platform: '%s')", method_type, PLATFORM) |
| 1417 | |
| 1418 | if OVERRIDE_PLATFORM: |
| 1419 | log.warning( |
| 1420 | "Platform override is set, using '%s' as platform " |
| 1421 | "instead of detected platform '%s'", |
| 1422 | OVERRIDE_PLATFORM, |
| 1423 | PLATFORM, |
| 1424 | ) |
| 1425 | platform = OVERRIDE_PLATFORM |
| 1426 | else: |
| 1427 | platform = PLATFORM |
| 1428 | |
| 1429 | if DEBUG >= 4: |
| 1430 | meth_strs = ", ".join(m.__name__ for m in METHODS) # type: str |
| 1431 | log.debug("%d methods available: %s", len(METHODS), meth_strs) |
| 1432 | |
| 1433 | # Filter methods by the type of MAC we're looking for, such as "ip" |
| 1434 | # for remote host methods or "iface" for local interface methods. |
| 1435 | type_methods = [ |
| 1436 | method |
| 1437 | for method in METHODS |
| 1438 | if (method.method_type != "ip" and method.method_type == method_type) |
| 1439 | # Methods with a type of "ip" can handle both IPv4 and IPv6 |
| 1440 | or (method.method_type == "ip" and method_type in ["ip4", "ip6"]) |
| 1441 | ] # type: List[Type[Method]] |
| 1442 | |
| 1443 | if not type_methods: |
| 1444 | _warn_critical("No valid methods matching MAC type '%s'" % method_type) |
| 1445 | return False |
| 1446 | |
| 1447 | if DEBUG >= 2: |
| 1448 | type_strs = ", ".join(tm.__name__ for tm in type_methods) # type: str |
| 1449 | log.debug( |
| 1450 | "%d type-filtered methods for '%s': %s", |
| 1451 | len(type_methods), |
| 1452 | method_type, |
| 1453 | type_strs, |
| 1454 | ) |