1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2015 - 2015 by Wilbert Berendsen and Pavel Roskin
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# See http://www.gnu.org/licenses/ for more information.
"""
Utilities to run external commands.
"""
import log
import widgets.dialog
class ExternalCommandDialog(widgets.dialog.Dialog):
"""Dialog that can run an external command, displaying the output.
If the dialog is dismissed, the command is stopped and the cleanup()
method is called.
You need to create a Job instance with the command, and call the run_job()
method to start it. The output is displayed in the dialog.
When the user cancels the dialog while the command is running, cleanup() is
called with the 'aborted' argument. When the user closes the dialog after
the command has run, cleanup() is called with either the 'success' or
'failure' argument, indicating whether the command exited normally.
You can reimplement that method to do things like removing temporary files,
etc.
"""
def __init__(self, parent=None):
super(ExternalCommandDialog, self).__init__(parent)
self.log = log.Log(self)
self.setMainWidget(self.log)
self.finished.connect(self._closed)
def run_job(self, job):
"""Run a job.Job()."""
self.setMessage(_("Please wait until the command finishes"))
self.setStandardButtons(('cancel',))
self.job = job
self.log.connectJob(job)
job.done.connect(self._done)
job.start()
def _done(self):
"""(internal) Called when the job has finished."""
self.setMessage(_("Command completed"))
self.setStandardButtons(('ok',))
def _closed(self):
"""(internal) Called when the user closes the dialog, calls cleanup()."""
if self.job.success is None:
self.job.abort()
state = "aborted"
elif self.job.success:
state = "success"
else:
state = "failure"
self.cleanup(state)
def cleanup(self, state):
"""Called when the dialog is closed.
state is one of three values:
- "success": the command exited normally
- "failure": the command exited with an error
- "aborted": the dialog was cancelled while the command was running.
Reimplement this method to do anything meaningful.
"""
pass
|