Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
Jason Fried edited this page Aug 20, 2019 · 4 revisions

pyaib comes with a SQLite db driver. The db component is designed to be dead simple to use, never throw exceptions, just work. It is an IRC bot after all not a bank. If the key doesn't exist return an Empty Item, if the bucket doesn't exist make one. If you try to delete a non existent key, do nothing. Simple

Any python object that can pass through json.dumps can be stored in the db.

Custom db Driver

Just decorate your db driver class, and implement all the methods mentioned in the doc at the top of pyaib.db from pyaib.db import db_driver

Some caveats:

  1. You have to specify the module path to your driver in the config item db.backend
  2. Its expected that if the module path above does not contain a . it refers to one of the drivers shipped with pyaib in pyaib.dbd.<name>
  3. The config for your db_driver is located in the config under db.driver.{module_name} where module_name is the name of the module containing your db_driver sans package path. So plugins.mydb would turn into db.driver.mydb in the config.

Get a db for your plugin

@plugin_class
@plugin_class.requires('db')
class SomePlugin(object):
    def __init__(self, context, config):
        self.db = context.db.get('SomePlugin')
        item = self.db.get('allowed_channels')
        item.value = ['#hacks', '#pyaib']
        item.commit()

Using the DB

context.db has the following methods

  • get(bucket, key=None) : Get a Bucket or Item object if key is specified
  • getAll(bucket) : Iterator for all items in a bucket
  • set(bucket, key, obj) : Store the obj given in the bucket under key
  • delete(bucket, key) : Delete the key from a bucket

Bucket Objects

A bucket is just a group of keys, its a namespace. And has the following methods

  • get(key) : Get an Item , will be a empty Item if it doesn't exists
  • getAll() : Iterator for all Items in the bucket.
  • set(key, obj) : Store and Object
  • delete(key) : delete by key

Item Objects

And Item represents a key, value in the Bucket.

Attributes:

  • item.bucket = Name of the bucket this item belongs too
  • item.key = The key for this item
  • item.value = The value of the item defaults to None

Methods:

  • reload() : Reload the key, value, bucket from the DB. Pretty much erases any un-committed changes.
  • delete() : Delete this Item from the DB
  • commit() : Save and changes to the Item attributes to the DB, if any.
Clone this wiki locally