eric6/Toolbox/SingleApplication.py

Wed, 01 Jan 2020 11:57:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 01 Jan 2020 11:57:23 +0100
changeset 7360
9190402e4505
parent 7229
53054eb5b15a
child 7781
607a6098cb44
permissions
-rw-r--r--

Updated copyright for 2020.

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
7360
9190402e4505 Updated copyright for 2020.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
3 # Copyright (c) 2004 - 2020 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
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
10
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
11 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
12
5775
3a8bedba97ab Fixed an issue in the single application client.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
13 from PyQt5.QtCore import QByteArray
3656
441956d8fce5 Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3484
diff changeset
14 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
15
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 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
17
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 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
19
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 class SingleApplicationServer(QLocalServer):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 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
24 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 def __init__(self, name):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 Constructor
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 @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
30 """
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
31 super(SingleApplicationServer, self).__init__()
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 res = self.listen(name)
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 if not res:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 # maybe it crashed last time
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 self.removeServer(name)
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 self.listen(name)
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
3345
071afe8be2a1 Changed signal/slot usage to not use constructs like 'triggered[()].connect(...)' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3160
diff changeset
39 self.newConnection.connect(self.__newConnection)
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 self.qsock = None
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 def __newConnection(self):
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 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
46 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 sock = self.nextPendingConnection()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48
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
49 # 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
50 # automatically.
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 if self.qsock is not None:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 return
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 self.qsock = sock
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
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
56 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
57 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
58
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
59 def __receiveJson(self):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 """
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
61 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
62 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 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
64 line = bytes(self.qsock.readLine()).decode()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 ## print(line) ##debug
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67
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
68 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
69 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
70 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
71 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
72 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
73 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
74 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
75 """ 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
76 """ 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
77 """ 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
78 """<p>Error: {0}</p>"""
6631
0a2f0feac79d Updated translations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6625
diff changeset
79 """<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
80 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
81 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
82 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
83 return
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84
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
85 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
86 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
87
3dfcbe478fd3 SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
88 self.handleCommand(command, arguments)
2087
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 def __disconnected(self):
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 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
93 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 self.qsock = None
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 def shutdown(self):
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 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
99 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 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
101 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
102 self.qsock.disconnected.disconnect(self.__disconnected)
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.qsock = None
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 self.close()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107
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
108 def handleCommand(self, command, arguments):
2087
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 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
111
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 <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
113
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
114 @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
115 @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
116 @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
117 @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
118 @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
119 implemented by a subclass
2087
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 raise RuntimeError("'handleCommand' must be overridden")
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
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 class SingleApplicationClient(object):
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 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
127 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 def __init__(self, name):
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 Constructor
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 @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
133 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 self.name = name
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 self.connected = False
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136
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
137 def connect(self, timeout=10000):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 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
140
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
141 @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
142 @type int
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 @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
144 <table>
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 <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
146 <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
147 </table>
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 self.sock = QLocalSocket()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 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
151 if self.sock.waitForConnected(timeout):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 self.connected = True
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 return 1
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 else:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 err = self.sock.error()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 if err == QLocalSocket.ServerNotFoundError:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 return 0
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 else:
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 return -err
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 def disconnect(self):
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 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
164 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 self.sock.disconnectFromServer()
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 self.connected = False
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 def processArgs(self, args):
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 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
171
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 <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
173
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 @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
175 @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
176 implemented by a subclass
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 raise RuntimeError("'processArgs' must be overridden")
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179
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
180 def sendCommand(self, command, arguments):
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 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
183
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
184 @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
185 @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
186 @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
187 @type list of str
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 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
190 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
191 "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
192 "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
193 }
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 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
195 "{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
196 ))
2087
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 self.sock.flush()
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 def errstr(self):
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 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
202
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 @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
204 """
795992a5c561 Made the sixth set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 return self.sock.errorString()

eric ide

mercurial