diff --git a/src/bitmessageqt/dialogs.py b/src/bitmessageqt/dialogs.py index cb82f348..3d92b776 100644 --- a/src/bitmessageqt/dialogs.py +++ b/src/bitmessageqt/dialogs.py @@ -1,15 +1,13 @@ -from PyQt4 import QtGui -from tr import _translate -from retranslateui import RetranslateMixin +import paths import widgets - -from newchandialog import NewChanDialog from address_dialogs import ( AddAddressDialog, NewAddressDialog, NewSubscriptionDialog, RegenerateAddressesDialog, SpecialAddressBehaviorDialog, EmailGatewayDialog ) - -import paths +from newchandialog import NewChanDialog +from PyQt4 import QtGui +from retranslateui import RetranslateMixin +from tr import _translate from version import softwareVersion @@ -32,7 +30,7 @@ class AboutDialog(QtGui.QDialog, RetranslateMixin): self.labelVersion.setText( self.labelVersion.text().replace( ':version:', version - ).replace(':branch:', commit or 'v%s' % version) + ).replace(':branch:', commit or 'v%s' % version) ) self.labelVersion.setOpenExternalLinks(True) @@ -58,7 +56,7 @@ class IconGlossaryDialog(QtGui.QDialog, RetranslateMixin): self.labelPortNumber.setText(_translate( "iconGlossaryDialog", "You are using TCP port %1. (This can be changed in the settings)." - ).arg(config.getint('bitmessagesettings', 'port'))) + ).arg(config.getint('bitmessagesettings', 'port'))) self.setFixedSize(QtGui.QWidget.sizeHint(self)) diff --git a/src/paths.py b/src/paths.py index 325fcd8b..c68701b4 100644 --- a/src/paths.py +++ b/src/paths.py @@ -1,75 +1,95 @@ -from os import environ, path -import sys +import os import re +import sys from datetime import datetime # When using py2exe or py2app, the variable frozen is added to the sys -# namespace. This can be used to setup a different code path for +# namespace. This can be used to setup a different code path for # binary distributions vs source distributions. -frozen = getattr(sys,'frozen', None) +frozen = getattr(sys, 'frozen', None) + def lookupExeFolder(): if frozen: if frozen == "macosx_app": # targetdir/Bitmessage.app/Contents/MacOS/Bitmessage - exeFolder = path.dirname(path.dirname(path.dirname(path.dirname(sys.executable)))) + path.sep + exeFolder = os.path.dirname(os.path.dirname(os.path.dirname( + os.path.dirname(sys.executable) + ))) else: - exeFolder = path.dirname(sys.executable) + path.sep + exeFolder = os.path.dirname(sys.executable) elif __file__: - exeFolder = path.dirname(__file__) + path.sep + exeFolder = os.path.dirname(__file__) else: - exeFolder = '' - return exeFolder + return '' + return exeFolder + os.path.sep + def lookupAppdataFolder(): APPNAME = "PyBitmessage" - if "BITMESSAGE_HOME" in environ: - dataFolder = environ["BITMESSAGE_HOME"] - if dataFolder[-1] not in [path.sep, path.altsep]: - dataFolder += path.sep - elif sys.platform == 'darwin': - if "HOME" in environ: - dataFolder = path.join(environ["HOME"], "Library/Application Support/", APPNAME) + '/' - else: - stringToLog = 'Could not find home folder, please report this message and your OS X version to the BitMessage Github.' - if 'logger' in globals(): - logger.critical(stringToLog) - else: - print stringToLog - sys.exit() + try: # for daemon + dataFolder = os.environ["BITMESSAGE_HOME"] + if dataFolder[-1] not in (os.path.sep, os.path.altsep): + dataFolder += os.path.sep + except KeyError: + pass + else: + return dataFolder + + if sys.platform == 'darwin': + try: + dataFolder = os.path.join( + os.environ["HOME"], "Library/Application Support/", APPNAME) + except KeyError: + log_msg = ( + 'Could not find home folder, please report this message' + ' and your OS X version to the BitMessage Github.' + ) + try: + logger.critical(log_msg) + except NameError: + print(log_msg) + sys.exit(1) elif 'win32' in sys.platform or 'win64' in sys.platform: - dataFolder = path.join(environ['APPDATA'].decode(sys.getfilesystemencoding(), 'ignore'), APPNAME) + path.sep + dataFolder = os.path.join(os.environ['APPDATA'].decode( + sys.getfilesystemencoding(), 'ignore'), APPNAME) else: from shutil import move try: - dataFolder = path.join(environ["XDG_CONFIG_HOME"], APPNAME) + config_dir = os.environ["XDG_CONFIG_HOME"] except KeyError: - dataFolder = path.join(environ["HOME"], ".config", APPNAME) + config_dir = os.path.join(os.environ["HOME"], ".config") - # Migrate existing data to the proper location if this is an existing install + dataFolder = os.path.join(config_dir, APPNAME) + + # Migrate existing data to the proper location + # if this is an existing install try: - move(path.join(environ["HOME"], ".%s" % APPNAME), dataFolder) - stringToLog = "Moving data folder to %s" % (dataFolder) - if 'logger' in globals(): - logger.info(stringToLog) - else: - print stringToLog + move( + os.path.join(os.environ["HOME"], ".%s" % APPNAME), dataFolder) + log_msg = "Moving data folder to %s" % dataFolder + try: + logger.info(log_msg) + except NameError: + print(log_msg) except IOError: # Old directory may not exist. pass - dataFolder = dataFolder + '/' - return dataFolder - + + return dataFolder + os.path.sep + + def codePath(): if frozen == "macosx_app": - codePath = environ.get("RESOURCEPATH") - elif frozen: # windows + codePath = os.environ.get("RESOURCEPATH") + elif frozen: # windows codePath = sys._MEIPASS - else: - codePath = path.dirname(__file__) + else: + codePath = os.path.dirname(__file__) return codePath + def tail(f, lines=20): total_lines_wanted = lines @@ -78,16 +98,17 @@ def tail(f, lines=20): block_end_byte = f.tell() lines_to_go = total_lines_wanted block_number = -1 - blocks = [] # blocks of size BLOCK_SIZE, in reverse order starting - # from the end of the file + # blocks of size BLOCK_SIZE, in reverse order starting + # from the end of the file + blocks = [] while lines_to_go > 0 and block_end_byte > 0: if (block_end_byte - BLOCK_SIZE > 0): # read the last block we haven't yet read - f.seek(block_number*BLOCK_SIZE, 2) + f.seek(block_number * BLOCK_SIZE, 2) blocks.append(f.read(BLOCK_SIZE)) else: # file too small, start from begining - f.seek(0,0) + f.seek(0, 0) # only read what was not read blocks.append(f.read(block_end_byte)) lines_found = blocks[-1].count('\n') @@ -99,9 +120,9 @@ def tail(f, lines=20): def lastCommit(): - githeadfile = path.join(codePath(), '..', '.git', 'logs', 'HEAD') + githeadfile = os.path.join(codePath(), '..', '.git', 'logs', 'HEAD') result = {} - if path.isfile(githeadfile): + if os.path.isfile(githeadfile): try: with open(githeadfile, 'rt') as githead: line = tail(githead, 1)