Source

muyhomepage2 / muyhomepage2 / pagehandlers / software.py

Full commit

import re
from muyhomepage2.pagehandlers import handler
from muyhomepage2.text import wikirst

class SoftwareHandler(handler.PageHandler):
    def content(self):
        return wikirst.format(self.page.body)

    @property
    def software(self):
        return SoftwareInfo(self.page)

    def template(self):
        return 'softwarepage.html'

    def extension(self, website, config):
        return None


class SoftwareInfo(object):
    def __init__(self, page):
        self.page = page

    @property
    def version(self):
        ver = self.page.headers.get('software-version')
        if not ver:
            ver = self.page.headers.get('x-software-version')
        if not ver:
            ver = ''
        return ver

    @property
    def language(self):
        lang = self.page.headers.get('software-lang')
        if not lang:
            lang = self.page.headers.get('x-software-lang')
        if not lang:
            lang = ''
        return lang

    @property
    def downloads(self):
        dl = (self.page.headers.get('downloads') or '').strip()
        if not dl:
            return []
        return list(parse_reflist(dl))

    @property
    def dependencies(self):
        dp = (self.page.headers.get('dependencies') or '').strip()
        if not dp:
            return []
        return list(parse_reflist(dp))

    @property
    def sources(self):
        rcs = self.page.headers.get('software-rcs')
        if not rcs:
            return []
        if rcs in ('Hg', 'Mercurial'):
            return self._hgsources()
        raise ValueError('unexpected rcs name: %r' % rcs)

    def _hgsources(self):
        url = self.page.headers.get('software-rcs-url')
        if not url:
            return []
        return [
            ('Type <tt class="literal">hg clone %s</tt> at the command'
             ' prompt to get a copy of the development source code.') % url,
            '<a href="%s">Source Code Web View</a>' % url,
            ]


def parse_reflist(txt):
    for part in (d.strip() for d in txt.split(';')):
        if not part:
            continue
        tmatch = re.search(r'(.+) <(.+)>', part.replace('\n', ' '))
        if not tmatch:
            raise ValueError('invalid section: %r' % part)
        label = tmatch.group(1).strip()
        url = tmatch.group(2).strip()
        yield (url, label)