kcg.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/python
  2. """
  3. This is the main program for KCG
  4. It imports all modules and starts the Gui
  5. """
  6. from PyQt4 import QtGui, QtCore
  7. import sys
  8. import os
  9. import argparse as ap
  10. import logging
  11. from logging import handlers
  12. from . import config
  13. # -------[ Register Logger here to enable logging before anything of this app is performed ]---------------
  14. logging.getLogger().setLevel(0)
  15. if not os.path.isdir(os.path.join(os.path.expanduser("~"),".kcg")):
  16. os.makedirs(os.path.join(os.path.expanduser("~"),".kcg"))
  17. fileLogHandler = handlers.RotatingFileHandler(os.path.join(os.path.expanduser("~"),".kcg","kcg.log.full"), maxBytes=10**7, backupCount=5)
  18. fileLogHandler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
  19. fileLogHandler.setLevel(0)
  20. logging.getLogger().addHandler(fileLogHandler)
  21. logging.info("==========[Start Application]===========")
  22. try:
  23. import subprocess
  24. val = subprocess.check_output(["git", "describe", "--always"]).strip()
  25. val = str(val, 'utf-8')
  26. print(val)
  27. if "fatal" not in val:
  28. logging.info('Current Git Hash: ' + val)
  29. except Exception as e:
  30. print(e)
  31. pass
  32. import KCG.base.kcgwidget as kcgw
  33. import KCG.config as config
  34. from KCG.config import Configuration
  35. logging.addLevelName(logging.INFO-1, 'VINFO')
  36. translator = QtCore.QTranslator()
  37. kcgw.translator = translator
  38. # kcgw.tr = translator.translate
  39. kcgw.tr = QtCore.QCoreApplication.translate
  40. config.install_path = os.path.dirname(config.__file__)
  41. try: # Try to use Erax for exception logging
  42. sys.path.append(os.path.join(os.path.expanduser('~'),"Documents","PythonProjects"))
  43. from erax import Erax
  44. exception_log_handler = Erax('https://psraspi.no-ip.biz/p/erax/insert/78e55a9524a191f7628f82a20bcaa167:kcg',
  45. no_epl_errors=True, cert='/tmp/raspi.pem')
  46. exception_log_handler.install()
  47. except ImportError:
  48. pass
  49. def print_version(verbose=False):
  50. """
  51. Print the version of the current used KCG instance
  52. :param verbose: print verbose?
  53. """
  54. # print "KCG - KAPTURE Control Gui"
  55. # print "=" * 30
  56. print("KCG")
  57. with open(os.path.join(config.install_path,'VERSION'), 'r') as v:
  58. print(v.read().strip())
  59. if verbose:
  60. print("=" * 30)
  61. print("Using Python:")
  62. print(sys.version)
  63. print("=" * 30)
  64. print("From: " + config.install_path)
  65. def inject_setting(section, setting, value, args):
  66. """
  67. Inject a setting from the command line
  68. :param section: the section to inject to
  69. :param setting: the setting to inject
  70. :param value: the value of this setting
  71. :param args: the argparse parsed instance
  72. """
  73. args.config += section + '->' + setting + '=' + str(value) + ';'
  74. def log_type(level):
  75. """
  76. Method to validate and cast the log level
  77. :param level: the level to validate and cast
  78. :return: a valid logging level
  79. """
  80. try:
  81. return int(level)
  82. except ValueError:
  83. try:
  84. return logging._checkLevel(level)
  85. except ValueError:
  86. raise ap.ArgumentTypeError("No valid log level.")
  87. def run():
  88. """
  89. Main Function, gets called when GUI is started
  90. :return:
  91. """
  92. app = QtGui.QApplication(sys.argv)
  93. app.installTranslator(translator)
  94. app.processEvents()
  95. # pixmap = QtGui.QPixmap(config.install_path+"icons/KCG_Logo.png").scaled(400, 400)
  96. # splash_screen = QtGui.QSplashScreen(pixmap)
  97. # splash_screen.setMask(pixmap.mask())
  98. # splash_screen.show()
  99. # splash_screen.update()
  100. # splash_screen.showMessage("Loading KCG", QtCore.Qt.AlignCenter | QtCore.Qt.AlignBottom, QtCore.Qt.red)
  101. # splash_screen.update()
  102. # app.processEvents()
  103. parser = ap.ArgumentParser("KCG - KAPTURE Control Gui")
  104. parser.add_argument('--config', type=str, default='', help='Override Configuration file settings.'
  105. 'Format: "Section->setting=content;Section->setting2=content;Section2->setting3=content" etc.')
  106. parser.add_argument('--version', action='store_true', help="Print Version and exit")
  107. parser.add_argument('--vversion', action='store_true', help="Print Version verbose and exit")
  108. parser.add_argument('--fpga-detection', action='store_true', help="If Present, use 'dev' detection mode (detect"
  109. "boards by using /dev/fpga# files.)")
  110. parser.add_argument('--log', type=log_type, default=config.logging.INFO, help="Set the log level")
  111. parser.add_argument('--testing', action='store_true', default=False,
  112. help="start KCG in testing version. DO NOT USE THIS IN PRODUCTION.")
  113. args = parser.parse_args()
  114. if args.version or args.vversion:
  115. print_version(args.vversion)
  116. sys.exit()
  117. kcgw.testing = args.testing
  118. if args.fpga_detection:
  119. inject_setting('Misc', 'board_detection_method', '"dev"', args)
  120. # logger = logging.getLogger()
  121. # logger.setLevel(args.log)
  122. # logging.logger = logger
  123. def vinfo(content):
  124. '''log with level VINFO'''
  125. logging.log(logging.getLevelName('VINFO'), content)
  126. logging.vinfo = vinfo
  127. conf = Configuration(args.config, log_level=args.log)
  128. conf.setup()
  129. while conf.error:
  130. conf.doSetup()
  131. if args.testing:
  132. config.default_subdirectory_name = 't'
  133. config.board_detection_method = 'dummy'
  134. import KCG.base.kcg as kcg
  135. gui = kcg.Gui()
  136. # splash_screen.finish(gui)
  137. gui.show()
  138. sys.exit(app.exec_())