# Path: code/api/env.py
# POSTGRES_HOST = os.environ.get("POSTGRES_HOST", None)
# 
# POSTGRES_USER = os.environ.get("POSTGRES_USER", None)
# 
# POSTGRES_PASS = os.environ.get("POSTGRES_PASS", None)
# 
# POSTGRES_DB = os.environ.get("POSTGRES_DB", None)


from datetime import datetime
from pony import orm
from node import messages
from node.enums import UNSET, INIT, PROPOSE, PREVOTE, VOTE, ACKNOWLEDGE
from .env import POSTGRES_HOST, POSTGRES_USER, POSTGRES_PASS, POSTGRES_DB
import logging
        return messages.Acknowledge(sequence_no=self.sequence_no, node=self.node, sender=self.sender, raw=self.raw)
    # end def
# end class


MSG_TYPE_CLASS_MAP = {
    INIT: DBInitMessage,
    PROPOSE: DBProposeMessage,
    PREVOTE: DBPrevoteMessage,
    VOTE: DBVoteMessage,
    # ...
    ACKNOWLEDGE: DBAcknowledge,
}


@orm.db_session
def to_db(msg):
    if msg is None:
        return None
    if isinstance(msg, dict):
        # is still dict (json)
        msg = messages.Message.from_dict(msg)  # make a Message subclass first.
    assert isinstance(msg, messages.Message)
    db_msg_clazz = MSG_TYPE_CLASS_MAP[msg.type]  # Key error = not implemented yet.
    assert issubclass(db_msg_clazz, DBMessage)
    db_msg = db_msg_clazz.to_db(msg)
    assert db_msg.type == msg.type
    return db_msg
# end def


# Path: code/api/env.py
# POSTGRES_HOST = os.environ.get("POSTGRES_HOST", None)
# 
# POSTGRES_USER = os.environ.get("POSTGRES_USER", None)
# 
# POSTGRES_PASS = os.environ.get("POSTGRES_PASS", None)
# 
# POSTGRES_DB = os.environ.get("POSTGRES_DB", None)


# Path: code/api/database.py
from datetime import datetime
from pony import orm
from node import messages
from node.enums import UNSET, INIT, PROPOSE, PREVOTE, VOTE, ACKNOWLEDGE
from .env import POSTGRES_HOST, POSTGRES_USER, POSTGRES_PASS, POSTGRES_DB
import logging

        return messages.Acknowledge(sequence_no=self.sequence_no, node=self.node, sender=self.sender, raw=self.raw)
    # end def
# end class


MSG_TYPE_CLASS_MAP = {
    INIT: DBInitMessage,
    PROPOSE: DBProposeMessage,
    PREVOTE: DBPrevoteMessage,
    VOTE: DBVoteMessage,
    # ...
    ACKNOWLEDGE: DBAcknowledge,
}


@orm.db_session
def to_db(msg):
    if msg is None:
        return None
    if isinstance(msg, dict):
        # is still dict (json)
        msg = messages.Message.from_dict(msg)  # make a Message subclass first.
    assert isinstance(msg, messages.Message)
    db_msg_clazz = MSG_TYPE_CLASS_MAP[msg.type]  # Key error = not implemented yet.
    assert issubclass(db_msg_clazz, DBMessage)
    db_msg = db_msg_clazz.to_db(msg)
    assert db_msg.type == msg.type
    return db_msg
# end def


db.bind("postgres", host=POSTGRES_HOST, user=POSTGRES_USER, password=POSTGRES_PASS, database=POSTGRES_DB)