File Resources: zope.browserresource.file

File-based browser resources.

zope.browserresource.file.parse_etags(value)[source]

Parse a list of entity tags.

HTTP/1.1 specifies the following syntax for If-Match/If-None-Match headers:

If-Match = "If-Match" ":" ( "*" | 1#entity-tag )
If-None-Match = "If-None-Match" ":" ( "*" | 1#entity-tag )

entity-tag = [ weak ] opaque-tag

weak       = "W/"
opaque-tag = quoted-string

quoted-string  = ( <"> *(qdtext) <"> )
qdtext         = <any TEXT except <">>

The backslash character ("\") may be used as a single-character
quoting mechanism only within quoted-string and comment constructs.

Examples:

>>> parse_etags('*')
['*']
>>> parse_etags(r' "qwerty", ,"foo",W/"bar" , "baz","\""')
['"qwerty"', '"foo"', 'W/"bar"', '"baz"', '"\\""']

Ill-formed headers are ignored

>>> parse_etags("not an etag at all")
[]
zope.browserresource.file.etag_matches(etag, tags)[source]

Check if the entity tag matches any of the given tags.

>>> etag_matches('"xyzzy"', ['"abc"', '"xyzzy"', 'W/"woof"'])
True
>>> etag_matches('"woof"', ['"abc"', 'W/"woof"'])
False
>>> etag_matches('"xyzzy"', ['*'])
True

Note that you pass quoted etags in both arguments!

zope.browserresource.file.quote_etag(etag)[source]

Quote an etag value

>>> quote_etag("foo")
'"foo"'

Special characters are escaped

>>> quote_etag('"')
'"\\""'
>>> quote_etag('\\')
'"\\\\"'
class zope.browserresource.file.File(path, name)[source]

Bases: object

An object representing a file on the filesystem.

These are created by FileResourceFactory for use with FileResource.

class zope.browserresource.file.FileResource(context, request)[source]

Bases: zope.publisher.browser.BrowserView, zope.browserresource.resource.Resource

Default implementation of IFileResource.

This class also implements zope.publisher.interfaces.browser.IBrowserPublisher.

publishTraverse(request, name)[source]

File resources can’t be traversed further, so raise NotFound if someone tries to traverse it.

>>> factory = FileResourceFactory(testFilePath, nullChecker, 'test.txt')
>>> request = TestRequest()
>>> resource = factory(request)
>>> resource.publishTraverse(request, '_testData')
Traceback (most recent call last):
...
NotFound: Object: None, name: '_testData'
browserDefault(request)[source]

Return a callable for processing browser requests.

>>> factory = FileResourceFactory(testFilePath, nullChecker, 'test.txt')
>>> request = TestRequest(REQUEST_METHOD='GET')
>>> resource = factory(request)
>>> view, next = resource.browserDefault(request)
>>> with open(testFilePath, 'rb') as f:
...     view() == f.read()
True
>>> next == ()
True
>>> request = TestRequest(REQUEST_METHOD='HEAD')
>>> resource = factory(request)
>>> view, next = resource.browserDefault(request)
>>> view() == b''
True
>>> next == ()
True
chooseContext()[source]

Choose the appropriate context.

This method can be overriden in subclasses, that need to choose appropriate file, based on current request or other condition, like, for example, i18n files.

See also

I18nFileResource

See also

II18nResourceDirective

GET()[source]

Return a file data for downloading with GET requests

>>> factory = FileResourceFactory(testFilePath, nullChecker, 'test.txt')
>>> request = TestRequest()
>>> resource = factory(request)
>>> with open(testFilePath, 'rb') as f:
...     resource.GET() == f.read()
True
>>> request.response.getHeader('Content-Type') == 'text/plain'
True
HEAD()[source]

Return proper headers and no content for HEAD requests

>>> factory = FileResourceFactory(testFilePath, nullChecker, 'test.txt')
>>> request = TestRequest()
>>> resource = factory(request)
>>> resource.HEAD() == b''
True
>>> request.response.getHeader('Content-Type') == 'text/plain'
True
class zope.browserresource.file.FileETag(context, request)[source]

Bases: object

Default implementation of IETag registered for IFileResource and zope.publisher.interfaces.browser.IBrowserRequest.

class zope.browserresource.file.FileResourceFactory(path, checker, name)[source]

Bases: object

Implementation of IResourceFactory producing FileResource.

The class itself provides IResourceFactoryFactory

resourceClass

alias of FileResource