-
-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create MediaTypeDict class for range matching (#1603)
- Loading branch information
1 parent
b8bdcc9
commit 9d7258c
Showing
8 changed files
with
95 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from fnmatch import fnmatch | ||
|
||
|
||
class MediaTypeDict(dict): | ||
""" | ||
A dictionary where keys can be either media types or media type ranges. When fetching a | ||
value from the dictionary, the provided key is checked against the ranges. The most specific | ||
key is chosen as prescribed by the OpenAPI spec, with `type/*` being preferred above | ||
`*/subtype`. | ||
""" | ||
|
||
def __getitem__(self, item): | ||
# Sort keys in order of specificity | ||
for key in sorted(self, key=lambda k: ("*" not in k, k), reverse=True): | ||
if fnmatch(item, key): | ||
return super().__getitem__(key) | ||
raise super().__getitem__(item) | ||
|
||
def get(self, item, default=None): | ||
try: | ||
return self[item] | ||
except KeyError: | ||
return default | ||
|
||
def __contains__(self, item): | ||
try: | ||
self[item] | ||
except KeyError: | ||
return False | ||
else: | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters