local_db.py 1.83 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base, AbstractConcreteBase
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine

from transform_frame import TransformationRules

from db_address import DB_ADDRESS

Base = declarative_base()

VERYSHORT = 50
SHORT = 250
LONG = 1250

class Subtree(Base):
    __tablename__ = 'descendant'
    id = Column(Integer, primary_key=True)
    ancestor = Column(Integer, nullable=False)
    descendant = Column(Integer, nullable = False)

class AbstractTransformationTable(AbstractConcreteBase, Base):
    __phantom__ = None

def make_table(name):
    class TransformationTable(AbstractTransformationTable):
        __tablename__ = name
        __mapper_args__ = { 'polymorphic_identity': name, 'concrete':True}

        id = Column(Integer, primary_key=True)
        frame1 = Column(Integer, index=True)
        signature1 = Column(String(SHORT))
        frame2 = Column(Integer)
        signature2 = Column(String(SHORT))
        similarity = Column(Float, index=True)

        def __init__(self, frame1, signature1, frame2, signature2, similarity):
            self.frame1 = frame1
            self.signature1 = signature1
            self.frame2 = frame2
            self.signature2 = signature2
            self.similarity = similarity

    return TransformationTable

def get_db_data(): # session, dictionary of TransformationTable
    rules = TransformationRules.get_rules()
    TT_dict = {str(rule): make_table('similarity_{}'.format(rule.sign())) for rule in rules}
    engine = create_engine(DB_ADDRESS)
    DBSession = sessionmaker(bind=engine)
    session = DBSession()
    return (session, TT_dict)

if __name__ == '__main__':
    # creating empty database disabled -- db_import instead
    pass