Clean up the libtool importer a bit. Add a context so we can use it
authorJohan Dahlin <jdahlin@async.com.br>
Wed, 29 Oct 2008 13:08:44 +0000 (13:08 +0000)
committerJohan Dahlin <johan@src.gnome.org>
Wed, 29 Oct 2008 13:08:44 +0000 (13:08 +0000)
2008-10-29  Johan Dahlin  <jdahlin@async.com.br>

    * giscanner/libtoolimporter.py:
    * giscanner/sourcescanner.py:
    Clean up the libtool importer a bit. Add a context so we
    can use it through a with statement.
    Don't just look in the current directory, look in the whole
    sys.path.

svn path=/trunk/; revision=830

ChangeLog
giscanner/libtoolimporter.py
giscanner/sourcescanner.py

index b5bd9d8a7042e9ef5d5a4ebefc797a19e335add1..8dfbe43ee80499504073c8ec2cb0190e2f99c11a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2008-10-29  Johan Dahlin  <jdahlin@async.com.br>
+
+       * giscanner/libtoolimporter.py:
+       * giscanner/sourcescanner.py:
+       Clean up the libtool importer a bit. Add a context so we
+       can use it through a with statement.
+       Don't just look in the current directory, look in the whole
+       sys.path.
+
 2008-10-29  Tommi Komulainen  <tommi.komulainen@iki.fi>
 
        Bug 558065 – gitesttypes should be installed
index 772a7831a75ecbd258183a703a5ca7f17bef15cd..19112e27a3fa6b66c0d279c6cd644ec9c6b7e155 100644 (file)
@@ -25,19 +25,19 @@ import sys
 from .utils import extract_libtool
 
 
-class LibToolImporter(object):
+class LibtoolImporter(object):
 
     def __init__(self, name, path):
         self.name = name
         self.path = path
 
-    @staticmethod
-    def find_module(name, path=None):
+    @classmethod
+    def find_module(cls, name, path=None):
         modname = name.split('.')[-1]
         for part in path or sys.path:
             full = os.path.join(part, '.libs', modname + '.la')
             if os.path.exists(full):
-                return LibToolImporter(name, full)
+                return cls(name, full)
 
     def load_module(self, name):
         realpath = extract_libtool(self.path)
@@ -45,10 +45,10 @@ class LibToolImporter(object):
                               ('.so', 'rb', 3))
         return mod
 
+    @classmethod
+    def __enter__(cls):
+        sys.meta_path.append(cls)
 
-def install_libtoolimporter():
-    sys.meta_path.append(LibToolImporter)
-
-
-def uninstall_libtoolimporter():
-    sys.meta_path.remove(LibToolImporter)
+    @classmethod
+    def __exit__(cls, type, value, traceback):
+        sys.meta_path.remove(cls)
index df3692cdf3865b60e2723b01534002b75f6a15f1..519a1d9510805cfd1da0aaaf3f6a301856acd717 100644 (file)
 # 02110-1301, USA.
 #
 
+from __future__ import with_statement
 import os
 import subprocess
 import tempfile
 
-from .libtoolimporter import install_libtoolimporter, uninstall_libtoolimporter
-install_libtoolimporter()
-from . import _giscanner
-uninstall_libtoolimporter()
+from .libtoolimporter import LibtoolImporter
+
 
 (CSYMBOL_TYPE_INVALID,
  CSYMBOL_TYPE_ELLIPSIS,
@@ -186,7 +185,9 @@ class SourceSymbol(object):
 class SourceScanner(object):
 
     def __init__(self):
-        self._scanner = _giscanner.SourceScanner()
+        with LibtoolImporter:
+            from _giscanner import SourceScanner
+        self._scanner = SourceScanner()
         self._filenames = []
         self._cpp_options = []