From 21922251e2a8f22817d5f7a0400f1fbda78618b9 Mon Sep 17 00:00:00 2001 From: Mahendra Date: Sat, 7 Apr 2018 12:59:09 +0530 Subject: [PATCH 1/5] helper_sql formating --- src/helper_sql.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/helper_sql.py b/src/helper_sql.py index fec67bef..d2dc9143 100644 --- a/src/helper_sql.py +++ b/src/helper_sql.py @@ -1,22 +1,26 @@ +"""Helper Sql performs sql operations.""" + import threading import Queue -sqlSubmitQueue = Queue.Queue() #SQLITE3 is so thread-unsafe that they won't even let you call it from different threads using your own locks. SQL objects can only be called from one thread. +sqlSubmitQueue = Queue.Queue() # SQLITE3 is so thread-unsafe that they won't +# even let you call it from different threads using your own locks. SQL +# objects #can only be called from one thread. sqlReturnQueue = Queue.Queue() sqlLock = threading.Lock() + def sqlQuery(sqlStatement, *args): sqlLock.acquire() sqlSubmitQueue.put(sqlStatement) if args == (): sqlSubmitQueue.put('') - elif type(args[0]) in [list, tuple]: + elif isinstance(args[0], (list, tuple)): sqlSubmitQueue.put(args[0]) else: sqlSubmitQueue.put(args) - - queryreturn, rowcount = sqlReturnQueue.get() + queryreturn, _ = sqlReturnQueue.get() sqlLock.release() return queryreturn @@ -37,14 +41,14 @@ def sqlExecuteChunked(sqlStatement, idCount, *args): sqlExecuteChunked.chunkSize - (len(args) - idCount) ): chunk_slice = args[ - i:i+sqlExecuteChunked.chunkSize - (len(args) - idCount) + i:i + sqlExecuteChunked.chunkSize - (len(args) - idCount) ] sqlSubmitQueue.put( sqlStatement.format(','.join('?' * len(chunk_slice))) ) # first static args, and then iterative chunk sqlSubmitQueue.put( - args[0:len(args)-idCount] + chunk_slice + args[0:len(args) - idCount] + chunk_slice ) retVal = sqlReturnQueue.get() totalRowCount += retVal[1] @@ -60,7 +64,6 @@ def sqlExecute(sqlStatement, *args): sqlSubmitQueue.put('') else: sqlSubmitQueue.put(args) - queryreturn, rowcount = sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() @@ -71,25 +74,27 @@ def sqlStoredProcedure(procName): sqlSubmitQueue.put(procName) sqlLock.release() + class SqlBulkExecute: def __enter__(self): sqlLock.acquire() return self - def __exit__(self, type, value, traceback): + def __exit__(self, exc_type, value, traceback): sqlSubmitQueue.put('commit') sqlLock.release() - def execute(self, sqlStatement, *args): + @staticmethod + def execute(sqlStatement, *args): sqlSubmitQueue.put(sqlStatement) - if args == (): sqlSubmitQueue.put('') else: sqlSubmitQueue.put(args) sqlReturnQueue.get() - def query(self, sqlStatement, *args): + @staticmethod + def query(sqlStatement, *args): sqlSubmitQueue.put(sqlStatement) if args == (): @@ -97,4 +102,3 @@ class SqlBulkExecute: else: sqlSubmitQueue.put(args) return sqlReturnQueue.get() - From 498557a6d6823e83ffec3defb254bb747c8c1e24 Mon Sep 17 00:00:00 2001 From: Mahendra Date: Sat, 7 Apr 2018 13:18:58 +0530 Subject: [PATCH 2/5] Removed unused variable queryreturn from helper_sql --- src/helper_sql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helper_sql.py b/src/helper_sql.py index d2dc9143..31bfe553 100644 --- a/src/helper_sql.py +++ b/src/helper_sql.py @@ -64,7 +64,7 @@ def sqlExecute(sqlStatement, *args): sqlSubmitQueue.put('') else: sqlSubmitQueue.put(args) - queryreturn, rowcount = sqlReturnQueue.get() + _, rowcount = sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() return rowcount From a012d4a70747ab532d849dca2c6536ce2ea6e9db Mon Sep 17 00:00:00 2001 From: Mahendra Date: Sat, 7 Apr 2018 13:35:31 +0530 Subject: [PATCH 3/5] helper_startup formating --- src/helper_startup.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/helper_startup.py b/src/helper_startup.py index c18c1a88..13b4f0d2 100644 --- a/src/helper_startup.py +++ b/src/helper_startup.py @@ -1,11 +1,10 @@ +"""Helper Start performs all the startup operations.""" + import ConfigParser from bmconfigparser import BMConfigParser import defaults import sys import os -import locale -import random -import string import platform from distutils.version import StrictVersion @@ -14,7 +13,10 @@ import paths import state import helper_random -storeConfigFilesInSameDirectoryAsProgramByDefault = False # The user may de-select Portable Mode in the settings if they want the config files to stay in the application data folder. +storeConfigFilesInSameDirectoryAsProgramByDefault = False # The user may +# de-select Portable Mode in the settings if they want the config files to +# stay in the application data folder. + def _loadTrustedPeer(): try: @@ -27,15 +29,16 @@ def _loadTrustedPeer(): host, port = trustedPeer.split(':') state.trustedPeer = state.Peer(host, int(port)) + def loadConfig(): if state.appdata: BMConfigParser().read(state.appdata + 'keys.dat') - #state.appdata must have been specified as a startup option. + # state.appdata must have been specified as a startup option. try: BMConfigParser().get('bitmessagesettings', 'settingsversion') print 'Loading config files from directory specified on startup: ' + state.appdata needToCreateKeysFile = False - except: + except Exception: needToCreateKeysFile = True else: @@ -45,16 +48,16 @@ def loadConfig(): print 'Loading config files from same directory as program.' needToCreateKeysFile = False state.appdata = paths.lookupExeFolder() - except: - # Could not load the keys.dat file in the program directory. Perhaps it - # is in the appdata directory. + except Exception: + # Could not load the keys.dat file in the program directory. + # Perhaps it is in the appdata directory. state.appdata = paths.lookupAppdataFolder() BMConfigParser().read(state.appdata + 'keys.dat') try: BMConfigParser().get('bitmessagesettings', 'settingsversion') print 'Loading existing config files from', state.appdata needToCreateKeysFile = False - except: + except Exception: needToCreateKeysFile = True if needToCreateKeysFile: @@ -110,7 +113,6 @@ def loadConfig(): BMConfigParser().set('bitmessagesettings', 'maxuploadrate', '0') BMConfigParser().set('bitmessagesettings', 'maxoutboundconnections', '8') BMConfigParser().set('bitmessagesettings', 'ttl', '367200') - #start:UI setting to stop trying to send messages after X days/months BMConfigParser().set( 'bitmessagesettings', 'stopresendingafterxdays', '') @@ -145,9 +147,9 @@ def loadConfig(): def isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections(): try: - if sys.platform[0:3]=="win": - VER_THIS=StrictVersion(platform.version()) + if sys.platform[0:3] == "win": + VER_THIS = StrictVersion(platform.version()) return StrictVersion("5.1.2600")<=VER_THIS and StrictVersion("6.0.6000")>=VER_THIS return False - except Exception as err: + except Exception: return False From a3dff6200eb2ec74dbe7f951b0f4acf2954f4d40 Mon Sep 17 00:00:00 2001 From: Mahendra Date: Sat, 7 Apr 2018 14:10:50 +0530 Subject: [PATCH 4/5] helper_search formating --- src/helper_search.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helper_search.py b/src/helper_search.py index 2217974f..b3d4f923 100644 --- a/src/helper_search.py +++ b/src/helper_search.py @@ -3,9 +3,9 @@ from helper_sql import * try: - from PyQt4 import QtCore, QtGui + from PyQt4 import QtGui haveQt = True -except: +except Exception: haveQt = False def search_translate (context, text): From 4840b2db5c4d35849be0868f6ec1dde87b3da787 Mon Sep 17 00:00:00 2001 From: Mahendra Date: Sat, 7 Apr 2018 19:20:29 +0530 Subject: [PATCH 5/5] add docstring for sqlQuery and sqlBulkExecute methrod in helper_sql and removed query method of sqlBulkExecute class --- src/helper_sql.py | 20 +++++++------------- src/helper_startup.py | 8 ++++---- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/helper_sql.py b/src/helper_sql.py index 31bfe553..18e05e03 100644 --- a/src/helper_sql.py +++ b/src/helper_sql.py @@ -3,14 +3,15 @@ import threading import Queue -sqlSubmitQueue = Queue.Queue() # SQLITE3 is so thread-unsafe that they won't -# even let you call it from different threads using your own locks. SQL -# objects #can only be called from one thread. +sqlSubmitQueue = Queue.Queue() +# SQLITE3 is so thread-unsafe that they won't even let you call it from different threads using your own locks. +# SQL objects #can only be called from one thread. sqlReturnQueue = Queue.Queue() sqlLock = threading.Lock() def sqlQuery(sqlStatement, *args): + """SQLLITE execute statement and return query.""" sqlLock.acquire() sqlSubmitQueue.put(sqlStatement) @@ -76,6 +77,8 @@ def sqlStoredProcedure(procName): class SqlBulkExecute: + """This is used when you have to execute the same statement in a cycle.""" + def __enter__(self): sqlLock.acquire() return self @@ -86,19 +89,10 @@ class SqlBulkExecute: @staticmethod def execute(sqlStatement, *args): + """Used for statements that do not return results.""" sqlSubmitQueue.put(sqlStatement) if args == (): sqlSubmitQueue.put('') else: sqlSubmitQueue.put(args) sqlReturnQueue.get() - - @staticmethod - def query(sqlStatement, *args): - sqlSubmitQueue.put(sqlStatement) - - if args == (): - sqlSubmitQueue.put('') - else: - sqlSubmitQueue.put(args) - return sqlReturnQueue.get() diff --git a/src/helper_startup.py b/src/helper_startup.py index 13b4f0d2..aaab59d9 100644 --- a/src/helper_startup.py +++ b/src/helper_startup.py @@ -13,9 +13,9 @@ import paths import state import helper_random -storeConfigFilesInSameDirectoryAsProgramByDefault = False # The user may -# de-select Portable Mode in the settings if they want the config files to -# stay in the application data folder. +StoreConfigFilesInSameDirectoryAsProgramByDefault = False +# The user may de-select Portable Mode in the settings if they want the config +# files to stay in the application data folder. def _loadTrustedPeer(): @@ -130,7 +130,7 @@ def loadConfig(): ensureNamecoinOptions() - if storeConfigFilesInSameDirectoryAsProgramByDefault: + if StoreConfigFilesInSameDirectoryAsProgramByDefault: # Just use the same directory as the program and forget about # the appdata folder state.appdata = ''