This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
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.
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:
- You have to specify the module path to your driver in the config item
db.backend
- Its expected that if the module path above does not contain a
.
it refers to one of the drivers shipped with pyaib inpyaib.dbd.<name>
- The config for your db_driver is located in the config under
db.driver.{module_name}
wheremodule_name
is the name of the module containing your db_driver sans package path. Soplugins.mydb
would turn intodb.driver.mydb
in the config.
@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()
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
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
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.