from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from config import Config
from database_mbase import MyBase
import os
f=file('config.cfg')
cfg=Config(f)


if cfg.get("db_main_type") == "mysql":
    engine = create_engine("mysql+pymysql://%s:%s@localhost/crawler_articles?charset=utf8" % (cfg.get("db_main_user"), cfg.get("db_main_pw"))) 
else:
    engine = create_engine('sqlite:///'+ os.path.join(cfg.db_path,cfg.db_mainfile), convert_unicode=True)


db_session = scoped_session(sessionmaker(autocommit=False,#  autoflush=False,
                                         bind=engine))

Base=declarative_base(cls=MyBase)                                         
def init_db():
    import models
    Base.metadata.create_all(bind=engine)
