|
| 1 | +""" |
| 2 | +Map GitHub repository name to bio.tools ID. |
| 3 | +
|
| 4 | +This module reconciles the GitHub repository name with the bio.tools ID. |
| 5 | +It applies a policy that prefers the GitHub name while ensuring uniqueness |
| 6 | +within bio.tools, generating alternative IDs if necessary. |
| 7 | +""" |
| 8 | + |
| 9 | +import httpx |
| 10 | + |
| 11 | +from bridge.builders import compose_biotools_metadata |
| 12 | +from bridge.logging import get_user_logger |
| 13 | +from bridge.pipelines.utils import normalize_text, str_contain_each_other |
| 14 | + |
| 15 | +logger = get_user_logger() |
| 16 | + |
| 17 | + |
| 18 | +async def _matching_biotools_id_exists(biotools_id: str) -> bool: |
| 19 | + """ |
| 20 | + Check if a bio.tools entry with the given ID already exists. |
| 21 | +
|
| 22 | + The check is performed by attempting to fetch bio.tools metadata |
| 23 | + for the given ID. If a 404 Not Found error is returned, the ID |
| 24 | + does not exist. Any other error is treated as an indication that |
| 25 | + the ID may exist, to avoid false negatives. |
| 26 | +
|
| 27 | + Parameters |
| 28 | + ---------- |
| 29 | + biotools_id : str |
| 30 | + Candidate bio.tools ID to check. |
| 31 | +
|
| 32 | + Returns |
| 33 | + ------- |
| 34 | + bool |
| 35 | + True if a matching bio.tools entry exists, False otherwise. |
| 36 | + """ |
| 37 | + try: |
| 38 | + matching_bt_metadata = await compose_biotools_metadata(identifier=biotools_id) |
| 39 | + except Exception as e: |
| 40 | + if isinstance(e, httpx.HTTPStatusError) and e.response.status_code == 404: |
| 41 | + return False |
| 42 | + else: |
| 43 | + logger.added(f"Failed to check existing bio.tools ID '{biotools_id}': {e}. Assuming it exists.") |
| 44 | + return True |
| 45 | + |
| 46 | + return matching_bt_metadata is not None |
| 47 | + |
| 48 | + |
| 49 | +async def map_biotools_id(gh_name: str | None, bt_id: str | None) -> str | None: |
| 50 | + """ |
| 51 | + Map and reconcile GitHub repository name to bio.tools ID. |
| 52 | +
|
| 53 | + Policy: |
| 54 | + 1. If no GitHub repo name is available, preserve existing bio.tools ID (even if None). |
| 55 | + 2. If the existing bio.tools ID and GitHub repo name contain each other |
| 56 | + (case-insensitive), preserve the existing bio.tools ID. |
| 57 | + 3. If they do not contain each other, log a conflict but continue. |
| 58 | + 4. If the GitHub repo name is not already used as a bio.tools ID, |
| 59 | + use it as the new bio.tools ID. |
| 60 | + 5. If the GitHub repo name is already used as a bio.tools ID, |
| 61 | + attempt to generate a unique ID by appending suffixes ``-1``, ``-2``, ... |
| 62 | + up to ``-99``. If a unique ID is found, use it. |
| 63 | + 6. If no unique ID can be generated, log a note and return ``None``, |
| 64 | + requiring manual intervention. |
| 65 | +
|
| 66 | + Parameters |
| 67 | + ---------- |
| 68 | + gh_name : str | None |
| 69 | + GitHub repository name. |
| 70 | + bt_id : str | None |
| 71 | + Existing bio.tools ID. |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + str | None |
| 76 | + Mapped bio.tools ID, or ``None`` if mapping failed. |
| 77 | + """ |
| 78 | + if gh_name is None: |
| 79 | + logger.unchanged("No GitHub repo name found, nothing to map.") |
| 80 | + return bt_id |
| 81 | + |
| 82 | + gh_norm = normalize_text(gh_name) |
| 83 | + bt_norm = normalize_text(bt_id or "") |
| 84 | + |
| 85 | + if bt_id is not None and str_contain_each_other(gh_norm, bt_norm): |
| 86 | + logger.exact(f"bio.tools ID '{bt_id}' and GitHub repo name '{gh_name}' contain each other") |
| 87 | + return bt_id |
| 88 | + |
| 89 | + if bt_id is not None and not str_contain_each_other(gh_norm, bt_norm): |
| 90 | + logger.conflict(f"bio.tools ID '{bt_id}' and GitHub repo name '{gh_name}' do not contain each other") |
| 91 | + |
| 92 | + if not await _matching_biotools_id_exists(gh_norm): |
| 93 | + logger.added(f"Using GitHub repo name '{gh_name}' as bio.tools ID") |
| 94 | + return gh_norm |
| 95 | + |
| 96 | + logger.conflict( |
| 97 | + f"GitHub repo name '{gh_name}' cannot be used as bio.tools ID because it matches an existing entry. " |
| 98 | + "Trying to generate a unique bio.tools ID." |
| 99 | + ) |
| 100 | + |
| 101 | + for suffix in range(1, 100): |
| 102 | + candidate_id = f"{gh_norm}-{suffix}" |
| 103 | + if not await _matching_biotools_id_exists(candidate_id): |
| 104 | + logger.added(f"Using generated bio.tools ID '{candidate_id}'") |
| 105 | + return candidate_id |
| 106 | + |
| 107 | + logger.note( |
| 108 | + f"Failed to generate unique bio.tools ID based on GitHub repo name '{gh_name}'. " |
| 109 | + "Please set bio.tools ID manually." |
| 110 | + ) |
| 111 | + return None |
0 commit comments