|
31 | 31 | from tptools import ( |
32 | 32 | Court, |
33 | 33 | CourtSelectionParams, |
| 34 | + DeviceCourtMap, |
34 | 35 | Draw, |
35 | 36 | Entry, |
36 | 37 | MatchSelectionParams, |
|
59 | 60 | ) |
60 | 61 | from tptools.namepolicy.policybase import RegexpSubstTuple |
61 | 62 | from tptools.util import ( |
62 | | - flatten_dict, |
63 | 63 | normalise_dict_values_for_query_string, |
64 | 64 | silence_logger, |
65 | 65 | ) |
@@ -413,93 +413,40 @@ def get_config( |
413 | 413 |
|
414 | 414 | def get_dev_map( |
415 | 415 | path: Annotated[pathlib.Path, Depends(get_devmap_path)], |
416 | | -) -> dict[str, int | str] | Never: |
417 | | - devmap: dict[str, int | str] = {} |
418 | | - |
| 416 | +) -> DeviceCourtMap: |
419 | 417 | try: |
420 | 418 | with open(path, "rb") as f: |
421 | | - devmap = tomllib.load(f) |
| 419 | + return DeviceCourtMap(f) |
422 | 420 |
|
423 | 421 | except FileNotFoundError: |
424 | 422 | logger.warning(f"Squore device to court map not found at {path}, ignoring…") |
425 | 423 |
|
426 | | - # TOML turns a key like 172.21.22.1 into a nested dict {"172":{"21":{"22"…} |
427 | | - # so for convenience, flatten this: |
428 | | - return flatten_dict(devmap) |
| 424 | + return DeviceCourtMap() |
429 | 425 |
|
430 | 426 |
|
431 | 427 | # }}} |
432 | 428 |
|
433 | 429 | # {{{ Court name to dev/mirror |
434 | 430 |
|
435 | 431 |
|
436 | | -def _normalise_court_name_for_matching(courtname: str) -> tuple[int | None, str] | None: |
437 | | - m = re.match( |
438 | | - r"(?:-?(?P<loc>\d+)-)?c(?:ourt *)?0*(?P<court>\d+)", |
439 | | - courtname, |
440 | | - flags=re.IGNORECASE, |
441 | | - ) |
442 | | - if m: |
443 | | - return int(loc) if (loc := m["loc"]) is not None else None, m["court"] |
444 | | - else: |
445 | | - return None |
446 | | - |
447 | | - |
448 | 432 | def get_court_for_dev( |
449 | | - dev_map: Annotated[dict[str, int | str], Depends(get_dev_map)], |
| 433 | + dev_map: Annotated[DeviceCourtMap, Depends(get_dev_map)], |
450 | 434 | courts: Annotated[list[Court], Depends(get_courts)], |
451 | 435 | clientip: Annotated[str, Depends(get_remote)], |
452 | 436 | ) -> Court | None: |
453 | | - courtname = None |
454 | | - if (courtname := dev_map.get(clientip)) is not None: |
455 | | - logger.debug(f"Device at IP {clientip} might want feed for {courtname}") |
456 | | - for court in courts: |
457 | | - # TODO: can we do better than this to identify the court when we are |
458 | | - # given a string that might not be what the current courtnamepolicy |
459 | | - # returns, or an ID? |
460 | | - if isinstance(courtname, int): |
461 | | - if courtname == court.id: |
462 | | - return court |
463 | | - |
464 | | - else: |
465 | | - term = _normalise_court_name_for_matching(courtname) |
466 | | - if term is not None: |
467 | | - comps = ( |
468 | | - [] |
469 | | - if court.location is None |
470 | | - else [ |
471 | | - f"{court.location.id}-{court.name}", |
472 | | - f"{court.location.id}-{court}", |
473 | | - ] |
474 | | - ) |
475 | | - |
476 | | - for comparename in ( |
477 | | - *comps, |
478 | | - court.name, |
479 | | - str(court), |
480 | | - ): |
481 | | - comp = _normalise_court_name_for_matching(comparename) |
482 | | - if ( |
483 | | - comp is not None |
484 | | - and (term[0] is None and term[1] == comp[1]) |
485 | | - or (term == comp) |
486 | | - ): |
487 | | - return court |
488 | | - |
489 | | - logger.debug(f"No court found in devmap for device with IP {clientip}") |
490 | | - return None |
| 437 | + return dev_map.find_court_for_ip(clientip, courts=courts) |
491 | 438 |
|
492 | 439 |
|
493 | 440 | def get_mirror_for_dev( |
494 | | - dev_map: Annotated[dict[str, int | str], Depends(get_dev_map)], |
| 441 | + dev_map: Annotated[DeviceCourtMap, Depends(get_dev_map)], |
495 | 442 | clientip: Annotated[str, Depends(get_remote)], |
496 | 443 | ) -> str | None: |
497 | 444 | othername = None |
498 | | - if (othername := dev_map.get(clientip)) is not None and re.fullmatch( |
| 445 | + if (othername := dev_map.find_match_for_ip(clientip)) is not None and re.fullmatch( |
499 | 446 | r"\w{6}-\d+-.+", othername := str(othername) |
500 | 447 | ): |
501 | 448 | logger.debug(f"Device at IP {clientip} wants to be mirror for {othername}") |
502 | | - return othername # type: ignore[return-value] |
| 449 | + return othername |
503 | 450 |
|
504 | 451 | logger.debug(f"No mirror device found in devmap for device with IP {clientip}") |
505 | 452 | return None |
|
0 commit comments