Source

muyhomepage2 / muyhomepage2 / site.py

Full commit

import os
from muyhomepage2 import util
from muyhomepage2 import page
from muyhomepage2 import metadata

class Site(object):
    def __init__(self):
        self.statemap = {}
        self.pages = set()

    def getpagefile(self, filename):
        for page in self.pages:
            if page.filename == filename:
                return page
        raise KeyError(filename)

    @property
    def tagmap(self):
        tmap = {}
        for page in self.pages:
            for tag in page.tags:
                tmap.setdefault(tag, []).append(page)
        return tmap

    def taglink(self, tag):
        for page in self.pages:
            if page.title == ('Tag:%s' % tag):
                return './%s.html' % util.encodetitle(page.title)
        return None



def write_site_pages(wsite, filename):
    pdata = ((p.filename, p.contentcode, p.title, p.action)
            for p in wsite.pages)
    fp = open(filename, 'w')
    try:
        metadata.writepagecache(pdata, fp)
    finally:
        fp.close()


def write_site_tags(wsite, filename):
    tdata = ((p.contentcode, p.tags) for p in wsite.pages)
    fp = open(filename, 'w')
    try:
        metadata.writetagcache(tdata, fp)
    finally:
        fp.close()


def read_site_pages(filename):
    pages = set()
    if not os.path.isfile(filename):
        return pages
    fp = open(filename)
    try:
        for fn, ccode, title, action in metadata.readpagecache(fp):
            pages.add(page.PseduoPage(fn, ccode, title, action))
    finally:
        fp.close()
    return pages


def read_site_tags(filename):
    tagmap = {}
    if not os.path.isfile(filename):
        return tagmap
    fp = open(filename)
    try:
        for ccode, tags in metadata.readtagcache(fp):
            tagmap[ccode] = tags
    finally:
        fp.close()
    return tagmap


def update_site_tags(wsite, filename):
    tagmap = read_site_tags(filename)
    for page in wsite.pages:
        page.tags = tagmap.get(page.contentcode, [])