123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #!/usr/bin/python
- """
- This is the main program for KCG
- It imports all modules and starts the Gui
- """
- from PyQt4 import QtGui, QtCore
- import sys
- import os
- import argparse as ap
- import logging
- from logging import handlers
- from . import config
- # -------[ Register Logger here to enable logging before anything of this app is performed ]---------------
- logging.getLogger().setLevel(0)
- if not os.path.isdir(os.path.join(os.path.expanduser("~"),".kcg")):
- os.makedirs(os.path.join(os.path.expanduser("~"),".kcg"))
- fileLogHandler = handlers.RotatingFileHandler(os.path.join(os.path.expanduser("~"),".kcg","kcg.log.full"), maxBytes=10**7, backupCount=5)
- fileLogHandler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
- fileLogHandler.setLevel(0)
- logging.getLogger().addHandler(fileLogHandler)
- logging.info("==========[Start Application]===========")
- try:
- import subprocess
- val = subprocess.check_output(["git", "describe", "--always"]).strip()
- val = str(val, 'utf-8')
- print(val)
- if "fatal" not in val:
- logging.info('Current Git Hash: ' + val)
- except Exception as e:
- print(e)
- pass
- import KCG.base.kcgwidget as kcgw
- import KCG.config as config
- from KCG.config import Configuration
- logging.addLevelName(logging.INFO-1, 'VINFO')
- translator = QtCore.QTranslator()
- kcgw.translator = translator
- # kcgw.tr = translator.translate
- kcgw.tr = QtCore.QCoreApplication.translate
- config.install_path = os.path.dirname(config.__file__)
- try: # Try to use Erax for exception logging
- sys.path.append(os.path.join(os.path.expanduser('~'),"Documents","PythonProjects"))
- from erax import Erax
- exception_log_handler = Erax('https://psraspi.no-ip.biz/p/erax/insert/78e55a9524a191f7628f82a20bcaa167:kcg',
- no_epl_errors=True, cert='/tmp/raspi.pem')
- exception_log_handler.install()
- except ImportError:
- pass
- def print_version(verbose=False):
- """
- Print the version of the current used KCG instance
- :param verbose: print verbose?
- """
- # print "KCG - KAPTURE Control Gui"
- # print "=" * 30
- print("KCG")
- with open(os.path.join(config.install_path,'VERSION'), 'r') as v:
- print(v.read().strip())
- if verbose:
- print("=" * 30)
- print("Using Python:")
- print(sys.version)
- print("=" * 30)
- print("From: " + config.install_path)
- def inject_setting(section, setting, value, args):
- """
- Inject a setting from the command line
- :param section: the section to inject to
- :param setting: the setting to inject
- :param value: the value of this setting
- :param args: the argparse parsed instance
- """
- args.config += section + '->' + setting + '=' + str(value) + ';'
- def log_type(level):
- """
- Method to validate and cast the log level
- :param level: the level to validate and cast
- :return: a valid logging level
- """
- try:
- return int(level)
- except ValueError:
- try:
- return logging._checkLevel(level)
- except ValueError:
- raise ap.ArgumentTypeError("No valid log level.")
- def run():
- """
- Main Function, gets called when GUI is started
- :return:
- """
- app = QtGui.QApplication(sys.argv)
- app.installTranslator(translator)
- app.processEvents()
- # pixmap = QtGui.QPixmap(config.install_path+"icons/KCG_Logo.png").scaled(400, 400)
- # splash_screen = QtGui.QSplashScreen(pixmap)
- # splash_screen.setMask(pixmap.mask())
- # splash_screen.show()
- # splash_screen.update()
- # splash_screen.showMessage("Loading KCG", QtCore.Qt.AlignCenter | QtCore.Qt.AlignBottom, QtCore.Qt.red)
- # splash_screen.update()
- # app.processEvents()
- parser = ap.ArgumentParser("KCG - KAPTURE Control Gui")
- parser.add_argument('--config', type=str, default='', help='Override Configuration file settings.'
- 'Format: "Section->setting=content;Section->setting2=content;Section2->setting3=content" etc.')
- parser.add_argument('--version', action='store_true', help="Print Version and exit")
- parser.add_argument('--vversion', action='store_true', help="Print Version verbose and exit")
- parser.add_argument('--fpga-detection', action='store_true', help="If Present, use 'dev' detection mode (detect"
- "boards by using /dev/fpga# files.)")
- parser.add_argument('--log', type=log_type, default=config.logging.INFO, help="Set the log level")
- parser.add_argument('--testing', action='store_true', default=False,
- help="start KCG in testing version. DO NOT USE THIS IN PRODUCTION.")
- args = parser.parse_args()
- if args.version or args.vversion:
- print_version(args.vversion)
- sys.exit()
- kcgw.testing = args.testing
- if args.fpga_detection:
- inject_setting('Misc', 'board_detection_method', '"dev"', args)
- # logger = logging.getLogger()
- # logger.setLevel(args.log)
- # logging.logger = logger
- def vinfo(content):
- '''log with level VINFO'''
- logging.log(logging.getLevelName('VINFO'), content)
- logging.vinfo = vinfo
- conf = Configuration(args.config, log_level=args.log)
- conf.setup()
- while conf.error:
- conf.doSetup()
- if args.testing:
- config.default_subdirectory_name = 't'
- config.board_detection_method = 'dummy'
- import KCG.base.kcg as kcg
- gui = kcg.Gui()
- # splash_screen.finish(gui)
- gui.show()
- sys.exit(app.exec_())
|