muyhomepage2 / muyhomepage2 / page.py

#!/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

from muyhomepage2 import util


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')

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

    @property
    def contentcode(self):
        return util.contentcode(self.filename)

    @property
    def tags(self):
        _tags = self.headers.get('tags')
        if _tags:
            return [v.strip() for v in _tags.split(',')]
        else:
            return []


class PseduoPage(object):
    def __init__(self, filename, contentcode, title, action):
        self.filename = filename
        self.contentcode = contentcode
        self.title = title
        self.action = action
        self.tags = []
        self.headers = {}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.