ws.cache package

class ws.cache.CacheDb(api, cache_dir, dbname, autocommit=True)

Bases: object

Base class for caching databases. The database is saved on disk in the gzipped JSON format. The data is represented by the data structure, whose type depends on the implementation in each subclass (generally a list or dict). There is also a meta structure holding the meta data such as timestamp of the last update.

The data elements can be accessed using the subscript syntax db["key"], which triggers a lazy update/initialization of the database. The meta data can be accessed as attributes (e.g. db.attribute), which does not trigger an update.

Parameters:
  • api (ws.client.api.API) – an instance of the API to work with
  • dbname (str) – a name of the database, usually the name of the subclass
  • autocommit (bool) – whether to automatically call dump() after each update of the database
meta = {}
data = None
ts_format = '%Y-%m-%dT%H:%M:%S.%f'

format for JSON (de)serialization of datetime.datetime timestamps

autocommit = None

period for automatic database commits

load(key=None)

Try to load data from disk. When data on disk does not exist yet, calls init() to initialize the database and dump() immediately after to save the initial state to disk.

Called automatically from __init__(), it is not necessary to call it manually.

Parameters:key – passed to init(), necessary for proper lazy initialization in case of multi-key database
dump()

Save data to disk. When autocommit is True, it is called automatically from init() and update().

After manual modification of the self.data structure it is necessary to call it manually if the change is to be persistent.

init(key=None)

Called by load() when data does not exist on disk yet. Responsible for:

  • initializing data structure and performing the initial API query,
  • calling dump() after the query depending on the value of autocommit,
  • updating self.meta["timestamp"], either manually or via _update_timestamp().

Has to be defined in subclasses.

Parameters:key – database key determining which part of the database should be initialized. Can be ignored in case of single-key databases.
update(key=None)

Called from accessors like __getitem__() a cache update. Responsible for:

  • performing an API query to update the cached data,
  • calling dump() after the query depending on the value of autocommit,
  • updating self.meta["timestamp"], either manually or via _update_timestamp().

Has to be defined in subclasses.

Parameters:key – database key determining which part of the database should be initialized. Can be ignored in case of single-key databases.
_update_timestamp()
_load_and_update(key=None)

Helper method called from the accessors.

__getitem__(key)
__iter__()
__reversed__()
__contains__(item)
__len__()
__getattr__(name)

Access a meta data element.

Returns:self.meta[name] if available, otherwise None
exception ws.cache.CacheDbError

Bases: Exception

Raised on database errors, e.g. when loading from disk failed.

class ws.cache.AllRevisionsProps(api, cache_dir, autocommit=True)

Bases: ws.cache.CacheDb

init(key=None)
Parameters:key – ignored
update(key=None)
Parameters:key – ignored
_get_last_revid_db()

Get ID of the last revision stored in the cache database.

_fetch_revisions(first, last)

Fetch properties of revisions in given numeric range and save the data to the database.

Parameters:
  • first – (int) revision ID to start fetching from
  • last – (int) revision ID to end fetching
class ws.cache.AllUsersProps(api, cache_dir, autocommit=True, active_days=30, round_to_midnight=False)

Bases: ws.cache.CacheDb

Parameters:
  • active_days – the time span in days to consider users as active
  • round_to_midnight – Whether to round timestamps to midnight when fetching recent changes. This affects the "recenteditcount" property, but not the total "editcount", which reflects the state as of the last update of the cache.
init(key=None)
Parameters:key – ignored
update(key=None)
Parameters:key – ignored
_find_changed_users()

Find users whose properties may have changed since the last update. Changes to edit counts are not taken into account.

Returns:list of user names
_find_active_users()

Find the users who were active in the last active_days days and count their recent edits.

Returns:a mapping of user names to their recenteditcount
_update_recent_edit_counts(rcusers)