1. David Chambers
  2. Mango


Mango / utils.py

# -*- coding: utf-8 -*-

from __future__ import with_statement
from contextlib import closing
import htmlentitydefs
import logging
import os
import re
import unicodedata
from urllib import urlencode
from urllib2 import Request, urlopen
from urlparse import urlparse

from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.template import loader, RequestContext
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe

import mango
import mango.settings
from mango.decorators import baseurl
from mango.exceptions import EmptySettingError, InvalidSettingError
from mango.template import Script, StyleSheet

RE = {
    'fragment': re.compile(r'(?s)(<code>.*?</code>|<pre>.*?</pre>|<skip>.*?</skip>)'),
    'replacements': (
        # ... -> ellipsis
        (re.compile(r'(?<![.])[.]{3}(?![.])'), u'\u2026'),
        # [space][hyphen][hyphen][space] -> [thin space][em dash][thin space]
        (re.compile(r' -- '), u'\u2009\u2014\u2009'),
    'snippet': re.compile(r'(?s)^<(code|pre|skip)>.*?</\1>$'),

_formatter = logging.Formatter(
        fmt='%(asctime)s %(name)s %(levelname)-8s : %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S')

_console = logging.StreamHandler()

_logfile = logging.FileHandler(filename=os.path.join(mango.settings.MANGO_PATH, 'mango.log'), encoding='utf-8')

logger = logging.getLogger('mango')

def absolutize(path):
    Returns the absolute path of `path`. If `path` is not (already) an absolute
    path, it is assumed to be relative to the project directory.
    >>> absolutize('mango/examples') == absolutize('mango/../mango/examples/')
    path = path.rstrip(u'/')
    if os.path.isabs(path):
        return path
    fragments = []
    head, tail = os.path.split(path)
    while tail:
        fragments.insert(0, tail)
        head, tail = os.path.split(head)
    return os.path.abspath(os.path.join(mango.settings.PROJECT_PATH, *fragments))

def akismet_api_key():
    def _(): pass
    _.cache = None

    def set(value):
        _.cache = (value,)
        return value

    def wrapper():
        if _.cache is not None:
            return _.cache[0]

        if mango.settings.AKISMET_API_KEY is None:
            return set(None)

        req = Request('http://rest.akismet.com/1.1/verify-key',
                    'key': mango.settings.AKISMET_API_KEY,
                    'blog': mango.settings.BASE_URL or baseurl()}),
                headers={'User-Agent': 'Mango/%s' % mango.VERSION})

        with closing(urlopen(req)) as page:
            if page.read().strip() == 'invalid':
                if settings.DEBUG:
                    raise InvalidSettingError('Akismet rejected the supplied API key',
                            'Double-check AKISMET_API_KEY in `mango/settings/custom.py`.')
                return set(None)
                return set(mango.settings.AKISMET_API_KEY)
    return wrapper
akismet_api_key = akismet_api_key()

def akismet_request(kind, dict_):
    return Request('http://%s.rest.akismet.com/1.1/%s' % (akismet_api_key(), kind),
            data=urlencode([(k, v.encode('utf-8')) for k, v in dict_.items()]),
            headers={'User-Agent': 'Mango/%s' % mango.VERSION})

def canonicalize(value, dict_):
    >>> d = {1: 'foo', 2: ('bar', 'baz')}
    >>> canonicalize(1, d)
    >>> canonicalize('foo', d)
    >>> canonicalize('baz', d)
    >>> canonicalize(3, d)
    if value in dict_:
        return value
    for k, v in dict_.items():
        for alias in [v] if isinstance(v, basestring) else v:
            if alias == value:
                return k
    return value

def html_response(template_name, request, context=None, status_code=None):
    if context is None:
        context = {}

    # provide access to the request (without relying upon a context processor)
    context['request'] = request

    # make Mango's settings accessible to templates
    for key, value in mango.settings.__dict__.items():
        if not key.startswith('_'):
            context[key] = value

    # Django settings
    context['settings'] = settings

    # set top-level category
    index = mango.main.Index.get()

    context['archives'] = index.archives()
    context['posts'] = index.descendants()
    context['tags'] = index.tags()

    context['stylesheets'] = stylesheets()
    context['scripts'] = scripts()

    response = HttpResponse(loader.render_to_string(template_name,
            context_instance=RequestContext(request, context)))
    if status_code:
        response.status_code = status_code
    return response

def lstrip(text, char='0'):
    return text[1:] if text.startswith(char) else text

def posts_directory():
    """ Creates "posts" directory if it does not already exist. """
    except OSError:
        if not os.path.exists(mango.settings.DOCUMENTS_PATH):
            raise OSError('Unable to create %s. Check permissions.' % mango.settings.DOCUMENTS_PATH)

def primary_author_email():
    Returns the primary author's e-mail address as specified in Mango's
    settings file.
    >>> mango.settings.PRIMARY_AUTHOR_NAME = u'David Chambers'
    >>> mango.settings.PRIMARY_AUTHOR_EMAIL = u'david@mango.io'
    >>> primary_author_email()
    u'David Chambers <david@mango.io>'
    >>> mango.settings.PRIMARY_AUTHOR_NAME = None
    >>> primary_author_email()
    name = mango.settings.PRIMARY_AUTHOR_NAME
    email = mango.settings.PRIMARY_AUTHOR_EMAIL

    if email:
        return '%s <%s>' % (name, email) if name else email

    raise EmptySettingError('PRIMARY_AUTHOR_EMAIL setting is empty',
            'Add PRIMARY_AUTHOR_EMAIL to `mango/settings/custom.py`.')

def replace(html):
    if not mango.settings.REPLACEMENTS:
        return html

    def replace(text):
        for pattern, repl in RE['replacements']:
            text = re.sub(pattern, repl, text)
        return text

    return mark_safe(u''.join(
            chunk if re.match(RE['snippet'], chunk) else replace(chunk)
            for chunk in re.split(RE['fragment'], html)))

def scripts():
    def _(): pass
    _.cache = None
    def wrapper():
        if _.cache is None:
            scripts_root = prefix = mango.settings.JS[0]
            if prefix and not urlparse(prefix).scheme and not prefix.startswith('/'):
                scripts_root = '%s%s' % (reverse('mango.views.index'), prefix)
            _.cache = [Script('%s%s' % (scripts_root, f)) for f in mango.settings.JS[1:]]
        return _.cache
    return wrapper
scripts = scripts()

def slugify(text):
    Normalizes string, converts to lowercase, removes non-alpha characters
    (except full stops), and converts spaces to hyphens.
    text = unicodedata.normalize('NFKD', text).encode('ascii', 'ignore')
    text = '.'.join([re.sub(r'[^\w\s-]', '', t) for t in text.split('.')]).strip().lower()
    return mark_safe(unicode(re.sub(r'[-\s]+', '-', text)))
slugify = stringfilter(slugify)

def stylesheets():
    def _(): pass
    _.cache = None
    def wrapper():
        if _.cache is None:
            stylesheets_root = prefix = mango.settings.CSS[0]
            if prefix and not urlparse(prefix).scheme and not prefix.startswith('/'):
                stylesheets_root = '%s%s' % (reverse('mango.views.index'), prefix)
            _.cache = [StyleSheet('%s%s' % (stylesheets_root, filename), media)
                    for media, filenames in mango.settings.CSS[1:]
                    for filename in ((filenames,) if isinstance(
                    filenames, basestring) else filenames)]
        return _.cache
    return wrapper
stylesheets = stylesheets()

def text_response(text, status_code=None):
    response = HttpResponse(text, content_type='text/plain; charset=utf-8')
    if status_code:
        response.status_code = status_code
    return response

# taken from http://effbot.org/zone/re-sub.htm#unescape-html (thanks, Fredrik!)
def unescape(text):
    Removes HTML or XML character references and entities from a text string.
    >>> unescape('E = mc&#178;')
    u'E = mc\\xb2'
    >>> unescape('Jack &amp; Jill')
    u'Jack & Jill'
    def fixup(m):
        text = m.group(0)
        if text[:2] == '&#':
            # character reference
                if text[:3] == '&#x':
                    return unichr(int(text[3:-1], 16))
                    return unichr(int(text[2:-1]))
            except ValueError:
            # named entity
                text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
            except KeyError:
                pass # leave as is
        return text
    return re.sub('&#?\w+;', fixup, text)