grokcore.message


Namegrokcore.message JSON
Version 4.0 PyPI version JSON
download
home_pagehttps://github.com/zopefoundation/grokcore.message
SummaryGrok messaging machinery
upload_time2023-08-28 09:53:57
maintainer
docs_urlNone
authorGrok Team
requires_python>=3.7
licenseZPL 2.1
keywords grok messages
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            grokcore.message
*****************

This package provides integration of `z3c.flashmessage`_ for a grok
setup. This means taking care of:

* Registering a global message receiver with the component
  architechture.

* Registering by default a global session-based message source named
  ``session``.

* Optionally (if including ``ram.zcml``) registering a global RAM
  stored message source named ``ram``.

* Providing components to make use of global message receivers and
  sources.

For details about what kind of messages we are talking about here,
please see the `z3c.flashmessage`_ documentation.

.. contents::


Setting up ``grokcore.message``
================================

When being grokked, ``grokcore.message`` registers

* a global session message source named ``session``

* a global message receiver.

Grokking of this package happens when the local ``configure.zcml`` is
executed. In standard Grok-based packages this often happens
automatically.

One can, of course, also grok the package manually:

  >>> import grokcore.component as grok
  >>> grok.testing.grok('grokcore.message')

This setups a global message receiver:

  >>> from z3c.flashmessage.interfaces import IMessageReceiver
  >>> from zope.component import getUtility
  >>> getUtility(IMessageReceiver)
  <z3c.flashmessage.receiver.GlobalMessageReceiver object at 0x...>

It also setups a session-based message source named ``session``:

  >>> from z3c.flashmessage.interfaces import IMessageSource
  >>> getUtility(IMessageSource, name=u'session')
  <z3c.flashmessage.sources.SessionMessageSource object at 0x...>

We provide also a RAM-stored message source that can be enabled by
including ``ram.zcml`` and is not registered by default:

  >>> getUtility(IMessageSource, name=u'ram')
  Traceback (most recent call last):
  ...
  zope.interface.interfaces.ComponentLookupError: (<InterfaceClass z3c.flashmessage.interfaces.IMessageSource>, 'ram')


You can enable this source by including ``ram.zcml`` from
``grokcore.message`` in your ZCML setup like this::

  <configure xmlns="http://namespaces.zope.org/zope">
    <include package="grokcore.message" file="ram.zcml" />
  </configure>

or, of course, by registering a RAMMessageSource manually:

  >>> from zope.component import provideUtility
  >>> from z3c.flashmessage.sources import RAMMessageSource
  >>> ram_source = RAMMessageSource()
  >>> provideUtility(ram_source, name=u'ram')

Now we can get the RAM source:

  >>> getUtility(IMessageSource, name=u'ram')
  <z3c.flashmessage.sources.RAMMessageSource object at 0x...>

Components (API)
================

``grokcore.message`` provides some extra-components and functions
beside the usual components from ``z3c.flashmessage``.

UniqueMessageSource
-------------------

A ``UniqueMessageSource`` is a message source that holds exactly zero
or one message. Note that messages are not stored persistent in a
``UniqueMessageSource`` instance and will be lost after restarting
your Zope instance.

It is a baseclass, which means that you have to derive from it to
register an instance as global utility upon your software being
grokked (see examples below).

Methods:

  **UniqueMessageSource.send(message[, type=u'message'])**
    Send a message ``message`` of type ``type``.

  **UniqueMessageSource.list(type=None)**
    Returns a generator object listing the message if one is stored.

  **UniqueMessageSource.delete(message)**
    Delete the message stored from source, if ``message`` is this
    message.

Convenience functions
---------------------

``grokcore.message`` provides a couple of convenience functions to
feed sources or get data from them.

**grokcore.message.send(message[, type='message'[, name='session']])**

  Send ``message`` to the message source ``name``.

  Returns ``True`` if the message could be sent
  successfully. Otherwise ``False`` is returned:

    >>> import grokcore.message
    >>> grokcore.message.send('Meet at dawn!')
    True

    >>> grokcore.message.send('Meat a fawn!', name='doesnotexist')
    False

