Source

muyhomepage2 / muyhomepage2 / util.py

Full commit
#!/usr/bin/env python
#
# Copyright 2009 John Mulligan <phlogistonjohn@asynchrono.us>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2, incorporated herein by reference.
#
"""utility functions
"""

import string


def encodetitle(title):
    """Create a simplified title string.
    """
    permitted = string.letters + string.digits + ':'
    temp = []
    for ch in title.lower():
        if ch in permitted:
            temp.append(ch)
        elif temp[-1] == '-':
            pass
        else:
            temp.append('-')
    # elminate leading and trailing dashes
    while temp[0] == '-':
        temp = temp[1:]
    while temp[-1] == '-':
        temp = temp[:-1]
    return ''.join(temp)


def contentcode(filename):
    csum = md5()
    fp = open(filename)
    try:
        while True:
            buf = fp.read(8192)
            if not buf:
                break
            csum.update(buf)
    finally:
        fp.close()
    return csum.hexdigest()