put database directory next to HUD_config.xml file

This commit is contained in:
sqlcoder 2010-01-26 21:02:37 +00:00
parent 45397695d3
commit 5c88c6b0ad
2 changed files with 18 additions and 10 deletions

View File

@ -113,10 +113,6 @@ log = get_logger("logging.conf")
APPLICATION_NAME_SHORT = 'fpdb' APPLICATION_NAME_SHORT = 'fpdb'
APPLICATION_VERSION = 'xx.xx.xx' APPLICATION_VERSION = 'xx.xx.xx'
DIR_SELF = os.path.dirname(get_exec_path())
#TODO: imo no good idea to place 'database' in parent dir
DIR_DATABASES = os.path.join(os.path.dirname(DIR_SELF), 'database')
DATABASE_TYPE_POSTGRESQL = 'postgresql' DATABASE_TYPE_POSTGRESQL = 'postgresql'
DATABASE_TYPE_SQLITE = 'sqlite' DATABASE_TYPE_SQLITE = 'sqlite'
DATABASE_TYPE_MYSQL = 'mysql' DATABASE_TYPE_MYSQL = 'mysql'
@ -429,6 +425,8 @@ class Config:
self.doc = doc self.doc = doc
self.file = file self.file = file
self.dir = os.path.dirname(self.file)
self.dir_databases = os.path.join(self.dir, 'database')
self.supported_sites = {} self.supported_sites = {}
self.supported_games = {} self.supported_games = {}
self.supported_databases = {} # databaseName --> Database instance self.supported_databases = {} # databaseName --> Database instance

View File

@ -41,6 +41,10 @@ except ImportError:
import FpdbSQLQueries import FpdbSQLQueries
import Configuration import Configuration
DB_VERSION = 118
# Variance created as sqlite has a bunch of undefined aggregate functions. # Variance created as sqlite has a bunch of undefined aggregate functions.
class VARIANCE: class VARIANCE:
@ -74,6 +78,7 @@ class fpdb_db:
if config is None: if config is None:
raise FpdbError('Configuration not defined') raise FpdbError('Configuration not defined')
self.config = config
self.settings = {} self.settings = {}
self.settings['os'] = "linuxmac" if os.name != "nt" else "windows" self.settings['os'] = "linuxmac" if os.name != "nt" else "windows"
@ -95,6 +100,8 @@ class fpdb_db:
self.user = user self.user = user
self.password = password self.password = password
self.database = database self.database = database
createTables = False
if backend == fpdb_db.MYSQL_INNODB: if backend == fpdb_db.MYSQL_INNODB:
import MySQLdb import MySQLdb
if use_pool: if use_pool:
@ -154,10 +161,12 @@ class fpdb_db:
else: else:
logging.warning("SQLite won't work well without 'sqlalchemy' installed.") logging.warning("SQLite won't work well without 'sqlalchemy' installed.")
if not os.path.isdir(Configuration.DIR_DATABASES) and not database == ":memory:": if not os.path.isdir(self.config.dir_databases) and not database == ":memory:":
print "Creating directory: '%s'" % (Configuration.DIR_DATABASES) print "Creating directory: '%s'" % (self.config.dir_databases)
os.mkdir(Configuration.DIR_DATABASES) logging.info("Creating directory: '%s'" % (self.config.dir_databases))
database = os.path.join(Configuration.DIR_DATABASES, database) os.mkdir(self.config.dir_databases)
database = os.path.join(self.config.dir_databases, database)
createTables = True
self.db = sqlite3.connect(database, detect_types=sqlite3.PARSE_DECLTYPES ) self.db = sqlite3.connect(database, detect_types=sqlite3.PARSE_DECLTYPES )
sqlite3.register_converter("bool", lambda x: bool(int(x))) sqlite3.register_converter("bool", lambda x: bool(int(x)))
sqlite3.register_adapter(bool, lambda x: "1" if x else "0") sqlite3.register_adapter(bool, lambda x: "1" if x else "0")
@ -179,8 +188,9 @@ class fpdb_db:
try: try:
self.cursor.execute("SELECT * FROM Settings") self.cursor.execute("SELECT * FROM Settings")
settings = self.cursor.fetchone() settings = self.cursor.fetchone()
if settings[0] != 118: if settings[0] != DB_VERSION:
print "outdated or too new database version - please recreate tables" logging.error("outdated or too new database version (%s) - please recreate tables"
% (settings[0]))
self.wrongDbVersion = True self.wrongDbVersion = True
except:# _mysql_exceptions.ProgrammingError: except:# _mysql_exceptions.ProgrammingError:
if database != ":memory:": print "failed to read settings table - please recreate tables" if database != ":memory:": print "failed to read settings table - please recreate tables"