1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
"""
****************************************************************************
convertall.py, the main program file
ConvertAll, a units conversion program
Copyright (C) 2020, Douglas W. Bell
This is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License, either Version 2 or any later
version. This program is distributed in the hope that it will be useful,
but WITTHOUT ANY WARRANTY. See the included LICENSE file for details.
*****************************************************************************
"""
__progname__ = 'ConvertAll'
__version__ = '0.8.0'
__author__ = 'Doug Bell'
dataFilePath = None # modified by install script if required
helpFilePath = None # modified by install script if required
iconPath = None # modified by install script if required
translationPath = 'translations'
lang = ''
import sys
import os.path
import locale
import getopt
import signal
import builtins
from PyQt5.QtCore import (QCoreApplication, QTranslator)
from PyQt5.QtWidgets import QApplication
def loadTranslator(fileName, app):
"""Load and install qt translator, return True if sucessful.
"""
translator = QTranslator(app)
modPath = os.path.abspath(sys.path[0])
if modPath.endswith('.zip'): # for py2exe
modPath = os.path.dirname(modPath)
path = os.path.join(modPath, translationPath)
result = translator.load(fileName, path)
if not result:
path = os.path.join(modPath, '..', translationPath)
result = translator.load(fileName, path)
if not result:
path = os.path.join(modPath, '..', 'i18n', translationPath)
result = translator.load(fileName, path)
if result:
QCoreApplication.installTranslator(translator)
return True
else:
print('Warning: translation file "{0}" could not be loaded'.
format(fileName))
return False
def setupTranslator(app):
"""Set language, load translators and setup translator function.
"""
try:
locale.setlocale(locale.LC_ALL, '')
except locale.Error:
pass
global lang
lang = os.environ.get('LC_MESSAGES', '')
if not lang:
lang = os.environ.get('LANG', '')
if not lang:
try:
lang = locale.getdefaultlocale()[0]
except ValueError:
pass
if not lang:
lang = ''
numTranslators = 0
if lang and lang[:2] not in ['C', 'en']:
numTranslators += loadTranslator('qt_{0}'.format(lang), app)
numTranslators += loadTranslator('convertall_{0}'.format(lang), app)
def translate(text, comment=''):
"""Translation function that sets context to calling module's
filename.
"""
try:
frame = sys._getframe(1)
fileName = frame.f_code.co_filename
finally:
del frame
context = os.path.basename(os.path.splitext(fileName)[0])
return QCoreApplication.translate(context, text, comment)
def markNoTranslate(text, comment=''):
return text
if numTranslators:
builtins._ = translate
else:
builtins._ = markNoTranslate
def main():
if len(sys.argv) > 1:
try:
opts, args = getopt.gnu_getopt(sys.argv, 'd:fhiqset',
['decimals=', 'fixed-decimals',
'help', 'interactive', 'quiet',
'sci-notation', 'eng-notation',
'test'])
except getopt.GetoptError:
# check that arguments aren't Qt GUI options
if sys.argv[1][:3] not in ['-ba', '-bg', '-bt', '-bu', '-cm',
'-di', '-do', '-fg', '-fn', '-fo',
'-ge', '-gr', '-im', '-in', '-na',
'-nc', '-no', '-re', '-se', '-st',
'-sy', '-ti', '-vi', '-wi']:
app = QCoreApplication(sys.argv)
setupTranslator(app)
import cmdline
cmdline.printUsage()
sys.exit(2)
else:
app = QCoreApplication(sys.argv)
setupTranslator(app)
import cmdline
try:
cmdline.parseArgs(opts, args[1:])
except KeyboardInterrupt:
pass
return
userStyle = '-style' in ' '.join(sys.argv)
app = QApplication(sys.argv)
setupTranslator(app) # must be before importing any convertall modules
import convertdlg
if not userStyle:
QApplication.setStyle('fusion')
win = convertdlg.ConvertDlg()
win.show()
signal.signal(signal.SIGINT, signal.SIG_IGN)
app.exec_()
if __name__ == '__main__':
main()
|