irc3.plugins.storage Storage plugin

Add a db attribute to the bot

Usage:

>>> config = ini2config("""
... [bot]
... includes =
...     irc3.plugins.storage
... storage = json://%s
... """ % json_file)
>>> bot = IrcBot(**config)

Then use it:

>>> bot.db['mykey'] = dict(key='value')
>>> 'mykey' in bot.db
True
>>> bot.db['mykey']
{'key': 'value'}
>>> bot.db.setdefault('mykey', key='default')
{'key': 'value'}
>>> bot.db.setdefault('mykey', item='default')
{'item': 'default'}
>>> bot.db.set('mykey', item='value')
>>> bot.db.setdefault('mykey', item='default')
{'item': 'value'}
>>> del bot.db['mykey']
>>> bot.db.get('mykey')
>>> bot.db.get('mykey', 'default')
'default'
>>> bot.db['mykey']
Traceback (most recent call last):
  ...
KeyError: 'mykey'
>>> 'mykey' in bot.db
False
>>> bot.db.setlist('mylist', ['foo', 'bar'])
>>> bot.db.getlist('mylist')
['foo', 'bar']
>>> del bot.db['mylist']

You can use an instance as key:

>>> class MyPlugin:
...     pass
>>> plugin = MyPlugin()
>>> bot.db[plugin] = dict(key='value')
>>> bot.db[plugin]
{'key': 'value'}
>>> del bot.db[plugin]
>>> bot.db.get(plugin)

You can also use shelve:

>>> config = ini2config("""
... [bot]
... includes =
...     irc3.plugins.storage
... storage = shelve://%s
... """ % db_file)
>>> bot = IrcBot(**config)
>>> bot.db['mykey'] = dict(key='value')
>>> bot.db['mykey']
{'key': 'value'}
>>> del bot.db['mykey']
>>> bot.db.get('mykey')
>>> bot.db.setlist('mylist', ['foo', 'bar'])
>>> bot.db.getlist('mylist')
['foo', 'bar']
>>> del bot.db['mylist']

Or redis:

>>> config = ini2config("""
... [bot]
... includes =
...     irc3.plugins.storage
... storage = redis://localhost:6379/10
... """)
>>> bot = IrcBot(**config)

Then use it:

>>> bot.db['mykey'] = dict(key='value')
>>> bot.db['mykey']
{'key': 'value'}
>>> del bot.db['mykey']
>>> bot.db.get('mykey')
>>> bot.db['mykey']
Traceback (most recent call last):
  ...
KeyError: 'mykey'
>>> bot.db.setlist('mylist', ['foo', 'bar'])
>>> bot.db.getlist('mylist')
['foo', 'bar']
>>> del bot.db['mylist']

Api

class irc3.plugins.storage.Storage(context)[source]
__contains__(key)[source]

Return True if storage contains key

__delitem__(key)[source]

Delete key in storage

__getitem__(key)[source]

Get storage value for key

__setitem__(key, value)[source]

Set storage value for key

get(key_, default=None)[source]

Get storage value for key or return default

set(key_, **kwargs)[source]

Update storage value for key with kwargs

setdefault(key_, **kwargs)[source]

Update storage value for key with kwargs iif the keys doesn’t exist. Return stored values