**grokcore.message.get_from_source([name=''])**

  Get a list of messages stored at message source registered under
  name ``name`` or ``None``.

  This action never deletes messages from the queried source.

    >>> import grokcore.message
    >>> grokcore.message.get_from_source('session')
    <generator object ...>

    >>> grokcore.message.get_from_source('not-existing') is None
    True

**grokcore.message.receive([name=''])**

  Receive the messages collected by the receiver registered under name
  ``name``.

  >>> import grokcore.message
  >>> msgs = list(grokcore.message.receive())
  >>> msgs
  [<z3c.flashmessage.message.Message object at 0x...>]

  >>> msgs[0].message
  'Meet at dawn!'

  Please note, that this action might delete messages from the sources
  they have been sent to as by 'receiving' messages you indicate that
  the messages have been processed.

  The session source for instance is now empty:

  >>> list(grokcore.message.get_from_source('session'))
  []

  Receiving again will give no results:

  >>> list(grokcore.message.receive())
  []


Examples
========

Creating a ``UniqueMessageSource``:

  >>> from grokcore.message import UniqueMessageSource
  >>> class MyUniqueMessageSource(UniqueMessageSource):
  ...   grok.name('uniq_source')

After being grokked, the source is automatically registered:

  >>> grok.testing.grok_component(
  ...     'MyUniqueMessageSource', MyUniqueMessageSource,
  ...     dotted_name='grokcore.message.tests')
  True

  >>> source = getUtility(IMessageSource, name='uniq_source')
  >>> source
  <...MyUniqueMessageSource object at 0x...>


It provides the methods required by the IMessageSource interface:

  >>> from z3c.flashmessage.interfaces import IMessageSource
  >>> from zope.interface import verify

  >>> verify.verifyClass(IMessageSource, MyUniqueMessageSource)
  True


We can list the message stored in the source:

  >>> source.list()
  <generator object ...>

  >>> list(source.list())
  []

  >>> source.send(message='Hello!', type='message')
  >>> list(source.list())
  [<z3c.flashmessage.message.PersistentMessage object at 0x...>]

  >>> print(list(source.list())[0].message)
  Hello!

When we send another message, the old one will be silenty discarded:

  >>> source.send(message='Hello again!', type='message')
  >>> len(list(source.list()))
  1

  >>> print(list(source.list())[0].message)
  Hello again!

We can delete the message:

  >>> msg = list(source.list())[0]
  >>> source.delete(msg)
  >>> len(list(source.list()))
  0

Examples for the convenience functions can be found above.


.. _z3c.flashmessage: http://pypi.python.org/pypi/z3c.flashmessage


CHANGES
*******

4.0 (2023-08-28)
================

- Drop support for Python 2.7, 3.4, 3.5, 3.6.

- Add support for Python 3.7, 3.8, 3.9, 3.10, 3.11.


3.0.1 (2018-01-17)
==================

- Replace the use of `grok.implements()` with the `@grok.implementer()`
  directive throughout.

3.0.0 (2018-01-15)
==================

- Python 3 compatibility.

0.4.3 (2016-02-15)
==================

- Update tests.

0.4.2 (2010-10-25)
==================

- Tests fixed by explicitely registering the IClientIdManager and
  ISessionDataContainer utilities. The ftesting.zcml was re-introduced for this.

0.4.1 (2010-10-25)
==================

- Remove ftesting.zcml that was not necessary anymore.

0.4 (2010-10-25)
================

* Make sure ``zope.session`` is configured, as this package claims to provide
  for a session based flash message machinery.

* Made package comply to zope.org repository policy.

0.3 (2010-03-05)
================

* ``UniqueMessageSource`` now implements the ``IMessageSource``
  interface completely, i.e. the ``type`` parameter is now optional
  when using ``UniqueMessageSource.send()``.

