pyairbnb 2.2.2__tar.gz → 2.2.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/PKG-INFO +28 -1
- pyairbnb-2.2.2/src/pyairbnb.egg-info/PKG-INFO → pyairbnb-2.2.3/README.md +26 -14
- pyairbnb-2.2.3/pyproject.toml +28 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/__init__.py +22 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/api.py +26 -2
- pyairbnb-2.2.3/src/pyairbnb/calendar_contract.py +34 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/calendarinfo.py +26 -3
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/details.py +0 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/experience.py +0 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/host.py +0 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/host_details.py +0 -0
- pyairbnb-2.2.3/src/pyairbnb/models/__init__.py +15 -0
- pyairbnb-2.2.3/src/pyairbnb/models/_validation.py +295 -0
- pyairbnb-2.2.3/src/pyairbnb/models/calendar.py +217 -0
- pyairbnb-2.2.3/src/pyairbnb/parsers/__init__.py +1 -0
- pyairbnb-2.2.3/src/pyairbnb/parsers/calendar.py +20 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/price.py +0 -0
- pyairbnb-2.2.3/src/pyairbnb/py.typed +1 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/reviews.py +0 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/search.py +11 -1
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/standardize.py +0 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/start.py +350 -12
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/utils.py +21 -0
- pyairbnb-2.2.2/README.md → pyairbnb-2.2.3/src/pyairbnb.egg-info/PKG-INFO +41 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb.egg-info/SOURCES.txt +13 -1
- pyairbnb-2.2.3/tests/test_calendar.py +131 -0
- pyairbnb-2.2.3/tests/test_calendar_fixtures.py +54 -0
- pyairbnb-2.2.3/tests/test_calendar_models.py +215 -0
- pyairbnb-2.2.3/tests/test_calendar_parser.py +51 -0
- pyairbnb-2.2.3/tests/test_search_arguments.py +138 -0
- pyairbnb-2.2.2/pyproject.toml +0 -15
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/LICENSE +0 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/setup.cfg +0 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb/parse.py +0 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb.egg-info/dependency_links.txt +0 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb.egg-info/requires.txt +0 -0
- {pyairbnb-2.2.2 → pyairbnb-2.2.3}/src/pyairbnb.egg-info/top_level.txt +0 -0
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pyairbnb
|
|
3
|
-
Version: 2.2.
|
|
3
|
+
Version: 2.2.3
|
|
4
4
|
Summary: Airbnb scraper in Python
|
|
5
5
|
Author-email: John Balvin <johnchristian@hotmail.es>
|
|
6
6
|
Project-URL: Homepage, https://github.com/johnbalvin/pyairbnb
|
|
7
7
|
Keywords: airbnb,scraper,crawler,bot,reviews
|
|
8
|
+
Requires-Python: >=3.10
|
|
8
9
|
Description-Content-Type: text/markdown
|
|
9
10
|
License-File: LICENSE
|
|
10
11
|
Requires-Dist: curl_cffi
|
|
@@ -132,6 +133,32 @@ data = pyairbnb.get_price(
|
|
|
132
133
|
)
|
|
133
134
|
```
|
|
134
135
|
|
|
136
|
+
### Getting a calendar
|
|
137
|
+
|
|
138
|
+
`get_calendar` validates Airbnb's calendar response and returns immutable
|
|
139
|
+
dataclasses by default. If Airbnb changes the response contract, the default
|
|
140
|
+
behavior emits one `CalendarContractWarning` and returns the complete raw list
|
|
141
|
+
of dictionaries instead, so no new fields are lost.
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
import pyairbnb
|
|
145
|
+
|
|
146
|
+
# CalendarMonth objects when the observed contract is valid.
|
|
147
|
+
calendar = pyairbnb.get_calendar(room_id="123")
|
|
148
|
+
|
|
149
|
+
# Always return the unvalidated Airbnb dictionaries.
|
|
150
|
+
raw_calendar = pyairbnb.get_calendar(
|
|
151
|
+
room_id="123",
|
|
152
|
+
return_dataclass=False,
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
# Return CalendarMonth objects or raise CalendarContractError.
|
|
156
|
+
strict_calendar = pyairbnb.get_calendar(
|
|
157
|
+
room_id="123",
|
|
158
|
+
on_dataclass_error="raise",
|
|
159
|
+
)
|
|
160
|
+
```
|
|
161
|
+
|
|
135
162
|
|
|
136
163
|
### Getting listings from user id
|
|
137
164
|
```Python
|
|
@@ -1,17 +1,3 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: pyairbnb
|
|
3
|
-
Version: 2.2.2
|
|
4
|
-
Summary: Airbnb scraper in Python
|
|
5
|
-
Author-email: John Balvin <johnchristian@hotmail.es>
|
|
6
|
-
Project-URL: Homepage, https://github.com/johnbalvin/pyairbnb
|
|
7
|
-
Keywords: airbnb,scraper,crawler,bot,reviews
|
|
8
|
-
Description-Content-Type: text/markdown
|
|
9
|
-
License-File: LICENSE
|
|
10
|
-
Requires-Dist: curl_cffi
|
|
11
|
-
Requires-Dist: bs4
|
|
12
|
-
Requires-Dist: requests
|
|
13
|
-
Dynamic: license-file
|
|
14
|
-
|
|
15
1
|
# Airbnb scraper in Python
|
|
16
2
|
|
|
17
3
|
## Overview
|
|
@@ -132,6 +118,32 @@ data = pyairbnb.get_price(
|
|
|
132
118
|
)
|
|
133
119
|
```
|
|
134
120
|
|
|
121
|
+
### Getting a calendar
|
|
122
|
+
|
|
123
|
+
`get_calendar` validates Airbnb's calendar response and returns immutable
|
|
124
|
+
dataclasses by default. If Airbnb changes the response contract, the default
|
|
125
|
+
behavior emits one `CalendarContractWarning` and returns the complete raw list
|
|
126
|
+
of dictionaries instead, so no new fields are lost.
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
import pyairbnb
|
|
130
|
+
|
|
131
|
+
# CalendarMonth objects when the observed contract is valid.
|
|
132
|
+
calendar = pyairbnb.get_calendar(room_id="123")
|
|
133
|
+
|
|
134
|
+
# Always return the unvalidated Airbnb dictionaries.
|
|
135
|
+
raw_calendar = pyairbnb.get_calendar(
|
|
136
|
+
room_id="123",
|
|
137
|
+
return_dataclass=False,
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
# Return CalendarMonth objects or raise CalendarContractError.
|
|
141
|
+
strict_calendar = pyairbnb.get_calendar(
|
|
142
|
+
room_id="123",
|
|
143
|
+
on_dataclass_error="raise",
|
|
144
|
+
)
|
|
145
|
+
```
|
|
146
|
+
|
|
135
147
|
|
|
136
148
|
### Getting listings from user id
|
|
137
149
|
```Python
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
|
|
7
|
+
version = '2.2.3'
|
|
8
|
+
name="pyairbnb"
|
|
9
|
+
description = 'Airbnb scraper in Python'
|
|
10
|
+
authors = [
|
|
11
|
+
{ name="John Balvin", email="johnchristian@hotmail.es" },
|
|
12
|
+
]
|
|
13
|
+
readme= "README.md"
|
|
14
|
+
keywords=['airbnb', 'scraper', 'crawler','bot','reviews']
|
|
15
|
+
dependencies=['curl_cffi','bs4','requests']
|
|
16
|
+
requires-python = ">=3.10"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
Homepage='https://github.com/johnbalvin/pyairbnb'
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
[tool.setuptools.packages.find]
|
|
24
|
+
where = ["src"]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.package-data]
|
|
28
|
+
pyairbnb = ["py.typed"]
|
|
@@ -17,6 +17,19 @@ from pyairbnb.start import (
|
|
|
17
17
|
)
|
|
18
18
|
from pyairbnb.details import get as get_metadata_from_url
|
|
19
19
|
from pyairbnb.price import get as get_price
|
|
20
|
+
from pyairbnb.calendar_contract import (
|
|
21
|
+
CalendarContractError,
|
|
22
|
+
CalendarContractIssue,
|
|
23
|
+
CalendarContractWarning,
|
|
24
|
+
CalendarExtractionError,
|
|
25
|
+
)
|
|
26
|
+
from pyairbnb.models import (
|
|
27
|
+
CalendarConditionRange,
|
|
28
|
+
CalendarConditions,
|
|
29
|
+
CalendarDay,
|
|
30
|
+
CalendarDayPrice,
|
|
31
|
+
CalendarMonth,
|
|
32
|
+
)
|
|
20
33
|
|
|
21
34
|
|
|
22
35
|
__all__ = [
|
|
@@ -38,4 +51,13 @@ __all__ = [
|
|
|
38
51
|
"experience_search",
|
|
39
52
|
"get_metadata_from_url",
|
|
40
53
|
"get_price",
|
|
54
|
+
"CalendarConditionRange",
|
|
55
|
+
"CalendarConditions",
|
|
56
|
+
"CalendarContractError",
|
|
57
|
+
"CalendarContractIssue",
|
|
58
|
+
"CalendarContractWarning",
|
|
59
|
+
"CalendarDay",
|
|
60
|
+
"CalendarDayPrice",
|
|
61
|
+
"CalendarExtractionError",
|
|
62
|
+
"CalendarMonth",
|
|
41
63
|
]
|
|
@@ -27,10 +27,34 @@ def get(proxy_url: str, timeout: Timeout = DEFAULT_TIMEOUT) -> str:
|
|
|
27
27
|
proxies = {"http": proxy_url, "https": proxy_url}
|
|
28
28
|
|
|
29
29
|
response = requests.get(ep, headers=headers, proxies=proxies, timeout=timeout)
|
|
30
|
-
response.raise_for_status()
|
|
30
|
+
response.raise_for_status()
|
|
31
31
|
|
|
32
32
|
body = response.text
|
|
33
|
-
|
|
33
|
+
api_key_match = regx_api_key.search(body)
|
|
34
|
+
|
|
35
|
+
if not api_key_match:
|
|
36
|
+
# Airbnb may return a domain-switch landing page instead of the actual homepage.
|
|
37
|
+
handoff_match = re.search(r'<form[^>]+action="([^"]+)"', body)
|
|
38
|
+
payload_match = re.search(r'<input[^>]+name="payload"[^>]+value="([^"]+)"', body)
|
|
39
|
+
|
|
40
|
+
if handoff_match and payload_match:
|
|
41
|
+
handoff_url = handoff_match.group(1)
|
|
42
|
+
payload_value = payload_match.group(1)
|
|
43
|
+
response = requests.post(
|
|
44
|
+
handoff_url,
|
|
45
|
+
data={"version": "1", "payload": payload_value},
|
|
46
|
+
headers=headers,
|
|
47
|
+
proxies=proxies,
|
|
48
|
+
timeout=60,
|
|
49
|
+
)
|
|
50
|
+
response.raise_for_status()
|
|
51
|
+
body = response.text
|
|
52
|
+
api_key_match = regx_api_key.search(body)
|
|
53
|
+
|
|
54
|
+
if not api_key_match:
|
|
55
|
+
raise Exception("Could not parse Airbnb API key from the homepage response.")
|
|
56
|
+
|
|
57
|
+
api_key = api_key_match.group()
|
|
34
58
|
api_key = api_key.replace('"api_config":{"key":"', '')
|
|
35
59
|
api_key = api_key.replace('"', "")
|
|
36
60
|
return api_key
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@dataclass(frozen=True)
|
|
5
|
+
class CalendarContractIssue:
|
|
6
|
+
path: str
|
|
7
|
+
kind: str
|
|
8
|
+
expected: str | None
|
|
9
|
+
actual: str | None
|
|
10
|
+
message: str
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _format_contract_mismatch(issues: tuple[CalendarContractIssue, ...]) -> str:
|
|
14
|
+
lines = [f"Calendar contract mismatch: {len(issues)} issues"]
|
|
15
|
+
lines.extend(f"- {issue.path}: {issue.message}" for issue in issues)
|
|
16
|
+
return "\n".join(lines)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class CalendarContractError(ValueError):
|
|
20
|
+
def __init__(self, issues: tuple[CalendarContractIssue, ...]):
|
|
21
|
+
self.issues = tuple(issues)
|
|
22
|
+
super().__init__(_format_contract_mismatch(self.issues))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class CalendarExtractionError(ValueError):
|
|
26
|
+
def __init__(self, path: str):
|
|
27
|
+
self.path = path
|
|
28
|
+
super().__init__(f"Calendar could not be extracted at {path}")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class CalendarContractWarning(UserWarning):
|
|
32
|
+
def __init__(self, issues: tuple[CalendarContractIssue, ...]):
|
|
33
|
+
self.issues = tuple(issues)
|
|
34
|
+
super().__init__(_format_contract_mismatch(self.issues))
|
|
@@ -3,11 +3,35 @@ import pyairbnb.utils as utils
|
|
|
3
3
|
from urllib.parse import urlencode
|
|
4
4
|
import json
|
|
5
5
|
from pyairbnb.utils import DEFAULT_TIMEOUT, Timeout
|
|
6
|
+
from pyairbnb.calendar_contract import CalendarExtractionError
|
|
6
7
|
|
|
7
8
|
ep = "https://www.airbnb.com/api/v3/PdpAvailabilityCalendar/8f08e03c7bd16fcad3c92a3592c19a8b559a0d0855a84028d1163d4733ed9ade/"
|
|
8
9
|
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
_CALENDAR_PATH = "$.data.merlin.pdpAvailabilityCalendar.calendarMonths"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _extract_calendar(data, *, require_path: bool):
|
|
15
|
+
calendar = utils.get_nested_value(
|
|
16
|
+
data, "data.merlin.pdpAvailabilityCalendar.calendarMonths", None
|
|
17
|
+
)
|
|
18
|
+
if calendar is None:
|
|
19
|
+
if require_path:
|
|
20
|
+
raise CalendarExtractionError(_CALENDAR_PATH)
|
|
21
|
+
return []
|
|
22
|
+
return calendar
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def get(
|
|
26
|
+
api_key: str,
|
|
27
|
+
room_id: str,
|
|
28
|
+
month: int,
|
|
29
|
+
year: int,
|
|
30
|
+
proxy_url: str,
|
|
31
|
+
timeout: Timeout = DEFAULT_TIMEOUT,
|
|
32
|
+
*,
|
|
33
|
+
require_calendar_path: bool = False,
|
|
34
|
+
):
|
|
11
35
|
headers = {
|
|
12
36
|
"Accept": "application/json",
|
|
13
37
|
"Content-Type": "application/json",
|
|
@@ -44,5 +68,4 @@ def get(api_key: str, room_id: str, month: int, year: int, proxy_url: str, timeo
|
|
|
44
68
|
response = requests.get(url, headers=headers, proxies=proxies, timeout=timeout)
|
|
45
69
|
response.raise_for_status()
|
|
46
70
|
data = response.json()
|
|
47
|
-
|
|
48
|
-
return calendar
|
|
71
|
+
return _extract_calendar(data, require_path=require_calendar_path)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from pyairbnb.models.calendar import (
|
|
2
|
+
CalendarConditionRange,
|
|
3
|
+
CalendarConditions,
|
|
4
|
+
CalendarDay,
|
|
5
|
+
CalendarDayPrice,
|
|
6
|
+
CalendarMonth,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"CalendarConditionRange",
|
|
11
|
+
"CalendarConditions",
|
|
12
|
+
"CalendarDay",
|
|
13
|
+
"CalendarDayPrice",
|
|
14
|
+
"CalendarMonth",
|
|
15
|
+
]
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from collections.abc import Callable
|
|
3
|
+
from datetime import date
|
|
4
|
+
from typing import TypeVar, cast
|
|
5
|
+
|
|
6
|
+
from pyairbnb.calendar_contract import CalendarContractError, CalendarContractIssue
|
|
7
|
+
|
|
8
|
+
_DATE_PATTERN = re.compile(r"^\d{4}-\d{2}-\d{2}$")
|
|
9
|
+
_T = TypeVar("_T")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _issue(
|
|
13
|
+
path: str,
|
|
14
|
+
kind: str,
|
|
15
|
+
expected: str | None,
|
|
16
|
+
value: object,
|
|
17
|
+
message: str,
|
|
18
|
+
*,
|
|
19
|
+
actual: str | None = None,
|
|
20
|
+
) -> CalendarContractIssue:
|
|
21
|
+
if actual is None:
|
|
22
|
+
actual = type(value).__name__
|
|
23
|
+
return CalendarContractIssue(path, kind, expected, actual, message)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def require_list(data: object, *, path: str) -> list[object]:
|
|
27
|
+
if type(data) is not list:
|
|
28
|
+
raise CalendarContractError(
|
|
29
|
+
(_issue(path, "invalid_container", "list", data, "expected a list"),)
|
|
30
|
+
)
|
|
31
|
+
return cast(list[object], data)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class _ObjectReader:
|
|
35
|
+
def __init__(
|
|
36
|
+
self,
|
|
37
|
+
data: object,
|
|
38
|
+
*,
|
|
39
|
+
path: str,
|
|
40
|
+
expected_fields: set[str],
|
|
41
|
+
) -> None:
|
|
42
|
+
self.path = path
|
|
43
|
+
self.issues: list[CalendarContractIssue] = []
|
|
44
|
+
self._data: dict[object, object] | None = None
|
|
45
|
+
|
|
46
|
+
if type(data) is not dict:
|
|
47
|
+
self.issues.append(
|
|
48
|
+
_issue(path, "invalid_container", "dict", data, "expected an object")
|
|
49
|
+
)
|
|
50
|
+
return
|
|
51
|
+
|
|
52
|
+
self._data = cast(dict[object, object], data)
|
|
53
|
+
received_fields = set(self._data)
|
|
54
|
+
for missing_field in sorted(expected_fields - received_fields):
|
|
55
|
+
self.issues.append(
|
|
56
|
+
CalendarContractIssue(
|
|
57
|
+
f"{path}.{missing_field}",
|
|
58
|
+
"missing_field",
|
|
59
|
+
"present",
|
|
60
|
+
None,
|
|
61
|
+
"missing required field",
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
for extra_field in sorted(received_fields - expected_fields, key=str):
|
|
65
|
+
self.issues.append(
|
|
66
|
+
_issue(
|
|
67
|
+
f"{path}.{extra_field}",
|
|
68
|
+
"extra_field",
|
|
69
|
+
None,
|
|
70
|
+
self._data[extra_field],
|
|
71
|
+
"unexpected field",
|
|
72
|
+
)
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
def _value(self, field: str) -> tuple[bool, object]:
|
|
76
|
+
if self._data is None or field not in self._data:
|
|
77
|
+
return False, None
|
|
78
|
+
return True, self._data[field]
|
|
79
|
+
|
|
80
|
+
def string(self, field: str) -> str | None:
|
|
81
|
+
present, value = self._value(field)
|
|
82
|
+
if not present:
|
|
83
|
+
return None
|
|
84
|
+
if type(value) is not str:
|
|
85
|
+
self.issues.append(
|
|
86
|
+
_issue(
|
|
87
|
+
f"{self.path}.{field}",
|
|
88
|
+
"invalid_type",
|
|
89
|
+
"str",
|
|
90
|
+
value,
|
|
91
|
+
"expected a string",
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
return None
|
|
95
|
+
return cast(str, value)
|
|
96
|
+
|
|
97
|
+
def nullable_string(self, field: str) -> str | None:
|
|
98
|
+
present, value = self._value(field)
|
|
99
|
+
if not present or value is None:
|
|
100
|
+
return None
|
|
101
|
+
return self.string(field)
|
|
102
|
+
|
|
103
|
+
def boolean(self, field: str) -> bool | None:
|
|
104
|
+
present, value = self._value(field)
|
|
105
|
+
if not present:
|
|
106
|
+
return None
|
|
107
|
+
if type(value) is not bool:
|
|
108
|
+
self.issues.append(
|
|
109
|
+
_issue(
|
|
110
|
+
f"{self.path}.{field}",
|
|
111
|
+
"invalid_type",
|
|
112
|
+
"bool",
|
|
113
|
+
value,
|
|
114
|
+
"expected a boolean",
|
|
115
|
+
)
|
|
116
|
+
)
|
|
117
|
+
return None
|
|
118
|
+
return cast(bool, value)
|
|
119
|
+
|
|
120
|
+
def nullable_boolean(self, field: str) -> bool | None:
|
|
121
|
+
present, value = self._value(field)
|
|
122
|
+
if not present or value is None:
|
|
123
|
+
return None
|
|
124
|
+
return self.boolean(field)
|
|
125
|
+
|
|
126
|
+
def integer(
|
|
127
|
+
self,
|
|
128
|
+
field: str,
|
|
129
|
+
*,
|
|
130
|
+
minimum: int = 0,
|
|
131
|
+
maximum: int | None = None,
|
|
132
|
+
) -> int | None:
|
|
133
|
+
present, value = self._value(field)
|
|
134
|
+
if not present:
|
|
135
|
+
return None
|
|
136
|
+
if type(value) is not int:
|
|
137
|
+
self.issues.append(
|
|
138
|
+
_issue(
|
|
139
|
+
f"{self.path}.{field}",
|
|
140
|
+
"invalid_type",
|
|
141
|
+
"int",
|
|
142
|
+
value,
|
|
143
|
+
"expected an integer",
|
|
144
|
+
)
|
|
145
|
+
)
|
|
146
|
+
return None
|
|
147
|
+
|
|
148
|
+
converted = cast(int, value)
|
|
149
|
+
if converted < minimum or (maximum is not None and converted > maximum):
|
|
150
|
+
expected = f"int from {minimum}"
|
|
151
|
+
if maximum is not None:
|
|
152
|
+
expected += f" to {maximum}"
|
|
153
|
+
self.issues.append(
|
|
154
|
+
_issue(
|
|
155
|
+
f"{self.path}.{field}",
|
|
156
|
+
"invalid_value",
|
|
157
|
+
expected,
|
|
158
|
+
value,
|
|
159
|
+
f"expected {expected}",
|
|
160
|
+
actual=repr(value),
|
|
161
|
+
)
|
|
162
|
+
)
|
|
163
|
+
return None
|
|
164
|
+
return converted
|
|
165
|
+
|
|
166
|
+
def date(self, field: str) -> str | None:
|
|
167
|
+
value = self.string(field)
|
|
168
|
+
if value is None:
|
|
169
|
+
return None
|
|
170
|
+
if not _DATE_PATTERN.fullmatch(value):
|
|
171
|
+
self.issues.append(
|
|
172
|
+
_issue(
|
|
173
|
+
f"{self.path}.{field}",
|
|
174
|
+
"invalid_value",
|
|
175
|
+
"date in YYYY-MM-DD format",
|
|
176
|
+
value,
|
|
177
|
+
"expected a date in YYYY-MM-DD format",
|
|
178
|
+
actual=repr(value),
|
|
179
|
+
)
|
|
180
|
+
)
|
|
181
|
+
return None
|
|
182
|
+
try:
|
|
183
|
+
date.fromisoformat(value)
|
|
184
|
+
except ValueError:
|
|
185
|
+
self.issues.append(
|
|
186
|
+
_issue(
|
|
187
|
+
f"{self.path}.{field}",
|
|
188
|
+
"invalid_value",
|
|
189
|
+
"valid calendar date",
|
|
190
|
+
value,
|
|
191
|
+
"expected a valid calendar date",
|
|
192
|
+
actual=repr(value),
|
|
193
|
+
)
|
|
194
|
+
)
|
|
195
|
+
return None
|
|
196
|
+
return value
|
|
197
|
+
|
|
198
|
+
def literal(self, field: str, expected: str) -> str | None:
|
|
199
|
+
value = self.string(field)
|
|
200
|
+
if value is not None and value != expected:
|
|
201
|
+
self.issues.append(
|
|
202
|
+
_issue(
|
|
203
|
+
f"{self.path}.{field}",
|
|
204
|
+
"invalid_value",
|
|
205
|
+
expected,
|
|
206
|
+
value,
|
|
207
|
+
f"expected {expected}",
|
|
208
|
+
actual=repr(value),
|
|
209
|
+
)
|
|
210
|
+
)
|
|
211
|
+
return None
|
|
212
|
+
return value
|
|
213
|
+
|
|
214
|
+
def null(self, field: str) -> None:
|
|
215
|
+
present, value = self._value(field)
|
|
216
|
+
if present and value is not None:
|
|
217
|
+
self.issues.append(
|
|
218
|
+
_issue(
|
|
219
|
+
f"{self.path}.{field}",
|
|
220
|
+
"invalid_value",
|
|
221
|
+
"null",
|
|
222
|
+
value,
|
|
223
|
+
"expected null",
|
|
224
|
+
actual=repr(value),
|
|
225
|
+
)
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
def model(
|
|
229
|
+
self,
|
|
230
|
+
field: str,
|
|
231
|
+
factory: Callable[..., _T],
|
|
232
|
+
) -> _T | None:
|
|
233
|
+
present, value = self._value(field)
|
|
234
|
+
if not present:
|
|
235
|
+
return None
|
|
236
|
+
try:
|
|
237
|
+
return factory(value, path=f"{self.path}.{field}")
|
|
238
|
+
except CalendarContractError as error:
|
|
239
|
+
self.issues.extend(error.issues)
|
|
240
|
+
return None
|
|
241
|
+
|
|
242
|
+
def models(
|
|
243
|
+
self,
|
|
244
|
+
field: str,
|
|
245
|
+
factory: Callable[..., _T],
|
|
246
|
+
) -> tuple[_T, ...] | None:
|
|
247
|
+
present, value = self._value(field)
|
|
248
|
+
if not present:
|
|
249
|
+
return None
|
|
250
|
+
if type(value) is not list:
|
|
251
|
+
self.issues.append(
|
|
252
|
+
_issue(
|
|
253
|
+
f"{self.path}.{field}",
|
|
254
|
+
"invalid_container",
|
|
255
|
+
"list",
|
|
256
|
+
value,
|
|
257
|
+
"expected a list",
|
|
258
|
+
)
|
|
259
|
+
)
|
|
260
|
+
return None
|
|
261
|
+
|
|
262
|
+
converted: list[_T] = []
|
|
263
|
+
valid = True
|
|
264
|
+
for index, item in enumerate(cast(list[object], value)):
|
|
265
|
+
try:
|
|
266
|
+
converted.append(factory(item, path=f"{self.path}.{field}[{index}]"))
|
|
267
|
+
except CalendarContractError as error:
|
|
268
|
+
self.issues.extend(error.issues)
|
|
269
|
+
valid = False
|
|
270
|
+
if not valid:
|
|
271
|
+
return None
|
|
272
|
+
return tuple(converted)
|
|
273
|
+
|
|
274
|
+
def invalid_value(
|
|
275
|
+
self,
|
|
276
|
+
field: str,
|
|
277
|
+
*,
|
|
278
|
+
expected: str,
|
|
279
|
+
actual: object,
|
|
280
|
+
message: str,
|
|
281
|
+
) -> None:
|
|
282
|
+
self.issues.append(
|
|
283
|
+
_issue(
|
|
284
|
+
f"{self.path}.{field}",
|
|
285
|
+
"invalid_value",
|
|
286
|
+
expected,
|
|
287
|
+
actual,
|
|
288
|
+
message,
|
|
289
|
+
actual=repr(actual),
|
|
290
|
+
)
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
def raise_if_invalid(self) -> None:
|
|
294
|
+
if self.issues:
|
|
295
|
+
raise CalendarContractError(tuple(self.issues))
|