Skip to main content

Injector-enabled Python application microframework

Project description

Build status

Cask is Injector-enabled, Python application microframework modelled after Flask.

It’s purpose is to make the amount of boilerplate you need to write when developing your application smaller. Here’s a simple example:

>>> from cask import Cask
>>> from injector import inject
>>>
>>> def configure(binder):
...     binder.bind(str, to='ABC')
...
>>> @Cask.run_main(modules=[configure])
... @inject(s=str)
... def main(s):
...     print(s)
...
ABC

Cask.run_main (it works both as class and instance method, see below) does the if __name__ == '__main__' check for you. So this

>>> app = Cask()
>>> @app.run_main
... def main():
...     print(111)
...
111

is shorter version of

>>> app = Cask()
>>> @app.main
... def main():
...     print(222)
...
>>> if __name__ == '__main__':
...     app.run()
...
222

Construction

Cask constructor and Cask.run_main class method accept the following optional keyword arguments:

  • modules - iterable of Injector modules, defaults to empty sequence

  • injector- instance of Injector to configure and use, by default new instance will be created for you

Hooks

Cask instance provides the following decorators allowing you to register hooks (note that you can inject into registered functions):

  • Cask.before_main - should expect no parameters, if non-None value is returned application execution main function won’t be executed

  • Cask.after_main - should expect single result parameter and return processed result (modified or not)

  • Cask.exception_handler(ExceptionCLass) - will handle exception raised during the application execution, should expect single argument called e and containing exception value:

    >>> app = Cask()
    >>> @app.exception_handler(Exception)
    ... def handle(e):
    ...     print('got exception %s' % (e,))
    ...
    >>> @app.run_main
    ... def main():
    ...     raise Exception(123)
    ...
    got exception 123
  • Cask.main - main function should expect no arguments and may return something

You can, of course, register more than one callable for hooks other than main.

Execution

This is what happens when you run Cask-based application:

  1. Injector is configured using provided modules

  2. before_main hooks are called

  3. If before_main hooks didn’t return value different than None, main hook is executed

  4. after_main hooks are called

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cask-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

File details

Details for the file cask-0.1.0.tar.gz.

File metadata

  • Download URL: cask-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cask-0.1.0.tar.gz
Algorithm Hash digest
SHA256 95025a81940d82faa220406f331731bb6f873564d5246ff64c474bfac42fcf7c
MD5 d433d1584190abe59be7a1b2b5046b29
BLAKE2b-256 382247bc7acefff7dd5e1db2914f3b831d51c70981b2c4f2248ef391cc47a373

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page