yawning_titan.db.yawning_titan_db.YawningTitanDB#

class yawning_titan.db.yawning_titan_db.YawningTitanDB(name, root=None)[source]#

Bases: object

An ABC that implements and extends the TinyDB query functions.

Methods

all

A wrapper for tinydb.table.Table.all().

close

Close the db.

count

Count how many docs are in the db.

get

Get a doc from its uuid.

insert

An extension of tinydb.table.Table.insert().

is_locked

Check whether a doc is locked for editing or not.

remove

Remove a document with a given _doc_metadata.uuid.

remove_by_cond

Remove documents that match a query.

search

A wrapper for tinydb.table.Table.search().

show

Show details of all entries in the db.

update

An extension of tinydb.table.Table.update().

upsert

A manual upsert method in place of tinydb.table.Table.upsert().

Attributes

db

The instance of TinyDB.

name

The DB name.

close()[source]#

Close the db.

classmethod is_locked(doc)[source]#

Check whether a doc is locked for editing or not.

Parameters:

doc – A doc.

Returns:

True if the doc is locked, otherwise False.

property name#

The DB name.

property db#

The instance of TinyDB.

count(cond=None)[source]#

Count how many docs are in the db. Extends tinydb.table.Table.count.

A YawningTitanQuery can be used to filter the count.

Parameters:

cond – An optional YawningTitanQuery. Has a default value of None.

Returns:

The number of docs counted.

all()[source]#

A wrapper for tinydb.table.Table.all().

show(verbose=False)[source]#

Show details of all entries in the db.

Parameters:

verbose – If True, all doc metadata details are shown, otherwise just the name is shown.

get(uuid)[source]#

Get a doc from its uuid.

Parameters:

uuid – A uuid.

Returns:

The matching doc if it exists, otherwise None.

Raise:

YawningTitanDBCriticalError when the search returns multiple docs with the same uuid.

search(cond)[source]#

A wrapper for tinydb.table.Table.search().

insert(doc, name=None, description=None, author=None)[source]#

An extension of tinydb.table.Table.insert().

If a doc doesn’t have DocMetadata, the default DocMetadata is set.

If a doc already exists with the same uuid, the insert is blocked and a YawningTitanDBCriticalError is raised alongside a log as CRITICAL level, as this would indicate db file is corrupted.

Parameters:
  • doc – A doc.

  • name – The doc name.

  • description – The doc description.

  • author – The docs author.

Returns:

The inserted doc.

Raise:

YawningTitanDBCriticalError when a doc already exists with the same uuid.

update(doc, uuid, name=None, description=None, author=None)[source]#

An extension of tinydb.table.Table.update().

Performs a check that prevents locked files from being updated and raises YawningTitanDBError alongside a log at INFO level.

Parameters:
  • doc – A doc.

  • uuid – The docs uuid.

  • name – The doc name.

  • description – The doc description.

  • author – The docs author.

Returns:

The updated doc.

Raise:

YawningTitanDBError if the doc is locked.

upsert(doc, uuid, name=None, description=None, author=None)[source]#

A manual upsert method in place of tinydb.table.Table.upsert().

If the docs uuid already exists, the args are passed to yawning_titan.db.yawning_titan_db.YawningTitanDB.update() to perform an update, otherwise they’re passed to insert() to perform an insert. This is done to make use of the existing uuid and locked file handling in the two methods.

Parameters:
  • doc – A doc.

  • uuid – The docs uuid.

  • name – The doc name.

  • description – The doc description.

  • author – The docs author.

Returns:

The updated doc.

remove_by_cond(cond)[source]#

Remove documents that match a query.

Parameters:

cond – A:class:~yawning_titan.db.query.YawningTitanQuery.

Returns:

The list of uuids from documents removed.

remove(uuid)[source]#

Remove a document with a given _doc_metadata.uuid.

Parameters:

uuid – A documents _doc_metadata.uuid.

Returns:

The uuid of the removed document.

Raises:

YawningTitanDBCriticalError when there is more than one doc with the same uuid to remove. YawningTitanDBError when an attempt to remove a locked doc is made.