Permalink
Browse files

Optional dependencies

  • Loading branch information...
1 parent 87c6f94 commit dd12c6cc22fcbb72e7d47b4df090de563bd22930 @shazow committed Oct 25, 2011
Showing with 48 additions and 3 deletions.
  1. +1 −1 MANIFEST.in
  2. +24 −2 composer/filters.py
  3. +9 −0 optional.txt
  4. +14 −0 setup.py
View
@@ -1 +1 @@
-include README.rst CHANGES.rst LICENSE.txt
+include README.rst CHANGES.rst LICENSE.txt optional.txt
View
@@ -6,6 +6,21 @@
import os
+try:
+ import markdown2
+except ImportError:
+ markdown2 = False
+
+try:
+ import mako.lookup
+ import mako.template
+except ImportError:
+ mako = False
+
+
+__all__ = ['Filter', 'Markdown', 'Mako', 'MakoContainer']
+
+
class Filter(object):
def __init__(self, index):
self.index = index
@@ -23,9 +38,10 @@ def __call__(self, content, route=None):
class Markdown(Filter):
def __init__(self, index, **markdown_kw):
- super(Markdown, self).__init__(index)
+ if not markdown2:
+ raise ImportError("Markdown filter requires the 'markdown2' package to be installed.")
- import markdown2
+ super(Markdown, self).__init__(index)
self.markdowner = markdown2.Markdown(**markdown_kw).convert
def __call__(self, content, route=None):
@@ -34,6 +50,9 @@ def __call__(self, content, route=None):
class Mako(Filter):
def __init__(self, index, **template_kw):
+ if not mako:
+ raise ImportError("Mako filter requires the 'Mako' package to be installed.")
+
super(Mako, self).__init__(index)
from mako.lookup import TemplateLookup
@@ -58,6 +77,9 @@ class MakoContainer(Mako):
pipes the ``content`` into the ``body`` context variable.
"""
def __init__(self, index, template, **lookup_kw):
+ if not mako:
+ raise ImportError("MakoContainer filter requires the 'Mako' package to be installed.")
+
super(MakoContainer, self).__init__(index, **lookup_kw)
template = os.path.relpath(self.index.absolute_path(template), '.')
View
@@ -0,0 +1,9 @@
+# Composer includes optional filters which have the following dependencies:
+#
+# Dependency: Filters:
+
+ markdown2 # composer.filters.Markdown
+ Mako # composer.filters.Mako, composer.filters.MakoContainer
+
+# You'll need to install the dependencies manually if you plan on using these
+# filters, else an ImportError exception will be raised during instantiation.
View
@@ -47,3 +47,17 @@
composer = composer.command:main
"""
)
+
+
+print """
+---
+
+%s
+
+---
+
+You can install all the optional dependencies with: ::
+
+ pip install -r optional.txt
+
+""" % open('optional.txt').read().strip()

0 comments on commit dd12c6c

Please sign in to comment.