summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorSteve Piercy <web@stevepiercy.com>2018-08-18 03:02:16 -0700
committerSteve Piercy <web@stevepiercy.com>2018-08-18 03:02:16 -0700
commit2891d1436c1b29fa9c312c18470c6d0c6f2436a3 (patch)
tree8ed2e335458c1922acb4ffb62c76f257cef9ba35 /docs
parentadf1facad95ee560ed6dac8192ba8bd201146e8d (diff)
downloadpyramid-2891d1436c1b29fa9c312c18470c6d0c6f2436a3.tar.gz
pyramid-2891d1436c1b29fa9c312c18470c6d0c6f2436a3.tar.bz2
pyramid-2891d1436c1b29fa9c312c18470c6d0c6f2436a3.zip
Clean up code-blocks in assets
Diffstat (limited to 'docs')
-rw-r--r--docs/narr/assets.rst282
1 files changed, 141 insertions, 141 deletions
diff --git a/docs/narr/assets.rst b/docs/narr/assets.rst
index f5a2f9684..2b0b90b4a 100644
--- a/docs/narr/assets.rst
+++ b/docs/narr/assets.rst
@@ -51,10 +51,10 @@ might address the asset using the :term:`asset specification`
inside a ``myapp`` package:
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.renderers import render_to_response
- render_to_response('myapp:templates/some_template.pt', {}, request)
+ from pyramid.renderers import render_to_response
+ render_to_response('myapp:templates/some_template.pt', {}, request)
"Under the hood", when this API is called, :app:`Pyramid` attempts to make
sense out of the string ``myapp:templates/some_template.pt`` provided by the
@@ -114,10 +114,10 @@ from the ``/var/www/static`` directory of the computer which runs the
:app:`Pyramid` application as URLs beneath the ``/static`` URL prefix.
.. code-block:: python
- :linenos:
+ :linenos:
- # config is an instance of pyramid.config.Configurator
- config.add_static_view(name='static', path='/var/www/static')
+ # config is an instance of pyramid.config.Configurator
+ config.add_static_view(name='static', path='/var/www/static')
The ``name`` represents a URL *prefix*. In order for files that live in the
``path`` directory to be served, a URL that requests one of them must begin
@@ -153,10 +153,10 @@ named ``some_package``, we can use a fully qualified :term:`asset
specification` as the ``path``:
.. code-block:: python
- :linenos:
+ :linenos:
- # config is an instance of pyramid.config.Configurator
- config.add_static_view(name='static', path='some_package:a/b/c/static')
+ # config is an instance of pyramid.config.Configurator
+ config.add_static_view(name='static', path='some_package:a/b/c/static')
The ``path`` provided to :meth:`~pyramid.config.Configurator.add_static_view`
may be a fully qualified :term:`asset specification` or an *absolute path*.
@@ -173,11 +173,11 @@ For example, :meth:`~pyramid.config.Configurator.add_static_view` may be fed a
``name`` argument which is ``http://example.com/images``:
.. code-block:: python
- :linenos:
+ :linenos:
- # config is an instance of pyramid.config.Configurator
- config.add_static_view(name='http://example.com/images',
- path='mypackage:images')
+ # config is an instance of pyramid.config.Configurator
+ config.add_static_view(name='http://example.com/images',
+ path='mypackage:images')
Because :meth:`~pyramid.config.Configurator.add_static_view` is provided with a
``name`` argument that is the URL ``http://example.com/images``, subsequent
@@ -208,10 +208,10 @@ static registration ``path`` attribute.
For example, let's assume you create a set of static declarations like so:
.. code-block:: python
- :linenos:
+ :linenos:
- config.add_static_view(name='static1', path='mypackage:assets/1')
- config.add_static_view(name='static2', path='mypackage:assets/2')
+ config.add_static_view(name='static1', path='mypackage:assets/1')
+ config.add_static_view(name='static2', path='mypackage:assets/2')
These declarations create URL-accessible directories which have URLs that begin
with ``/static1`` and ``/static2``, respectively. The assets in the
@@ -225,16 +225,16 @@ configuration. Instead, use the :meth:`~pyramid.request.Request.static_url`
API to generate them for you. For example:
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.renderers import render_to_response
+ from pyramid.renderers import render_to_response
- def my_view(request):
- css_url = request.static_url('mypackage:assets/1/foo.css')
- js_url = request.static_url('mypackage:assets/2/foo.js')
- return render_to_response('templates/my_template.pt',
- dict(css_url=css_url, js_url=js_url),
- request=request)
+ def my_view(request):
+ css_url = request.static_url('mypackage:assets/1/foo.css')
+ js_url = request.static_url('mypackage:assets/2/foo.js')
+ return render_to_response('templates/my_template.pt',
+ dict(css_url=css_url, js_url=js_url),
+ request=request)
If the request "application URL" of the running system is
``http://example.com``, the ``css_url`` generated above would be:
@@ -254,9 +254,9 @@ a *URL* instead of a view name. For example, the ``name`` argument may be
``http://example.com`` while the ``path`` given may be ``mypackage:images``:
.. code-block:: python
- :linenos:
+ :linenos:
- config.add_static_view(name='http://example.com/images',
+ config.add_static_view(name='http://example.com/images',
path='mypackage:images')
Under such a configuration, the URL generated by ``static_url`` for assets
@@ -264,10 +264,10 @@ which begin with ``mypackage:images`` will be prefixed with
``http://example.com/images``:
.. code-block:: python
- :linenos:
+ :linenos:
- request.static_url('mypackage:images/logo.png')
- # -> http://example.com/images/logo.png
+ request.static_url('mypackage:images/logo.png')
+ # -> http://example.com/images/logo.png
Using :meth:`~pyramid.request.Request.static_url` in conjunction with a
:meth:`~pyramid.config.Configurator.add_static_view` makes it possible to put
@@ -282,23 +282,23 @@ named ``media_location`` which we can set to an external URL in production when
our assets are hosted on a CDN.
.. code-block:: python
- :linenos:
+ :linenos:
- media_location = settings.get('media_location', 'static')
+ media_location = settings.get('media_location', 'static')
- config = Configurator(settings=settings)
- config.add_static_view(path='myapp:static', name=media_location)
+ config = Configurator(settings=settings)
+ config.add_static_view(path='myapp:static', name=media_location)
Now we can optionally define the setting in our ini file:
.. code-block:: ini
- :linenos:
+ :linenos:
- # production.ini
- [app:main]
- use = egg:myapp#main
+ # production.ini
+ [app:main]
+ use = egg:myapp#main
- media_location = http://static.example.com/
+ media_location = http://static.example.com/
It is also possible to serve assets that live outside of the source by
referring to an absolute path on the filesystem. There are two ways to
@@ -320,9 +320,9 @@ absolute path.
.. code-block:: python
- config.add_static_view(path='myapp:static_images', name='static')
- config.override_asset(to_override='myapp:static_images/',
- override_with='/abs/path/to/images/')
+ config.add_static_view(path='myapp:static_images', name='static')
+ config.override_asset(to_override='myapp:static_images/',
+ override_with='/abs/path/to/images/')
From this configuration it is now possible to use
:meth:`~pyramid.request.Request.static_url` to generate URLs to the data in the
@@ -369,25 +369,25 @@ resource's old URL.
assets using :meth:`~pyramid.config.Configurator.add_cache_buster`:
.. code-block:: python
- :linenos:
+ :linenos:
- import time
- from pyramid.static import QueryStringConstantCacheBuster
+ import time
+ from pyramid.static import QueryStringConstantCacheBuster
- # config is an instance of pyramid.config.Configurator
- config.add_static_view(name='static', path='mypackage:folder/static/')
- config.add_cache_buster(
- 'mypackage:folder/static/',
- QueryStringConstantCacheBuster(str(int(time.time()))))
+ # config is an instance of pyramid.config.Configurator
+ config.add_static_view(name='static', path='mypackage:folder/static/')
+ config.add_cache_buster(
+ 'mypackage:folder/static/',
+ QueryStringConstantCacheBuster(str(int(time.time()))))
Adding the cachebuster instructs :app:`Pyramid` to add the current time for
a static asset to the query string in the asset's URL:
.. code-block:: python
- :linenos:
+ :linenos:
- js_url = request.static_url('mypackage:folder/static/js/myapp.js')
- # Returns: 'http://www.example.com/static/js/myapp.js?x=1445318121'
+ js_url = request.static_url('mypackage:folder/static/js/myapp.js')
+ # Returns: 'http://www.example.com/static/js/myapp.js?x=1445318121'
When the web server restarts, the time constant will change and therefore so
will its URL.
@@ -434,45 +434,45 @@ way the asset token is generated. To do this just subclass
the hash of the current commit:
.. code-block:: python
- :linenos:
-
- import os
- import subprocess
- from pyramid.static import QueryStringCacheBuster
-
- class GitCacheBuster(QueryStringCacheBuster):
- """
- Assuming your code is installed as a Git checkout, as opposed to an egg
- from an egg repository like PYPI, you can use this cachebuster to get
- the current commit's SHA1 to use as the cache bust token.
- """
- def __init__(self, param='x', repo_path=None):
- super(GitCacheBuster, self).__init__(param=param)
- if repo_path is None:
- repo_path = os.path.dirname(os.path.abspath(__file__))
- self.sha1 = subprocess.check_output(
- ['git', 'rev-parse', 'HEAD'],
- cwd=repo_path).strip()
-
- def tokenize(self, pathspec):
- return self.sha1
+ :linenos:
+
+ import os
+ import subprocess
+ from pyramid.static import QueryStringCacheBuster
+
+ class GitCacheBuster(QueryStringCacheBuster):
+ """
+ Assuming your code is installed as a Git checkout, as opposed to an egg
+ from an egg repository like PYPI, you can use this cachebuster to get
+ the current commit's SHA1 to use as the cache bust token.
+ """
+ def __init__(self, param='x', repo_path=None):
+ super(GitCacheBuster, self).__init__(param=param)
+ if repo_path is None:
+ repo_path = os.path.dirname(os.path.abspath(__file__))
+ self.sha1 = subprocess.check_output(
+ ['git', 'rev-parse', 'HEAD'],
+ cwd=repo_path).strip()
+
+ def tokenize(self, pathspec):
+ return self.sha1
A simple cache buster that modifies the path segment can be constructed as
well:
.. code-block:: python
- :linenos:
+ :linenos:
- import posixpath
+ import posixpath
- class PathConstantCacheBuster(object):
- def __init__(self, token):
- self.token = token
+ class PathConstantCacheBuster(object):
+ def __init__(self, token):
+ self.token = token
- def __call__(self, request, subpath, kw):
- base_subpath, ext = posixpath.splitext(subpath)
- new_subpath = base_subpath + self.token + ext
- return new_subpath, kw
+ def __call__(self, request, subpath, kw):
+ base_subpath, ext = posixpath.splitext(subpath)
+ new_subpath = base_subpath + self.token + ext
+ return new_subpath, kw
The caveat with this approach is that modifying the path segment
changes the file name, and thus must match what is actually on the
@@ -512,25 +512,25 @@ Assuming an example ``manifest.json`` like:
.. code-block:: json
- {
- "css/main.css": "css/main-678b7c80.css",
- "images/background.png": "images/background-a8169106.png"
- }
+ {
+ "css/main.css": "css/main-678b7c80.css",
+ "images/background.png": "images/background-a8169106.png"
+ }
The following code would set up a cachebuster:
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.static import ManifestCacheBuster
+ from pyramid.static import ManifestCacheBuster
- config.add_static_view(
- name='http://mycdn.example.com/',
- path='mypackage:static')
+ config.add_static_view(
+ name='http://mycdn.example.com/',
+ path='mypackage:static')
- config.add_cache_buster(
- 'mypackage:static/',
- ManifestCacheBuster('myapp:static/manifest.json'))
+ config.add_cache_buster(
+ 'mypackage:static/',
+ ManifestCacheBuster('myapp:static/manifest.json'))
It's important to note that the cache buster only handles generating
cache-busted URLs for static assets. It does **NOT** provide any solutions for
@@ -624,10 +624,10 @@ root that exists at the end of your routing table, create an instance of the
application root as below.
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.static import static_view
- static_view = static_view('/path/to/static/dir', use_subpath=True)
+ from pyramid.static import static_view
+ static_view = static_view('/path/to/static/dir', use_subpath=True)
.. note::
@@ -641,13 +641,13 @@ accessible as ``/<filename>`` using a configuration method in your
application's startup code.
.. code-block:: python
- :linenos:
+ :linenos:
- # .. every other add_route declaration should come
- # before this one, as it will, by default, catch all requests
+ # .. every other add_route declaration should come
+ # before this one, as it will, by default, catch all requests
- config.add_route('catchall_static', '/*subpath')
- config.add_view('myapp.static.static_view', route_name='catchall_static')
+ config.add_route('catchall_static', '/*subpath')
+ config.add_view('myapp.static.static_view', route_name='catchall_static')
The special name ``*subpath`` above is used by the
:class:`~pyramid.static.static_view` view callable to signify the path of the
@@ -660,15 +660,15 @@ You can register a simple view callable to serve a single static asset. To do
so, do things "by hand". First define the view callable.
.. code-block:: python
- :linenos:
+ :linenos:
- import os
- from pyramid.response import FileResponse
+ import os
+ from pyramid.response import FileResponse
- def favicon_view(request):
- here = os.path.dirname(__file__)
- icon = os.path.join(here, 'static', 'favicon.ico')
- return FileResponse(icon, request=request)
+ def favicon_view(request):
+ here = os.path.dirname(__file__)
+ icon = os.path.join(here, 'static', 'favicon.ico')
+ return FileResponse(icon, request=request)
The above bit of code within ``favicon_view`` computes "here", which is a path
relative to the Python file in which the function is defined. It then creates
@@ -682,17 +682,17 @@ You might register such a view via configuration as a view callable that should
be called as the result of a traversal:
.. code-block:: python
- :linenos:
+ :linenos:
- config.add_view('myapp.views.favicon_view', name='favicon.ico')
+ config.add_view('myapp.views.favicon_view', name='favicon.ico')
Or you might register it to be the view callable for a particular route:
.. code-block:: python
- :linenos:
+ :linenos:
- config.add_route('favicon', '/favicon.ico')
- config.add_view('myapp.views.favicon_view', route_name='favicon')
+ config.add_route('favicon', '/favicon.ico')
+ config.add_view('myapp.views.favicon_view', route_name='favicon')
Because this is a simple view callable, it can be protected with a
:term:`permission` or can be configured to respond under different
@@ -750,11 +750,11 @@ An individual call to :meth:`~pyramid.config.Configurator.override_asset` can
override a single asset. For example:
.. code-block:: python
- :linenos:
+ :linenos:
- config.override_asset(
- to_override='some.package:templates/mytemplate.pt',
- override_with='another.package:othertemplates/anothertemplate.pt')
+ config.override_asset(
+ to_override='some.package:templates/mytemplate.pt',
+ override_with='another.package:othertemplates/anothertemplate.pt')
The string value passed to both ``to_override`` and ``override_with`` sent to
the ``override_asset`` API is called an :term:`asset specification`. The colon
@@ -764,17 +764,17 @@ specified, the override attempts to resolve every lookup into a package from
the directory of another package. For example:
.. code-block:: python
- :linenos:
+ :linenos:
- config.override_asset(to_override='some.package',
- override_with='another.package')
+ config.override_asset(to_override='some.package',
+ override_with='another.package')
Individual subdirectories within a package can also be overridden:
.. code-block:: python
- :linenos:
+ :linenos:
- config.override_asset(to_override='some.package:templates/',
+ config.override_asset(to_override='some.package:templates/',
override_with='another.package:othertemplates/')
If you wish to override a directory with another directory, you *must* make
@@ -798,10 +798,10 @@ resides (or the ``package`` argument to the
:class:`~pyramid.config.Configurator` class construction). For example:
.. code-block:: python
- :linenos:
+ :linenos:
- config.override_asset(to_override='.subpackage:templates/',
- override_with='another.package:templates/')
+ config.override_asset(to_override='.subpackage:templates/',
+ override_with='another.package:templates/')
Multiple calls to ``override_asset`` which name a shared ``to_override`` but a
different ``override_with`` specification can be "stacked" to form a search
@@ -841,25 +841,25 @@ manifest, and the new assets served by their own cache buster. To do this,
option. For example:
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.static import ManifestCacheBuster
+ from pyramid.static import ManifestCacheBuster
- # define a static view for myapp:static assets
- config.add_static_view('static', 'myapp:static')
+ # define a static view for myapp:static assets
+ config.add_static_view('static', 'myapp:static')
- # setup a cache buster for your app based on the myapp:static assets
- my_cb = ManifestCacheBuster('myapp:static/manifest.json')
- config.add_cache_buster('myapp:static', my_cb)
+ # setup a cache buster for your app based on the myapp:static assets
+ my_cb = ManifestCacheBuster('myapp:static/manifest.json')
+ config.add_cache_buster('myapp:static', my_cb)
- # override an asset
- config.override_asset(
- to_override='myapp:static/background.png',
- override_with='theme:static/background.png')
+ # override an asset
+ config.override_asset(
+ to_override='myapp:static/background.png',
+ override_with='theme:static/background.png')
- # override the cache buster for theme:static assets
- theme_cb = ManifestCacheBuster('theme:static/manifest.json')
- config.add_cache_buster('theme:static', theme_cb, explicit=True)
+ # override the cache buster for theme:static assets
+ theme_cb = ManifestCacheBuster('theme:static/manifest.json')
+ config.add_cache_buster('theme:static', theme_cb, explicit=True)
In the above example there is a default cache buster, ``my_cb``, for all
assets served from the ``myapp:static`` folder. This would also affect