Pyjoyment.Static - Serve static files

from Pyjoyment.Static import Static

static = Static()
static.classes.append('MyApp.Controller.Foo')
static.paths.append('/home/sri/public')

# Or import the module, as in Google Python Style Guide

import Pyjoyment.Static

static = Pyjoyment.Static.new()
static.classes.append('MyApp.Controller.Foo')
static.paths.append('/home/sri/public')

Pyjoyment.Static is a static file server with Range, If-Modified-Since and If-None-Match support, based on RFC 7232 and RFC 7233.

A file of paths with a precompressed variant next to it, as app.css.gz of gzip -k, is served as the variant to the clients whose Accept-Encoding takes its encoding, with the encodings and the extensions of encodings. A variant older than its file is ignored, and a Range request gets the file.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojolicious::Static of Mojolicious 9.49.

class Pyjoyment.Static.Pyjoyment_Static[source]

Pyjoyment.Static inherits all methods from Pyjo.Base and implements the following new ones.

asset_dir
dir = static.asset_dir
static.asset_dir = 'assets'

Subdirectory used for all static assets, defaults to assets.

asset_path(asset: str) → str[source]
path = static.asset_path('/app.js')

Get static asset path.

cache_control: str | Callable[[Pyjoyment.Controller.object, str], str | None] | None
cache_control = static.cache_control
static.cache_control = 'public, max-age=60'
static.cache_control = lambda c, rel: 'no-cache' if rel.endswith('.html') else 'public, max-age=60'

Cache-Control of the files that are served, also with 304, unless the response has one already, or a function that gets the controller and the path of the file, relative to paths and without prefix, and returns it, or None for none, defaults to None. The files of immutable get theirs.

# Revalidate every file after a minute
static.cache_control = 'public, max-age=60'
classes
classes = static.classes
static.classes = ['__main__']

Classes to use for finding files in DATA sections with Pyjo.Loader, first one has the highest precedence, defaults to __main__. Only files with exactly one extension will be used, like index.html. Note that for files to be detected, these classes need to have already been loaded and added before warmup() is called, which usually happens automatically during application startup.

# Add another class with static files in DATA section
static.classes.append('Pyjoyment.Plugin.Fun')

# Add another class with static files in DATA section and higher precedence
static.classes.insert(0, 'Pyjoyment.Plugin.MoreFun')
dispatch(c: Pyjoyment.Controller.object) → bool | None[source]
boolean = static.dispatch(Pyjoyment.Controller.new())

Serve static file for Pyjoyment.Controller object.

encodings
encodings = static.encodings
static.encodings = {'gzip': '.gz'}

Encodings of the precompressed variants of the files of paths and the extensions of their files, in their order of preference, defaults to {'br': '.br', 'zstd': '.zst', 'gzip': '.gz'}, as brotli -k, zstd -k and gzip -k write them. A request whose Accept-Encoding takes an encoding gets the variant with its Content-Encoding, and the Content-Type of the file.

# Serve the files as they are only
static.encodings = {}
extra
extra = static.extra
static.extra = {'foo/bar.txt': '/home/sri/myapp/bar.txt'}

Paths for extra files to be served from locations other than paths, such as the images used by the built-in exception and not found pages. Note that extra files are only served if no better alternative could be found in paths and classes.

# Remove built-in favicon
del static.extra['favicon.ico']
file(rel: str) → Pyjo_Asset_File | Pyjo_Asset_Memory | None[source]
asset = static.file('images/logo.png')
asset = static.file('../lib/MyApp.py')

Build Pyjo.Asset.File or Pyjo.Asset.Memory object for a file, relative to paths or from classes, or return None if it doesn’t exist. Note that this method uses a relative path, but does not protect from traversing to parent directories.

content = static.file('foo/bar.html').slurp()
file_path(file: str) → str[source]
path = static.file_path('/index.html')

Get static file path with prefix if it has been configured.

immutable: str | re.Pattern[str] | None
immutable = static.immutable
static.immutable = r'\.[0-9a-f]{12}\.[^/.]+$'

Regular expression of the paths of the files, relative to paths and without prefix, whose names have a hash of their content, as the ones of a bundler, and never change, so that they are served with Cache-Control: public, max-age=31536000, immutable in place of cache_control, defaults to None. The assets of the development mode are served with Cache-Control: no-cache still.

# The names of ManifestStaticFilesStorage of Django
static.immutable = re.compile(r'\.[0-9a-f]{12}\.[^/.]+$')
is_fresh(c: Pyjoyment.Controller.object, *, etag: str | None = None, last_modified: float | None = None) → bool[source]
boolean = static.is_fresh(Pyjoyment.Controller.new(), etag='abc')
boolean = static.is_fresh(Pyjoyment.Controller.new(), etag='W/"def"')

Check freshness of request by comparing the If-None-Match and If-Modified-Since request headers to the ETag and Last-Modified response headers.

These options are currently available:

etag

etag='abc'
etag='W/"abc"'

Add ETag header before comparing.

last_modified

last_modified=epoch

Add Last-Modified header before comparing.

paths: list[str]
paths = static.paths
static.paths = ['/home/sri/public']

Directories to serve static files from, first one has the highest precedence.

# Add another "public" directory
static.paths.append('/home/sri/public')

# Add another "public" directory with higher precedence
static.paths.insert(0, '/home/sri/themes/blue/public')
prefix: str | None
prefix = static.prefix
static.prefix = '/static'

Prefix to use for all static files, defaults to None. This can be very useful for production deployments where the reverse proxy server should take over serving static files.

serve(c: Pyjoyment.Controller.object, rel: str) → bool | None[source]
boolean = static.serve(Pyjoyment.Controller.new(), 'images/logo.png')
boolean = static.serve(Pyjoyment.Controller.new(), '../lib/MyApp.py')

Serve a specific file, relative to paths or from classes. Note that this method uses a relative path, but does not protect from traversing to parent directories.

serve_asset(c: Pyjoyment.Controller.object, asset: Pyjo_Asset) → Any[source]
static.serve_asset(Pyjoyment.Controller.new(), Pyjo.Asset.File.new())

Serve a Pyjo.Asset.File or Pyjo.Asset.Memory object with Range, If-Modified-Since and If-None-Match support.

warmup() → None[source]
static.warmup()

Prepare static files from classes and static assets for future use.