feat(urllib.request): add importlib resource handler
This commit is contained in:
parent
24806959bb
commit
59713aefb8
2 changed files with 90 additions and 11 deletions
|
|
@ -1,4 +1,6 @@
|
|||
import email
|
||||
import importlib.resources
|
||||
import mimetypes
|
||||
from urllib.request import URLError
|
||||
import urllib.request
|
||||
|
||||
|
|
@ -6,15 +8,13 @@ import urllib.request
|
|||
class PkgHandler(urllib.request.BaseHandler):
|
||||
"""
|
||||
"""
|
||||
def pkg_open(self, req):
|
||||
def pkg_open(self, req) -> urllib.request.addinfourl:
|
||||
pkg_files = importlib.resources.files(req.host)
|
||||
|
||||
raise Exception(sorted(pkg_files.glob('**/*')))
|
||||
|
||||
try:
|
||||
fh = list(
|
||||
fh = next(
|
||||
pkg_files.glob(req.selector.lstrip('//'))
|
||||
)[0].open('rb')
|
||||
).open('rb')
|
||||
except Exception as e:
|
||||
raise URLError(f'{e.__class__.__name__}: {e}') from e
|
||||
|
||||
|
|
@ -22,15 +22,17 @@ class PkgHandler(urllib.request.BaseHandler):
|
|||
size = fh.tell();
|
||||
fh.seek(0);
|
||||
|
||||
mtype, _ = mimetypes.guess_type(url)
|
||||
mtype, _ = mimetypes.guess_type(req.selector)
|
||||
|
||||
headers = email.message_from_string(
|
||||
'Content-Type: %s\nContent-Length: %d\n' %
|
||||
(mtype or 'text/plain', size)
|
||||
)
|
||||
|
||||
if not mtype or mtype.starts_with('text/'):
|
||||
if not mtype or mtype.startswith('text/'):
|
||||
fh.close()
|
||||
fh = importlib.resources.files(req.host).glob(req.selector)[0].open('r')
|
||||
fh = next(
|
||||
pkg_files.glob(req.selector.lstrip('//'))
|
||||
).open('r')
|
||||
|
||||
return urllib.request.addinfourl(fh, header)
|
||||
return urllib.request.addinfourl(fh, headers, None)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue