# Path: playa/ext/zodb.py
# class ZODB(IterableUserDict):
#     """ZODB extension for Flask: persistence of native Python objects.
# 
#     Basic setup::
# 
#         from ZODB.FileStorage import FileStorage
#         from flask import Flask, redirect, render_template_string
#         from flaskext.zodb import ZODB
# 
#         app = Flask(__name__)
#         app.config.from_object(__name__)
#         db = ZODB(app)
# 
#         @app.route('/')
#         @app.route('/<message>')
#         def index(message=None):
#             if message is not None:
#                 db['message'] = message
#                 return redirect('/')
#             return render_template_string('Latest message: {{ message }}',
#                                           message=db['message'])
# 
#     During requests the ``db`` object acts like a Python ``dict``. Any changes
#     made to it *directly* will persist, changes made to mutable objects
#     within will have to be marked as mutated. This is done for you if you
#     inherit :class:`Model` for your own classes and use :attr:`List` and
#     :attr:`Mapping` as substitutes for Python's ``list`` and ``dict``.
# 
#     Outside of requests, the object can be used as a context manager if
#     called, yielding the root object to be used inside the context.
#     See :meth:`__call__`.
# 
#     """
# 
#     cb = None
# 
#     def __init__(self, app=None):
#         self.app = app
#         if app is not None:
#             self.init_app(app)
# 
#     def init(self, cb):
#         """Register a callback to be called with the root mapping when the
#         database is first loaded. Useful for setting defaults.
# 
#         """
#         self.cb = cb
#         return cb
# 
#     @cached_property
#     def db(self):
#         db = DB(os.path.join(self.app.config['DATA_PATH'], 'playa.zodb'))
#         if self.cb is not None:
#             with self.transaction(db) as root:
#                 self.cb(root)
#         return db
# 
#     @property
#     def connection(self):
#         """Request-local database connection."""
#         return g._zodb_connection
# 
#     @connection.setter
#     def connection(self, new):
#         g._zodb_connection = new
# 
#     @property
#     def root(self):
#         """Root object for the request-local connection."""
#         return self.connection.root()
# 
#     @property
#     def data(self):
#         return self.root
# 
#     def init_app(self, app):
#         self.app = app
#         if not hasattr(app, 'extensions'):
#             app.extensions = {}
#         app.extensions['zodb'] = self
# 
#         @app.before_request
#         def open_db():
#             self.connection = self.db.open()
#             transaction.begin()
# 
#         @app.after_request
#         def close_db(response):
#             transaction.commit()
#             self.connection.close()
#             return response
# 
#     @contextmanager
#     def transaction(self, db=None):
#         if db is None:
#             db = self.db
#         try:
#             connection = db.open()
#             transaction.begin()
#             yield connection.root()
#         finally:
#             transaction.commit()
#             connection.close()
# 
#     def __call__(self):
#         """Transactional context, for database access outside of requests.
# 
#         ::
# 
#             with db() as root:
#                 root['this'] = 'is committed at the end of the context.'
# 
#         """
#         return self.transaction()
# 
# Path: playa/models.py
# class Users(OOBTree):
#     def new(self, username, password):
#         user = User(username=username)
#         user.set_password(password)
#         self[username] = user
#         return user
# 
# class Playlists(OOBTree):
#     def new(self, name, owner=None):
#         id = uuid.uuid4().hex
#         playlist = Playlist(id=id, name=name, owner=owner)
#         self[id] = playlist
#         return playlist


from playa.ext.zodb import ZODB
from playa.models import Users, Playlists
"""
playa.db
~~~~~~~~~~

:copyright: (c) 2011 DISQUS.
:license: Apache License 2.0, see LICENSE for more details.
"""


db = ZODB()

@db.init
def set_defaults(root):
    if 'users' not in root:

# Path: playa/ext/zodb.py
# class ZODB(IterableUserDict):
#     """ZODB extension for Flask: persistence of native Python objects.
# 
#     Basic setup::
# 
#         from ZODB.FileStorage import FileStorage
#         from flask import Flask, redirect, render_template_string
#         from flaskext.zodb import ZODB
# 
#         app = Flask(__name__)
#         app.config.from_object(__name__)
#         db = ZODB(app)
# 
#         @app.route('/')
#         @app.route('/<message>')
#         def index(message=None):
#             if message is not None:
#                 db['message'] = message
#                 return redirect('/')
#             return render_template_string('Latest message: {{ message }}',
#                                           message=db['message'])
# 
#     During requests the ``db`` object acts like a Python ``dict``. Any changes
#     made to it *directly* will persist, changes made to mutable objects
#     within will have to be marked as mutated. This is done for you if you
#     inherit :class:`Model` for your own classes and use :attr:`List` and
#     :attr:`Mapping` as substitutes for Python's ``list`` and ``dict``.
# 
#     Outside of requests, the object can be used as a context manager if
#     called, yielding the root object to be used inside the context.
#     See :meth:`__call__`.
# 
#     """
# 
#     cb = None
# 
#     def __init__(self, app=None):
#         self.app = app
#         if app is not None:
#             self.init_app(app)
# 
#     def init(self, cb):
#         """Register a callback to be called with the root mapping when the
#         database is first loaded. Useful for setting defaults.
# 
#         """
#         self.cb = cb
#         return cb
# 
#     @cached_property
#     def db(self):
#         db = DB(os.path.join(self.app.config['DATA_PATH'], 'playa.zodb'))
#         if self.cb is not None:
#             with self.transaction(db) as root:
#                 self.cb(root)
#         return db
# 
#     @property
#     def connection(self):
#         """Request-local database connection."""
#         return g._zodb_connection
# 
#     @connection.setter
#     def connection(self, new):
#         g._zodb_connection = new
# 
#     @property
#     def root(self):
#         """Root object for the request-local connection."""
#         return self.connection.root()
# 
#     @property
#     def data(self):
#         return self.root
# 
#     def init_app(self, app):
#         self.app = app
#         if not hasattr(app, 'extensions'):
#             app.extensions = {}
#         app.extensions['zodb'] = self
# 
#         @app.before_request
#         def open_db():
#             self.connection = self.db.open()
#             transaction.begin()
# 
#         @app.after_request
#         def close_db(response):
#             transaction.commit()
#             self.connection.close()
#             return response
# 
#     @contextmanager
#     def transaction(self, db=None):
#         if db is None:
#             db = self.db
#         try:
#             connection = db.open()
#             transaction.begin()
#             yield connection.root()
#         finally:
#             transaction.commit()
#             connection.close()
# 
#     def __call__(self):
#         """Transactional context, for database access outside of requests.
# 
#         ::
# 
#             with db() as root:
#                 root['this'] = 'is committed at the end of the context.'
# 
#         """
#         return self.transaction()
# 
# Path: playa/models.py
# class Users(OOBTree):
#     def new(self, username, password):
#         user = User(username=username)
#         user.set_password(password)
#         self[username] = user
#         return user
# 
# class Playlists(OOBTree):
#     def new(self, name, owner=None):
#         id = uuid.uuid4().hex
#         playlist = Playlist(id=id, name=name, owner=owner)
#         self[id] = playlist
#         return playlist


# Path: playa/db.py
from playa.ext.zodb import ZODB
from playa.models import Users, Playlists

"""
playa.db
~~~~~~~~~~

:copyright: (c) 2011 DISQUS.
:license: Apache License 2.0, see LICENSE for more details.
"""


db = ZODB()

@db.init
def set_defaults(root):
    if 'users' not in root:

        root['users'] = Users()