Exemple #1
0
def add_static_route(config, package, subdir, cache_max_age=3600,
    **add_route_args):
    """Add a route and view to serve static files from a directory.

    I create a catchall route that serves all URLs from a directory of static
    files if the corresponding file exists. Subdirectories are also handled.
    For example, the URL "/robots.txt" corresponds to file
    "PACKAGE/SUBDIR/robots.txt", and "/images/header/logo.png"
    corresponds to "PACKAGE/SUBDIR/images/header/logo.png".  If the file
    doesn't exist, the route won't match the URL, and Pyramid will continue to
    the next route or traversal. The route name is 'static', which must not
    conflict with your application's other routes.

    Usage in the application's __init__.py::

        from akhet.static import add_static_route
        add_static_route(config, "myapp", "static")

    Or, more conveniently::

        config.include("akhet")
        config.add_static_route("myapp", "static")

    This serves URLs from the "static" directory in package "myapp".

    Arguments:

    * ``config``: a ``pyramid.config.Configurator`` instance.

    * ``package``: the name of the Python package containing the static files.

    * ``subdir``: the subdirectory in the package that contains the files.
      This should be a relative directory with '/' separators regardless of
      platform. 

    * ``cache_max_age``: influences the ``Expires`` and ``Max-Age``
      response headers returned by the view (default is 3600 seconds or five
      minutes).

    * ``**add_route_args``: additional arguments to ``config.add_route``.
      'name' defaults to "static" but can be overridden. (Every route in your
      application must have a unique name.) 'pattern' and 'view' may not be
      specified and will raise TypeError if they are.
    """
    for bad_arg in ["pattern", "view"]:
        if bad_arg in add_route_args:
            raise TypeError("keyword arg '%s' is not allowed")
    name = add_route_args.pop("name", "static")
    pattern = "/*subpath"
    asset = "%s:%s" % (package, subdir)
    view = static(asset, cache_max_age)
    pred = StaticViewPredicate(package, subdir)
    custom_predicates = add_route_args.pop("custom_predicates", [])
    custom_predicates = list(custom_predicates)
    custom_predicates.insert(0, pred)
    config.add_route(name, pattern, view=view, 
        custom_predicates=custom_predicates, **add_route_args)
Exemple #2
0
 def _makeOne(self, path, package_name):
     from pyramid.view import static
     return static(path, package_name)
Exemple #3
0
    def _makeOne(self, path, package_name):
        from pyramid.view import static

        return static(path, package_name)
Exemple #4
0
    def test_scanned(self):
        from pyramid.interfaces import IRequest
        from pyramid.interfaces import IView
        from pyramid.interfaces import IViewClassifier
        from pyramid.configuration import Configurator
        from pyramid.tests import test_integration
        config = Configurator()
        config.scan(test_integration)
        reg = config.registry
        view = reg.adapters.lookup(
            (IViewClassifier, IRequest, INothing), IView, name='')
        self.assertEqual(view, wsgiapptest)

here = os.path.dirname(__file__)
staticapp = static(os.path.join(here, 'fixtures'))

class TestStaticApp(unittest.TestCase):
    def test_it(self):
        from webob import Request
        context = DummyContext()
        from StringIO import StringIO
        request = Request({'PATH_INFO':'',
                           'SCRIPT_NAME':'',
                           'SERVER_NAME':'localhost',
                           'SERVER_PORT':'80',
                           'REQUEST_METHOD':'GET',
                           'wsgi.version':(1,0),
                           'wsgi.url_scheme':'http',
                           'wsgi.input':StringIO()})
        request.subpath = ['minimal.pt']
Exemple #5
0
from pyramid.view import static
static_view = static('oerpub.rhaptoslabs.cnxml2htmlpreview:preview_css')
Exemple #6
0
from cone.tile import registerTile
from cone.app.browser.layout import ProtectedContentTile
from c3s.app.model import (
    C3SApp,
    Band,
    Track,
)

from pyramid.view import static
static_view = static('static')

registerTile('content',
             'cone.app:browser/templates/listing.pt',
             interface=C3SApp,
             class_=ProtectedContentTile,
             permission='login',
             strict=False)


registerTile('content',
             'cone.app:browser/templates/listing.pt',
             interface=Band,
             class_=ProtectedContentTile,
             permission='login',
             strict=False)


registerTile('content',
             'c3s.app:browser/templates/track.pt',
             interface=Track,
             class_=ProtectedContentTile,
Exemple #7
0
        from pyramid.interfaces import IView
        from pyramid.interfaces import IViewClassifier
        from pyramid.config import Configurator
        from pyramid.tests import test_integration
        config = Configurator()
        config.scan(test_integration)
        config.commit()
        reg = config.registry
        view = reg.adapters.lookup((IViewClassifier, IRequest, INothing),
                                   IView,
                                   name='')
        self.assertEqual(view, wsgiapptest)


here = os.path.dirname(__file__)
staticapp = static(os.path.join(here, 'fixtures'))


class TestStaticApp(unittest.TestCase):
    def test_it(self):
        from webob import Request
        context = DummyContext()
        from StringIO import StringIO
        request = Request({
            'PATH_INFO': '',
            'SCRIPT_NAME': '',
            'SERVER_NAME': 'localhost',
            'SERVER_PORT': '80',
            'REQUEST_METHOD': 'GET',
            'wsgi.version': (1, 0),
            'wsgi.url_scheme': 'http',
        from pyramid.interfaces import IRequest
        from pyramid.interfaces import IView
        from pyramid.interfaces import IViewClassifier
        from pyramid.config import Configurator
        from pyramid.tests import test_integration

        config = Configurator()
        config.scan(test_integration)
        config.commit()
        reg = config.registry
        view = reg.adapters.lookup((IViewClassifier, IRequest, INothing), IView, name="")
        self.assertEqual(view, wsgiapptest)


here = os.path.dirname(__file__)
staticapp = static(os.path.join(here, "fixtures"))


class TestStaticApp(unittest.TestCase):
    def test_it(self):
        from webob import Request

        context = DummyContext()
        from StringIO import StringIO

        request = Request(
            {
                "PATH_INFO": "",
                "SCRIPT_NAME": "",
                "SERVER_NAME": "localhost",
                "SERVER_PORT": "80",
Exemple #9
0
from pyramid.config import Configurator
from plingback.resources import TripleStore


from pyramid.view import static

static_view = static("plingback:static")


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """

    config = Configurator(settings=settings)

    # Set up the output api
    config.add_route(
        "pling_attribute",
        "/api/plings/{id}/{attribute}",
        view="plingback.output.views.handler",
        view_renderer="json",
        request_method="GET",
        factory=TripleStore,
    )
    config.add_route(
        "scoped_attribute",
        "/api/{scope}/{id}/{attribute}",
        view="plingback.output.views.handler",
        view_renderer="json",
        request_method="GET",
        factory=TripleStore,