home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / externaltools / capture.py < prev    next >
Encoding:
Python Source  |  2006-08-27  |  3.6 KB  |  129 lines

  1. # -*- coding: utf-8 -*-
  2. #    Gedit External Tools plugin
  3. #    Copyright (C) 2005-2006  Steve Fr├⌐cinaux <steve@istique.net>
  4. #
  5. #    This program is free software; you can redistribute it and/or modify
  6. #    it under the terms of the GNU General Public License as published by
  7. #    the Free Software Foundation; either version 2 of the License, or
  8. #    (at your option) any later version.
  9. #
  10. #    This program is distributed in the hope that it will be useful,
  11. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #    GNU General Public License for more details.
  14. #
  15. #    You should have received a copy of the GNU General Public License
  16. #    along with this program; if not, write to the Free Software
  17. #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  18.  
  19. __all__ = ('Capture', )
  20.  
  21. import os, sys, signal
  22. import locale
  23. import subprocess
  24. import gobject
  25.  
  26. class Capture(gobject.GObject):
  27.     CAPTURE_STDOUT = 0x01
  28.     CAPTURE_STDERR = 0x02
  29.     CAPTURE_BOTH   = 0x03
  30.  
  31.     __gsignals__ = {
  32.         'stdout-line'  : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,)),
  33.         'stderr-line'  : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,)),
  34.         'begin-execute': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, tuple()),
  35.         'end-execute'  : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT,))
  36.     }
  37.  
  38.     def __init__(self, command, cwd = None, env = {}):
  39.         gobject.GObject.__init__(self)
  40.         self.pipe = None
  41.         self.env = env
  42.         self.cwd = cwd
  43.         self.flags = self.CAPTURE_BOTH
  44.         self.command = command
  45.         self.input_text = None
  46.     
  47.     def set_env(self, **values):
  48.         self.env.update(**values)
  49.     
  50.     def set_command(self, command):
  51.         self.command = command
  52.     
  53.     def set_flags(self, flags):
  54.         self.flags = flags
  55.     
  56.     def set_input(self, text):
  57.         self.input_text = text
  58.     
  59.     def set_cwd(self, cwd):
  60.         self.cwd = cwd
  61.     
  62.     def execute(self):
  63.         if self.command is None:
  64.             return
  65.         
  66.         # Initialize pipe
  67.         popen_args = {
  68.             'cwd'  : self.cwd,
  69.             'shell': True,
  70.             'env'  : self.env
  71.         }
  72.         
  73.         if self.input_text is not None:
  74.             popen_args['stdin'] = subprocess.PIPE
  75.         if self.flags & self.CAPTURE_STDOUT:
  76.             popen_args['stdout'] = subprocess.PIPE
  77.         if self.flags & self.CAPTURE_STDERR:
  78.             popen_args['stderr'] = subprocess.PIPE
  79.         
  80.         self.pipe = subprocess.Popen(self.command, **popen_args)
  81.         
  82.         # Signal
  83.         self.emit('begin-execute')
  84.  
  85.         # IO
  86.         if self.input_text is not None:
  87.             self.pipe.stdin.write(self.input_text)
  88.             self.pipe.stdin.close()
  89.         if self.flags & self.CAPTURE_STDOUT:
  90.             gobject.io_add_watch(self.pipe.stdout,
  91.                                  gobject.IO_IN | gobject.IO_HUP,
  92.                                  self.on_output)
  93.         if self.flags & self.CAPTURE_STDERR:
  94.             gobject.io_add_watch(self.pipe.stderr,
  95.                                  gobject.IO_IN | gobject.IO_HUP,
  96.                                  self.on_output)
  97.  
  98.         # Wait for the process to complete
  99.         gobject.child_watch_add(self.pipe.pid, self.on_child_end)
  100.         
  101.     def on_output(self, source, condition):
  102.         line = source.readline()
  103.  
  104.         if len(line) > 0:
  105.             try:
  106.                 line = unicode(line, 'utf-8')
  107.             except:
  108.                 line = unicode(line, 
  109.                                locale.getdefaultlocale()[1],
  110.                                'replace')
  111.  
  112.             if source == self.pipe.stdout:
  113.                 self.emit('stdout-line', line)
  114.             else:
  115.                 self.emit('stderr-line', line)
  116.             return True
  117.  
  118.         return False
  119.  
  120.     def stop(self, error_code = -1):
  121.         if self.pipe is not None:
  122.             os.kill(self.pipe.pid, signal.SIGTERM)
  123.             self.pipe = None
  124.  
  125.     def on_child_end(self, pid, error_code):
  126.         # In an idle, so it is emitted after all the std*-line signals
  127.         # have been intercepted
  128.         gobject.idle_add(self.emit, 'end-execute', error_code)
  129.