0.2 (2010-03-03)
================

* The utility function ``send`` now takes a ``name`` argument,
  allowing the choice of the target message source.

0.1 (2010-03-03)
================

* Factored out from former versions of ``grokui.admin``, ``grok`` and
  ``megrok.layout`` respectively.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/zopefoundation/grokcore.message",
    "name": "grokcore.message",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "Grok Messages",
    "author": "Grok Team",
    "author_email": "grok-dev@zope.dev",
    "download_url": "https://files.pythonhosted.org/packages/ec/bd/d20c945ebf820c07b76bef8d64ff8d4ba31f1c6343067da841330c248e5a/grokcore.message-4.0.tar.gz",
    "platform": null,
    "description": "grokcore.message\n*****************\n\nThis package provides integration of `z3c.flashmessage`_ for a grok\nsetup. This means taking care of:\n\n* Registering a global message receiver with the component\n  architechture.\n\n* Registering by default a global session-based message source named\n  ``session``.\n\n* Optionally (if including ``ram.zcml``) registering a global RAM\n  stored message source named ``ram``.\n\n* Providing components to make use of global message receivers and\n  sources.\n\nFor details about what kind of messages we are talking about here,\nplease see the `z3c.flashmessage`_ documentation.\n\n.. contents::\n\n\nSetting up ``grokcore.message``\n================================\n\nWhen being grokked, ``grokcore.message`` registers\n\n* a global session message source named ``session``\n\n* a global message receiver.\n\nGrokking of this package happens when the local ``configure.zcml`` is\nexecuted. In standard Grok-based packages this often happens\nautomatically.\n\nOne can, of course, also grok the package manually:\n\n  >>> import grokcore.component as grok\n  >>> grok.testing.grok('grokcore.message')\n\nThis setups a global message receiver:\n\n  >>> from z3c.flashmessage.interfaces import IMessageReceiver\n  >>> from zope.component import getUtility\n  >>> getUtility(IMessageReceiver)\n  <z3c.flashmessage.receiver.GlobalMessageReceiver object at 0x...>\n\nIt also setups a session-based message source named ``session``:\n\n  >>> from z3c.flashmessage.interfaces import IMessageSource\n  >>> getUtility(IMessageSource, name=u'session')\n  <z3c.flashmessage.sources.SessionMessageSource object at 0x...>\n\nWe provide also a RAM-stored message source that can be enabled by\nincluding ``ram.zcml`` and is not registered by default:\n\n  >>> getUtility(IMessageSource, name=u'ram')\n  Traceback (most recent call last):\n  ...\n  zope.interface.interfaces.ComponentLookupError: (<InterfaceClass z3c.flashmessage.interfaces.IMessageSource>, 'ram')\n\n\nYou can enable this source by including ``ram.zcml`` from\n``grokcore.message`` in your ZCML setup like this::\n\n  <configure xmlns=\"http://namespaces.zope.org/zope\">\n    <include package=\"grokcore.message\" file=\"ram.zcml\" />\n  </configure>\n\nor, of course, by registering a RAMMessageSource manually:\n\n  >>> from zope.component import provideUtility\n  >>> from z3c.flashmessage.sources import RAMMessageSource\n  >>> ram_source = RAMMessageSource()\n  >>> provideUtility(ram_source, name=u'ram')\n\nNow we can get the RAM source:\n\n  >>> getUtility(IMessageSource, name=u'ram')\n  <z3c.flashmessage.sources.RAMMessageSource object at 0x...>\n\nComponents (API)\n================\n\n``grokcore.message`` provides some extra-components and functions\nbeside the usual components from ``z3c.flashmessage``.\n\nUniqueMessageSource\n-------------------\n\nA ``UniqueMessageSource`` is a message source that holds exactly zero\nor one message. Note that messages are not stored persistent in a\n``UniqueMessageSource`` instance and will be lost after restarting\nyour Zope instance.\n\nIt is a baseclass, which means that you have to derive from it to\nregister an instance as global utility upon your software being\ngrokked (see examples below).\n\nMethods:\n\n  **UniqueMessageSource.send(message[, type=u'message'])**\n    Send a message ``message`` of type ``type``.\n\n  **UniqueMessageSource.list(type=None)**\n    Returns a generator object listing the message if one is stored.\n\n  **UniqueMessageSource.delete(message)**\n    Delete the message stored from source, if ``message`` is this\n    message.\n\nConvenience functions\n---------------------\n\n``grokcore.message`` provides a couple of convenience functions to\nfeed sources or get data from them.\n\n**grokcore.message.send(message[, type='message'[, name='session']])**\n\n  Send ``message`` to the message source ``name``.\n\n  Returns ``True`` if the message could be sent\n  successfully. Otherwise ``False`` is returned:\n\n    >>> import grokcore.message\n    >>> grokcore.message.send('Meet at dawn!')\n    True\n\n    >>> grokcore.message.send('Meat a fawn!', name='doesnotexist')\n    False\n\n**grokcore.message.get_from_source([name=''])**\n\n  Get a list of messages stored at message source registered under\n  name ``name`` or ``None``.\n\n  This action never deletes messages from the queried source.\n\n    >>> import grokcore.message\n    >>> grokcore.message.get_from_source('session')\n    <generator object ...>\n\n    >>> grokcore.message.get_from_source('not-existing') is None\n    True\n\n**grokcore.message.receive([name=''])**\n\n  Receive the messages collected by the receiver registered under name\n  ``name``.\n\n  >>> import grokcore.message\n  >>> msgs = list(grokcore.message.receive())\n  >>> msgs\n  [<z3c.flashmessage.message.Message object at 0x...>]\n\n  >>> msgs[0].message\n  'Meet at dawn!'\n\n  Please note, that this action might delete messages from the sources\n  they have been sent to as by 'receiving' messages you indicate that\n  the messages have been processed.\n\n  The session source for instance is now empty:\n\n  >>> list(grokcore.message.get_from_source('session'))\n  []\n\n  Receiving again will give no results:\n\n  >>> list(grokcore.message.receive())\n  []\n\n\nExamples\n========\n\nCreating a ``UniqueMessageSource``:\n\n  >>> from grokcore.message import UniqueMessageSource\n  >>> class MyUniqueMessageSource(UniqueMessageSource):\n  ...   grok.name('uniq_source')\n\nAfter being grokked, the source is automatically registered:\n\n  >>> grok.testing.grok_component(\n  ...     'MyUniqueMessageSource', MyUniqueMessageSource,\n  ...     dotted_name='grokcore.message.tests')\n  True\n\n  >>> source = getUtility(IMessageSource, name='uniq_source')\n  >>> source\n  <...MyUniqueMessageSource object at 0x...>\n\n\nIt provides the methods required by the IMessageSource interface:\n\n  >>> from z3c.flashmessage.interfaces import IMessageSource\n  >>> from zope.interface import verify\n\n  >>> verify.verifyClass(IMessageSource, MyUniqueMessageSource)\n  True\n\n\nWe can list the message stored in the source:\n\n  >>> source.list()\n  <generator object ...>\n\n  >>> list(source.list())\n  []\n\n  >>> source.send(message='Hello!', type='message')\n  >>> list(source.list())\n  [<z3c.flashmessage.message.PersistentMessage object at 0x...>]\n\n  >>> print(list(source.list())[0].message)\n  Hello!\n\nWhen we send another message, the old one will be silenty discarded:\n\n  >>> source.send(message='Hello again!', type='message')\n  >>> len(list(source.list()))\n  1\n\n  >>> print(list(source.list())[0].message)\n  Hello again!\n\nWe can delete the message:\n\n  >>> msg = list(source.list())[0]\n  >>> source.delete(msg)\n  >>> len(list(source.list()))\n  0\n\nExamples for the convenience functions can be found above.\n\n\n.. _z3c.flashmessage: http://pypi.python.org/pypi/z3c.flashmessage\n\n\nCHANGES\n*******\n\n4.0 (2023-08-28)\n================\n\n- Drop support for Python 2.7, 3.4, 3.5, 3.6.\n\n- Add support for Python 3.7, 3.8, 3.9, 3.10, 3.11.\n\n\n3.0.1 (2018-01-17)\n==================\n\n- Replace the use of `grok.implements()` with the `@grok.implementer()`\n  directive throughout.\n\n3.0.0 (2018-01-15)\n==================\n\n- Python 3 compatibility.\n\n0.4.3 (2016-02-15)\n==================\n\n- Update tests.\n\n0.4.2 (2010-10-25)\n==================\n\n- Tests fixed by explicitely registering the IClientIdManager and\n  ISessionDataContainer utilities. The ftesting.zcml was re-introduced for this.\n\n0.4.1 (2010-10-25)\n==================\n\n- Remove ftesting.zcml that was not necessary anymore.\n\n0.4 (2010-10-25)\n================\n\n* Make sure ``zope.session`` is configured, as this package claims to provide\n  for a session based flash message machinery.\n\n* Made package comply to zope.org repository policy.\n\n0.3 (2010-03-05)\n================\n\n* ``UniqueMessageSource`` now implements the ``IMessageSource``\n  interface completely, i.e. the ``type`` parameter is now optional\n  when using ``UniqueMessageSource.send()``.\n\n0.2 (2010-03-03)\n================\n\n* The utility function ``send`` now takes a ``name`` argument,\n  allowing the choice of the target message source.\n\n0.1 (2010-03-03)\n================\n\n* Factored out from former versions of ``grokui.admin``, ``grok`` and\n  ``megrok.layout`` respectively.\n\n",
    "bugtrack_url": null,
    "license": "ZPL 2.1",
    "summary": "Grok messaging machinery",
    "version": "4.0",
    "project_urls": {
        "Homepage": "https://github.com/zopefoundation/grokcore.message"
    },
    "split_keywords": [
        "grok",
        "messages"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "be276c7b895e7f658913ce9735b3e3c945d2467318180b6c43d962b38a7bc612",
                "md5": "6dfeccc9d7ad32734f0b73221af20452",
                "sha256": "ee81e990bfaa2d3c6eae312798cb61c85e1f154c795def483485949940576093"
            },
            "downloads": -1,
            "filename": "grokcore.message-4.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6dfeccc9d7ad32734f0b73221af20452",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 11799,
            "upload_time": "2023-08-28T09:53:53",
            "upload_time_iso_8601": "2023-08-28T09:53:53.615939Z",
            "url": "https://files.pythonhosted.org/packages/be/27/6c7b895e7f658913ce9735b3e3c945d2467318180b6c43d962b38a7bc612/grokcore.message-4.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ecbdd20c945ebf820c07b76bef8d64ff8d4ba31f1c6343067da841330c248e5a",
                "md5": "4f0d872d021d8cf5bc45f29ec2a034be",
                "sha256": "484e7f128adeadd8955c6f7655fd39423202eed376af5b4efc15ea786c5363ad"
            },
            "downloads": -1,
            "filename": "grokcore.message-4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4f0d872d021d8cf5bc45f29ec2a034be",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 10126,
            "upload_time": "2023-08-28T09:53:57",
            "upload_time_iso_8601": "2023-08-28T09:53:57.597134Z",
            "url": "https://files.pythonhosted.org/packages/ec/bd/d20c945ebf820c07b76bef8d64ff8d4ba31f1c6343067da841330c248e5a/grokcore.message-4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-28 09:53:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "zopefoundation",
    "github_project": "grokcore.message",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "grokcore.message"
}
        
Elapsed time: 0.10384s