Source

muyhomepage2 / muyhomepage2 / page.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.
#
"""page data
"""

import os
import email.parser


def parse(name):
    """Parses a page file and returns a Page object.
    """
    fp = open(name)
    try:
        message = email.parser.Parser().parse(fp)
    finally:
        fp.close()
    headers = dict([(k.lower(), v) for k, v in message.items()])
    return Page(name, headers, message.get_payload())


def walkpages(root, suffix='.page'):
    """Generates a series of page filenames from the given root directory.
    """
    for dname, dirs, files in os.walk(root):
        for fn in files:
            if fn.endswith(suffix):
                yield os.path.join(dname, fn)


class Page(object):
    """A page object.
    """
    def __init__(self, filename, headers=None, body=None):
        self.filename = filename
        self.headers = headers
        self.body = body

    @property
    def title(self):
        return self.headers.get('title')