import logbook
import rethinkdb as rdb

from piper.db import core as db


class RethinkManager(object):
    def __init__(self, db):
        self.db = db

        self.log = logbook.Logger(self.__class__.__name__)


class AgentManager(RethinkManager, db.AgentManager):
    def get(self):
        raise NotImplementedError()

    def lock(self, build):
        self.set_lock(build, True)

    def unlock(self, build):
        self.set_lock(build, False)

    def set_lock(self, build, locked):
        raise NotImplementedError()


class BuildManager(RethinkManager, db.BuildManager):
    def add(self, build):
        raise NotImplementedError()

    def update(self, build, **extra):
        raise NotImplementedError()

    def get(self, build_id):
        raise NotImplementedError()

    def all(self):
        raise NotImplementedError()


class ConfigManager(RethinkManager, db.ConfigManager):
    def register(self, build, project=None):
        raise NotImplementedError()


class ProjectManager(RethinkManager, db.ProjectManager):
    def get(self, build):
        raise NotImplementedError()


class VCSManager(RethinkManager, db.VCSManager):
    def get(self, build):
        raise NotImplementedError()


class PropertyManager(RethinkManager, db.PropertyManager):
    def update(self, classes):
        raise NotImplementedError()


class PropertyNamespaceManager(RethinkManager, db.PropertyNamespaceManager):
    def get(self, name):
        raise NotImplementedError()


class RethinkDB(db.Database):
    def setup(self, config):
        """
        Used for setting up a session when starting piper

        """

        self._config = config
        # TODO: Error handling if connection refused.
        self.conn = rdb.connect(
            host=config.raw['db']['host'],
            port=config.raw['db']['port'],
            db=config.raw['db']['db'],
            auth_key=config.raw['db'].get('auth_key', ''),
        )

        self.setup_managers()

    def init(self, config):
        """
        Used for initial creation of database when none exists.
        Used by `piperd db init`

        """

        raise NotImplementedError()

    def setup_managers(self):
        self.build = BuildManager(self)
        self.config = ConfigManager(self)
        self.project = ProjectManager(self)
        self.vcs = VCSManager(self)
        self.property = PropertyManager(self)
        self.property_namespace = PropertyNamespaceManager(self)

@@ -145,16 +145,68 @@
 .db = db
+%0A        self.table = db.conn.table(self.table_name)
 %0A%0A      
@@ -305,32 +305,58 @@
 .AgentManager):%0A
+    table_name = 'agent'%0A%0A
     def get(self
@@ -646,32 +646,58 @@
 .BuildManager):%0A
+    table_name = 'build'%0A%0A
     def add(self
@@ -718,35 +718,12 @@
     
-raise NotImplementedError()
+pass
 %0A%0A  
@@ -965,32 +965,59 @@
 ConfigManager):%0A
+    table_name = 'config'%0A%0A
     def register
@@ -1057,35 +1057,12 @@
     
-raise NotImplementedError()
+pass
 %0A%0A%0Ac
@@ -1109,32 +1109,60 @@
 rojectManager):%0A
+    table_name = 'project'%0A%0A
     def get(self
@@ -1250,32 +1250,56 @@
 db.VCSManager):%0A
+    table_name = 'vcs'%0A%0A
     def get(self
@@ -1397,32 +1397,61 @@
 opertyManager):%0A
+    table_name = 'property'%0A%0A
     def update(s
@@ -1576,24 +1576,63 @@
 ceManager):%0A
+    table_name = 'property_namespace'%0A%0A
     def get(
@@ -1708,24 +1708,218 @@
 .Database):%0A
+    managers = (%0A        AgentManager,%0A        BuildManager,%0A        ConfigManager,%0A        ProjectManager,%0A        VCSManager,%0A        PropertyManager,%0A        PropertyNamespaceManager,%0A    )%0A%0A
     def setu
@@ -2592,269 +2592,264 @@
     
-self.build = BuildManager(self)%0A        self.config = ConfigManager(self)%0A        self.project = ProjectManager(self)%0A        self.vcs = VCSManager(self)%0A        self.property = PropertyManager(self)%0A        self.property_namespace = PropertyNamespaceManager(self
+%22%22%22%0A        Create instances of all manager classes, setting them as attributes%0A        with the same name as their table names.%0A%0A        %22%22%22%0A%0A        for manager in self.managers:%0A            man = manager(self)%0A            setattr(self, man.table_name, man
 )%0A

Add missing manager