summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheron Luhn <theron@luhn.com>2025-08-24 21:35:04 -0700
committerTheron Luhn <theron@luhn.com>2025-08-24 21:35:04 -0700
commit832cae47693b4c07a1fa826dce13b9af7a91ebaf (patch)
treee9b93a8a823de29ef65592b8eb9471910f2dc384
parenteb0e01a0fd7aff84c345b1c43bd4fa9d2fe04441 (diff)
downloadpyramid-832cae47693b4c07a1fa826dce13b9af7a91ebaf.tar.gz
pyramid-832cae47693b4c07a1fa826dce13b9af7a91ebaf.tar.bz2
pyramid-832cae47693b4c07a1fa826dce13b9af7a91ebaf.zip
Add `get_spec` to asset sources.
-rw-r--r--src/pyramid/config/assets.py8
-rw-r--r--tests/test_config/test_assets.py35
2 files changed, 43 insertions, 0 deletions
diff --git a/src/pyramid/config/assets.py b/src/pyramid/config/assets.py
index 6f2ddbe4a..2838db1e5 100644
--- a/src/pyramid/config/assets.py
+++ b/src/pyramid/config/assets.py
@@ -223,6 +223,11 @@ class PackageAssetSource:
def get_path(self, resource_name):
return f'{self.prefix}{resource_name}'
+ def get_spec(self, resource_name):
+ path = self.get_path(resource_name)
+ if pkg_resources.resource_exists(self.pkg_name, path):
+ return f'{self.pkg_name}:{path}'
+
def get_filename(self, resource_name):
path = self.get_path(resource_name)
if pkg_resources.resource_exists(self.pkg_name, path):
@@ -270,6 +275,9 @@ class FSAssetSource:
path = self.prefix
return path
+ def get_spec(self, resource_name):
+ return self.get_filename(resource_name)
+
def get_filename(self, resource_name):
path = self.get_path(resource_name)
if os.path.exists(path):
diff --git a/tests/test_config/test_assets.py b/tests/test_config/test_assets.py
index 1d2cfcd5c..5c535ec6c 100644
--- a/tests/test_config/test_assets.py
+++ b/tests/test_config/test_assets.py
@@ -916,6 +916,24 @@ class TestPackageAssetSource(AssetSourceIntegrationTests, unittest.TestCase):
klass = self._getTargetClass()
return klass(package, prefix)
+ def test_get_spec(self):
+ source = self._makeOne('')
+ self.assertEqual(
+ source.get_spec('test_assets.py'),
+ 'tests.test_config:test_assets.py',
+ )
+
+ def test_get_spec_with_prefix(self):
+ source = self._makeOne('test_assets.py')
+ self.assertEqual(
+ source.get_spec(''),
+ 'tests.test_config:test_assets.py',
+ )
+
+ def test_get_spec_file_doesnt_exist(self):
+ source = self._makeOne('')
+ self.assertIsNone(source.get_spec('wont_exist'))
+
class TestFSAssetSource(AssetSourceIntegrationTests, unittest.TestCase):
def _getTargetClass(self):
@@ -927,6 +945,23 @@ class TestFSAssetSource(AssetSourceIntegrationTests, unittest.TestCase):
klass = self._getTargetClass()
return klass(os.path.join(base_prefix, prefix))
+ def test_get_spec(self):
+ source = self._makeOne('')
+ self.assertEqual(
+ source.get_spec('test_assets.py'),
+ os.path.join(here, 'test_assets.py'),
+ )
+
+ def test_get_spec_with_prefix(self):
+ source = self._makeOne('test_assets.py')
+ self.assertEqual(
+ source.get_spec(''), os.path.join(here, 'test_assets.py')
+ )
+
+ def test_get_spec_file_doesnt_exist(self):
+ source = self._makeOne('')
+ self.assertEqual(source.get_spec('wont_exist'), None)
+
class TestDirectoryOverride(unittest.TestCase):
def _getTargetClass(self):