feat: support template_dirs config settings

Resolves Issue #746
This commit is contained in:
BJ Dierkes 2025-05-09 18:49:17 -05:00
parent 8b038170d8
commit 0cf3873cd1
4 changed files with 69 additions and 7 deletions

View File

@ -8,7 +8,8 @@ Bugs:
Features:
- None
- `[core.foundation]` Support for App.Meta.template_dirs.
- [Issue #746](https://github.com/datafolklabs/cement/issues/749)
Refactoring:

View File

@ -23,3 +23,4 @@ documentation, or testing:
- Christian Hengl (rednar)
- sigma67
- Blake Jameson (blakejameson)
- Tom Freudenberg (TomFreudenberg)

View File

@ -497,6 +497,7 @@ class App(meta.MetaMixin):
'plugin_dir',
'ignore_deprecation_warnings',
'template_dir',
'template_dirs',
'mail_handler',
'cache_handler',
'log_handler',
@ -1521,6 +1522,17 @@ class App(meta.MetaMixin):
'home_dir': fs.HOME_DIR,
}
# Check if template_dirs is overridden by config
config_template_dirs = []
if 'template_dirs' in self.config.keys(self._meta.config_section):
config_template_dirs = self.config.get(self._meta.label, 'template_dirs')
if isinstance(config_template_dirs, str):
config_template_dirs = [x.strip() for x in config_template_dirs.split(',')]
# reverse it becuase it will be reverse again (keep user preference)
config_template_dirs.reverse()
# generate a final list of directories based on precedence (user level
# paths take precedence).
@ -1529,7 +1541,17 @@ class App(meta.MetaMixin):
if d not in template_dirs:
template_dirs.append(d)
for d in self._meta.template_dirs:
# If config overrides template_dirs, use config instead of meta template_dirs
if config_template_dirs:
for d in config_template_dirs:
template_dirs.append(d)
else:
for d in self._meta.template_dirs:
d = d.format(**template_dict)
if d not in template_dirs:
template_dirs.append(d)
for d in self._meta.core_user_template_dirs:
d = d.format(**template_dict)
if d not in template_dirs:
template_dirs.append(d)
@ -1538,11 +1560,6 @@ class App(meta.MetaMixin):
d = self._meta.template_dir.format(**template_dict)
template_dirs.append(d)
for d in self._meta.core_user_template_dirs:
d = d.format(**template_dict)
if d not in template_dirs:
template_dirs.append(d)
# reset final list
self._meta.template_dirs = []

View File

@ -779,6 +779,49 @@ def test_template_dirs(tmp, rando):
assert tmp.dir in app._meta.template_dirs
def test_template_dirs_config_override(tmp, rando):
class ThisTestApp(TestApp):
class Meta:
label = rando
template_dirs = ['/original/path']
with ThisTestApp() as app:
# Set template_dirs in config - should override Meta.template_dirs
app.config.set(app._meta.config_section, 'template_dirs', [tmp.dir, '/config/path2'])
app._setup_template_handler()
assert tmp.dir in app._meta.template_dirs
assert '/config/path2' in app._meta.template_dirs
assert '/original/path' not in app._meta.template_dirs
def test_template_dirs_string_conversion(tmp, rando):
class ThisTestApp(TestApp):
class Meta:
label = rando
with ThisTestApp() as app:
# Set template_dirs as comma-separated string - should convert to list
app.config.set(app._meta.config_section, 'template_dirs', f'{tmp.dir}, /path2 , /path3')
app._setup_template_handler()
assert tmp.dir in app._meta.template_dirs
assert '/path2' in app._meta.template_dirs
assert '/path3' in app._meta.template_dirs
def test_template_dir_and_dirs_interaction(tmp, rando):
class ThisTestApp(TestApp):
class Meta:
label = rando
template_dir = '/single/dir'
with ThisTestApp() as app:
# Both template_dir and template_dirs should be processed
app.config.set(app._meta.config_section, 'template_dirs', [tmp.dir])
app._setup_template_handler()
assert tmp.dir in app._meta.template_dirs
assert '/single/dir' in app._meta.template_dirs
def test_core_system_plugin_dirs(tmp, rando):
class ThisTestApp(TestApp):
class Meta: