eric7/Toolbox/SingleApplication.py

Sat, 22 May 2021 18:51:46 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 22 May 2021 18:51:46 +0200
branch
eric7
changeset 8356
68ec9c3d4de5
parent 8322
b422b4e77d19
child 8358
144a6b854f70
permissions
-rw-r--r--

Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.

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
8318
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
12 from PyQt6.QtCore import QByteArray
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
13 from PyQt6.QtNetwork import QLocalServer, QLocalSocket
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
8356
68ec9c3d4de5 Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8322
diff changeset
15 from E5Gui import EricMessageBox
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
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 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
30 super().__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
7973
e836d196e888 Fixed some code style issues detected by the upgraded eradicate.py.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
65 ## print(line) ## debug # __IGNORE_WARNING_M891__
2087
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:
8356
68ec9c3d4de5 Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8322
diff changeset
70 EricMessageBox.critical(
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
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())),
8356
68ec9c3d4de5 Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8322
diff changeset
80 EricMessageBox.Ok)
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
81 return
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82
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
83 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
84 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
85
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 self.handleCommand(command, arguments)
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 def __disconnected(self):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 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
91 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 self.qsock = None
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 def shutdown(self):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 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
97 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 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
99 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
100 self.qsock.disconnected.disconnect(self.__disconnected)
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 self.qsock = None
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 self.close()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105
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
106 def handleCommand(self, command, arguments):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 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
109
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 <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
111
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
112 @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
113 @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
114 @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
115 @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
116 @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
117 implemented by a subclass
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 raise RuntimeError("'handleCommand' must be overridden")
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121
8207
d359172d11be Applied some more code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8143
diff changeset
122 class SingleApplicationClient:
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 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
125 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 def __init__(self, name):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 Constructor
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 @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
131 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 self.name = name
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 self.connected = False
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134
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
135 def connect(self, timeout=10000):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 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
138
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
139 @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
140 @type int
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 @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
142 <table>
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 <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
144 <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
145 </table>
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 self.sock = QLocalSocket()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 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
149 if self.sock.waitForConnected(timeout):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 self.connected = True
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 return 1
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 else:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 err = self.sock.error()
8143
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7973
diff changeset
154 if err == QLocalSocket.LocalSocketError.ServerNotFoundError:
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 return 0
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 else:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 return -err
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 def disconnect(self):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 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
162 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 self.sock.disconnectFromServer()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 self.connected = False
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 def processArgs(self, args):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 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
169
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 <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
171
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 @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
173 @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
174 implemented by a subclass
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 raise RuntimeError("'processArgs' must be overridden")
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177
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
178 def sendCommand(self, command, arguments):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 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
181
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
182 @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
183 @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
184 @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
185 @type list of str
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 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
188 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
189 "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
190 "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
191 }
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 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
193 "{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
194 ))
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 self.sock.flush()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 def errstr(self):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 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
200
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 @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
202 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 return self.sock.errorString()

eric ide

mercurial