1
0
Fork 0

fix: improve backward compat for util.get_liburl()

ready to drop the old logic from tailbone
This commit is contained in:
Lance Edgar 2024-08-15 23:03:39 -05:00
parent f5ac66f264
commit 429f3c69b5
2 changed files with 60 additions and 6 deletions

View file

@ -27,6 +27,7 @@ Web Utilities
import importlib
import json
import logging
import warnings
import sqlalchemy as sa
@ -153,20 +154,34 @@ def get_libver(
config = request.wutta_config
# nb. we prefer a setting to be named like: wuttaweb.libver.vue
# but for back-compat this also can work: wuttaweb.vue_version
# but for back-compat this also can work: tailbone.libver.vue
# and for more back-compat this can work: wuttaweb.vue_version
# however that compat only works for some of the settings...
if not default_only:
# nb. new/preferred setting
version = config.get(f'{prefix}.libver.{key}')
version = config.get(f'wuttaweb.libver.{key}')
if version:
return version
# fallback to caller-specified prefix
if prefix != 'wuttaweb':
version = config.get(f'{prefix}.libver.{key}')
if version:
warnings.warn(f"config for {prefix}.libver.{key} is deprecated; "
f"please set wuttaweb.libver.{key} instead",
DeprecationWarning)
return version
if key == 'buefy':
if not default_only:
# nb. old/legacy setting
version = config.get(f'{prefix}.buefy_version')
if version:
warnings.warn(f"config for {prefix}.buefy_version is deprecated; "
"please set wuttaweb.libver.buefy instead",
DeprecationWarning)
return version
if not configured_only:
return 'latest'
@ -182,6 +197,9 @@ def get_libver(
# nb. old/legacy setting
version = config.get(f'{prefix}.vue_version')
if version:
warnings.warn(f"config for {prefix}.vue_version is deprecated; "
"please set wuttaweb.libver.vue instead",
DeprecationWarning)
return version
if not configured_only:
return '2.6.14'
@ -293,16 +311,35 @@ def get_liburl(
config = request.wutta_config
if not default_only:
# nb. new/preferred setting
url = config.get(f'wuttaweb.liburl.{key}')
if url:
return url
# fallback to caller-specified prefix
url = config.get(f'{prefix}.liburl.{key}')
if url:
warnings.warn(f"config for {prefix}.liburl.{key} is deprecated; "
f"please set wuttaweb.liburl.{key} instead",
DeprecationWarning)
return url
if configured_only:
return
version = get_libver(request, key, prefix=prefix)
version = get_libver(request, key, prefix=prefix,
configured_only=False,
default_only=default_only)
# load fanstatic libcache if configured
static = config.get('wuttaweb.static_libcache.module')
if not static:
static = config.get(f'{prefix}.static_libcache.module')
if static:
warnings.warn(f"config for {prefix}.static_libcache.module is deprecated; "
"please set wuttaweb.static_libcache.module instead",
DeprecationWarning)
if static:
static = importlib.import_module(static)
needed = request.environ['fanstatic.needed']

View file

@ -59,6 +59,11 @@ class TestGetLibVer(TestCase):
version = util.get_libver(self.request, 'buefy')
self.assertEqual(version, '0.9.29')
def test_buefy_custom_old_tailbone(self):
self.config.setdefault('tailbone.libver.buefy', '0.9.28')
version = util.get_libver(self.request, 'buefy', prefix='tailbone')
self.assertEqual(version, '0.9.28')
def test_buefy_custom_new(self):
self.config.setdefault('wuttaweb.libver.buefy', '0.9.29')
version = util.get_libver(self.request, 'buefy')
@ -221,10 +226,11 @@ class TestGetLibUrl(TestCase):
def tearDown(self):
testing.tearDown()
def setup_fanstatic(self):
def setup_fanstatic(self, register=True):
self.pyramid_config.include('pyramid_fanstatic')
self.config.setdefault('wuttaweb.static_libcache.module',
'tests.test_util')
if register:
self.config.setdefault('wuttaweb.static_libcache.module',
'tests.test_util')
needed = MagicMock()
needed.library_url = MagicMock(return_value='/fanstatic')
@ -240,6 +246,11 @@ class TestGetLibUrl(TestCase):
url = util.get_liburl(self.request, 'buefy')
self.assertEqual(url, '/lib/buefy.js')
def test_buefy_custom_tailbone(self):
self.config.setdefault('tailbone.liburl.buefy', '/tailbone/buefy.js')
url = util.get_liburl(self.request, 'buefy', prefix='tailbone')
self.assertEqual(url, '/tailbone/buefy.js')
def test_buefy_default_only(self):
self.config.setdefault('wuttaweb.liburl.buefy', '/lib/buefy.js')
url = util.get_liburl(self.request, 'buefy', default_only=True)
@ -254,6 +265,12 @@ class TestGetLibUrl(TestCase):
url = util.get_liburl(self.request, 'buefy')
self.assertEqual(url, '/wutta/fanstatic/buefy.js')
def test_buefy_fanstatic_tailbone(self):
self.setup_fanstatic(register=False)
self.config.setdefault('tailbone.static_libcache.module', 'tests.test_util')
url = util.get_liburl(self.request, 'buefy', prefix='tailbone')
self.assertEqual(url, '/wutta/fanstatic/buefy.js')
def test_buefy_css_default(self):
url = util.get_liburl(self.request, 'buefy.css')
self.assertEqual(url, 'https://unpkg.com/buefy@latest/dist/buefy.min.css')