myapp.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from cement.core import foundation, handler
  2. from cement.core.controller import CementBaseController, expose
  3. class AbstractBaseController(CementBaseController):
  4. """
  5. This is an abstract base class that is useless on its own, but used
  6. by other classes to sub-class from and to share common commands and
  7. arguments. This should not be confused with the `MyAppBaseController`
  8. used as the ``base_controller`` namespace.
  9. """
  10. class Meta:
  11. stacked_on = 'base'
  12. stacked_type = 'nested'
  13. arguments = [
  14. ( ['-f', '--foo'], dict(help='notorious foo option')),
  15. ]
  16. def _setup(self, base_app):
  17. super(AbstractBaseController, self)._setup(base_app)
  18. # add a common object that will be used in any sub-class
  19. self.reusable_dict = dict()
  20. @expose(hide=True)
  21. def default(self):
  22. """
  23. This command will be shared within all controllers that sub-class
  24. from here. It can also be overridden in the sub-class, but for
  25. this example we are making it dynamic.
  26. """
  27. # do something with self.my_shared_obj here?
  28. if 'some_key' in self.reusable_dict.keys():
  29. pass
  30. # or do something with parsed args?
  31. if self.app.pargs.foo:
  32. print "Foo option was passed with value: %s" % self.app.pargs.foo
  33. # or maybe do something dynamically
  34. print("Inside %s.default()" % self.__class__.__name__)
  35. class MyAppBaseController(CementBaseController):
  36. """
  37. This is the application base controller, but we don't want to use our
  38. abstract base class here.
  39. """
  40. class Meta:
  41. label = 'base'
  42. @expose(hide=True)
  43. def default(self):
  44. print("Inside MyAppBaseController.default()")
  45. class Controller1(AbstractBaseController):
  46. """
  47. This controller sub-classes from the abstract base class as to inherite
  48. shared arguments, and commands.
  49. """
  50. class Meta:
  51. label = 'controller1'
  52. @expose()
  53. def command1(self):
  54. print("Inside Controller1.command1()")
  55. class Controller2(AbstractBaseController):
  56. """
  57. This controller also sub-classes from the abstract base class as to
  58. inherite shared arguments, and commands.
  59. """
  60. class Meta:
  61. label = 'controller2'
  62. @expose()
  63. def command2(self):
  64. print("Inside Controller2.command2()")
  65. def main():
  66. # create the app
  67. app = foundation.CementApp('myapp')
  68. try:
  69. # register controllers
  70. handler.register(MyAppBaseController)
  71. handler.register(Controller1)
  72. handler.register(Controller2)
  73. # setup the app
  74. app.setup()
  75. # run it
  76. app.run()
  77. finally:
  78. # close it
  79. app.close()
  80. if __name__ == '__main__':
  81. main()