Merged in master (pull request #26)
fix(init): add custom context manager for pkg resources
This commit is contained in:
commit
f607529be3
3 changed files with 35 additions and 5 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
[metadata]
|
[metadata]
|
||||||
name = httpaste-victorykit
|
name = httpaste-victorykit
|
||||||
version = 1.0.7-alpha
|
version = 1.0.8-alpha
|
||||||
author = Tiara Rodney
|
author = Tiara Rodney
|
||||||
author_email = t.rodney@victoryk.it
|
author_email = t.rodney@victoryk.it
|
||||||
description = a versatile HTTP pastebin
|
description = a versatile HTTP pastebin
|
||||||
|
|
|
||||||
|
|
@ -144,14 +144,13 @@ from configparser import ConfigParser
|
||||||
from ast import literal_eval
|
from ast import literal_eval
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from os import environ
|
from os import environ
|
||||||
from importlib.resources import path as pkg_resource_path
|
|
||||||
|
|
||||||
from connexion import FlaskApp
|
from connexion import FlaskApp
|
||||||
from connexion.resolver import RestyResolver
|
from connexion.resolver import RestyResolver
|
||||||
|
|
||||||
from httpaste.model import Backend
|
from httpaste.model import Backend
|
||||||
from httpaste.backend import get_backend_map
|
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 (
|
from httpaste.helper.http import (
|
||||||
BadRequestError,
|
BadRequestError,
|
||||||
ForbiddenError,
|
ForbiddenError,
|
||||||
|
|
@ -303,7 +302,8 @@ def get_flask_app(
|
||||||
options = {"swagger_ui": server_config.swagger_ui}
|
options = {"swagger_ui": server_config.swagger_ui}
|
||||||
|
|
||||||
#context manager returns a pathlib.Path object
|
#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 = FlaskApp(__name__, specification_dir=path.parent)
|
||||||
|
|
||||||
application.add_api(
|
application.add_api(
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,11 @@
|
||||||
from random import choice
|
from random import choice
|
||||||
from base64 import b64decode
|
from base64 import b64decode
|
||||||
from urllib.parse import urljoin
|
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):
|
class DecodeError(Exception):
|
||||||
"""
|
"""
|
||||||
|
|
@ -30,3 +35,28 @@ def decode(data: str, encoding: str) -> bytes:
|
||||||
def join_url(base:str, url: str) -> str:
|
def join_url(base:str, url: str) -> str:
|
||||||
|
|
||||||
return urljoin(base, url, True)
|
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()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue