2424"""
2525
2626import sys
27+ import os
28+ import posixpath
29+ lazy import urllib .parse
30+ lazy import warnings
31+ lazy from argparse import ArgumentParser
32+
2733
2834try :
2935 from _winapi import _mimetypes_read_windows_registry
@@ -132,17 +138,13 @@ def guess_type(self, url, strict=True):
132138 Optional 'strict' argument when False adds a bunch of commonly found,
133139 but non-standard types.
134140 """
135- # Lazy import to improve module import time
136- import os
137- import urllib .parse
138141
139142 url = os .fspath (url )
140143 p = urllib .parse .urlparse (url )
141144 if p .scheme and len (p .scheme ) > 1 :
142145 scheme = p .scheme
143146 url = p .path
144147 else :
145- import warnings
146148 warnings .warn (
147149 "Passing a file path to guess_type() is deprecated and will be "
148150 "removed in a future version. Use guess_file_type() instead." ,
@@ -170,19 +172,13 @@ def guess_type(self, url, strict=True):
170172 type = 'text/plain'
171173 return type , None # never compressed, so encoding is None
172174
173- # Lazy import to improve module import time
174- import posixpath
175-
176175 return self ._guess_file_type (url , strict , posixpath .splitext )
177176
178177 def guess_file_type (self , path , * , strict = True ):
179178 """Guess the type of a file based on its path.
180179
181180 Similar to guess_type(), but takes file path instead of URL.
182181 """
183- # Lazy import to improve module import time
184- import os
185-
186182 path = os .fsdecode (path )
187183 path = os .path .splitdrive (path )[1 ]
188184 return self ._guess_file_type (path , strict , os .path .splitext )
@@ -442,9 +438,6 @@ def init(files=None):
442438 else :
443439 db = _db
444440
445- # Lazy import to improve module import time
446- import os
447-
448441 for file in files :
449442 if os .path .isfile (file ):
450443 db .read (file )
@@ -732,8 +725,6 @@ def _default_mime_types():
732725
733726
734727def _parse_args (args ):
735- from argparse import ArgumentParser
736-
737728 parser = ArgumentParser (
738729 description = 'map filename extensions to MIME types' ,
739730 )
@@ -766,7 +757,6 @@ def _main(args=None):
766757 results .append (f"error: unknown type { gtype } " )
767758 return results
768759 else :
769- import urllib .parse
770760 for gtype in args .type :
771761 p = urllib .parse .urlparse (gtype )
772762 if p .scheme and len (p .scheme ) > 1 :
@@ -781,8 +771,6 @@ def _main(args=None):
781771
782772
783773if __name__ == '__main__' :
784- import sys
785-
786774 results = _main ()
787775 print ("\n " .join (results ))
788776 sys .exit (any (result .startswith ("error: " ) for result in results ))
0 commit comments