root / PyCon07 / material / pycommunity_undocumented / resttask.py

Revision 22:f65fce6a4a5c, 5.2 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""" reSTructuredText base task
21"""
22import os
23import logging
24
25from Cheetah.Template import Template
26from utils import rest2Web
27from utils import extractHeadTitle
28from utils import linkChecker
29from generator import BaseTask
30
31class TaskView(object):
32    """generates a base view"""
33    def __init__(self, templatefile, restfile, glossaryfile):
34        title, content = rest2Web(restfile)
35        content = linkChecker(content, glossaryfile)
36        self.title = title
37        self._template = Template(open(templatefile).read(),
38                                  searchList=[{'content': content,
39                                               'title': title}])
40
41    def render(self):
42        """renders the html"""
43        return self.title, str(self._template)
44    __call__ = render
45
46class TaskListView(TaskView):
47    """generates a base view for lists"""
48    def __init__(self, templatefile, filelist, title):
49        self.title = title
50        self._template = Template(open(templatefile).read(),
51                                  searchList=[{'files': filelist,
52                                                'title': title}])
53
54class RestTask(BaseTask):
55    """creates the html content"""
56    _templatefile = None
57 
58    def _getRestFiles(self, folder):
59        """lists all rest file, given a folder"""
60        files = []
61        for file_ in os.listdir(folder):
62            if file_.endswith('.txt'):
63                files.append(file_)
64        return [os.path.realpath(os.path.join(folder, file_))
65                for file_ in files]
66
67    def _getFolders(self, configuration):
68        """returns the folders"""
69        raise NotImplementedError
70
71    def _getTemplateFile(self, configuration):
72        """returns the template"""
73        raise NotImplementedError
74
75    def _getTemplateListFile(self, configuration):
76        """returns the template list"""
77        raise NotImplementedError
78
79    def _run(self, configuration):
80        """generates recipes"""
81        targets = configuration.targets.values()
82        folders = self._getFolders(configuration)
83
84        for folder in folders:
85            # not recursive
86            restfiles = self._getRestFiles(folder)
87                               
88            for target in targets:
89               
90                # making the subfolder
91                subfolder = os.path.join(target, '%ss' % self._getName())
92                if not os.path.exists(subfolder):
93                    os.mkdir(subfolder)
94
95                # creating files
96                titles = []
97                for restfile in restfiles:
98                    # generating the view
99                    view = TaskView(self._getTemplateFile(configuration),
100                                    restfile, configuration.glossary)
101                    title, result = view()
102                    titles.append(title)
103                    # calculating the file path 
104                    file_name = os.path.split(restfile)[-1].split('.')[0]
105                    path = os.path.join(subfolder, '%s.html' % file_name)
106                    path = os.path.realpath(path)
107                    logging.info('writing %s' % path)
108                    html_file = open(path, 'w')
109                    try:
110                        html_file.write(result)
111                    finally:
112                        html_file.close()
113
114                # making the root file, with the real
115                # content of the subfolder, that can
116                # contains more than what the present loop does
117                def getFileInfos(folder, file_):
118                    path = os.path.join(folder, file_)
119                    title = extractHeadTitle(path)
120                    url = '%ss/%s' % (self._getName(), file_)
121                    return title, url
122
123                restfiles = [getFileInfos(subfolder, file_)
124                             for file_ in os.listdir(subfolder)
125                             if file_.endswith('.html')]
126               
127                rootfile = os.path.join(target, '%ss.html' % self._getName())
128                view = TaskListView(self._getTemplateListFile(configuration),
129                                    restfiles, self._getName())
130                title, result = view()
131                rootfile = os.path.realpath(rootfile)
132                logging.info('writing %s' % rootfile)
133                html_file = open(rootfile, 'w')
134                try:
135                    html_file.write(result)
136                finally:
137                    html_file.close()
Note: See TracBrowser for help on using the browser.