# Path: src/petmail/database.py
# class DBError(Exception):
# class ObservableDatabase:
# def get_schema(version):
# def serialize(row):
#     def __init__(self, connection):
#     def subscribe(self, table, observer):
#     def unsubscribe(self, table, observer):
#     def execute(self, sql, values=None):
#     def insert(self, sql, values, table=None, tags={}):
#     def update(self, sql, values, table=None, id=None, tags={}):
#     def delete(self, sql, values, table, id, tags={}):
#     def commit(self):
# def get_db(dbfile, stderr=sys.stderr):
# def make_observable_db(dbfile, stderr=sys.stderr):
#     VERSION = 1
# 
# Path: src/petmail/util.py
# def equal(a, b):
#     # not vulnerable to timing attack
#     return hashlib.sha256(a).digest() == hashlib.sha256(b).digest()
# 
# def make_nonce():
#     return base64.b32encode(os.urandom(32)).strip("=").lower()
# 
# Path: src/petmail/errors.py
# class CommandError(Exception):
#     def __init__(self, msg):
#         self.msg = msg


import json, collections
from pkg_resources import resource_string, resource_filename
from twisted.application import service, strports
from twisted.web import server, static, resource, http
from .database import Notice
from .util import equal, make_nonce
from .errors import CommandError
        new_value = self.some_keys(notice.new_value,
                                   ["id", "cid", "mailbox_record_json"])
        self.deliver_event(notice, new_value, "mailboxes")

    def deliver_local_mailbox_event(self, adv_local, local_url):
        data = json.dumps({ "type": "advertise_local_mailbox",
                            "adv_local": adv_local,
                            "local_url": local_url })
        self.events_protocol.sendEvent(data)

    def deliver_event(self, notice, new_value, event_type):
        data = json.dumps({ "type": event_type,
                            "action": notice.action,
                            "id": notice.id,
                            "new_value": new_value,
                            "tags": notice.tags,
                            })
        self.events_protocol.sendEvent(data)

class EventChannelDispatcher(resource.Resource):
    def __init__(self, db, agent):
        resource.Resource.__init__(self)
        self.db = db
        self.agent = agent
        self.event_channels = {}
        self.unclaimed_event_channels = set()
        # We keep unclaimed EventChannels alive until a timeout. We keep
        # claimed EventChannels alive until they tell us they're done.

    def add_event_channel(self):

# Path: src/petmail/database.py
# class DBError(Exception):
# class ObservableDatabase:
# def get_schema(version):
# def serialize(row):
#     def __init__(self, connection):
#     def subscribe(self, table, observer):
#     def unsubscribe(self, table, observer):
#     def execute(self, sql, values=None):
#     def insert(self, sql, values, table=None, tags={}):
#     def update(self, sql, values, table=None, id=None, tags={}):
#     def delete(self, sql, values, table, id, tags={}):
#     def commit(self):
# def get_db(dbfile, stderr=sys.stderr):
# def make_observable_db(dbfile, stderr=sys.stderr):
#     VERSION = 1
# 
# Path: src/petmail/util.py
# def equal(a, b):
#     # not vulnerable to timing attack
#     return hashlib.sha256(a).digest() == hashlib.sha256(b).digest()
# 
# def make_nonce():
#     return base64.b32encode(os.urandom(32)).strip("=").lower()
# 
# Path: src/petmail/errors.py
# class CommandError(Exception):
#     def __init__(self, msg):
#         self.msg = msg


# Path: src/petmail/web.py
import json, collections
from pkg_resources import resource_string, resource_filename
from twisted.application import service, strports
from twisted.web import server, static, resource, http
from .database import Notice
from .util import equal, make_nonce
from .errors import CommandError

        new_value = self.some_keys(notice.new_value,
                                   ["id", "cid", "mailbox_record_json"])
        self.deliver_event(notice, new_value, "mailboxes")

    def deliver_local_mailbox_event(self, adv_local, local_url):
        data = json.dumps({ "type": "advertise_local_mailbox",
                            "adv_local": adv_local,
                            "local_url": local_url })
        self.events_protocol.sendEvent(data)

    def deliver_event(self, notice, new_value, event_type):
        data = json.dumps({ "type": event_type,
                            "action": notice.action,
                            "id": notice.id,
                            "new_value": new_value,
                            "tags": notice.tags,
                            })
        self.events_protocol.sendEvent(data)

class EventChannelDispatcher(resource.Resource):
    def __init__(self, db, agent):
        resource.Resource.__init__(self)
        self.db = db
        self.agent = agent
        self.event_channels = {}
        self.unclaimed_event_channels = set()
        # We keep unclaimed EventChannels alive until a timeout. We keep
        # claimed EventChannels alive until they tell us they're done.

    def add_event_channel(self):

        esid = make_nonce()