-
-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setup flow when registering new template handler #745
Comments
The issue here is:
In case of activating the extension, the module is loaded and There I add the new Handler by Now it's But while the Handler is derrived from an Output/TemplateHandler it's Question: How to register the handler that is in the Hopefully this makes the issue clearer? |
@TomFreudenberg I admit that I do not follow perfectly. The The Jinja2OutputHandler relies on the Jinja2TemplateHandler, so you will want to sub-class/implement both. I've done an example here. Also note, that I implemented the myapp/ext/my_jinja2.py from cement.core.output import OutputHandler
from cement.ext.ext_jinja2 import Jinja2OutputHandler, Jinja2TemplateHandler
### @derks > use a framework hook to run code at the desired runtime
def add_template_dirs(app):
d = app.config.get('my.jinja2', 'template_dirs')
for p in d:
app.add_template_dir(p)
class MyJinja2OutputHandler(Jinja2OutputHandler):
class Meta(Jinja2OutputHandler.Meta):
label = 'my_jinja2'
config_section = 'my.jinja2'
def _setup(self, app) -> None:
### @derks > There is a bug in Jinja2OutputHandler
### Super OutputHandler here instead of MyJinja2OutputHandler
super(OutputHandler, self)._setup(app)
self.templater = self.app.handler.resolve('template', self._meta.label, setup=True) # type: ignore
class MyJinja2TemplateHandler(Jinja2TemplateHandler):
class Meta(Jinja2TemplateHandler.Meta):
"""Handler meta-data."""
label = 'my_jinja2'
#: Id for config
config_section = 'my.jinja2'
#: Configuration default values
config_defaults = {
'template_dirs': [],
}
def __init__(self, *args, **kw):
super(MyJinja2TemplateHandler, self).__init__(*args, **kw)
def _setup(self, app):
super(MyJinja2TemplateHandler, self)._setup(app)
def load(app):
app.handler.register(MyJinja2OutputHandler)
app.handler.register(MyJinja2TemplateHandler)
app.hook.register('pre_run', add_template_dirs) main.py from cement import App, TestApp, init_defaults
from cement.core.exc import CaughtSignal
from .core.exc import MyAppError
from .controllers.base import Base
from .ext.my_jinja2 import Jinja2TemplateHandler
# configuration defaults
CONFIG = init_defaults('myapp')
CONFIG['myapp']['foo'] = 'bar'
class MyApp(App):
class Meta:
label = 'myapp'
# configuration defaults
config_defaults = CONFIG
# call sys.exit() on close
exit_on_close = True
# load additional framework extensions
extensions = [
'yaml',
'colorlog',
'myapp.ext.my_jinja2'
]
# configuration handler
config_handler = 'yaml'
# configuration file suffix
config_file_suffix = '.yml'
# set the log handler
log_handler = 'colorlog'
# set the output handler
output_handler = 'my_jinja2'
# set template Handler
template_handler = 'my_jinja2'
# register handlers
handlers = [
Base
]
class MyAppTest(TestApp,MyApp):
"""A sub-class of MyApp that is better suited for testing."""
class Meta:
label = 'myapp'
def main():
with MyApp() as app:
try:
app.run()
# print(app.config.get_dict())
print(f"APP TEMPLATE DIRS: {app._meta.template_dirs}")
except AssertionError as e:
print('AssertionError > %s' % e.args[0])
app.exit_code = 1
if app.debug is True:
import traceback
traceback.print_exc()
except MyAppError as e:
print('MyAppError > %s' % e.args[0])
app.exit_code = 1
if app.debug is True:
import traceback
traceback.print_exc()
except CaughtSignal as e:
# Default Cement signals are SIGINT and SIGTERM, exit 0 (non-error)
print('\n%s' % e)
app.exit_code = 0
if __name__ == '__main__':
main() Run (debug):
|
Hi @derks
I created a new template handler as extension. Inside the extension there is my
load()
to register.I shortened the source for the issue ...
The
_setup
is now already called on foundation.pyalready on
self._setup_extension_handler()
and not atself._setup_template_handler()
In this case the
config
is not yet upset and my app raises an errorbecause
self._meta.template_dirs
inadd_template_dir
stilNone
.How to register a template handler from an extension, that will run it's setup at the right place?
Thanks for your hint
The text was updated successfully, but these errors were encountered: