[gedit/wip/etfixes: 1/2] Small refactoring
- From: Paolo Borelli <pborelli src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit/wip/etfixes: 1/2] Small refactoring
- Date: Sat, 29 Mar 2014 14:40:34 +0000 (UTC)
commit ef4ce7b274eb2ce1afa9855f052716ef03ee9a0d
Author: Paolo Borelli <pborelli gnome org>
Date: Sat Mar 29 11:52:38 2014 +0100
Small refactoring
plugins/externaltools/tools/capture.py | 65 ++++++++++++++++----------------
1 files changed, 32 insertions(+), 33 deletions(-)
---
diff --git a/plugins/externaltools/tools/capture.py b/plugins/externaltools/tools/capture.py
index db98da7..488d6ee 100644
--- a/plugins/externaltools/tools/capture.py
+++ b/plugins/externaltools/tools/capture.py
@@ -18,28 +18,31 @@
__all__ = ('Capture', )
-import os, sys, signal
+import os
+import sys
+import signal
import locale
import subprocess
import fcntl
from gi.repository import GLib, GObject
+
class Capture(GObject.Object):
CAPTURE_STDOUT = 0x01
CAPTURE_STDERR = 0x02
- CAPTURE_BOTH = 0x03
+ CAPTURE_BOTH = 0x03
CAPTURE_NEEDS_SHELL = 0x04
-
+
WRITE_BUFFER_SIZE = 0x4000
__gsignals__ = {
- 'stdout-line' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,)),
- 'stderr-line' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,)),
+ 'stdout-line': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,)),
+ 'stderr-line': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,)),
'begin-execute': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, tuple()),
- 'end-execute' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_INT,))
+ 'end-execute': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_INT,))
}
- def __init__(self, command, cwd = None, env = {}):
+ def __init__(self, command, cwd=None, env={}):
GObject.GObject.__init__(self)
self.pipe = None
self.env = env
@@ -69,11 +72,11 @@ class Capture(GObject.Object):
# Initialize pipe
popen_args = {
- 'cwd' : self.cwd,
+ 'cwd': self.cwd,
'shell': self.flags & self.CAPTURE_NEEDS_SHELL,
- 'env' : self.env
+ 'env': self.env
}
-
+
if self.input_text is not None:
popen_args['stdin'] = subprocess.PIPE
if self.flags & self.CAPTURE_STDOUT:
@@ -94,31 +97,16 @@ class Capture(GObject.Object):
self.pipe = None
self.emit('stderr-line', _('Could not execute command: %s') % (e, ))
return
-
- # Signal
+
self.emit('begin-execute')
-
- if self.flags & self.CAPTURE_STDOUT:
- # Set non blocking
- flags = fcntl.fcntl(self.pipe.stdout.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK
- fcntl.fcntl(self.pipe.stdout.fileno(), fcntl.F_SETFL, flags)
- self.out_channel = GLib.IOChannel.unix_new(self.pipe.stdout.fileno())
- self.out_channel_id = GLib.io_add_watch(self.out_channel,
- GLib.PRIORITY_DEFAULT,
- GLib.IOCondition.IN | GLib.IOCondition.HUP |
GLib.IOCondition.ERR,
- self.on_output)
+ if self.flags & self.CAPTURE_STDOUT:
+ self.out_channel, self.out_channel_id = self.add_watch(self.pipe.stdout.fileno(),
+ self.on_output)
if self.flags & self.CAPTURE_STDERR:
- # Set non blocking
- flags = fcntl.fcntl(self.pipe.stderr.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK
- fcntl.fcntl(self.pipe.stderr.fileno(), fcntl.F_SETFL, flags)
-
- self.err_channel = GLib.IOChannel.unix_new(self.pipe.stderr.fileno())
- self.err_channel_id = GLib.io_add_watch(self.err_channel,
- GLib.PRIORITY_DEFAULT,
- GLib.IOCondition.IN | GLib.IOCondition.HUP |
GLib.IOCondition.ERR,
- self.on_err_output)
+ self.err_channel, self.err_channel_id = self.add_watch(self.pipe.stderr.fileno(),
+ self.on_err_output)
# IO
if self.input_text is not None:
@@ -129,7 +117,18 @@ class Capture(GObject.Object):
self.idle_write_id = GLib.idle_add(self.idle_write_chunk)
# Wait for the process to complete
- GLib.child_watch_add(GLib.PRIORITY_DEFAULT, self.pipe.pid, self.on_child_end)
+ GLib.child_watch_add(GLib.PRIORITY_DEFAULT,
+ self.pipe.pid,
+ self.on_child_end)
+
+ def add_watch(self, fd, io_func):
+ channel = GLib.IOChannel.unix_new(fd)
+ channel.set_flags(channel.get_flags() | GLib.IOFlags.NONBLOCK)
+ channel_id = GLib.io_add_watch(channel,
+ GLib.PRIORITY_DEFAULT,
+ GLib.IOCondition.IN | GLib.IOCondition.HUP | GLib.IOCondition.ERR,
+ io_func)
+ return (channel, channel_id)
def idle_write_chunk(self):
if not self.pipe:
@@ -202,7 +201,7 @@ class Capture(GObject.Object):
return ret
- def stop(self, error_code = -1):
+ def stop(self, error_code=-1):
if self.idle_write_id:
GLib.source_remove(self.idle_write_id)
self.idle_write_id = 0
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]