feat(urllib): init PkgHandler
This commit is contained in:
parent
59dcc2c7c0
commit
1ea3b3a24d
3 changed files with 45 additions and 0 deletions
0
src/byteb4rb1e/utils/urllib/__init__.py
Normal file
0
src/byteb4rb1e/utils/urllib/__init__.py
Normal file
36
src/byteb4rb1e/utils/urllib/request.py
Normal file
36
src/byteb4rb1e/utils/urllib/request.py
Normal 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)
|
||||||
9
tests/unit/byteb4rb1e/utils/urllib/test_request.py
Normal file
9
tests/unit/byteb4rb1e/utils/urllib/test_request.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
from byteb4rb1e.utils.urllib.request import PkgHandler
|
||||||
|
|
||||||
|
class TestPkgHandler:
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
def test_default(self):
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
pass
|
||||||
Loading…
Add table
Add a link
Reference in a new issue