示例#1
0
class DataManager():
    def __init__(self, config):
        global mysql_active
        self.config = config
        self.init()

    def stop(self):
        if mysql_active:
            logging.debug("Database: MySQL: Stopped")
        else:
            logging.debug("Database: SQLite: Stopped")

    def init(self):
        global mysql_active

        if mysql_active:
            try:
                logging.debug("Database: MySQL: Started")
                self.engine = create_engine(
                    "mysql://" + self.config['database.mysql']['username'] +
                    ":" +
                    utils.decode(self.config['general']['install_id'],
                                 self.config['database.mysql']['password']) +
                    "@" + self.config['database.mysql']['host'] + ":" +
                    str(self.config['database.mysql']['port']) + "/" +
                    self.config['database.mysql']['database'] +
                    "?charset=utf8",
                    pool_recycle=3600,
                    echo=False)
                logging.info("Database: MySQL: " +
                             self.config['database.mysql']['database'] + " (" +
                             self.config['database.mysql']['host'] + ":" +
                             str(self.config['database.mysql']['port']) + ")")
            except Exception, e:
                mysql_active = False
                logging.error("Database: MySQL: Failed (" +
                              self.config['database.mysql']['database'] +
                              " [" + self.config['database.mysql']['host'] +
                              ":" +
                              str(self.config['database.mysql']['port']) +
                              "])")
                logging.error("Database: MySQL: Failed (" + str(e) + ")")
                logging.warning("Database: Switching to SQLite Engine")

        if not mysql_active:
            logging.debug("Database: SQLite: Started")

            db = self.config['database.sqlite']['database']
            if db == "": db = "comicstreamer"
            db += u".sqlite"

            self.dbfile = self.config['database.sqlite']['location']

            if self.dbfile == "":
                self.dbfile = os.path.join(AppFolders.appData(), db)
            else:
                if os.path.isdir(self.dbfile):
                    self.dbfile = os.path.join(self.dbfile, db)
                else:
                    logging.error(
                        "Database: SQLite: Database Location Unavailable (" +
                        self.dbfile + ")")
                    logging.warning(
                        "Database: Switching to SQLite Engine Default Database Location"
                    )
                    self.dbfile = os.path.join(AppFolders.appData(), db)

            try:
                self.engine = create_engine(u'sqlite:///' + self.dbfile,
                                            echo=False)
                logging.info("Database: SQLite: (" + self.dbfile + ")")
            except:
                logging.error("Database: SQLite Failed (" + self.dbfile + ")")
                logging.warning(
                    "Database: Switching to SQLite Engine Default Database Location"
                )
                self.dbfile = os.path.join(AppFolders.appData(),
                                           "comicstreamer.sqlite")
                try:
                    self.engine = create_engine('sqlite:///' + self.dbfile,
                                                echo=False)
                    logging.info("Database: SQLite: (" + self.dbfile + ")")
                except:
                    logging.error("Database: SQLite: Failed (" + self.dbfile +
                                  ")")

        if mysql_active:
            session_factory = sessionmaker(
                bind=self.engine,
                expire_on_commit=True,
                autoflush=True,
                autocommit=False
            )  #, autoflush=False, autocommit=True, expire_on_commit=True) #,autocommit=True)
        else:
            session_factory = sessionmaker(
                bind=self.engine, expire_on_commit=True
            )  #, autocommit=True) #, autoflush=False, autocommit=True, expire_on_commit=True) #,autocommit=True)
        self.Session = scoped_session(session_factory)