_item module#

class cache_manager._item.CacheItem(key, version=1, status=0, date=None, filename=None, ext=None, label=None, attrs=None, _id=None, last_read=None, last_search=None, read_count=None, search_count=None, cache=None)[source]#

Cache item class, stores a single cache item information.

NOTE: Alternative creation function can be found in the class method new. The __init__ method creates an instance based on the key attribute (generally for internal use), while the new method takes a URI and is more intended for user interface.

Args:
key:

Unique key name for the item. The creation method CacheItem.new provides it automatically as an alphanumeric string (see CacheItem.serialize for details).

version:

Version number of the item. Optional, defaults to 1.

status:

Status of the entry as integer (see _status.Status for more info). Optional, defaults to 1.

date:

Date of the entry, if none is provided, takes the current time. Optional, defaults to None.

filename:

Name of the file associated to the item. Optional, defaults to None.

ext:

Extension of the file associated to the item. Optional, defaults to None (automatically extracted from the file name).

label:

Label for the item (e.g. type, group, category…). Optional, defaults to None

attrs:

Extra attributes associated to the item. Keys are the attribute names and values their content. These attributes will be stored in the attribute tables according to their data type automatically. Optional, defaults to None.

_id:

Internal ID number. Optional, defaults to None.

last_read:

Date of last reading of the entry. Optional, defaults to None.

last_search:

Date of last search for the item. Optional, defaults to None.

read_count:

Counter of reads for the item. Optional, defaults to None.

search_count:

Counter for the times the item has been searched. Optional, defaults to None.

cache:

The Cache instance where the item belongs. Optional, defaults to None.

Attrs:
key:

Unique key name for the item.

version:

Current version number of the item.

date:

Creation date of the entry.

filename:

Name of the file associated to the item.

ext:

Extension of the file associated to the item.

label:

Item label (e.g. type, group, category…).

attrs:

Extra attributes associated to the item. Keys are the attribute names and values their content.

last_read:

Date of last reading of the entry.

last_search:

Date of last search for the item.

read_count:

Counter of reads for the item.

search_count:

Counter for the times the item has been searched.

cache:

The Cache instance where the item belongs.

cache_fname:

The file name of the associated cache database.

path:

Path to the file associated to the item.

rstatus:

Current status of the item instance.

status:

Status of the item as registered in the Cache.

uri:

Uniform Resource Identifier.

version_id:

Identifier as a combination of item key and version number.

failed()[source]#

Sets the status to failed.

classmethod new(uri=None, params=None, version=0, status=0, date=None, filename=None, ext=None, label=None, attrs=None, last_read=None, last_search=None, read_count=0, search_count=0, cache=None)[source]#

Creates a new item.

Return type

CacheItem

Args:
uri:

Uniform Resource Identifier. Optional, defaults to None.

params:

Collection of parameters passed to the __init__ method in dict format where key-value pairs correspond to parameter-value respectively. Optional, defaults to None.

version:

The version nuber of the new item. Optional, defaults to 0.

status:

Status of the new item. Optional, defaults to 0.

date:

Date of the entry, if none is provided, takes the current time. Optional, defaults to None.

filename:

Name of the file associated to the item. Optional, defaults to None.

ext:

Extension of the file associated to the item. Optional, defaults to None (automatically extracted from the file name).

label:

Label for the item (e.g. type, group, category…). Optional, defaults to None

attrs:

Extra attributes associated to the item. Keys are the attribute names and values their content. These attributes will be stored in the attribute tables according to their data type automatically. Optional, defaults to None.

last_read:

Date of last reading of the entry. Optional, defaults to None.

last_search:

Date of last search for the item. Optional, defaults to None.

read_count:

Counter of reads for the item. Optional, defaults to 0.

search_count:

Counter for the times the item has been searched. Optional, defaults to 0.

cache:

The Cache instance where the item belongs. Optional, defaults to None.

Returns:

Instance of the newly created CacheItem.

open(**kwargs)[source]#

Opens the file in reading mode.

Return type

UnionType[str, IO, dict[str, str | IO], None]

Args:
**kwargs:

Keyword arguments are passed directly to the Opener class.

Returns:

The resulting opened file content. The type of content will depend on the passed arguments. See the Opener documentation for more details.

ready()[source]#

Sets the status to ready.

remove(disk=False, keep_record=True)[source]#

Removes the item from the cache database (if available).

Args:
disk:

Whether to also remove the file from disk. Optional, defaults to False.

keep_record:

Whether to keep the record in the database (i.e. marks the item status as trashed without actually deleting the record). Optional, defaults to True.

classmethod serialize(params=None)[source]#

Serializes the parameters dictionary and hashes the result to generate an identifier. See _utils.hash for more information.

Return type

str

Args:
params:

The parameters dictionary to generate the hash from.

Returns:

The resulting hash from the serailized parameters.

property cache_fname: str#

Gives the file name as stored in the cache.

Returns:

Combination of version_id (key + version) and the file extension.

property path: str#

Gives the path of the file.

Returns:

The path to the cache item file (generally the cache path + the cache_fname).

property rstatus: int#

Gives the current status for the cache item instance.

Returns:

The integer value defining the status.

property status: int#

Gives the current status for the cache item as stored in the cache (if any, otherwise returns the status as stored in the CacheItem).

Returns:

The integer value defining the status.

property uri: str | None#

Provides the Uniform Resource Identifier if available.

Returns:

The Uniform Resource Identifier or None if not defined.

property version_id: str#

Gives the unique version identifier based on the key of the item and the version number.

Returns:

Version identifier as [key]-[version].