1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
import argparse |
5
|
|
|
import logging |
6
|
|
|
import sys |
7
|
|
|
import os |
8
|
|
|
|
9
|
|
|
from PyQt5 import QtCore |
10
|
|
|
from PyQt5 import QtWidgets |
11
|
|
|
from PyQt5.QtQml import QQmlApplicationEngine |
12
|
|
|
|
13
|
|
|
from qtodotxt.main_controller import MainController |
14
|
|
|
from qtodotxt.lib.file import FileObserver |
15
|
|
|
from qtodotxt.lib.tendo_singleton import SingleInstance |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def _parseArgs(): |
19
|
|
|
if len(sys.argv) > 1 and sys.argv[1].startswith('-psn'): |
20
|
|
|
del sys.argv[1] |
21
|
|
|
parser = argparse.ArgumentParser(description='QTodoTxt') |
22
|
|
|
parser.add_argument('file', type=str, nargs='?', metavar='TEXTFILE', help='open the specified file') |
23
|
|
|
parser.add_argument( |
24
|
|
|
'-l', |
25
|
|
|
'--loglevel', |
26
|
|
|
type=str, |
27
|
|
|
nargs=1, |
28
|
|
|
metavar='LOGLEVEL', |
29
|
|
|
default=['WARN'], |
30
|
|
|
choices=['DEBUG', 'INFO', 'WARNING', 'WARN', 'ERROR', 'CRITICAL'], |
31
|
|
|
help='set one of these logging levels: DEBUG, INFO, WARNING, ERROR, CRITICAL') |
32
|
|
|
return parser.parse_args() |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def _setupLogging(loglevel): |
36
|
|
|
numeric_level = getattr(logging, loglevel[0].upper(), None) |
37
|
|
|
if isinstance(numeric_level, int): |
38
|
|
|
logging.basicConfig( |
39
|
|
|
format='{asctime}.{msecs:.0f} [{name}] {levelname}: {message}', |
40
|
|
|
level=numeric_level, |
41
|
|
|
style='{', |
42
|
|
|
datefmt='%H:%M:%S') |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def setupAnotherInstanceEvent(controller): |
46
|
|
|
# Connecting to a processor reading TMP file |
47
|
|
|
needSingleton = QtCore.QSettings().value("Preferences/singleton", False, type=bool) |
48
|
|
|
if needSingleton: |
49
|
|
|
dirname = os.path.dirname(sys.argv[0]) |
50
|
|
|
fileObserver = FileObserver() |
51
|
|
|
fileObserver.addPath(dirname) |
52
|
|
|
#FIXME maybe do something in qml |
53
|
|
|
#fileObserver.dirChangetSig.connect(controller.anotherInstanceEvent) |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def setupSingleton(args): |
57
|
|
|
needSingleton = QtCore.QSettings().value("Preferences/singleton", False, type=bool) |
58
|
|
|
if int(needSingleton): |
59
|
|
|
me = SingleInstance() |
60
|
|
|
dirname = os.path.dirname(sys.argv[0]) |
61
|
|
|
tempFileName = dirname + "/qtodo.tmp" |
62
|
|
|
if me.initialized is True: |
63
|
|
|
if os.path.isfile(tempFileName): |
64
|
|
|
os.remove(tempFileName) |
65
|
|
|
else: |
66
|
|
|
f = open(tempFileName, 'w') |
67
|
|
|
f.flush() |
68
|
|
|
f.close() |
69
|
|
|
sys.exit(-1) |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def run(): |
73
|
|
|
# First set some application settings for QSettings |
74
|
|
|
QtCore.QCoreApplication.setOrganizationName("QTodoTxt") |
75
|
|
|
QtCore.QCoreApplication.setApplicationName("QTodoTxt2") |
76
|
|
|
# Now set up our application and start |
77
|
|
|
app = QtWidgets.QApplication(sys.argv) |
78
|
|
|
# it is said, that this is lighter: |
79
|
|
|
# (without qwidgets, as we probably don't need them anymore, when transition to qml is done) |
80
|
|
|
# app = QtGui.QGuiApplication(sys.argv) |
81
|
|
|
|
82
|
|
|
name = QtCore.QLocale.system().name() |
83
|
|
|
translator = QtCore.QTranslator() |
84
|
|
|
if translator.load(str(name) + ".qm", "..//i18n"): |
85
|
|
|
app.installTranslator(translator) |
86
|
|
|
|
87
|
|
|
args = _parseArgs() |
88
|
|
|
|
89
|
|
|
setupSingleton(args) |
90
|
|
|
|
91
|
|
|
_setupLogging(args.loglevel) |
92
|
|
|
|
93
|
|
|
engine = QQmlApplicationEngine(parent=app) |
94
|
|
|
controller = MainController(args) |
95
|
|
|
engine.rootContext().setContextProperty("mainController", controller) |
96
|
|
|
path = os.path.dirname(__file__) |
97
|
|
|
engine.addImportPath(path + '/qml/') |
98
|
|
|
engine.load(path + '/qml/QTodoTxt.qml') |
99
|
|
|
|
100
|
|
|
setupAnotherInstanceEvent(controller) |
101
|
|
|
|
102
|
|
|
controller.start() |
103
|
|
|
app.exec_() |
104
|
|
|
sys.exit() |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
if __name__ == "__main__": |
108
|
|
|
run() |
109
|
|
|
|