Commits

Christian Tismer  committed cf544cd

version 0.6 with removed debug/log stuff from import_mapper and simplifications

  • Participants
  • Parent commits 1ade1e4
  • Tags v0.6

Comments (0)

Files changed (4)

 
 setup(
     name = "tiffany",
-    version = "0.5",
+    version = "0.6",
     author = "Christian Tismer",
     author_email = "tismer@stackless.com",
     description = ("Tiffany -- read/write/arrange any multi-page Tiff, any compression"),

File tiffany/__init__.py

 __file__ = os.path.realpath(__file__)
 
 __author__ = "Christian Tismer <tismer@stackless.com>"
-__version__ = "0.5"
+__version__ = "0.6"
 
 def start_mapper():
     from tiffany.import_mapper import install_mapper, mapped_dir

File tiffany/compat.py

 """
 compat.py should be imported first.
-It does certain patches and modifications for Python3.
+It does certain patches and modifications for Python3 backward compatibility.
 """
 
 import sys

File tiffany/import_mapper.py

 import os
 import types
 
-if sys.version_info[0] < 3:
-    import __builtin__ as builtins
-else:
-    import builtins
-
-rootpath = os.path.dirname(os.path.dirname(__file__))
-
-package_dir = os.path.join(rootpath, 'tiffany')
-package_dir = os.path.normpath(package_dir)
-mapped_dir = os.path.join(package_dir, 'from-pil-py3')
-
-mapped_modules = []
-for name in os.listdir(mapped_dir):
-    if name.endswith('.py'):
-        mapped_modules.append(name[:-3])
+from os.path import normpath, join, dirname
+from tiffany import compat  # side effect: defines builtins
+import builtins
 
 package_prefix = 'tiffany'
-
+mapped_dir = normpath(join(dirname(__file__), 'from-pil-py3'))
+mapped_modules = tuple(name[:-3] for name in os.listdir(mapped_dir)
+                       if name.endswith('.py'))
 orig__import = __import__
-DEBUG = False
 
 
 class MyImporter(object):
-    def __init__(self):
-        self.indent = 0
-
-    def prot(self, *args):
-        if DEBUG:
-            ind = '  ' * self.indent
-            name, gl, lc, fl, lv = self.args
-            msg = ind + '__import__(' + repr(name)
-            if fl:
-                msg += ', fromlist=' + repr(fl)
-            if lv != -1:
-                msg += ', level=' + repr(lv)
-            msg += ')'
-            print(msg + ' ' + ' '.join(map(str, args)))
 
     def __call__(self, name, gl=None, lc=None, fl=None, lv=-1):
         self.args = (name, gl, lc, fl, lv)
-        prot = self.prot
         context_path = os.path.dirname(sys._getframe(1).f_code.co_filename)
         if context_path != mapped_dir:
-            prot('not my context')
             return orig__import(name, gl, lc, fl, lv)
         if name == '':
             # undoing the relative imports from the python3 patch
         origname = name
         mapped = False
         if name in ('ImageColor', 'ImagePalette'):
-            prot('faked, not really needed for tiffany')
             truename = package_prefix + '.' + name
             sys.modules[truename] = types.ModuleType(name)
             if is_from:
                 retname = truename
             return sys.modules[retname]
         if name == 'FixTk':
-            prot('rejected')
             raise ImportError('hey these imports are weird! (ctypes?!)')
         if name == '_imaging':
-            prot('rejected')
             raise ImportError('we don\'t want no binaries')
         if name.endswith('ImagePlugin') and name not in wanted_plugins:
-            prot('rejected')
-            raise ImportError('rejected')
-        try:
-            indent = self.indent
-            if name in mapped_modules:
-                # turn 'import Image' into 'from x.y import Image'
-                fl = fl or []
-                fl.append(name)
-                truename = package_prefix + '.' + name
-                self.args = (package_prefix, gl, lc, fl, lv)
-                prot('was "import %s"' % name)
-                self.indent += 1
-                orig__import(package_prefix, gl, lc, fl, lv)
-                retname = package_prefix if is_from else truename
-                ret = sys.modules[retname]
-            else:
-                prot('normal')
-                self.indent += 1
-                ret = orig__import(name, gl, lc, fl, lv)
-
-        finally:
-            self.indent = indent
+            raise ImportError('rejected unwanted plugin')
+        if name in mapped_modules:
+            # turn 'import Image' into 'from x.y import Image'
+            fl = fl or []
+            fl.append(name)
+            truename = package_prefix + '.' + name
+            self.args = (package_prefix, gl, lc, fl, lv)
+            orig__import(package_prefix, gl, lc, fl, lv)
+            retname = package_prefix if is_from else truename
+            ret = sys.modules[retname]
+        else:
+            ret = orig__import(name, gl, lc, fl, lv)
         return ret
 
 wanted_plugins = ['TiffImagePlugin']
 
 def uninstall_mapper():
     builtins.__import__ = orig__import
-
-if __name__ == '__main__':
-
-    DEBUG = True
-    modules_before = set(sys.modules)
-
-    sys.path.insert(0, rootpath)
-    install_mapper()
-    from tiffany import Image, TiffImagePlugin
-    print('--- now using open ---')
-
-    imfile = os.path.join(package_dir, 'test/data/recipe_1.tiff')
-    im = Image.open(imfile)
-    sys.path.remove(rootpath)
-
-    import pprint
-    modules_added = [_name for _name in sys.modules.keys()
-                     if _name not in modules_before]
-    pprint.pprint(sorted(modules_added))