eric6/Toolbox/SingleApplication.py

Wed, 30 Dec 2020 11:00:05 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 30 Dec 2020 11:00:05 +0100
changeset 7923
91e843545d9a
parent 7781
607a6098cb44
child 7973
e836d196e888
permissions
-rw-r--r--

Updated copyright for 2021.

2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
3 # Copyright (c) 2004 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the single application server and client.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
10 import json
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
11
5775
3a8bedba97ab Fixed an issue in the single application client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
12 from PyQt5.QtCore import QByteArray
3656
441956d8fce5 Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3484
diff changeset
13 from PyQt5.QtNetwork import QLocalServer, QLocalSocket
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
15 from E5Gui import E5MessageBox
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
16
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
17 import Utilities
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
18
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 class SingleApplicationServer(QLocalServer):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 Class implementing the single application server base class.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 def __init__(self, name):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 Constructor
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 @param name name this server is listening to (string)
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2302
diff changeset
30 super(SingleApplicationServer, self).__init__()
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 res = self.listen(name)
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 if not res:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 # maybe it crashed last time
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 self.removeServer(name)
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 self.listen(name)
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37
3345
071afe8be2a1 Changed signal/slot usage to not use constructs like 'triggered[()].connect(...)' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3160
diff changeset
38 self.newConnection.connect(self.__newConnection)
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 self.qsock = None
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 def __newConnection(self):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 Private slot to handle a new connection.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 sock = self.nextPendingConnection()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
48 # If we already have a connection, refuse this one. It will be closed
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 # automatically.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 if self.qsock is not None:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 return
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 self.qsock = sock
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
55 self.qsock.readyRead.connect(self.__receiveJson)
3345
071afe8be2a1 Changed signal/slot usage to not use constructs like 'triggered[()].connect(...)' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3160
diff changeset
56 self.qsock.disconnected.connect(self.__disconnected)
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
58 def __receiveJson(self):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 """
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
60 Private method to receive the data from the client.
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 while self.qsock and self.qsock.canReadLine():
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 line = bytes(self.qsock.readLine()).decode()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 ## print(line) ##debug
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
67 try:
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
68 commandDict = json.loads(line.strip())
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
69 except (TypeError, ValueError) as err:
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
70 E5MessageBox.critical(
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
71 None,
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
72 self.tr("Single Application Protocol Error"),
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
73 self.tr("""<p>The response received from the single"""
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
74 """ application client could not be decoded."""
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
75 """ Please report this issue with the received"""
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
76 """ data to the eric bugs email address.</p>"""
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
77 """<p>Error: {0}</p>"""
6631
0a2f0feac79d Updated translations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6625
diff changeset
78 """<p>Data:<br/>{1}</p>""").format(
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
79 str(err), Utilities.html_encode(line.strip())),
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
80 E5MessageBox.StandardButtons(
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
81 E5MessageBox.Ok))
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
82 return
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
84 command = commandDict["command"]
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
85 arguments = commandDict["arguments"]
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
86
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
87 self.handleCommand(command, arguments)
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 def __disconnected(self):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 Private method to handle the closure of the socket.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 self.qsock = None
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 def shutdown(self):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 Public method used to shut down the server.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 if self.qsock is not None:
3345
071afe8be2a1 Changed signal/slot usage to not use constructs like 'triggered[()].connect(...)' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3160
diff changeset
100 self.qsock.readyRead.disconnect(self.__parseLine)
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 self.qsock.disconnected.disconnect(self.__disconnected)
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 self.qsock = None
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 self.close()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
107 def handleCommand(self, command, arguments):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 Public slot to handle the command sent by the client.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 <b>Note</b>: This method must be overridden by subclasses.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
113 @param command command sent by the client
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
114 @type str
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
115 @param arguments list of command arguments
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
116 @type list of str
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
117 @exception RuntimeError raised to indicate that this method must be
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
118 implemented by a subclass
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 raise RuntimeError("'handleCommand' must be overridden")
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 class SingleApplicationClient(object):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 Class implementing the single application client base class.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 def __init__(self, name):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 Constructor
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 @param name name of the local server to connect to (string)
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 self.name = name
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 self.connected = False
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135
6625
a67fee7bc09c Web Browser: changed the web browser logic inside eric to use a remote controlled web browser process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6622
diff changeset
136 def connect(self, timeout=10000):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 Public method to connect the single application client to its server.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139
6625
a67fee7bc09c Web Browser: changed the web browser logic inside eric to use a remote controlled web browser process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6622
diff changeset
140 @param timeout connection timeout value in milliseconds
a67fee7bc09c Web Browser: changed the web browser logic inside eric to use a remote controlled web browser process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6622
diff changeset
141 @type int
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 @return value indicating success or an error number. Value is one of:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 <table>
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 <tr><td>0</td><td>No application is running</td></tr>
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 <tr><td>1</td><td>Application is already running</td></tr>
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 </table>
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 self.sock = QLocalSocket()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 self.sock.connectToServer(self.name)
6625
a67fee7bc09c Web Browser: changed the web browser logic inside eric to use a remote controlled web browser process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6622
diff changeset
150 if self.sock.waitForConnected(timeout):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 self.connected = True
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 return 1
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 else:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 err = self.sock.error()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 if err == QLocalSocket.ServerNotFoundError:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 return 0
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 else:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 return -err
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 def disconnect(self):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 Public method to disconnect from the Single Appliocation server.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 self.sock.disconnectFromServer()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 self.connected = False
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 def processArgs(self, args):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 Public method to process the command line args passed to the UI.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 <b>Note</b>: This method must be overridden by subclasses.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 @param args command line args (list of strings)
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
174 @exception RuntimeError raised to indicate that this method must be
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
175 implemented by a subclass
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 raise RuntimeError("'processArgs' must be overridden")
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
179 def sendCommand(self, command, arguments):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 Public method to send the command to the application server.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
183 @param command command to be sent to the server
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
184 @type str
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
185 @param arguments list of command arguments
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
186 @type list of str
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 if self.connected:
6622
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
189 commandDict = {
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
190 "command": command,
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
191 "arguments": arguments,
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
192 }
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
193 self.sock.write(QByteArray(
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
194 "{0}\n".format(json.dumps(commandDict)).encode()
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
195 ))
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 self.sock.flush()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 def errstr(self):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 Public method to return a meaningful error string for the last error.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 @return error string for the last error (string)
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 return self.sock.errorString()

eric ide

mercurial