Toolbox/SingleApplication.py

Wed, 12 Dec 2018 19:52:24 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 12 Dec 2018 19:52:24 +0100
changeset 6625
a67fee7bc09c
parent 6622
3dfcbe478fd3
child 6631
0a2f0feac79d
permissions
-rw-r--r--

Web Browser: changed the web browser logic inside eric to use a remote controlled web browser process.

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

eric ide

mercurial