core.filestorage

Filestorage is a way to store files locally or on a remote server, with the interface being the same, no matter where the files are stored.

Based on https://docs.pyfilesystem.org/en/latest/

See onegov.core.framework.Framework.filestorage for more information.

Attributes

_requires_normalization

Exceptions

IllegalBackReference

Too many backrefs exist in a path.

Classes

Filestorage

A minimal implementation of fs/PyFilesystem's OSFS

FilestorageFile

Defines a static file served by the application. The difference

Functions

random_filename(→ str)

Returns a random filename that can't be guessed.

normpath(→ str)

get_filestorage_file(→ FilestorageFile | None)

view_filestorage_file(→ str)

Renders the given filestorage file in the browser.

delete_static_file(→ None)

Deletes the given filestorage file. By default the permission is

Module Contents

core.filestorage.random_filename() str[source]

Returns a random filename that can’t be guessed.

exception core.filestorage.IllegalBackReference(path: str)[source]

Bases: ValueError

Too many backrefs exist in a path.

core.filestorage._requires_normalization[source]
core.filestorage.normpath(path: str) str[source]
class core.filestorage.Filestorage(root_path: _typeshed.StrOrBytesPath, create: bool = False, create_mode: int = 511, expand_vars: bool = True)[source]

A minimal implementation of fs/PyFilesystem’s OSFS class, that can do everything we need in OneGov.

Since fs was no longer maintained and the maintained alternative fsspec does not yet have type hints of any kind and we don’t really use anything other than OSFS in any of our deployments this seemed like the most sensible solution.

We can always migrate to fsspec later, if we do end up needing more control again.

_lock[source]
_root_path = b'.'[source]
hassyspath(path: str) bool[source]
validatepath(path: str) str[source]
getsyspath(path: str) str[source]
hasurl(path: str) bool[source]
geturl(path: str) str[source]
exists(path: str) bool[source]
isdir(path: str) bool[source]
isfile(path: str) bool[source]
opendir(path: str) Self[source]
makedir(path: str, recreate: bool = False) Self[source]
listdir(path: str) list[str][source]
create(path: str) bool[source]
touch(path: str) None[source]
remove(path: str) None[source]
removetree(path: str) None[source]
open(path: str, mode: _typeshed.OpenTextMode = 'r', buffering: int = -1, encoding: str | None = None, errors: str | None = None, newline: str = '') io.TextIOWrapper[source]
open(path: str, mode: _typeshed.OpenBinaryMode, buffering: Literal[0], encoding: None = None, errors: None = None, newline: str = '') io.FileIO
open(path: str, mode: _typeshed.OpenBinaryModeUpdating, buffering: Literal[-1, 1] = -1, encoding: None = None, errors: None = None, newline: str = '') io.BufferedRandom
open(path: str, mode: _typeshed.OpenBinaryModeWriting, buffering: Literal[-1, 1] = -1, encoding: None = None, errors: None = None, newline: str = '') io.BufferedWriter
open(path: str, mode: _typeshed.OpenBinaryModeReading, buffering: Literal[-1, 1] = -1, encoding: None = None, errors: None = None, newline: str = '') io.BufferedReader
open(path: str, mode: _typeshed.OpenBinaryMode, buffering: int = -1, encoding: None = None, errors: None = None, newline: str = '') IO[bytes]
open(path: str, mode: str, buffering: int = -1, encoding: str | None = None, errors: str | None = None, newline: str = '') IO[Any]
writebytes(path: str, contents: bytes) None[source]
readbytes(path: str) bytes[source]
writetext(path: str, contents: str) None[source]
readtext(path: str) str[source]
getmodified(path: str) datetime.datetime[source]
__repr__() str[source]
__str__() str[source]
class core.filestorage.FilestorageFile(path: str)[source]

Defines a static file served by the application. The difference between this and onegov.core.static.StaticFile is the storage.

Filestorage files are stored per application_id locally or on the cloud. Static files are the same for the whole application class and they are deployed statically. That means they are not content, but part of the distribution.

Note that this is only used if the file is local. Files stored in the filestorage should be linked using onegov.core.request.CoreRequest.filestorage_link(), which might result in a local path, for which this class is used. Or it might result in a remote path that is served by some different webserver.

storage = 'filestorage'[source]
path[source]
property absorb: str[source]
core.filestorage.get_filestorage_file(app: onegov.core.framework.Framework, absorb: str) FilestorageFile | None[source]
core.filestorage.view_filestorage_file(self: FilestorageFile, request: core.request.CoreRequest) str[source]

Renders the given filestorage file in the browser.

core.filestorage.delete_static_file(self: FilestorageFile, request: core.request.CoreRequest) None[source]

Deletes the given filestorage file. By default the permission is Private. An application using the framework can override this though.

Since a DELETE can only be sent through AJAX it is protected by the same-origin policy. That means that we don’t need to use any CSRF protection here.

That being said, browser bugs and future changes in the HTML standard make it possible for this to happen one day. Therefore, a time-limited token must be passed as query parameter to this function.

New tokens can be acquired through request.new_csrf_token.