feat(urllib): init PkgHandler

This commit is contained in:
Tiara Rodney 2025-06-20 20:54:32 +02:00
parent 59dcc2c7c0
commit 1ea3b3a24d
No known key found for this signature in database
GPG key ID: 5F43FAB4FBE5B5EB
3 changed files with 45 additions and 0 deletions

View file

View file

@ -0,0 +1,36 @@
import importlib.resources
from urllib.request import URLError
import urllib.request
class PkgHandler(urllib.request.BaseHandler):
"""
"""
def pkg_open(self, req):
pkg_files = importlib.resources.files(req.host)
raise Exception(sorted(pkg_files.glob('**/*')))
try:
fh = list(
pkg_files.glob(req.selector.lstrip('//'))
)[0].open('rb')
except Exception as e:
raise URLError(f'{e.__class__.__name__}: {e}') from e
fh.seek(0, 2);
size = fh.tell();
fh.seek(0);
mtype, _ = mimetypes.guess_type(url)
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/'):
fh.close()
fh = importlib.resources.files(req.host).glob(req.selector)[0].open('r')
return urllib.request.addinfourl(fh, header)

View file

@ -0,0 +1,9 @@
from byteb4rb1e.utils.urllib.request import PkgHandler
class TestPkgHandler:
"""
"""
def test_default(self):
"""
"""
pass