forked from olivierkes/manuskript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
126 lines (96 loc) · 3.98 KB
/
main.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
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
# -*- coding: utf-8 -*-
import faulthandler
import logging.config
import sys
import traceback
from PyQt5.QtCore import QLocale, QTranslator, QSettings
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, qApp
from path import Path
import yaml
from manuskript import constants
faulthandler.enable()
# load logging configuration from 'logging.yaml'
with open(Path(__file__).parent / 'manuskript' / 'logging.yaml', 'rt') as f:
logging.config.dictConfig(yaml.load(f))
logger = logging.getLogger('manuskript')
# TODO: add a statusBarHandler to logger
SYS_EXCEPT_HOOK = sys.excepthook
def _excepthook(typ, value, trace):
""" Override the standard error handling to log any uncatched exception """
QApplication.restoreOverrideCursor()
logger.error("{}\n{}\n{}".format(typ.__name__, value, ''.join(traceback.format_tb(trace))))
SYS_EXCEPT_HOOK(typ, value, trace)
sys.excepthook = _excepthook
__VERSION__ = constants.VERSION
def prepare():
"""
Instantiate the QApplication, load settings, translations and theme, create the user data directory
"""
logger.info("Running manuskript version {}.".format(constants.VERSION))
app = QApplication(sys.argv)
app.setOrganizationName(constants.APP_NAME)
app.setOrganizationDomain(constants.APP_WEBSITE)
app.setApplicationName(constants.APP_NAME)
app.setApplicationVersion(constants.VERSION)
icon = QIcon()
for i in [16, 32, 64, 128, 256, 512]:
icon_file = constants.MAIN_DIR / "icons/Manuskript/icon-{}px.png".format(i)
if icon_file.exists():
icon.addFile(icon_file)
else:
logger.warning("%s does not exist", icon_file.name)
app.setWindowIcon(icon)
app.setStyle("Fusion")
# Load style from QSettings
qsettings = QSettings(app.organizationName(), app.applicationName())
if qsettings.contains("applicationStyle"):
style = qsettings.value("applicationStyle")
app.setStyle(style)
# Translation process
locale = QLocale.system().name()
appTranslator = QTranslator(app) # By default: locale
# Load translation from settings
translation_file = ""
if qsettings.contains("applicationTranslation"):
translation_file = qsettings.value("applicationTranslation")
logger.info("Found translation in settings: %s", translation_file)
else:
translation_file = "manuskript_{}.qm".format(locale)
logger.info("No translation in settings, use: %s", translation_file)
if appTranslator.load(constants.MAIN_DIR / "i18n" / translation_file):
app.installTranslator(appTranslator)
logger.info(app.tr("Loaded translation: {}.").format(translation_file))
else:
logger.warning(app.tr("No translator found or loaded for locale %s", locale))
QIcon.setThemeSearchPaths(QIcon.themeSearchPaths() + [constants.ICONS_DIR])
QIcon.setThemeName("NumixMsk")
# Font siue
if qsettings.contains("appFontSize"):
f = qApp.font()
f.setPointSize(qsettings.value("appFontSize", type=int))
qApp.setFont(f)
# creates app directories
constants.USER_DATA_DIR.mkdir_p()
return app
def run():
"""
Run Manuskript
"""
# Run separates prepare and launch for two reasons:
# 1. I've read somewhere it helps with potential segfault (see comment below)
# 2. So that prepare can be used in tests, without running the whole thing
app = prepare()
# Parse sys args
args = sys.argv[1:]
project_path = ""
if args and Path(args[0]).ext == ".msk" and Path(args[0]).exists():
project_path = Path(args[0]).abspath()
from manuskript.mainWindow import MainWindow
mw = MainWindow(project_path, app.cursorFlashTime())
mw.show()
r = app.exec_()
logger.info("-- manuskript exited with code %s --", r)
sys.exit(r)
if __name__ == "__main__":
run()