Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Bugs:

Features:

- None
- `[core.foundation]` Support for App.Meta.template_dirs.
- [Issue #746](https://github.com/datafolklabs/cement/issues/749)
Comment on lines +11 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect issue reference.

The changelog entry references Issue #749, but according to the PR objectives, this feature addresses Issue #746.

-  - [Issue #746](https://github.com/datafolklabs/cement/issues/749)
+  - [Issue #746](https://github.com/datafolklabs/cement/issues/746)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- `[core.foundation]` Support for App.Meta.template_dirs.
- [Issue #746](https://github.com/datafolklabs/cement/issues/749)
- `[core.foundation]` Support for App.Meta.template_dirs.
- [Issue #746](https://github.com/datafolklabs/cement/issues/746)
🤖 Prompt for AI Agents
In CHANGELOG.md around lines 11 to 12, the issue reference is incorrect; it
currently points to Issue #749 instead of the correct Issue #746. Update the
changelog entry to reference Issue #746 to accurately reflect the PR objectives.


Refactoring:

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ documentation, or testing:
- Christian Hengl (rednar)
- sigma67
- Blake Jameson (blakejameson)
- Tom Freudenberg (TomFreudenberg)
29 changes: 23 additions & 6 deletions cement/core/foundation.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ class Meta:
'plugin_dir',
'ignore_deprecation_warnings',
'template_dir',
'template_dirs',
'mail_handler',
'cache_handler',
'log_handler',
Expand Down Expand Up @@ -1521,6 +1522,17 @@ def _setup_template_handler(self) -> None:
'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).

Expand All @@ -1529,7 +1541,17 @@ def _setup_template_handler(self) -> None:
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)
Expand All @@ -1538,11 +1560,6 @@ def _setup_template_handler(self) -> None:
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 = []

Expand Down
43 changes: 43 additions & 0 deletions tests/core/test_foundation.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,49 @@ class Meta:
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:
Expand Down