# Path: BAC0/core/io/IOExceptions.py
# class RemovedPointException(Exception):
#     """
#     When defining a device from DB it may not be identical to the
#     actual device.
#     """
# 
#     pass
# 
# class NoResponseFromController(Exception):
#     """
#     This exception is used when trying to read or write and there is not answer.
#     """
# 
#     pass


import pickle
import os.path
import sqlite3
import contextlib
    import pandas as pd
    from pandas.io import sql
    from pandas.core.base import DataError
        from pandas import Timestamp
        from pandas.lib import Timestamp
from ..core.io.IOExceptions import RemovedPointException, NoResponseFromController

    def points_from_sql(self, db_name):
        """
        Retrieve point list from SQL database
        """
        points = self._read_from_sql("SELECT * FROM history;", db_name)
        return list(points.columns.values)[1:]

    def his_from_sql(self, db_name, point):
        """
        Retrive point histories from SQL database
        """
        his = self._read_from_sql('select * from "{}"'.format("history", db_name))
        his.index = his["index"].apply(Timestamp)
        return his.set_index("index")[point]

    def value_from_sql(self, db_name, point):
        """
        Take last known value as the value
        """
        return self.his_from_sql(db_name, point).last_valid_index()

    def read_point_prop(self, device_name, point):
        """
        Points properties retrieved from pickle
        """
        with open("{}.bin".format(device_name), "rb") as file:
            try:
                _point = pickle.load(file)["points"][point]
            except KeyError:

# Path: BAC0/core/io/IOExceptions.py
# class RemovedPointException(Exception):
#     """
#     When defining a device from DB it may not be identical to the
#     actual device.
#     """
# 
#     pass
# 
# class NoResponseFromController(Exception):
#     """
#     This exception is used when trying to read or write and there is not answer.
#     """
# 
#     pass


# Path: BAC0/db/sql.py
import pickle
import os.path
import sqlite3
import contextlib
    import pandas as pd
    from pandas.io import sql
    from pandas.core.base import DataError
        from pandas import Timestamp
        from pandas.lib import Timestamp
from ..core.io.IOExceptions import RemovedPointException, NoResponseFromController


    def points_from_sql(self, db_name):
        """
        Retrieve point list from SQL database
        """
        points = self._read_from_sql("SELECT * FROM history;", db_name)
        return list(points.columns.values)[1:]

    def his_from_sql(self, db_name, point):
        """
        Retrive point histories from SQL database
        """
        his = self._read_from_sql('select * from "{}"'.format("history", db_name))
        his.index = his["index"].apply(Timestamp)
        return his.set_index("index")[point]

    def value_from_sql(self, db_name, point):
        """
        Take last known value as the value
        """
        return self.his_from_sql(db_name, point).last_valid_index()

    def read_point_prop(self, device_name, point):
        """
        Points properties retrieved from pickle
        """
        with open("{}.bin".format(device_name), "rb") as file:
            try:
                _point = pickle.load(file)["points"][point]
            except KeyError:

                raise RemovedPointException(