code style refactoring

This commit is contained in:
shportix 2023-10-13 14:14:57 +03:00
parent 31210e93ea
commit b396ab212f
No known key found for this signature in database
GPG Key ID: 2594F3870220CC1E
2 changed files with 95 additions and 129 deletions

View File

@ -1,7 +1,5 @@
# pylint: disable=protected-access, too-many-locals, too-many-branches, too-many-statements
""" """
sqlThread is defined here SQLThread is defined here
""" """
import logging import logging
@ -38,87 +36,80 @@ class BitmessageDB(object):
self.conn = None self.conn = None
self.cur = None self.cur = None
self._connection_build() self._connection_build()
self.root_path = os.path.dirname(os.path.dirname(__file__))
def _connection_build(self): def _connection_build(self):
self._connection_build_internal('messages.dat', False) self._connection_build_internal('messages.dat', False)
def _connection_build_internal(self, file_name="messages.dat", memory=False): def _connection_build_internal(
""" self, file_name="messages.dat", memory=False
Stablish SQL connection ):
""" """Establish SQL connection"""
if memory: self.conn = sqlite3.connect(
self.conn = sqlite3.connect(':memory:') ':memory:' if memory else os.path.join(state.appdata, file_name))
else:
self.conn = sqlite3.connect(os.path.join(state.appdata + file_name))
self.conn.text_factory = str self.conn.text_factory = str
self.cur = self.conn.cursor() self.cur = self.conn.cursor()
self.cur.execute('PRAGMA secure_delete = true') self.cur.execute("PRAGMA secure_delete = true")
def __get_current_settings_version(self): def __get_current_settings_version(self):
""" """Get current setting Version"""
Get current setting Version self.cur.execute(
""" "SELECT value FROM settings WHERE key='version'")
query = "SELECT value FROM settings WHERE key='version'"
parameters = ()
self.cur.execute(query, parameters)
try: try:
return int(self.cur.fetchall()[0][0]) return int(self.cur.fetchall()[0][0])
except (ValueError, IndexError): except (ValueError, IndexError):
return 0 return 0
def _upgrade_one_level_sql_statement(self, file_name): def _upgrade_one_level_sql_statement(self, file_name):
""" """Upgrade database versions with applying sql scripts"""
Upgrade database versions with applying sql scripts
"""
self.initialize_sql("init_version_{}".format(file_name)) self.initialize_sql("init_version_{}".format(file_name))
def initialize_sql(self, file_name): def initialize_sql(self, file_name):
""" """Initializing sql"""
Initializing sql
"""
try: try:
with open(os.path.join(self.root_path, "pybitmessage/sql/{}.sql".format(file_name))) as sql_file: with open(os.path.join(
paths.codePath(), 'sql', '{}.sql'.format(file_name))
) as sql_file:
sql_as_string = sql_file.read() sql_as_string = sql_file.read()
self.cur.executescript(sql_as_string) self.cur.executescript(sql_as_string)
return True return True
except OSError as err: except OSError as err:
logger.debug('The file is missing. Error message: %s\n', str(err)) logger.debug('The file is missing. Error message: %s\n',
str(err))
except IOError as err: except IOError as err:
logger.debug( logger.debug(
'ERROR trying to initialize database. Error message: %s\n', str(err)) 'ERROR trying to initialize database. Error message: %s\n',
str(err))
except sqlite3.Error as err: except sqlite3.Error as err:
logger.error(err) logger.error(err)
except Exception as err: except Exception as err:
logger.debug( logger.debug(
'ERROR trying to initialize database. Error message: %s\n', str(err)) 'ERROR trying to initialize database. Error message: %s\n',
str(err))
return False return False
@property @property
def sql_schema_version(self): def sql_schema_version(self):
""" """Getter for get current schema version"""
Getter for get current schema version
"""
return self.__get_current_settings_version() return self.__get_current_settings_version()
def upgrade_to_latest(self): def upgrade_to_latest(self):
""" """Initialize upgrade level"""
Initialize upgrade level self.cur.execute(
""" "SELECT name FROM sqlite_master WHERE type='table' AND name='settings'")
if not self.cur.fetchall():
query = "SELECT name FROM sqlite_master WHERE type='table' AND name='settings'"
parameters = ()
self.cur.execute(query, parameters)
if self.cur.fetchall() == []:
# The settings table doesn't exist. We need to make it. # The settings table doesn't exist. We need to make it.
logger.debug( logger.debug(
"In messages.dat database, creating new 'settings' table.") "In messages.dat database, creating new 'settings' table.")
self.cur.execute( self.cur.execute(
'''CREATE TABLE settings (key text, value blob, UNIQUE(key) ON CONFLICT REPLACE)''') "CREATE TABLE settings (key text, value blob, UNIQUE(key)"
self.cur.execute('''INSERT INTO settings VALUES('version','1')''') " ON CONFLICT REPLACE)")
self.cur.execute('''INSERT INTO settings VALUES('lastvacuumtime',?)''', ( self.cur.execute("INSERT INTO settings VALUES('version','1')")
int(time.time()),)) self.cur.execute(
logger.debug('In messages.dat database, removing an obsolete field from the pubkeys table.') "INSERT INTO settings VALUES('lastvacuumtime',?)",
(int(time.time()),))
logger.debug(
'In messages.dat database, removing an obsolete field'
'from the pubkeys table.')
# initiate sql file # initiate sql file
self.initialize_sql("upg_sc_if_old_ver_1") self.initialize_sql("upg_sc_if_old_ver_1")
@ -133,71 +124,65 @@ class BitmessageDB(object):
self.conn.commit() self.conn.commit()
def check_columns_can_store_binary_null(self): def check_columns_can_store_binary_null(self):
""" """Check if sqlite can store binary zeros."""
Check if sqlite can store binary zeros.
"""
try: try:
testpayload = '\x00\x00' testpayload = '\x00\x00'
t = ('1234', 1, testpayload, '12345678', 'no') t = ('1234', 1, testpayload, '12345678', 'no')
self.cur.execute('''INSERT INTO pubkeys VALUES(?,?,?,?,?)''', t) self.cur.execute("INSERT INTO pubkeys VALUES(?,?,?,?,?)", t)
self.conn.commit() self.conn.commit()
self.cur.execute( self.cur.execute(
'''SELECT transmitdata FROM pubkeys WHERE address='1234' ''') "SELECT transmitdata FROM pubkeys WHERE address='1234' ")
queryreturn = self.cur.fetchall() transmitdata = self.cur.fetchall()[-1][0]
for row in queryreturn: self.cur.execute("DELETE FROM pubkeys WHERE address='1234' ")
transmitdata, = row
self.cur.execute('''DELETE FROM pubkeys WHERE address='1234' ''')
self.conn.commit() self.conn.commit()
if transmitdata == '': if transmitdata != testpayload:
logger.fatal( logger.fatal(
'Problem: The version of SQLite you have cannot store Null values.' 'Problem: The version of SQLite you have cannot store Null'
' Please download and install the latest revision of your version of Python' 'values. Please download and install the latest revision'
' (for example, the latest Python 2.7 revision) and try again.\n') 'of your version of Python (for example, the latest '
'Python 2.7 revision) and try again.\n')
logger.fatal( logger.fatal(
'PyBitmessage will now exit very abruptly.' 'PyBitmessage will now exit very abruptly.'
' You may now see threading errors related to this abrupt exit' ' You may now see threading errors related to this abrupt'
' but the problem you need to solve is related to SQLite.\n\n') 'exit but the problem you need to solve is related to'
os._exit(0) 'SQLite.\n\n')
os._exit(1)
except Exception as err: except Exception as err:
sqlThread().error_handler(err, 'null value test') sqlThread.error_handler(err, 'null value test')
def check_vacuum(self): # pylint: disable=too-many-locals, too-many-branches, too-many-statements, def check_vacuum(self):
# Redefinition-of-parameters-type-from-tuple-to-str, R0204, line-too-long, E501
""" """
Check vacuum and apply sql queries for different different conditions. Check vacuum and apply sql queries for different conditions.
Let us check to see the last time we vaccumed the messages.dat file. Let us check to see the last time we vaccumed the messages.dat file.
If it has been more than a month let's do it now. If it has been more than a month let's do it now.
""" """
self.cur.execute(
query = "SELECT value FROM settings WHERE key='lastvacuumtime'" "SELECT value FROM settings WHERE key='lastvacuumtime'")
parameters = () try:
self.cur.execute(query, parameters) date = self.cur.fetchall()[-1][0]
queryreturn = self.cur.fetchall() except IndexError:
for row in queryreturn: return
value, = row if int(date) < int(time.time()) - 86400:
if int(value) < int(time.time()) - 86400: logger.info(
logger.info('It has been a long time since the messages.dat file has been vacuumed. Vacuuming now...') 'It has been a long time since the messages.dat file'
try: ' has been vacuumed. Vacuuming now...')
self.cur.execute(''' VACUUM ''') try:
except Exception as err: self.cur.execute(''' VACUUM ''')
sqlThread().error_handler(err, 'VACUUM') except Exception as err:
query = "update settings set value=? WHERE key='lastvacuumtime'" sqlThread.error_handler(err, 'VACUUM')
parameters = (int(time.time()),) self.cur.execute(
self.cur.execute(query, parameters) "UPDATE settings SET value=? WHERE key='lastvacuumtime'",
(int(time.time()),))
def upgrade_config_parser_setting_version(self, settingsversion): def upgrade_config_parser_setting_version(self, settingsversion):
""" """Upgrade schema with respect setting version"""
Upgrade schema with respect setting version
"""
self.initialize_sql("config_setting_ver_{}".format(settingsversion)) self.initialize_sql("config_setting_ver_{}".format(settingsversion))
def initialize_schema(self): def initialize_schema(self):
""" """Initialize DB schema"""
Initialize DB schema
"""
try: try:
inbox_exists = list(self.cur.execute('PRAGMA table_info(inbox)')) inbox_exists = list(self.cur.execute("PRAGMA table_info(inbox)"))
if not inbox_exists: if not inbox_exists:
self.initialize_sql("initialize_schema") self.initialize_sql("initialize_schema")
self.conn.commit() self.conn.commit()
@ -206,13 +191,13 @@ class BitmessageDB(object):
if str(err) == 'table inbox already exists': if str(err) == 'table inbox already exists':
logger.debug('Database file already exists.') logger.debug('Database file already exists.')
else: else:
os._exit( logger.fatal(
'ERROR trying to create database file (message.dat). Error message: %s\n' % str(err)) 'Error trying to create database file (message.dat).'
' Error message: %s\n', str(err))
os._exit(1)
def create_sql_function(self): def create_sql_function(self):
""" """Apply create_function to DB"""
Apply create_function to DB
"""
try: try:
self.conn.create_function("enaddr", 3, func=encodeAddress, deterministic=True) self.conn.create_function("enaddr", 3, func=encodeAddress, deterministic=True)
except (TypeError, sqlite3.NotSupportedError) as err: except (TypeError, sqlite3.NotSupportedError) as err:
@ -248,10 +233,10 @@ class sqlThread(threading.Thread):
def upgrade_config_setting_version(self): def upgrade_config_setting_version(self):
""" """
upgrade config parser setting version. upgrade config parser setting version.
If the settings version is equal to 2 or 3 then the If the settings version is equal to 2 or 3 then the
sqlThread will modify the pubkeys table and change sqlThread will modify the pubkeys table and change
the settings version to 4. the settings version to 4.
""" """
while self.sql_config_settings_version < self.max_setting_level: while self.sql_config_settings_version < self.max_setting_level:
self.db.upgrade_config_parser_setting_version(self.sql_config_settings_version) self.db.upgrade_config_parser_setting_version(self.sql_config_settings_version)
@ -259,9 +244,7 @@ class sqlThread(threading.Thread):
@staticmethod @staticmethod
def error_handler(err, command, query=None, parameters=None): def error_handler(err, command, query=None, parameters=None):
""" """Common error handler"""
Common error handler
"""
if str(err) == 'database or disk is full': if str(err) == 'database or disk is full':
logger.fatal( logger.fatal(
"(While %s) Alert: Your disk or data storage volume is full. sqlThread will now exit.", command "(While %s) Alert: Your disk or data storage volume is full. sqlThread will now exit.", command
@ -290,18 +273,14 @@ class sqlThread(threading.Thread):
os._exit(0) os._exit(0)
def is_query_commit(self): def is_query_commit(self):
""" """When query == 'commit'"""
When query == 'commit'
"""
try: try:
self.db.conn.commit() self.db.conn.commit()
except Exception as err: except Exception as err:
self.error_handler(err, 'committing') self.error_handler(err, 'committing')
def is_query_movemessagstoprog(self): def is_query_movemessagstoprog(self):
""" """When query == 'movemessagstoprogs'"""
When query == 'movemessagstoprogs'
"""
logger.debug('the sqlThread is moving the messages.dat file to the local program directory.') logger.debug('the sqlThread is moving the messages.dat file to the local program directory.')
try: try:
self.db.conn.commit() self.db.conn.commit()
@ -315,9 +294,7 @@ class sqlThread(threading.Thread):
self.db.cur = self.db.conn.cursor() self.db.cur = self.db.conn.cursor()
def is_query_deleteandvacuume(self): def is_query_deleteandvacuume(self):
""" """When query == 'deleteandvacuume'"""
When query == 'deleteandvacuume'
"""
try: try:
self.db.cur.execute(''' VACUUM ''') self.db.cur.execute(''' VACUUM ''')
except Exception as err: except Exception as err:
@ -327,9 +304,7 @@ class sqlThread(threading.Thread):
self.db.conn.commit() self.db.conn.commit()
def is_query_other(self, query): def is_query_other(self, query):
""" """When the query can be default or other '"""
When the query can be default or other '
"""
parameters = helper_sql.sqlSubmitQueue.get() parameters = helper_sql.sqlSubmitQueue.get()
try: try:
self.db.cur.execute(query, parameters) self.db.cur.execute(query, parameters)
@ -339,9 +314,7 @@ class sqlThread(threading.Thread):
self.error_handler(err, 'cur.execute', query, parameters) self.error_handler(err, 'cur.execute', query, parameters)
def is_query_movemessagestoappdata(self): def is_query_movemessagestoappdata(self):
""" """When query == 'movemessagestoappdata'"""
When query == 'movemessagestoappdata'
"""
logger.debug('the sqlThread is moving the messages.dat file to the Appdata folder.') logger.debug('the sqlThread is moving the messages.dat file to the Appdata folder.')
try: try:
self.db.conn.commit() self.db.conn.commit()
@ -355,16 +328,12 @@ class sqlThread(threading.Thread):
self.db.cur = self.db.conn.cursor() self.db.cur = self.db.conn.cursor()
def is_query_exit(self): def is_query_exit(self):
""" """When query == 'exit'"""
When query == 'exit'
"""
self.db.conn.close() self.db.conn.close()
logger.info('sqlThread exiting gracefully.') logger.info('sqlThread exiting gracefully.')
def loop_queue(self): def loop_queue(self):
""" """Looping queue and process them"""
Looping queue and process them
"""
query = helper_sql.sqlSubmitQueue.get() query = helper_sql.sqlSubmitQueue.get()
if query == 'commit': if query == 'commit':
self.is_query_commit() self.is_query_commit()
@ -416,9 +385,8 @@ class sqlThread(threading.Thread):
class TestDB(BitmessageDB): class TestDB(BitmessageDB):
""" """Database connection build for test e"""
Database connection build for test e
"""
def _connection_build(self): def _connection_build(self):
self._connection_build_internal("memory", True) self._connection_build_internal("memory", True)
return self.conn, self.cur return self.conn, self.cur

View File

@ -47,9 +47,7 @@ class TestFnBitmessageDB(TestSqlBase, unittest.TestCase): # pylint: disable=pro
""" Test case for Sql function""" """ Test case for Sql function"""
def setUp(self): def setUp(self):
""" """setup for test case"""
setup for test case
"""
self._setup_db() self._setup_db()
def test_create_function(self): def test_create_function(self):