]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/werkzeug/datastructures/etag.py
1 from __future__
import annotations
3 from collections
.abc
import Collection
6 class ETags(Collection
):
7 """A set that can be used to check if one etag is present in a collection
11 def __init__(self
, strong_etags
=None, weak_etags
=None, star_tag
=False):
12 if not star_tag
and strong_etags
:
13 self
._strong
= frozenset(strong_etags
)
15 self
._strong
= frozenset()
17 self
._weak
= frozenset(weak_etags
or ())
18 self
.star_tag
= star_tag
20 def as_set(self
, include_weak
=False):
21 """Convert the `ETags` object into a python set. Per default all the
22 weak etags are not part of this set."""
23 rv
= set(self
._strong
)
28 def is_weak(self
, etag
):
29 """Check if an etag is weak."""
30 return etag
in self
._weak
32 def is_strong(self
, etag
):
33 """Check if an etag is strong."""
34 return etag
in self
._strong
36 def contains_weak(self
, etag
):
37 """Check if an etag is part of the set including weak and strong tags."""
38 return self
.is_weak(etag
) or self
.contains(etag
)
40 def contains(self
, etag
):
41 """Check if an etag is part of the set ignoring weak tags.
42 It is also possible to use the ``in`` operator.
46 return self
.is_strong(etag
)
48 def contains_raw(self
, etag
):
49 """When passed a quoted tag it will check if this tag is part of the
50 set. If the tag is weak it is checked against weak and strong tags,
51 otherwise strong only."""
52 from ..http
import unquote_etag
54 etag
, weak
= unquote_etag(etag
)
56 return self
.contains_weak(etag
)
57 return self
.contains(etag
)
60 """Convert the etags set into a HTTP header string."""
64 [f
'"{x}"' for x
in self
._strong
] + [f
'W/"{x}"' for x
in self
._weak
]
67 def __call__(self
, etag
=None, data
=None, include_weak
=False):
68 if [etag
, data
].count(None) != 1:
69 raise TypeError("either tag or data required, but at least one")
71 from ..http
import generate_etag
73 etag
= generate_etag(data
)
75 if etag
in self
._weak
:
77 return etag
in self
._strong
80 return bool(self
.star_tag
or self
._strong
or self
._weak
)
83 return self
.to_header()
86 return len(self
._strong
)
89 return iter(self
._strong
)
91 def __contains__(self
, etag
):
92 return self
.contains(etag
)
95 return f
"<{type(self).__name__} {str(self)!r}>"