"""A remote role's area: the places a remote hire may live in. `area` takes the first of these that names a place: 1. the model's `remote_where`, the names `extract` read in the posting, each read as a place text that says remote ("Europe", "anywhere") 2. the parts of the role's place texts that say remote in words (`Gazetteer.remote_places`): "Germany (Remote)", "Home Based - EMEA", "Remote, Global" 3. the role's resolved places, each as its country: Databricks' "Denmark" with the host's remote flag is Denmark, and Airtame's "København, Hovedstaden, Denmark; Remote" too, since an office town says where the company is, not that the hire must live in it. A place the reader marked remote without the word is the host's flag, and read so. "Remote" alone names no place, so a role with nothing else has no area: it is remote, area not stated. The area is kept as its widest places (`widest`), in the order found. """ from __future__ import annotations from collections.abc import Iterable, Sequence from app import places def widest(gaz: places.Gazetteer, ids: Iterable[int]) -> tuple[int, ...]: """The places no other one of them lies in, in order, each once: Germany and Europe is Europe. Anywhere alone when it is one of them, since it holds every other.""" found = tuple(dict.fromkeys(ids)) if gaz.anywhere in found: return (gaz.anywhere,) if gaz.anywhere is not None else () return tuple(p for p in found if not any(q in found for q in gaz.ancestors(p))) def _country(gaz: places.Gazetteer, place_id: int) -> int: """The country a place lies in, else the place itself (a continent, Øresund).""" return gaz.by_key.get(("country", gaz.country(place_id)), place_id) def area(gaz: places.Gazetteer, texts: Sequence[str], place_ids: Sequence[int], model: Sequence[str] | None = None) -> tuple[int, ...]: """The area of a remote role. `texts` are its place texts, `place_ids` its resolved places, `model` the names `extract` read (None never asked, `[]` not stated).""" for found in ( [p for name in model or () for p in gaz.remote_places(name, marked=True)], [p for text in texts for p in gaz.remote_places(text)], [_country(gaz, p) for p in place_ids]): if found: return widest(gaz, found) return ()