|
23 | 23 | read_mime_types(file) -- parse one file, return a dictionary or None
|
24 | 24 | """
|
25 | 25 |
|
26 |
| -import os |
27 |
| -import sys |
28 |
| -import posixpath |
29 |
| -import urllib.parse |
30 |
| - |
31 | 26 | try:
|
32 | 27 | from _winapi import _mimetypes_read_windows_registry
|
33 | 28 | except ImportError:
|
@@ -119,6 +114,10 @@ def guess_type(self, url, strict=True):
|
119 | 114 | Optional 'strict' argument when False adds a bunch of commonly found,
|
120 | 115 | but non-standard types.
|
121 | 116 | """
|
| 117 | + # Lazy import to improve module import time |
| 118 | + import os |
| 119 | + import urllib.parse |
| 120 | + |
122 | 121 | # TODO: Deprecate accepting file paths (in particular path-like objects).
|
123 | 122 | url = os.fspath(url)
|
124 | 123 | p = urllib.parse.urlparse(url)
|
@@ -146,13 +145,20 @@ def guess_type(self, url, strict=True):
|
146 | 145 | if '=' in type or '/' not in type:
|
147 | 146 | type = 'text/plain'
|
148 | 147 | return type, None # never compressed, so encoding is None
|
| 148 | + |
| 149 | + # Lazy import to improve module import time |
| 150 | + import posixpath |
| 151 | + |
149 | 152 | return self._guess_file_type(url, strict, posixpath.splitext)
|
150 | 153 |
|
151 | 154 | def guess_file_type(self, path, *, strict=True):
|
152 | 155 | """Guess the type of a file based on its path.
|
153 | 156 |
|
154 | 157 | Similar to guess_type(), but takes file path instead of URL.
|
155 | 158 | """
|
| 159 | + # Lazy import to improve module import time |
| 160 | + import os |
| 161 | + |
156 | 162 | path = os.fsdecode(path)
|
157 | 163 | path = os.path.splitdrive(path)[1]
|
158 | 164 | return self._guess_file_type(path, strict, os.path.splitext)
|
@@ -399,6 +405,9 @@ def init(files=None):
|
399 | 405 | else:
|
400 | 406 | db = _db
|
401 | 407 |
|
| 408 | + # Lazy import to improve module import time |
| 409 | + import os |
| 410 | + |
402 | 411 | for file in files:
|
403 | 412 | if os.path.isfile(file):
|
404 | 413 | db.read(file)
|
@@ -445,7 +454,7 @@ def _default_mime_types():
|
445 | 454 | }
|
446 | 455 |
|
447 | 456 | # Before adding new types, make sure they are either registered with IANA,
|
448 |
| - # at http://www.iana.org/assignments/media-types |
| 457 | + # at https://www.iana.org/assignments/media-types/media-types.xhtml |
449 | 458 | # or extensions, i.e. using the x- prefix
|
450 | 459 |
|
451 | 460 | # If you add to these, please keep them sorted by mime type.
|
@@ -646,6 +655,7 @@ def _default_mime_types():
|
646 | 655 |
|
647 | 656 | def _main():
|
648 | 657 | import getopt
|
| 658 | + import sys |
649 | 659 |
|
650 | 660 | USAGE = """\
|
651 | 661 | Usage: mimetypes.py [options] type
|
|
0 commit comments