root / PyCon07 / material / pycommunity_undocumented / glossary.py

Revision 22:f65fce6a4a5c, 3.1 kB (checked in by Tarek Ziad?? <tarek@…>, 21 months ago)

added pycommunity for documentation

Line 
1#!/usr/bin/python
2# -*- coding: UTF-8 -*-
3#
4# Copyright (c) 2006 Tarek Ziadé <tarek@ziade.org>
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License
8# as published by the Free Software Foundation; either version 2
9# of the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19# $Id: $
20""" Glossary reader and render
21"""
22import os
23import logging
24
25from Cheetah.Template import Template
26from generator import BaseTask
27from generator import registerTask
28
29class GlossaryReader(list):
30    """reads glossary entries"""
31    def __init__(self, filename):
32        def _clean(element):
33            element = element.lower()
34            return element.strip()
35        self.filename = filename
36        words = []
37        for line in open(filename):
38            line = line.strip()
39            if line.startswith('#'):
40                continue
41            elements = line.split(':')
42            if len(elements) != 2:
43                continue
44            word = _clean(elements[0])
45            if word in words:
46                continue
47            self.append((word, _clean(elements[1])))
48            words.append(word)
49
50    def getDefinition(self, word):
51        """returns a definition"""
52        word = word.lower()
53        for word_, definition in self:
54            if word_ == word:
55                return definition
56        raise KeyError('%s not found' % word)
57
58class GlossaryView(object):
59    """renders a view using a cheetah template"""
60    def __init__(self, templatefile, glossary):
61         self._glossary = glossary
62         self._template = Template(open(templatefile).read(),
63                                   searchList=[{'glossary': glossary}])
64       
65    def render(self):
66        """renders the html"""
67        return str(self._template)
68    __call__ = render
69
70class GlossaryTask(BaseTask):
71    """creates the glossary file"""
72
73    def _getName(self):
74        """returns the task name"""
75        return 'glossary'
76
77    def _run(self, configuration):
78        """reads the glossary file, and generate
79        the html file"""
80        targets = configuration.targets.values()
81        glossary = GlossaryReader(configuration.glossary)
82        # XXX see if we want to externalize this path
83        view = GlossaryView(configuration.templates['glossary'], glossary)
84        result = view()
85        for target in targets:
86            path = os.path.join(target, 'glossary.html')
87            path = os.path.realpath(path)
88            logging.info('writing %s' % path)
89            glossary_file = open(path, 'w')
90            try:
91                glossary_file.write(result)
92            finally:
93                glossary_file.close()
94
95registerTask(GlossaryTask)
Note: See TracBrowser for help on using the browser.