Source code for wow.search

  1"""Filters for Battle.net search endpoints.
  2
  3Search methods take ``filters``, a mapping from a document field to a value::
  4
  5    client.items.search(
  6        filters={
  7            "item_class.id": 2,  # equals
  8            "quality.type": ["EPIC", "LEGENDARY"],  # any of
  9            "required_level": Range(50, 60),  # 50 <= level <= 60
 10            "inventory_type.type": Not("SHIELD"),  # not equal
 11        },
 12        order_by=["level:desc", "id"],
 13    )
 14
 15Field names follow the search document, with dots for nesting. Localized fields take a locale suffix, as in
 16``name.en_US``.
 17"""
 18
 19from collections.abc import Mapping, Sequence
 20from dataclasses import dataclass
 21from typing import Final
 22
 23__all__ = ["MAX_PAGE_SIZE", "FilterValue", "Not", "Range", "encode_search"]
 24
 25MAX_PAGE_SIZE: Final = 1000
 26"""The largest ``page_size`` Battle.net accepts, and also the most results one query returns."""
 27
 28type Scalar = str | int
 29
 30
 31@dataclass(frozen=True, slots=True)
[docs] 32class Range: 33 """Match a numeric field between two bounds; leave one out for an open range. 34 35 Attributes: 36 min: The lower bound, or ``None`` for no lower bound. 37 max: The upper bound, or ``None`` for no upper bound. 38 inclusive: Whether the bounds themselves match. 39 """ 40 41 min: int | None = None 42 max: int | None = None 43 inclusive: bool = True 44 45 def __post_init__(self) -> None: 46 if self.min is None and self.max is None: 47 msg = "Range needs at least one bound" 48 raise ValueError(msg) 49
[docs] 50 def encode(self) -> str: 51 """Render Blizzard's range syntax, such as ``[50,60]`` or ``(,77)``.""" 52 low = "" if self.min is None else str(self.min) 53 high = "" if self.max is None else str(self.max) 54 opening, closing = ("[", "]") if self.inclusive else ("(", ")") 55 return f"{opening}{low},{high}{closing}"
56 57 58@dataclass(frozen=True, slots=True)
[docs] 59class Not: 60 """Match documents whose field is not this value. 61 62 Attributes: 63 value: The value to exclude. 64 """ 65 66 value: Scalar
67 68 69type FilterValue = Scalar | bool | Range | Not | Sequence[Scalar] 70"""A filter value: equals for a scalar, any-of for a sequence, plus :class:`Range` and :class:`Not`.""" 71 72 73def _scalar(value: Scalar) -> str: 74 if isinstance(value, bool): 75 return "true" if value else "false" 76 return str(value) 77 78