mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-20 13:23:18 -07:00
Fix quotes - standardize to single-quoted strings
This commit is contained in:
parent
85b4e22046
commit
c5343889fb
30 changed files with 1257 additions and 1257 deletions
100
core/main_db.py
100
core/main_db.py
|
@ -12,7 +12,7 @@ import core
|
|||
from core import logger
|
||||
|
||||
|
||||
def db_filename(filename="nzbtomedia.db", suffix=None):
|
||||
def db_filename(filename='nzbtomedia.db', suffix=None):
|
||||
"""
|
||||
@param filename: The sqlite database filename to use. If not specified,
|
||||
will be made to be nzbtomedia.db
|
||||
|
@ -21,16 +21,16 @@ def db_filename(filename="nzbtomedia.db", suffix=None):
|
|||
@return: the correct location of the database file.
|
||||
"""
|
||||
if suffix:
|
||||
filename = "{0}.{1}".format(filename, suffix)
|
||||
filename = '{0}.{1}'.format(filename, suffix)
|
||||
return core.os.path.join(core.APP_ROOT, filename)
|
||||
|
||||
|
||||
class DBConnection(object):
|
||||
def __init__(self, filename="nzbtomedia.db", suffix=None, row_type=None):
|
||||
def __init__(self, filename='nzbtomedia.db', suffix=None, row_type=None):
|
||||
|
||||
self.filename = filename
|
||||
self.connection = sqlite3.connect(db_filename(filename), 20)
|
||||
if row_type == "dict":
|
||||
if row_type == 'dict':
|
||||
self.connection.row_factory = self._dict_factory
|
||||
else:
|
||||
self.connection.row_factory = sqlite3.Row
|
||||
|
@ -38,13 +38,13 @@ class DBConnection(object):
|
|||
def check_db_version(self):
|
||||
result = None
|
||||
try:
|
||||
result = self.select("SELECT db_version FROM db_version")
|
||||
result = self.select('SELECT db_version FROM db_version')
|
||||
except sqlite3.OperationalError as e:
|
||||
if "no such table: db_version" in e.args[0]:
|
||||
if 'no such table: db_version' in e.args[0]:
|
||||
return 0
|
||||
|
||||
if result:
|
||||
return int(result[0]["db_version"])
|
||||
return int(result[0]['db_version'])
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
@ -58,12 +58,12 @@ class DBConnection(object):
|
|||
while attempt < 5:
|
||||
try:
|
||||
if args is None:
|
||||
logger.log("{name}: {query}".format(name=self.filename, query=query), logger.DB)
|
||||
logger.log('{name}: {query}'.format(name=self.filename, query=query), logger.DB)
|
||||
cursor = self.connection.cursor()
|
||||
cursor.execute(query)
|
||||
sql_result = cursor.fetchone()[0]
|
||||
else:
|
||||
logger.log("{name}: {query} with args {args}".format
|
||||
logger.log('{name}: {query} with args {args}'.format
|
||||
(name=self.filename, query=query, args=args), logger.DB)
|
||||
cursor = self.connection.cursor()
|
||||
cursor.execute(query, args)
|
||||
|
@ -72,15 +72,15 @@ class DBConnection(object):
|
|||
# get out of the connection attempt loop since we were successful
|
||||
break
|
||||
except sqlite3.OperationalError as error:
|
||||
if "unable to open database file" in error.args[0] or "database is locked" in error.args[0]:
|
||||
logger.log(u"DB error: {msg}".format(msg=error), logger.WARNING)
|
||||
if 'unable to open database file' in error.args[0] or 'database is locked' in error.args[0]:
|
||||
logger.log(u'DB error: {msg}'.format(msg=error), logger.WARNING)
|
||||
attempt += 1
|
||||
time.sleep(1)
|
||||
else:
|
||||
logger.log(u"DB error: {msg}".format(msg=error), logger.ERROR)
|
||||
logger.log(u'DB error: {msg}'.format(msg=error), logger.ERROR)
|
||||
raise
|
||||
except sqlite3.DatabaseError as error:
|
||||
logger.log(u"Fatal error executing query: {msg}".format(msg=error), logger.ERROR)
|
||||
logger.log(u'Fatal error executing query: {msg}'.format(msg=error), logger.ERROR)
|
||||
raise
|
||||
|
||||
return sql_result
|
||||
|
@ -101,26 +101,26 @@ class DBConnection(object):
|
|||
sql_result.append(self.connection.execute(qu[0]))
|
||||
elif len(qu) > 1:
|
||||
if log_transaction:
|
||||
logger.log(u"{query} with args {args}".format(query=qu[0], args=qu[1]), logger.DEBUG)
|
||||
logger.log(u'{query} with args {args}'.format(query=qu[0], args=qu[1]), logger.DEBUG)
|
||||
sql_result.append(self.connection.execute(qu[0], qu[1]))
|
||||
self.connection.commit()
|
||||
logger.log(u"Transaction with {x} query's executed".format(x=len(querylist)), logger.DEBUG)
|
||||
logger.log(u'Transaction with {x} query\'s executed'.format(x=len(querylist)), logger.DEBUG)
|
||||
return sql_result
|
||||
except sqlite3.OperationalError as error:
|
||||
sql_result = []
|
||||
if self.connection:
|
||||
self.connection.rollback()
|
||||
if "unable to open database file" in error.args[0] or "database is locked" in error.args[0]:
|
||||
logger.log(u"DB error: {msg}".format(msg=error), logger.WARNING)
|
||||
if 'unable to open database file' in error.args[0] or 'database is locked' in error.args[0]:
|
||||
logger.log(u'DB error: {msg}'.format(msg=error), logger.WARNING)
|
||||
attempt += 1
|
||||
time.sleep(1)
|
||||
else:
|
||||
logger.log(u"DB error: {msg}".format(msg=error), logger.ERROR)
|
||||
logger.log(u'DB error: {msg}'.format(msg=error), logger.ERROR)
|
||||
raise
|
||||
except sqlite3.DatabaseError as error:
|
||||
if self.connection:
|
||||
self.connection.rollback()
|
||||
logger.log(u"Fatal error executing query: {msg}".format(msg=error), logger.ERROR)
|
||||
logger.log(u'Fatal error executing query: {msg}'.format(msg=error), logger.ERROR)
|
||||
raise
|
||||
|
||||
return sql_result
|
||||
|
@ -135,25 +135,25 @@ class DBConnection(object):
|
|||
while attempt < 5:
|
||||
try:
|
||||
if args is None:
|
||||
logger.log(u"{name}: {query}".format(name=self.filename, query=query), logger.DB)
|
||||
logger.log(u'{name}: {query}'.format(name=self.filename, query=query), logger.DB)
|
||||
sql_result = self.connection.execute(query)
|
||||
else:
|
||||
logger.log(u"{name}: {query} with args {args}".format
|
||||
logger.log(u'{name}: {query} with args {args}'.format
|
||||
(name=self.filename, query=query, args=args), logger.DB)
|
||||
sql_result = self.connection.execute(query, args)
|
||||
self.connection.commit()
|
||||
# get out of the connection attempt loop since we were successful
|
||||
break
|
||||
except sqlite3.OperationalError as error:
|
||||
if "unable to open database file" in error.args[0] or "database is locked" in error.args[0]:
|
||||
logger.log(u"DB error: {msg}".format(msg=error), logger.WARNING)
|
||||
if 'unable to open database file' in error.args[0] or 'database is locked' in error.args[0]:
|
||||
logger.log(u'DB error: {msg}'.format(msg=error), logger.WARNING)
|
||||
attempt += 1
|
||||
time.sleep(1)
|
||||
else:
|
||||
logger.log(u"DB error: {msg}".format(msg=error), logger.ERROR)
|
||||
logger.log(u'DB error: {msg}'.format(msg=error), logger.ERROR)
|
||||
raise
|
||||
except sqlite3.DatabaseError as error:
|
||||
logger.log(u"Fatal error executing query: {msg}".format(msg=error), logger.ERROR)
|
||||
logger.log(u'Fatal error executing query: {msg}'.format(msg=error), logger.ERROR)
|
||||
raise
|
||||
|
||||
return sql_result
|
||||
|
@ -171,37 +171,37 @@ class DBConnection(object):
|
|||
|
||||
def gen_params(my_dict):
|
||||
return [
|
||||
"{key} = ?".format(key=k)
|
||||
'{key} = ?'.format(key=k)
|
||||
for k in my_dict.keys()
|
||||
]
|
||||
|
||||
changes_before = self.connection.total_changes
|
||||
items = list(value_dict.values()) + list(key_dict.values())
|
||||
self.action(
|
||||
"UPDATE {table} "
|
||||
"SET {params} "
|
||||
"WHERE {conditions}".format(
|
||||
'UPDATE {table} '
|
||||
'SET {params} '
|
||||
'WHERE {conditions}'.format(
|
||||
table=table_name,
|
||||
params=", ".join(gen_params(value_dict)),
|
||||
conditions=" AND ".join(gen_params(key_dict))
|
||||
params=', '.join(gen_params(value_dict)),
|
||||
conditions=' AND '.join(gen_params(key_dict))
|
||||
),
|
||||
items
|
||||
)
|
||||
|
||||
if self.connection.total_changes == changes_before:
|
||||
self.action(
|
||||
"INSERT OR IGNORE INTO {table} ({columns}) "
|
||||
"VALUES ({values})".format(
|
||||
'INSERT OR IGNORE INTO {table} ({columns}) '
|
||||
'VALUES ({values})'.format(
|
||||
table=table_name,
|
||||
columns=", ".join(map(text_type, value_dict.keys())),
|
||||
values=", ".join(["?"] * len(value_dict.values()))
|
||||
columns=', '.join(map(text_type, value_dict.keys())),
|
||||
values=', '.join(['?'] * len(value_dict.values()))
|
||||
),
|
||||
list(value_dict.values())
|
||||
)
|
||||
|
||||
def table_info(self, table_name):
|
||||
# FIXME ? binding is not supported here, but I cannot find a way to escape a string manually
|
||||
cursor = self.connection.execute("PRAGMA table_info({0})".format(table_name))
|
||||
cursor = self.connection.execute('PRAGMA table_info({0})'.format(table_name))
|
||||
columns = {}
|
||||
for column in cursor:
|
||||
columns[column['name']] = {'type': column['type']}
|
||||
|
@ -232,31 +232,31 @@ class DBSanityCheck(object):
|
|||
# ===============
|
||||
|
||||
def upgrade_database(connection, schema):
|
||||
logger.log(u"Checking database structure...", logger.MESSAGE)
|
||||
logger.log(u'Checking database structure...', logger.MESSAGE)
|
||||
_process_upgrade(connection, schema)
|
||||
|
||||
|
||||
def pretty_name(class_name):
|
||||
return ' '.join([x.group() for x in re.finditer("([A-Z])([a-z0-9]+)", class_name)])
|
||||
return ' '.join([x.group() for x in re.finditer('([A-Z])([a-z0-9]+)', class_name)])
|
||||
|
||||
|
||||
def _process_upgrade(connection, upgrade_class):
|
||||
instance = upgrade_class(connection)
|
||||
logger.log(u"Checking {name} database upgrade".format
|
||||
logger.log(u'Checking {name} database upgrade'.format
|
||||
(name=pretty_name(upgrade_class.__name__)), logger.DEBUG)
|
||||
if not instance.test():
|
||||
logger.log(u"Database upgrade required: {name}".format
|
||||
logger.log(u'Database upgrade required: {name}'.format
|
||||
(name=pretty_name(upgrade_class.__name__)), logger.MESSAGE)
|
||||
try:
|
||||
instance.execute()
|
||||
except sqlite3.DatabaseError as error:
|
||||
print(u"Error in {name}: {msg}".format
|
||||
print(u'Error in {name}: {msg}'.format
|
||||
(name=upgrade_class.__name__, msg=error))
|
||||
raise
|
||||
logger.log(u"{name} upgrade completed".format
|
||||
logger.log(u'{name} upgrade completed'.format
|
||||
(name=upgrade_class.__name__), logger.DEBUG)
|
||||
else:
|
||||
logger.log(u"{name} upgrade not required".format
|
||||
logger.log(u'{name} upgrade not required'.format
|
||||
(name=upgrade_class.__name__), logger.DEBUG)
|
||||
|
||||
for upgradeSubClass in upgrade_class.__subclasses__():
|
||||
|
@ -269,23 +269,23 @@ class SchemaUpgrade(object):
|
|||
self.connection = connection
|
||||
|
||||
def has_table(self, table_name):
|
||||
return len(self.connection.action("SELECT 1 FROM sqlite_master WHERE name = ?;", (table_name,)).fetchall()) > 0
|
||||
return len(self.connection.action('SELECT 1 FROM sqlite_master WHERE name = ?;', (table_name,)).fetchall()) > 0
|
||||
|
||||
def has_column(self, table_name, column):
|
||||
return column in self.connection.table_info(table_name)
|
||||
|
||||
def add_column(self, table, column, data_type="NUMERIC", default=0):
|
||||
self.connection.action("ALTER TABLE {0} ADD {1} {2}".format(table, column, data_type))
|
||||
self.connection.action("UPDATE {0} SET {1} = ?".format(table, column), (default,))
|
||||
def add_column(self, table, column, data_type='NUMERIC', default=0):
|
||||
self.connection.action('ALTER TABLE {0} ADD {1} {2}'.format(table, column, data_type))
|
||||
self.connection.action('UPDATE {0} SET {1} = ?'.format(table, column), (default,))
|
||||
|
||||
def check_db_version(self):
|
||||
result = self.connection.select("SELECT db_version FROM db_version")
|
||||
result = self.connection.select('SELECT db_version FROM db_version')
|
||||
if result:
|
||||
return int(result[-1]["db_version"])
|
||||
return int(result[-1]['db_version'])
|
||||
else:
|
||||
return 0
|
||||
|
||||
def inc_db_version(self):
|
||||
new_version = self.check_db_version() + 1
|
||||
self.connection.action("UPDATE db_version SET db_version = ?", [new_version])
|
||||
self.connection.action('UPDATE db_version SET db_version = ?', [new_version])
|
||||
return new_version
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue