src/eric7/Plugins/VcsPlugins/vcsMercurial/HgServeDialog.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
9 9
10 import os 10 import os
11 11
12 from PyQt6.QtCore import QProcess, Qt, QSize 12 from PyQt6.QtCore import QProcess, Qt, QSize
13 from PyQt6.QtGui import QAction, QTextCursor, QBrush 13 from PyQt6.QtGui import QAction, QTextCursor, QBrush
14 from PyQt6.QtWidgets import ( 14 from PyQt6.QtWidgets import QToolBar, QPlainTextEdit, QSpinBox, QComboBox
15 QToolBar, QPlainTextEdit, QSpinBox, QComboBox
16 )
17 15
18 from EricWidgets.EricApplication import ericApp 16 from EricWidgets.EricApplication import ericApp
19 from EricWidgets import EricMessageBox 17 from EricWidgets import EricMessageBox
20 from EricWidgets.EricMainWindow import EricMainWindow 18 from EricWidgets.EricMainWindow import EricMainWindow
21 19
27 25
28 class HgServeDialog(EricMainWindow): 26 class HgServeDialog(EricMainWindow):
29 """ 27 """
30 Class implementing a dialog for the Mercurial server. 28 Class implementing a dialog for the Mercurial server.
31 """ 29 """
30
32 def __init__(self, vcs, path, parent=None): 31 def __init__(self, vcs, path, parent=None):
33 """ 32 """
34 Constructor 33 Constructor
35 34
36 @param vcs reference to the vcs object 35 @param vcs reference to the vcs object
37 @type Hg 36 @type Hg
38 @param path path of the repository to serve 37 @param path path of the repository to serve
39 @type str 38 @type str
40 @param parent reference to the parent widget 39 @param parent reference to the parent widget
41 @type QWidget 40 @type QWidget
42 """ 41 """
43 super().__init__(parent) 42 super().__init__(parent)
44 43
45 self.vcs = vcs 44 self.vcs = vcs
46 self.__repoPath = path 45 self.__repoPath = path
47 46
48 self.__styles = ["paper", "coal", "gitweb", "monoblue", "spartan", ] 47 self.__styles = [
49 48 "paper",
49 "coal",
50 "gitweb",
51 "monoblue",
52 "spartan",
53 ]
54
50 self.setWindowTitle(self.tr("Mercurial Server")) 55 self.setWindowTitle(self.tr("Mercurial Server"))
51 56
52 iconSuffix = "dark" if ericApp().usesDarkPalette() else "light" 57 iconSuffix = "dark" if ericApp().usesDarkPalette() else "light"
53 58
54 self.__startAct = QAction( 59 self.__startAct = QAction(
55 UI.PixmapCache.getIcon( 60 UI.PixmapCache.getIcon(
56 os.path.join("VcsPlugins", "vcsMercurial", "icons", 61 os.path.join(
57 "startServer-{0}.svg".format(iconSuffix))), 62 "VcsPlugins",
58 self.tr("Start Server"), self) 63 "vcsMercurial",
64 "icons",
65 "startServer-{0}.svg".format(iconSuffix),
66 )
67 ),
68 self.tr("Start Server"),
69 self,
70 )
59 self.__startAct.triggered.connect(self.__startServer) 71 self.__startAct.triggered.connect(self.__startServer)
60 self.__stopAct = QAction( 72 self.__stopAct = QAction(
61 UI.PixmapCache.getIcon( 73 UI.PixmapCache.getIcon(
62 os.path.join("VcsPlugins", "vcsMercurial", "icons", 74 os.path.join(
63 "stopServer-{0}.svg".format(iconSuffix))), 75 "VcsPlugins",
64 self.tr("Stop Server"), self) 76 "vcsMercurial",
77 "icons",
78 "stopServer-{0}.svg".format(iconSuffix),
79 )
80 ),
81 self.tr("Stop Server"),
82 self,
83 )
65 self.__stopAct.triggered.connect(self.__stopServer) 84 self.__stopAct.triggered.connect(self.__stopServer)
66 self.__browserAct = QAction( 85 self.__browserAct = QAction(
67 UI.PixmapCache.getIcon("home"), 86 UI.PixmapCache.getIcon("home"), self.tr("Start Browser"), self
68 self.tr("Start Browser"), self) 87 )
69 self.__browserAct.triggered.connect(self.__startBrowser) 88 self.__browserAct.triggered.connect(self.__startBrowser)
70 89
71 self.__portSpin = QSpinBox(self) 90 self.__portSpin = QSpinBox(self)
72 self.__portSpin.setMinimum(2048) 91 self.__portSpin.setMinimum(2048)
73 self.__portSpin.setMaximum(65535) 92 self.__portSpin.setMaximum(65535)
74 self.__portSpin.setToolTip(self.tr("Enter the server port")) 93 self.__portSpin.setToolTip(self.tr("Enter the server port"))
75 self.__portSpin.setValue( 94 self.__portSpin.setValue(self.vcs.getPlugin().getPreferences("ServerPort"))
76 self.vcs.getPlugin().getPreferences("ServerPort")) 95
77
78 self.__styleCombo = QComboBox(self) 96 self.__styleCombo = QComboBox(self)
79 self.__styleCombo.addItems(self.__styles) 97 self.__styleCombo.addItems(self.__styles)
80 self.__styleCombo.setToolTip(self.tr("Select the style to use")) 98 self.__styleCombo.setToolTip(self.tr("Select the style to use"))
81 self.__styleCombo.setCurrentIndex(self.__styleCombo.findText( 99 self.__styleCombo.setCurrentIndex(
82 self.vcs.getPlugin().getPreferences("ServerStyle"))) 100 self.__styleCombo.findText(
83 101 self.vcs.getPlugin().getPreferences("ServerStyle")
102 )
103 )
104
84 self.__serverToolbar = QToolBar(self.tr("Server"), self) 105 self.__serverToolbar = QToolBar(self.tr("Server"), self)
85 self.__serverToolbar.addAction(self.__startAct) 106 self.__serverToolbar.addAction(self.__startAct)
86 self.__serverToolbar.addAction(self.__stopAct) 107 self.__serverToolbar.addAction(self.__stopAct)
87 self.__serverToolbar.addSeparator() 108 self.__serverToolbar.addSeparator()
88 self.__serverToolbar.addWidget(self.__portSpin) 109 self.__serverToolbar.addWidget(self.__portSpin)
89 self.__serverToolbar.addWidget(self.__styleCombo) 110 self.__serverToolbar.addWidget(self.__styleCombo)
90 111
91 self.__browserToolbar = QToolBar(self.tr("Browser"), self) 112 self.__browserToolbar = QToolBar(self.tr("Browser"), self)
92 self.__browserToolbar.addAction(self.__browserAct) 113 self.__browserToolbar.addAction(self.__browserAct)
93 114
94 self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.__serverToolbar) 115 self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.__serverToolbar)
95 self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.__browserToolbar) 116 self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.__browserToolbar)
96 117
97 self.__log = QPlainTextEdit(self) 118 self.__log = QPlainTextEdit(self)
98 self.setCentralWidget(self.__log) 119 self.setCentralWidget(self.__log)
99 120
100 # polish up the dialog 121 # polish up the dialog
101 self.__startAct.setEnabled(True) 122 self.__startAct.setEnabled(True)
102 self.__stopAct.setEnabled(False) 123 self.__stopAct.setEnabled(False)
103 self.__browserAct.setEnabled(False) 124 self.__browserAct.setEnabled(False)
104 self.__portSpin.setEnabled(True) 125 self.__portSpin.setEnabled(True)
105 self.__styleCombo.setEnabled(True) 126 self.__styleCombo.setEnabled(True)
106 self.resize(QSize(800, 600).expandedTo(self.minimumSizeHint())) 127 self.resize(QSize(800, 600).expandedTo(self.minimumSizeHint()))
107 128
108 self.process = QProcess() 129 self.process = QProcess()
109 self.process.finished.connect(self.__procFinished) 130 self.process.finished.connect(self.__procFinished)
110 self.process.readyReadStandardOutput.connect(self.__readStdout) 131 self.process.readyReadStandardOutput.connect(self.__readStdout)
111 self.process.readyReadStandardError.connect(self.__readStderr) 132 self.process.readyReadStandardError.connect(self.__readStderr)
112 133
113 self.cNormalFormat = self.__log.currentCharFormat() 134 self.cNormalFormat = self.__log.currentCharFormat()
114 self.cErrorFormat = self.__log.currentCharFormat() 135 self.cErrorFormat = self.__log.currentCharFormat()
115 self.cErrorFormat.setForeground( 136 self.cErrorFormat.setForeground(QBrush(Preferences.getUI("LogStdErrColour")))
116 QBrush(Preferences.getUI("LogStdErrColour"))) 137
117
118 def __startServer(self): 138 def __startServer(self):
119 """ 139 """
120 Private slot to start the Mercurial server. 140 Private slot to start the Mercurial server.
121 """ 141 """
122 port = self.__portSpin.value() 142 port = self.__portSpin.value()
123 style = self.__styleCombo.currentText() 143 style = self.__styleCombo.currentText()
124 144
125 exe = getHgExecutable() 145 exe = getHgExecutable()
126 146
127 args = self.vcs.initCommand("serve") 147 args = self.vcs.initCommand("serve")
128 args.append("-v") 148 args.append("-v")
129 args.append("--port") 149 args.append("--port")
130 args.append(str(port)) 150 args.append(str(port))
131 args.append("--style") 151 args.append("--style")
132 args.append(style) 152 args.append(style)
133 153
134 self.process.setWorkingDirectory(self.__repoPath) 154 self.process.setWorkingDirectory(self.__repoPath)
135 155
136 self.process.start(exe, args) 156 self.process.start(exe, args)
137 procStarted = self.process.waitForStarted(5000) 157 procStarted = self.process.waitForStarted(5000)
138 if procStarted: 158 if procStarted:
139 self.__startAct.setEnabled(False) 159 self.__startAct.setEnabled(False)
140 self.__stopAct.setEnabled(True) 160 self.__stopAct.setEnabled(True)
144 self.vcs.getPlugin().setPreferences("ServerPort", port) 164 self.vcs.getPlugin().setPreferences("ServerPort", port)
145 self.vcs.getPlugin().setPreferences("ServerStyle", style) 165 self.vcs.getPlugin().setPreferences("ServerStyle", style)
146 else: 166 else:
147 EricMessageBox.critical( 167 EricMessageBox.critical(
148 self, 168 self,
149 self.tr('Process Generation Error'), 169 self.tr("Process Generation Error"),
150 self.tr( 170 self.tr(
151 'The process {0} could not be started. ' 171 "The process {0} could not be started. "
152 'Ensure, that it is in the search path.' 172 "Ensure, that it is in the search path."
153 ).format(exe)) 173 ).format(exe),
154 174 )
175
155 def __stopServer(self): 176 def __stopServer(self):
156 """ 177 """
157 Private slot to stop the Mercurial server. 178 Private slot to stop the Mercurial server.
158 """ 179 """
159 if ( 180 if (
160 self.process is not None and 181 self.process is not None
161 self.process.state() != QProcess.ProcessState.NotRunning 182 and self.process.state() != QProcess.ProcessState.NotRunning
162 ): 183 ):
163 self.process.terminate() 184 self.process.terminate()
164 self.process.waitForFinished(5000) 185 self.process.waitForFinished(5000)
165 if self.process.state() != QProcess.ProcessState.NotRunning: 186 if self.process.state() != QProcess.ProcessState.NotRunning:
166 self.process.kill() 187 self.process.kill()
167 188
168 self.__startAct.setEnabled(True) 189 self.__startAct.setEnabled(True)
169 self.__stopAct.setEnabled(False) 190 self.__stopAct.setEnabled(False)
170 self.__browserAct.setEnabled(False) 191 self.__browserAct.setEnabled(False)
171 self.__portSpin.setEnabled(True) 192 self.__portSpin.setEnabled(True)
172 self.__styleCombo.setEnabled(True) 193 self.__styleCombo.setEnabled(True)
173 194
174 def __startBrowser(self): 195 def __startBrowser(self):
175 """ 196 """
176 Private slot to start a browser for the served repository. 197 Private slot to start a browser for the served repository.
177 """ 198 """
178 ui = ericApp().getObject("UserInterface") 199 ui = ericApp().getObject("UserInterface")
179 ui.launchHelpViewer( 200 ui.launchHelpViewer("http://localhost:{0}".format(self.__portSpin.value()))
180 "http://localhost:{0}".format(self.__portSpin.value())) 201
181
182 def closeEvent(self, e): 202 def closeEvent(self, e):
183 """ 203 """
184 Protected slot implementing a close event handler. 204 Protected slot implementing a close event handler.
185 205
186 @param e close event 206 @param e close event
187 @type QCloseEvent 207 @type QCloseEvent
188 """ 208 """
189 self.__stopServer() 209 self.__stopServer()
190 210
191 def __procFinished(self, exitCode, exitStatus): 211 def __procFinished(self, exitCode, exitStatus):
192 """ 212 """
193 Private slot connected to the finished signal. 213 Private slot connected to the finished signal.
194 214
195 @param exitCode exit code of the process 215 @param exitCode exit code of the process
196 @type int 216 @type int
197 @param exitStatus exit status of the process 217 @param exitStatus exit status of the process
198 @type QProcess.ExitStatus 218 @type QProcess.ExitStatus
199 """ 219 """
200 self.__stopServer() 220 self.__stopServer()
201 221
202 def __readStdout(self): 222 def __readStdout(self):
203 """ 223 """
204 Private slot to handle the readyReadStandardOutput signal. 224 Private slot to handle the readyReadStandardOutput signal.
205 225
206 It reads the output of the process and inserts it into the log. 226 It reads the output of the process and inserts it into the log.
207 """ 227 """
208 if self.process is not None: 228 if self.process is not None:
209 s = str(self.process.readAllStandardOutput(), 229 s = str(
210 self.vcs.getEncoding(), 'replace') 230 self.process.readAllStandardOutput(), self.vcs.getEncoding(), "replace"
231 )
211 self.__appendText(s, False) 232 self.__appendText(s, False)
212 233
213 def __readStderr(self): 234 def __readStderr(self):
214 """ 235 """
215 Private slot to handle the readyReadStandardError signal. 236 Private slot to handle the readyReadStandardError signal.
216 237
217 It reads the error output of the process and inserts it into the log. 238 It reads the error output of the process and inserts it into the log.
218 """ 239 """
219 if self.process is not None: 240 if self.process is not None:
220 s = str(self.process.readAllStandardError(), 241 s = str(
221 self.vcs.getEncoding(), 'replace') 242 self.process.readAllStandardError(), self.vcs.getEncoding(), "replace"
243 )
222 self.__appendText(s, True) 244 self.__appendText(s, True)
223 245
224 def __appendText(self, txt, error=False): 246 def __appendText(self, txt, error=False):
225 """ 247 """
226 Private method to append text to the end. 248 Private method to append text to the end.
227 249
228 @param txt text to insert 250 @param txt text to insert
229 @type str 251 @type str
230 @param error flag indicating to insert error text 252 @param error flag indicating to insert error text
231 @type bool 253 @type bool
232 """ 254 """

eric ide

mercurial