API documentation

metadataDB

Author: Corrado Motta Date: 10/2022

This module is a customized wrapper of PysonDB-V2, which implements a Lightweight JSON based database. Through the functions of this module it is possible to add, remove, edit and visualize the metadata collected in the database.

Other information

This module requires the installation of PysonDB to work. Check https://pysondb.github.io/pysonDB-v2/ for installing into your python environment.

exception metadataDB.Error(method, message)

Base class for exceptions.

Parameters
  • method (str) – Method in which the error occurred.

  • message (str) – Explanation of the error.

class metadataDB.metadataDB(db, unique_key=None)

Class that contains the PysonDB object. If the database is not existing, it creates the database, otherwise it connects to the existing file.

Parameters
  • db (str) – Path to the existing or new database.

  • unique_key (str) – Key that shall have a unique value in the entire dataset.

add(metadata_entry)

Adds a new entry to the current database if it does not exist yet or if some of the values contained are different.

Parameters

metadata_entry (dict) – Dictionary of the metadata entry.

Returns

True if added, false if not (because it already exists).

Return type

bool

dict_exists(item)

Returns true if the dictionary exists.

Parameters

item (dict) – The item to look for.

Returns

True if present, false if not present.

Return type

bool

element_exists(key, name)

Returns true if the element exists.

Parameters
  • key (str) – Which key should be used for the look up.

  • name (str) – The name that should be searched.

Returns

True if present, false if not present.

Return type

bool

getAll()

Returns all elements.

Returns

A dict containing all elements.

Return type

dict

getById(id)

Returns the database entry found for the required ID.

Parameters

id (str) – The database ID.

Returns

The dictionary of the element. None if not present.

Return type

dict

getEntry(key, name)

Returns the database entries found for the key value pair.

Parameters
  • key (str) – Which key should be used for the look up.

  • name (str) – The name that should be searched.

Returns

The dictionaries of the found entries. None if not present.

Return type

list

getId(key, name)

Returns the database entry found for the required element.

Parameters
  • key (str) – Which key should be used for the look up.

  • name (str) – The name that should be searched.

Returns

The dictionary of the element. None if not present.

Return type

dict

id_exists(id)

Returns true if ID exists.

Parameters

id (str) – The id of the element.

Returns

True if present, false if not present.

Return type

bool

removeEntry(id)

Deletes the specific entry.

Parameters

id (str) – The database entry ID.

Returns

the deleted dictionary.

Return type

dict

unique_value_exists(value)

Returns true if the value is already used by the unique key of one of the entries.

Parameters

value (str) – The value to look for.

Returns

True if present, false if not present.

Return type

bool

updateEntry(id, updatedEntry)

Updates the specific entry by passing a new dictionary. It can contain the whole set of keys value of any of the subset.

Parameters
  • id (str) – The database entry ID.

  • updatedEntry (dict) – The new dictionary.

Returns

the updated dictionary.

Return type

dict

exception metadataDB.notUniqueError(method, message, ids)

Error that occur when more than one element is found in the database for a query.

Parameters
  • method (str) – Method in which the error occurred.

  • message (str) – Explanation of the error.

  • ids (list) – List of IDs.

exception metadataDB.uniqueKeyError(method, message, value)

Error that occur when a new entry has an already existing value for the unique key.

Parameters
  • method (str) – Method in which the error occurred.

  • message (str) – Explanation of the error.

  • value (str) – The unique value.