diff --git a/setup.cfg b/setup.cfg index 0382db4..d05333e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = httpaste-victorykit -version = 1.0.7-alpha +version = 1.0.8-alpha author = Tiara Rodney author_email = t.rodney@victoryk.it description = a versatile HTTP pastebin diff --git a/src/httpaste/__init__.py b/src/httpaste/__init__.py index ca5aeff..999721b 100755 --- a/src/httpaste/__init__.py +++ b/src/httpaste/__init__.py @@ -144,14 +144,13 @@ from configparser import ConfigParser from ast import literal_eval from io import StringIO from os import environ -from importlib.resources import path as pkg_resource_path from connexion import FlaskApp from connexion.resolver import RestyResolver from httpaste.model import Backend from httpaste.backend import get_backend_map -from httpaste.helper.common import generate_random_string +from httpaste.helper.common import (generate_random_string, tmp_pkg_resource_text_path) from httpaste.helper.http import ( BadRequestError, ForbiddenError, @@ -303,7 +302,8 @@ def get_flask_app( options = {"swagger_ui": server_config.swagger_ui} #context manager returns a pathlib.Path object - with pkg_resource_path('httpaste.schema', 'httpaste.openapi.json') as path: + with tmp_pkg_resource_text_path('httpaste.schema', 'httpaste.openapi.json') as path: + application = FlaskApp(__name__, specification_dir=path.parent) application.add_api( diff --git a/src/httpaste/helper/common.py b/src/httpaste/helper/common.py index 92cc6d7..8edacd1 100644 --- a/src/httpaste/helper/common.py +++ b/src/httpaste/helper/common.py @@ -1,6 +1,11 @@ from random import choice from base64 import b64decode from urllib.parse import urljoin +from importlib.resources import read_text +from tempfile import mkdtemp +from pathlib import Path +from contextlib import contextmanager + class DecodeError(Exception): """ @@ -29,4 +34,29 @@ def decode(data: str, encoding: str) -> bytes: def join_url(base:str, url: str) -> str: - return urljoin(base, url, True) \ No newline at end of file + return urljoin(base, url, True) + + +@contextmanager +def tmp_pkg_resource_text_path(package:str, resource:str) -> Path: + """context manager for accessing package resources from a real path + + this applies to the circumstance of the package living inside of an + egg and therefore is unable to provide real existing paths to any + module that may require it. + + :param package: dot seperated package name + :param resource: basename of resource inside package + + :returns: a Path-like object + """ + data = read_text(package, resource) + tmp_dirname = mkdtemp() + tmp_dirpath = Path(tmp_dirname) + tmp_file = tmp_dirpath.joinpath(resource) + tmp_file.write_text(data) + try: + yield tmp_file + finally: + tmp_file.unlink() + tmp_dirpath.rmdir() \ No newline at end of file