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

Add support for hooks/handlers on CementApp.Meta #282

Closed
derks opened this Issue Sep 30, 2014 · 2 comments

Comments

Projects
None yet
1 participant
@derks
Member

derks commented Sep 30, 2014

Should be able to do the following:

class MyApp(CementApp):
    class Meta:
        label = 'myapp'

        hooks = [
            ('pre_run', some_hook_function),
            ('pre_close', some_cleanup_function),
        ]

        handlers = [
            MyBaseController,
            MySecondController,
            MyThirdController,
        ]

Basically eleviating the need to call hook.register and handler.register for single file scripts, or multi-file applications that just want to import controllers in from other files and not use CementApp.Meta.bootstrap.

@derks derks self-assigned this Sep 30, 2014

@derks derks added this to the 2.5.2 Development milestone Sep 30, 2014

@derks

This comment has been minimized.

Show comment
Hide comment
@derks

derks Sep 30, 2014

Member

The above would obviously need to be run early on, making it possible to do the following:

with MyApp() as app:
    app.run()

And not the current process which looks like the following (becuase controllers/handlers need to be registered after app creation and before setup):

app = MyApp()

handler.register(MyBaseController)
handler.register(MySecondController)
# ... etc

app.setup()
app.run()
app.close()
Member

derks commented Sep 30, 2014

The above would obviously need to be run early on, making it possible to do the following:

with MyApp() as app:
    app.run()

And not the current process which looks like the following (becuase controllers/handlers need to be registered after app creation and before setup):

app = MyApp()

handler.register(MyBaseController)
handler.register(MySecondController)
# ... etc

app.setup()
app.run()
app.close()

@derks derks added the 1 - Ready label Feb 25, 2015

@derks derks removed the 1 - Ready label May 6, 2015

derks added a commit that referenced this issue May 6, 2015

derks added a commit that referenced this issue May 6, 2015

@derks

This comment has been minimized.

Show comment
Hide comment
@derks

derks May 6, 2015

Member

This has been merged into cement/master. Hooks and handlers can now be defined and registered directly from CementApp.Meta:

from cement.core import handler, hook
from cement.core.foundation import CementApp
from cement.core.controller import CementBaseController, expose
from cement.core.interface import Interface

class MyCustomInterface(Interface):
    class IMeta:
        label = 'my_custom_interface'

def my_arg_hook(app):
    print('Inside my_arg_hook')

def my_custom_hook(app):
    print('Inside my_custom_hook')

class BaseController(CementBaseController):
    class Meta:
        label = 'base'

    @expose(hide=True)
    def default(self):
        for res in hook.run('my_hook', self.app):
            pass

        print('Inside MyController.default()')

class SecondController(CementBaseController):
    class Meta:
        label = 'second'
        stacked_on = 'base'
        stacked_type = 'nested'

    @expose(hide=True)
    def default(self):
        print('Inside SecondController.default()')


class MyApp(CementApp):
    class Meta:
        label = 'myapp'
        base_controller = 'base'

        define_hooks = [
            'my_hook',
            ]

        define_handlers = [
            MyCustomInterface,
            ]

        hooks = [
            ('post_argument_parsing', my_arg_hook),
            ('my_hook', my_custom_hook),
            ]

        handlers = [
            BaseController,
            SecondController,
            ]

with MyApp() as app:
    try:
        app.run()
    except Exception as e:
        print(e)
Member

derks commented May 6, 2015

This has been merged into cement/master. Hooks and handlers can now be defined and registered directly from CementApp.Meta:

from cement.core import handler, hook
from cement.core.foundation import CementApp
from cement.core.controller import CementBaseController, expose
from cement.core.interface import Interface

class MyCustomInterface(Interface):
    class IMeta:
        label = 'my_custom_interface'

def my_arg_hook(app):
    print('Inside my_arg_hook')

def my_custom_hook(app):
    print('Inside my_custom_hook')

class BaseController(CementBaseController):
    class Meta:
        label = 'base'

    @expose(hide=True)
    def default(self):
        for res in hook.run('my_hook', self.app):
            pass

        print('Inside MyController.default()')

class SecondController(CementBaseController):
    class Meta:
        label = 'second'
        stacked_on = 'base'
        stacked_type = 'nested'

    @expose(hide=True)
    def default(self):
        print('Inside SecondController.default()')


class MyApp(CementApp):
    class Meta:
        label = 'myapp'
        base_controller = 'base'

        define_hooks = [
            'my_hook',
            ]

        define_handlers = [
            MyCustomInterface,
            ]

        hooks = [
            ('post_argument_parsing', my_arg_hook),
            ('my_hook', my_custom_hook),
            ]

        handlers = [
            BaseController,
            SecondController,
            ]

with MyApp() as app:
    try:
        app.run()
    except Exception as e:
        print(e)

@derks derks closed this May 6, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment