Make translitcodec optional, and work nicely without it.
authorChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 7 Jan 2013 03:41:08 +0000 (21:41 -0600)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 7 Jan 2013 03:41:08 +0000 (21:41 -0600)
mediagoblin/tools/url.py
setup.py

index de16536ca4290d616831c73ba6a1c289009e3738..8604ad5f377a1113f58bf90a41946c51265877cd 100644 (file)
 
 import re
 # This import *is* used; see word.encode('tranlit/long') below.
-import translitcodec
+from unicodedata import normalize
+
+try:
+    import translitcodec
+    USING_TRANSLITCODEC = True
+except ImportError:
+    USING_TRANSLITCODEC = False
 
 
 _punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
@@ -28,8 +34,11 @@ def slugify(text, delim=u'-'):
     """
     result = []
     for word in _punct_re.split(text.lower()):
-        # Uses translitcodec!
-        word = word.encode('translit/long')
+        if USING_TRANSLITCODEC:
+            word = word.encode('translit/long')
+        else:
+            word = normalize('NFKD', word).encode('ascii', 'ignore')
+
         if word:
             result.append(word)
     return unicode(delim.join(result))
index dc4bd5e636af4d6e5959c707ce81d199af384457..12284f26a302de60a1ea500a1c0331d1e640ddda 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -53,13 +53,14 @@ setup(
         'jinja2',
         'sphinx',
         'Babel',
-        'translitcodec',
         'argparse',
         'webtest',
         'ConfigObj',
         'Markdown',
         'sqlalchemy>=0.7.0',
         'sqlalchemy-migrate',
+        ## This is optional!
+        # 'translitcodec',
         ## For now we're expecting that users will install this from
         ## their package managers.
         # 'lxml',