python-reflexion/01-importers/http_importer.py
2025-10-21 20:38:10 +02:00

49 lines
1.4 KiB
Python

from importlib import _bootstrap_external,_bootstrap
import urllib.request
from urllib.parse import urlparse
from urllib.error import HTTPError
class HttpImporter(_bootstrap_external._LoaderBasics):
def __init__(self, path):
if not isinstance(path, str):
raise TypeError(f"expected str, not {type(path)!r}")
url = urlparse(path)
if not url.scheme in ('http', 'https'):
raise ValueError(f'url scheme not supported: `{url.scheme}`')
self.base_url = url.geturl().rstrip()
def find_spec(self, fullname, target=None):
module_url = self.base_url + '/' + fullname.rpartition('.')[2]
urls = (
(module_url + '/__init__.py', True),
(module_url + '.py', False)
)
for (url, is_package) in urls:
try:
with urllib.request.urlopen(url) as res:
self.data = res.read()
self.url = url
except HTTPError:
continue
spec = _bootstrap.ModuleSpec(
fullname,
self, # loader
origin=url,
is_package=is_package
)
if is_package:
spec.submodule_search_locations.append(module_url)
return spec
return None
def get_code(self, fullname):
return compile(self.data, self.url, 'exec', dont_inherit=True)