Skip to main content

megrok.resourceviewlet is a package meant to include resources using layer, context and view discriminations.

Setup

Let’s import and init the necessary work environment:

>>> import grokcore.component as grok
>>> from grokcore import view, viewlet
>>> from zope.app.wsgi.testlayer import Browser

>>> browser = Browser()
>>> browser.handleErrors = False

Library

We first declare a resource. We’ll include it in our page:

>>> from fanstatic import Resource, Library
>>> myLibrary = Library('test_library', 'ftests/resources')
>>> Thing = Resource(myLibrary, 'thing.js')

This step is done by an entry point. For the testing, we do it by hand:

>>> from zope.fanstatic.zcml import create_factory
>>> from zope.component import getGlobalSiteManager
>>> from zope.publisher.interfaces.browser import IBrowserRequest
>>> from zope.interface import Interface

>>> resource_factory = create_factory(myLibrary)
>>> getGlobalSiteManager().registerAdapter(
...      resource_factory, (IBrowserRequest,), Interface, myLibrary.name)

Components

To demonstrate our resource viewlet, we first need a page to render. This page contains a content provider named ‘resources’:

>>> from zope.interface import Interface

>>> class Index(view.View):
...   view.require("zope.Public")
...   view.context(Interface)
...
...   template = view.PageTemplate("""<html><head>
...     <tal:resources replace='provider:resources' />
...   </head></html>""")

>>> grok.testing.grok_component('index', Index)
True

Manager

We now register a content provider named ‘resources’. It will be a ResourcesManager. An ResourcesManager is a component dedicated in rendering ResourceViewlets:

>>> from megrok.resourceviewlet import ResourcesManager

>>> class Resources(ResourcesManager):
...   viewlet.context(Interface)

>>> grok.testing.grok_component('resources', Resources)
True

Viewlet

Now, we register a ResourceViewlet, including our resource. The declaration is very straightforward:

>>> from megrok.resourceviewlet import ResourceViewlet

>>> class SomeViewlet(ResourceViewlet):
...   viewlet.context(Interface)
...   resources = [Thing]

>>> grok.testing.grok_component('viewlet', SomeViewlet)
True

By default, a ResourceViewlet is registered for an instance of ResourcesManager. Most of the time, a page contains only one of these content providers. If it’s not the case, make sure to provide your own viewletmanager directive value.

Rendering

Rendering our page should render the ResourcesManager and therefore, include our resource:

>>> browser.open('http://localhost/@@index')
>>> print browser.contents
<html><head>
  <script type="text/javascript"
   src="http://localhost/fanstatic/test_library/thing.js"></script>
</head></html>

It works ! Enjoy.

Changelog

0.2 (2011-01-18)

  • megrok.resourceviewlet now works and depends on fanstatic.

  • Dependencies has been greatly reduced.

0.1 (2009-12-24)

  • Initial release

Metadata

Release files for megrok.resourceviewlet 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for megrok.resourceviewlet 0.2
File Size Uploaded Provenance Actions
megrok.resourceviewlet-0.2.tar.gz 7.2 kB Without provenance Details for megrok.resourceviewlet-0.2.tar.gz

Release files / megrok.resourceviewlet-0.2.tar.gz

Download URL megrok.resourceviewlet-0.2.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
bad77f62df07eadcb00989770d19bb6d56e071547e79f5bc51d8f1a115634a4d
BLAKE2b-256 checksum
How to use checksums
93b69eb562ba1579afffab9bc45ff1098b1dfdf105c46bb5343df2482e414dd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for megrok.resourceviewlet 0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page