Commits

J.A. Roberts Tunney committed bec8c46

better pil checking

Comments (0)

Files changed (3)

fabulous/image.py

 import itertools
 import textwrap
 
-try:
-    from PIL import Image as Pills
-except ImportError:
-    raise ImportError(textwrap.dedent("""
-        I'm sorry, I can't render images without PIL :'(
-
-        Ubuntu Users: sudo apt-get install python-imaging
-
-        Windows Users: The PIL people should have something easy to
-          install that you can download from their website.
-
-        Everyone Else: This is like the hardest library in the world
-          to manually install.  If your package manager doesn't have
-          it, you can try running ``sudo easy_install pil`` once you
-          get your hands on a C compiler the development headers for
-          ``python``, ``libz``, ``libjpeg``, ``libgif``, ``libpng``,
-          ``libungif4``, ``libfreetype6``, and maybe more >_>
-        """]))
 from grapefruit import Color
 
 from fabulous import utils, xterm256
     pad = ' '
 
     def __init__(self, path, width=None):
+        pil_check()
+        from PIL import Image as Pills
         self.img = Pills.open(path)
         self.resize(width)
 
 
 import os
+
 import grapefruit
-# let image module do its pil check thing first
+
 from fabulous import image, color
-from PIL import Image, ImageFont, ImageDraw
 
 
 DEFAULT_FONT = os.path.join(os.path.dirname(__file__), 'fonts', 'IndUni-H-Bold.otf')
 class Text(image.Image):
     def __init__(self, text, fsize=20, color="#0099ff", font=DEFAULT_FONT,
                  shadow=False, scew=None):
+        pil_check()
+        from PIL import Image, ImageFont, ImageDraw
         self.text = text
         self.color = grapefruit.Color.NewFromHtml(color)
         self.font = ImageFont.truetype(font, fsize)

fabulous/utils.py

 
 term = TerminalInfo()
 term.bgcolor = 'black'
+
+
+def pil_check():
+    """Check for PIL with friendly error message
+
+    We check for PIL at runtime because it'd be a far greater evil to
+    put it in the setup_requires list.
+    """
+    try:
+        import PIL
+    except ImportError:
+        raise ImportError(textwrap.dedent("""
+            I'm sorry, I can't render images without PIL :'(
+
+            Ubuntu Users: sudo apt-get install python-imaging
+
+            Windows Users: The PIL people should have something easy to
+              install that you can download from their website.
+
+            Everyone Else: This is like the hardest library in the world
+              to manually install.  If your package manager doesn't have
+              it, you can try running ``sudo easy_install pil`` once you
+              get your hands on a C compiler the development headers for
+              ``python``, ``libz``, ``libjpeg``, ``libgif``, ``libpng``,
+              ``libungif4``, ``libfreetype6``, and maybe more >_>
+            """]))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.