-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathitems.py
71 lines (51 loc) · 2.2 KB
/
items.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import enum
import io
import zipfile
from typing import Any, TypedDict
import pydantic
import rarfile
Data = (
# "in read binary mode, it returns an io.BufferedReader" https://docs.python.org/3/library/functions.html#open
# https://docs.python.org/3/library/tarfile.html#tarfile.TarFile.extractfile
io.BufferedReader
# https://rarfile.readthedocs.io/api.html#rarfile.RarFile.open (CompressedFileSpider)
| rarfile.RarExtFile
# https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.open (CompressedFileSpider)
| zipfile.ZipExtFile
| pydantic.conbytes(strict=True, min_length=1)
# `dict` behaves better last. https://github.com/open-contracting/kingfisher-collect/issues/995
| dict
)
base_kwargs = {'validate_assignment': True}
class DataType(str, enum.Enum):
record = "record"
release = "release"
record_package = "record_package"
release_package = "release_package"
class Errors(TypedDict):
http_code: pydantic.conint(strict=True, ge=100, lt=600)
class Resource(pydantic.BaseModel, **base_kwargs):
file_name: pydantic.constr(strict=True, regex=r'^[^/\\]+$')
url: pydantic.HttpUrl
class DataResource(Resource, arbitrary_types_allowed=True, use_enum_values=True):
data_type: DataType
data: Data
# Added by the FilesStore extension, for the KingfisherProcessAPI2 extension to refer to the file.
path: str = ""
@pydantic.validator('data', pre=True) # `pre` is needed to prevent pydantic from type casting
def check_data(cls, v):
# pydantic has no `condict()` to set `strict=True` or `min_properties=1`. pydantic/pydantic#1277
if not isinstance(v, Data | bytes):
raise AssertionError(f'{type(v).__name__} is not a valid type') # noqa: TRY004 # false positive
if not v:
raise AssertionError('ensure this value is non-empty')
return v
class File(DataResource):
pass
# This doesn't inherit from the File class, because we want isinstance(item, File) to be false for FileItem instances.
class FileItem(DataResource):
number: pydantic.conint(strict=True, gt=0)
class FileError(Resource):
errors: Errors
class PluckedItem(pydantic.BaseModel, **base_kwargs):
value: Any