fietsboek.data module

Data manager for fietsboek.

Data are objects that belong to a track (such as images), but are not stored in the database itself. This module makes access to such data objects easier.

class fietsboek.data.DataManager(data_dir)

Bases: object

Data manager.

The data manager is usually provided as request.data_manager and can be used to access track’s images and other on-disk data.

Variables:

data_dir – Path to the data folder.

initialize(track_id)

Creates the data directory for a track.

Raises:

FileExistsError – If the directory already exists.

Parameters:

track_id (int) – ID of the track.

Return type:

TrackDataDir

Returns:

The manager that can be used to manage this track’s data.

initialize_user(user_id)

Creates the data directory for a user.

Raises:

FileExistsError – If the directory already exists.

Parameters:

user_id (int) – ID of the user.

Return type:

UserDataDir

Returns:

The manager that can be used to manage this user’s data.

maintenance_mode()

Checks whether the maintenance mode is enabled.

If maintenance mode is enabled, returns the reason given.

If maintenance mode is disabled, returns None.

Return type:

Optional[str]

Returns:

The maintenance mode state.

open(track_id)

Opens a track’s data directory.

Raises:

FileNotFoundError – If the track directory does not exist.

Parameters:

track_id (int) – ID of the track.

Return type:

TrackDataDir

Returns:

The manager that can be used to manage this track’s data.

open_user(user_id)

Opens a user’s data directory.

Raises:

FileNotFoundError – If the user directory does not exist.

Parameters:

user_id (int) – ID of the user.

Return type:

UserDataDir

Returns:

The manager that can be used to manage this user’s data.

purge(track_id)

Forcefully purges all data from the given track.

This function logs errors but raises no exception, as such it can always be used to clean up after a track.

class fietsboek.data.TrackDataDir(track_id, path, *, journal=False, is_fresh=False)

Bases: object

Manager for a single track’s data.

If initialized with journal = True, then you can use rollback() to roll back the changes in case of an error. In case of no error, use commit() to commit the changes. If you don’t want the “journalling” semantics, use journal = False.

add_image(image, filename=None)

Saves an image to a track.

Parameters:
  • image (BinaryIO) – The image, as a file-like object to read from.

  • filename (Optional[str]) – The image’s original filename.

Return type:

str

Returns:

The ID of the saved image.

backup()

Create a backup of the GPX file.

backup_path()

Path of the GPX backup file.

Return type:

Path

Returns:

The path of the backup file.

commit()

Commits all changed and deletes the journal.

Note that this function will do nothing if the journal is disabled, meaning it can always be called.

compress_gpx(data, quality=4)

Set the GPX content to the compressed form of data.

If you want to write compressed data directly, use gpx_path() to get the path of the GPX file.

Parameters:
  • data (bytes) – The GPX data (uncompressed).

  • quality (int) – Compression quality, from 0 to 11 - 11 is highest quality but slowest compression speed.

decompress_gpx()

Returns the GPX bytes decompressed.

Return type:

bytes

Returns:

The saved GPX file, decompressed.

delete_image(image_id)

Deletes an image from a track.

Raises:

FileNotFoundError – If the given image could not be found.

Parameters:

image_id (str) – ID of the image.

engrave_metadata(title, description, author_name, time, *, gpx=None)

Engrave the given metadata into the GPX file.

Note that this will overwrite all existing metadata in the given fields.

If None is given, it will erase that specific part of the metadata.

Parameters:
  • title (Optional[str]) – The title of the track.

  • description (Optional[str]) – The description of the track.

  • creator – Name of the track’s creator.

  • time (Optional[datetime]) – Time of the track.

  • gpx (Optional[GPX]) – The pre-parsed GPX track, to save time if it is already parsed.

gpx_path()

Returns the path of the GPX file.

This file contains the (brotli) compressed GPX data.

Return type:

Path

Returns:

The path where the GPX is supposed to be.

image_path(image_id)

Returns a path to a saved image.

Raises:

FileNotFoundError – If the given image could not be found.

Parameters:

image_id (str) – ID of the image.

Return type:

Path

Returns:

A path pointing to the requested image.

images()

Returns a list of images that belong to the track.

Parameters:

track_id – Numerical ID of the track.

Return type:

List[str]

Returns:

A list of image IDs.

lock()

Returns a FileLock that can be used to lock access to the track’s data.

Return type:

UnixFileLock

Returns:

The lock responsible for locking this data directory.

purge()

Purge all data pertaining to the track.

This function logs errors but raises no exception, as such it can always be used to clean up after a track.

rollback()

Rolls back the journal, e.g. in case of error.

Raises:

ValueError – If the data directory was opened without the journal, this raises ValueError.

size()

Returns the size of the data that this track entails.

Return type:

int

Returns:

The size of bytes that this track consumes.

class fietsboek.data.UserDataDir(user_id, path)

Bases: object

Manager for a single user’s data.

heatmap_path()

Returns the path for the heatmap tile file.

Return type:

Path

Returns:

The path of the heatmap SQLite databse.

tilehunt_path()

Returns the path for the tilehunt tile file.

Return type:

Path

Returns:

The path of the tilehunt SQLite database.

fietsboek.data.generate_filename(filename)

Generates a safe-to-use filename for uploads.

If possible, tries to keep parts of the original filename intact, such as the extension.

Parameters:

filename (Optional[str]) – The original filename.

Return type:

str

Returns:

The generated filename.