{ "info": { "author": "Grok Team", "author_email": "grok-dev@zope.org", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "megrok.resourcelibrary: Resources in Grok\n=========================================\n\nIntroduction\n------------\n\nGrok already comes equipped with a simple way to expose static file\nresources, the ``static`` directory.\n\n``megrok.resourcelibrary`` allows the more flexible inclusion of\nstatic file resources in Grok. It uses the ``zc.resourcelibrary``\npackage to do this.\n\nA resource library is essentially like a directory like the ``static``\ndirectory of a package, full of static resources, such as CSS files,\njavascript files and images. Resources are intended to be used from\nHTML pages, as additional resources to help display a particular\nlayout or user interface.\n\nHow is ``megrok.resourcelibrary`` more flexible than Grok's default\n``static`` directory?\n\n* A resource library can be in a layer.\n\n* A resource library can have a non-public permission.\n\n* A resource library can more easily be packaged for reuse by other\n libraries. Resource libraries have unique names under the control of\n the developer.\n\n* A resource library can automatically include some resources (such as\n javascript or css) in the ``head`` section of a web page whenever a\n particular widget needs it.\n\n* A resource library can also depend on other libraries.\n\nBasic example\n-------------\n\nLet's see how this all works. First we need to grok this package\nitself (this is normally done from ZCML)::\n\n >>> from grok.testing import grok\n >>> grok('megrok.resourcelibrary.meta')\n\nNow we can set up a simple resource library::\n \n >>> import grok\n >>> import megrok.resourcelibrary\n >>> class SomeLibrary(megrok.resourcelibrary.ResourceLibrary):\n ... megrok.resourcelibrary.directory('tests/example')\n\nWe need to grok this to make it available (in normal use this is done\nautomatically for you)::\n\n >>> from grok.testing import grok_component\n >>> grok_component('SomeLibrary', SomeLibrary)\n True\n\nThe resources in this directory are now published, by default under\nthe class name of the library, lower cased (therefore\n``somelibrary``)::\n\n >>> from zope.testbrowser.testing import Browser\n >>> browser = Browser()\n >>> browser.open('http://localhost/@@/somelibrary/my-lib/included.js')\n >>> print browser.contents\n function be_annoying() {\n alert('Hi there!');\n }\n\nThe default name can be overridden by using the ``grok.name``\ndirective::\n\n >>> class SomeLibrary2(megrok.resourcelibrary.ResourceLibrary):\n ... grok.name('some-library')\n ... megrok.resourcelibrary.directory('tests/example')\n >>> grok_component('SomeLibrary2', SomeLibrary2)\n True\n >>> browser.open('http://localhost/@@/some-library/my-lib/included.js')\n >>> print browser.contents\n function be_annoying() {\n alert('Hi there!');\n }\n\nIt's an error to point to a directory that doesn't exist::\n\n >>> class WrongDirectory(megrok.resourcelibrary.ResourceLibrary):\n ... grok.name('wrong-directory')\n ... megrok.resourcelibrary.directory('tests/doesnt_exist')\n >>> grok_component('WrongDirectory', WrongDirectory)\n Traceback (most recent call last):\n ...\n GrokError: Directory 'tests/doesnt_exist' is not a valid directory passed to the 'wrong-directory' directive.\n\nAutomactic inclusion of resources\n---------------------------------\n\nWe now set up a resource library that automatically includes two\nresources whenever it is used in a web page, namely ``included.js``\nand ``included.css``::\n\n >>> class MyLib(megrok.resourcelibrary.ResourceLibrary):\n ... grok.name('my-lib')\n ... megrok.resourcelibrary.directory('tests/example/my-lib')\n ... megrok.resourcelibrary.include('included.js')\n ... megrok.resourcelibrary.include('included.css')\n >>> grok_component('MyLib', MyLib)\n True\n\nThis is how you require the library to be loaded in a particular page\ntemplate::\n\n \n\n``test_template_2`` makes this requirement, so the included Javascript\nshould be included::\n\n >>> browser.open('http://localhost/zc.resourcelibrary.test_template_2')\n >>> '/@@/my-lib/included.js' in browser.contents\n True\n\nAnd the resource is also published::\n\n >>> browser.open('/@@/my-lib/included.js')\n >>> print browser.contents\n function be_annoying() {\n alert('Hi there!');\n }\n\nA reference to the CSS is also inserted into the HTML::\n\n >>> browser.open('http://localhost/zc.resourcelibrary.test_template_2')\n >>> '/@@/my-lib/included.css' in browser.contents\n True\n \nAnd the CSS is available from the URL referenced::\n\n >>> browser.open('/@@/my-lib/included.css')\n >>> print browser.contents\n div .border {\n border: 1px silid black;\n }\n\nProgrammatically signalling resource requirements\n-------------------------------------------------\n\nAbove we've demonstrated the use of the ``resource_library`` namespace\nin ZPT. Library usage can also be signalled programmatically, for\ninstance in a view::\n\n >>> import grok\n >>> from zope.interface import Interface\n >>> class View(grok.View):\n ... grok.context(Interface)\n ... def render(self):\n ... MyLib.need()\n ... return 'Example'\n >>> grok_component('View', View)\n True\n\n >>> browser.open('http://localhost/view')\n >>> '/@@/my-lib/included.js' in browser.contents\n True\n\nThis also works for libraries which don't have an explicit ``grok.name``::\n\n >>> class MyLib2(megrok.resourcelibrary.ResourceLibrary):\n ... megrok.resourcelibrary.directory('tests/example/my-lib')\n ... megrok.resourcelibrary.include('included.js')\n ... megrok.resourcelibrary.include('included.css')\n >>> grok_component('MyLib2', MyLib2)\n True\n\n >>> class View2(grok.View):\n ... grok.context(Interface)\n ... def render(self):\n ... MyLib2.need()\n ... return 'Example'\n >>> grok_component('View2', View2)\n True\n\n >>> browser.open('http://localhost/view2')\n >>> '/@@/mylib2/included.js' in browser.contents\n True\n\nYou can also signal inclusion by library name instead (like is done in page templates)::\n\n >>> class View3(grok.View):\n ... grok.context(Interface)\n ... def render(self):\n ... megrok.resourcelibrary.need('my-lib')\n ... return 'Example'\n\n >>> grok_component('View3', View3)\n True\n\n >>> browser.open('http://localhost/view3')\n >>> '/@@/my-lib/included.js' in browser.contents\n True\n\nMaking resource libraries depend on other ones\n----------------------------------------------\n\nWe can make a resource library depend on another one::\n\n >>> class Dependency(megrok.resourcelibrary.ResourceLibrary):\n ... megrok.resourcelibrary.directory('tests/example')\n ... megrok.resourcelibrary.include('1.js')\n >>> grok_component('Dependency', Dependency)\n True\n \n >>> class Dependent(megrok.resourcelibrary.ResourceLibrary):\n ... megrok.resourcelibrary.directory('tests/example')\n ... megrok.resourcelibrary.include('2.css')\n ... megrok.resourcelibrary.depend(Dependency)\n >>> grok_component('Dependent', Dependent)\n True\n\nLet's make a view that needs ``Dependent``::\n\n >>> class DependentView(grok.View):\n ... grok.context(Interface)\n ... def render(self):\n ... Dependent.need()\n ... return 'Example'\n >>> grok_component('DependentView', DependentView)\n True\n\nThe included code of both the original and the dependency will now\nshow up::\n\n >>> browser.open('http://localhost/dependentview')\n >>> '/@@/dependency/1.js' in browser.contents\n True\n >>> '/@@/dependent/2.css' in browser.contents\n True\n\nProtecting resources\n--------------------\n\nIt's possible to give a resource a permission::\n\n >>> class MyPermission(grok.Permission):\n ... grok.name(\"my.permission\")\n >>> grok_component('MyPermission', MyPermission)\n True\n\n >>> class MyLib3(megrok.resourcelibrary.ResourceLibrary):\n ... megrok.resourcelibrary.directory('tests/example/my-lib')\n ... grok.require(MyPermission)\n >>> grok_component('MyLib3', MyLib3)\n True\n\nXXX This doesn't work yet, as resources don't do their own security\nchecks but rely on proxies, which Grok has removed... Need to introduce\nnew resources/factories to do checks by hand.\n\nChanges\n=======\n\n0.9.2 (2008-08-08)\n------------------\n\n* Grokker mistakenly relied on (not imported) ``GrokImportError`` in failure\n case. Corrected this to ``GrokError`` and added a test for it.\n\n* Rename ``use`` directive to ``depend`` and make it take a class\n argument instead of a library name.\n\n0.9.1 (2008-08-06)\n------------------\n\n* Turn off zip-safeness.\n\n* Actually add a meta.zcml that loads up the grokker!\n\n0.9 (2008-08-06)\n----------------\n\n* Initial public release.", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "UNKNOWN", "keywords": "", "license": "ZPL", "maintainer": null, "maintainer_email": null, "name": "megrok.resourcelibrary", "package_url": "https://pypi.org/project/megrok.resourcelibrary/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/megrok.resourcelibrary/", "project_urls": { "Download": "UNKNOWN", "Homepage": "UNKNOWN" }, "release_url": "https://pypi.org/project/megrok.resourcelibrary/0.9.2/", "requires_dist": null, "requires_python": null, "summary": "Static resource library support for Grok.", "version": "0.9.2" }, "last_serial": 794653, "releases": { "0.9": [ { "comment_text": "", "digests": { "md5": "5e134e6984b7c2b833c3a5f3529535f4", "sha256": "f190dca8de929d8951e160f728d3ec056239fbd1d10b430e855585720f5702e6" }, "downloads": -1, "filename": "megrok.resourcelibrary-0.9.tar.gz", "has_sig": false, "md5_digest": "5e134e6984b7c2b833c3a5f3529535f4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7330, "upload_time": "2008-08-06T18:09:17", "url": "https://files.pythonhosted.org/packages/0b/68/f2e9f36008962efddeb13ed375651e1caded32c90a707656761e4c5d78a6/megrok.resourcelibrary-0.9.tar.gz" } ], "0.9.1": [ { "comment_text": "", "digests": { "md5": "eee983c19a356b61600a48f047abc057", "sha256": "f1035ced09cd429cf778d1c263798d31404a63ff880120a823c66bead1331886" }, "downloads": -1, "filename": "megrok.resourcelibrary-0.9.1.tar.gz", "has_sig": false, "md5_digest": "eee983c19a356b61600a48f047abc057", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7487, "upload_time": "2008-08-06T18:42:09", "url": "https://files.pythonhosted.org/packages/94/fc/8e39de0816aa2a7564cba6b50f0da543945614c908c9ce585887250dd178/megrok.resourcelibrary-0.9.1.tar.gz" } ], "0.9.2": [ { "comment_text": "", "digests": { "md5": "6a8f3b917b823ef7d87e3bbc4ecc2344", "sha256": "495c1c557eca82dbf5b364da7f9257d38ce72f24655b5bed8207b9d7a3aa7bfb" }, "downloads": -1, "filename": "megrok.resourcelibrary-0.9.2.tar.gz", "has_sig": false, "md5_digest": "6a8f3b917b823ef7d87e3bbc4ecc2344", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8188, "upload_time": "2008-08-08T19:06:21", "url": "https://files.pythonhosted.org/packages/55/1f/c7c80135d09c88e82e2bdbaec5191046fb2b796229a49f4b103454062445/megrok.resourcelibrary-0.9.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "6a8f3b917b823ef7d87e3bbc4ecc2344", "sha256": "495c1c557eca82dbf5b364da7f9257d38ce72f24655b5bed8207b9d7a3aa7bfb" }, "downloads": -1, "filename": "megrok.resourcelibrary-0.9.2.tar.gz", "has_sig": false, "md5_digest": "6a8f3b917b823ef7d87e3bbc4ecc2344", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8188, "upload_time": "2008-08-08T19:06:21", "url": "https://files.pythonhosted.org/packages/55/1f/c7c80135d09c88e82e2bdbaec5191046fb2b796229a49f4b103454062445/megrok.resourcelibrary-0.9.2.tar.gz" } ] }