Skip to content

Commit

Permalink
Add types to media types script and module
Browse files Browse the repository at this point in the history
Split from #410.
  • Loading branch information
adamchainz committed Nov 3, 2022
1 parent 68fdf10 commit b6af746
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions scripts/generate_default_media_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
media_types_py = module_dir / "../src/whitenoise/media_types.py"


def main():
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--check", action="store_true")
args = parser.parse_args()
Expand Down Expand Up @@ -50,7 +50,7 @@ def main():


FUNCTION_TEMPLATE = '''\
def default_types():
def default_types() -> dict[str, str]:
"""
We use our own set of default media types rather than the system-supplied
ones. This ensures consistent media type behaviour across varied
Expand All @@ -64,7 +64,7 @@ def default_types():
}}'''


def get_default_types_function():
def get_default_types_function() -> str:
types_map = get_types_map()
lines = [
f' "{suffix}": "{media_type}",'
Expand All @@ -73,7 +73,7 @@ def get_default_types_function():
return FUNCTION_TEMPLATE.format(entries="\n".join(lines))


def get_types_map():
def get_types_map() -> dict[str, str]:
nginx_data = get_nginx_data()
matches = re.findall(r"(\w+/.*?)\s+(.*?);", nginx_data)
types_map = {}
Expand All @@ -91,7 +91,7 @@ def get_types_map():
return dict(sorted(types_map.items()))


def get_nginx_data():
def get_nginx_data() -> str:
conn = http.client.HTTPSConnection("raw.githubusercontent.com")
with closing(conn):
conn.request("GET", "/nginx/nginx/master/conf/mime.types")
Expand Down
6 changes: 3 additions & 3 deletions src/whitenoise/media_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
class MediaTypes:
__slots__ = ("types_map",)

def __init__(self, *, extra_types=None):
def __init__(self, *, extra_types: dict[str, str] | None = None) -> None:
self.types_map = default_types()
if extra_types is not None:
self.types_map.update(extra_types)

def get_type(self, path):
def get_type(self, path: str) -> str:
name = os.path.basename(path).lower()
media_type = self.types_map.get(name)
if media_type is not None:
Expand All @@ -20,7 +20,7 @@ def get_type(self, path):
return self.types_map.get(extension, "application/octet-stream")


def default_types():
def default_types() -> dict[str, str]:
"""
We use our own set of default media types rather than the system-supplied
ones. This ensures consistent media type behaviour across varied
Expand Down

0 comments on commit b6af746

Please sign in to comment.