-
Notifications
You must be signed in to change notification settings - Fork 5
/
gelectrical_launcher.py
executable file
·85 lines (75 loc) · 3.02 KB
/
gelectrical_launcher.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# run.py
#
# Copyright 2014 Manu Varkey <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import sys, os, io, logging, appdirs
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib, GObject
# Get logger object
log = logging.getLogger()
# Override stdout and stderr with NullWriter in GUI --noconsole mode
# This allow to avoid a bug where tqdm try to write on NoneType
# https://github.com/tqdm/tqdm/issues/794#issuecomment-1426204074
class NullWriter:
def write(self, data):
pass
if sys.stdout is None:
sys.stdout = NullWriter()
if sys.stderr is None:
sys.stderr = NullWriter()
from gelectrical import MainApp, misc
if __name__ == '__main__':
# Setup logging
# Setup Logging to temporary file
log_file_temp = io.StringIO()
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
stream=log_file_temp,level=logging.INFO)
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
stream=sys.stdout,level=logging.INFO)
# Logging to stdout
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
log.addHandler(ch)
# Log all uncaught exceptions
def handle_exception(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
log.error("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))
sys.excepthook = handle_exception
# Initialise main window
log.info('Start Program Execution')
app = MainApp()
log.info('Entering Gtk main loop')
app.run(sys.argv)
log.info('End Program Execution')
# Copy temporary logfile to log folder
dirs = appdirs.AppDirs(misc.PROGRAM_NAME, misc.PROGRAM_AUTHOR, version=misc.PROGRAM_VER)
log_dir = misc.posix_path(dirs.user_data_dir, 'logs')
log_file = misc.posix_path(log_dir, misc.PROGRAM_NAME + '.log')
if not os.path.exists(log_dir):
os.makedirs(log_dir)
with open(log_file, 'w', encoding="utf-8") as fobj:
fobj.write(log_file_temp.getvalue())