root / PyCon07 / material / pycommunity_documented / index.py

Revision 57:efd3c5bd5313, 3.0 kB (checked in by Tarek Ziad?? <tarek@…>, 23 months ago)

more on recipes

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""" Index render
21"""
22import os
23import logging
24from shutil import copyfile
25
26from Cheetah.Template import Template
27
28from generator import BaseTask
29from generator import registerTask
30from utils import rest2Web
31
32class IndexView(object):
33    """renders a view using a cheetah template"""
34    def __init__(self, templatefile, options):
35        title, content = rest2Web(options['index'])
36        self._template = Template(open(templatefile).read(),
37                                  searchList=[{'options': options,
38                                               'title': title,
39                                               'content': content}])
40       
41    def render(self):
42        """renders the html"""
43        return str(self._template)
44    __call__ = render
45
46class IndexTask(BaseTask):
47    """creates the index file"""
48
49    def _getName(self):
50        """returns the task name"""
51        return 'index'
52
53    def _run(self, configuration):
54        """reads the glossary file, and generate
55        the html file"""
56        css = configuration.templates['css']
57        media_folder = configuration.media
58        targets = configuration.targets.values()
59        view = IndexView(configuration.templates['index'],
60                         configuration.options)
61        result = view()
62        for target in targets:
63            path = os.path.join(target, 'index.html')
64            path = os.path.realpath(path)
65            self._writeFile(path, result)
66           
67            # copy the css into the target directory
68            filename = os.path.split(css)[-1]
69            target_css = os.path.join(target, filename)
70            logging.info('copying css to %s' % target_css)
71            copyfile(css, target_css)
72
73            # copy the media folder
74            logging.info('copying media')
75            for file_ in os.listdir(media_folder):
76                fullpath = os.path.join(media_folder, file_)
77                if not os.path.isfile(fullpath):
78                    continue
79                target_folder = os.path.join(target, 'media')
80                targetpath = os.path.join(target_folder, file_)
81                if not os.path.exists(target_folder):
82                    os.mkdir(target_folder)
83                copyfile(fullpath, targetpath)
84         
85registerTask(IndexTask)
Note: See TracBrowser for help on using the browser.