API reference

The API for the tasks project.

This module provides classes and functions to interact with the tasks database programmatically.

cusy.tasks.api.Task class

class cusy.tasks.api.Task(summary: str = None, owner: str = None, state: str = 'todo', id: int = None)[source]

Defines the tasks type with the attributes summary, owner and state.

Parameters:
  • summary (str) – Summary of a task. Defaults to None.

  • owner (str) – The person working on a task. Defaults to None.

  • state (str) – The status of a task. Defaults to ‘todo’.

  • id (int) – The unique identifier for the task. Defaults to None.

classmethod from_dict(d)[source]

Return a task instance from a dict.

Parameters:

d (dict) – Dictionary containing task attributes.

Returns:

Task instance created from dictionary values.

Return type:

Task

to_dict()[source]

Return a dict from a task instance.

Returns:

Dictionary containing the task’s attributes.

Return type:

dict

cusy.tasks.api.TasksDB class

class cusy.tasks.api.TasksDB(db_path)[source]

Database class to access the tasks_db file.

Parameters:

db_path (str or pathlib.Path) – Path to the database file.

add_task(task: Task)[source]

Add a task to the database.

Parameters:

task (Task) – The Task instance to add to the database.

Returns:

The task id of the newly added task.

Return type:

int

Raises:

MissingSummaryError – If the task has no summary.

close()[source]

Close the database connection.

count()[source]

Return the number of tasks in the database.

Returns:

The number of tasks in the database.

Return type:

int

delete_all()[source]

Remove all tasks from the database.

delete_task(task_id: int)[source]

Remove a task from the database.

Parameters:

task_id (int) – The ID of the task to delete.

Raises:

InvalidTaskIdError – If no task with the given ID exists.

finish(task_id: int)[source]

Set a task state to ‘done’.

Parameters:

task_id (int) – The ID of the task to update.

Raises:

InvalidTaskIdError – If no task with the given ID exists.

get_task(task_id: int)[source]

Return a task for the corresponding id.

Parameters:

task_id (int) – ID of the task to retrieve.

Returns:

Task instance from the database.

Return type:

Task

Raises:

InvalidTaskIdError – If no task with the given ID exists.

list_tasks(owner=None, state=None)[source]

Return a list of tasks filtered by owner and/or state.

Parameters:
  • owner (str, optional) – Filter tasks by this owner. Defaults to

  • None.

  • state (str, optional) – Filter tasks by this state. Defaults to

  • None.

Returns:

List of Task instances matching the filters. If no filters are specified, returns all tasks.

Return type:

list[Task]

path()[source]

Return the path to the database.

Returns:

Path to the database file.

Return type:

str or pathlib.Path

start(task_id: int)[source]

Set a task state to ‘in progress’.

Parameters:

task_id (int) – The ID of the task to update.

Raises:

InvalidTaskIdError – If no task with the given ID exists.

update_task(task_id: int, task_mods: Task)[source]

Update a task with modifications.

Parameters:
  • task_id (int) – The ID of the task to update.

  • task_mods (Task) – Task instance containing the modifications to

  • apply.

Raises:

InvalidTaskIdError – If no task with the given ID exists.

Exceptions

exception cusy.tasks.api.TasksError[source]

Base exception class for the tasks module.

Parent class for MissingSummaryError and InvalidTaskIdError exceptions.

exception cusy.tasks.api.MissingSummaryError[source]

Exception raised when a task is added without a summary.

Raised when cusy.tasks.api.TasksDB.add_task is called with a task that has no summary.

exception cusy.tasks.api.InvalidTaskIdError[source]

Exception raised when an operation is performed with an invalid task ID.

Raised when trying to access or modify a task that doesn’t exist.