~ubuntu-branches/debian/sid/pydoctor/sid

« back to all changes in this revision

Viewing changes to pydoctor/templatewriter/writer.py

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij
  • Date: 2013-09-15 13:58:49 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130915135849-g206zbvqwvz0ptdg
Tags: 0.5b1+bzr603-1
* New upstream snapshot.
* Switch from cdbs to dh.
* Bump standards version to 3.9.4 (no changes).
* Update Vcs header.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Badly named module that contains the driving code for the rendering."""
 
2
 
 
3
from pydoctor.templatewriter.util import link, templatefile
 
4
from pydoctor.templatewriter import DOCTYPE, pages, summary
 
5
from pydoctor import model
 
6
 
 
7
from twisted.web.template import flattenString
 
8
 
 
9
import os, shutil
 
10
 
 
11
 
 
12
def flattenToFile(fobj, page):
 
13
    fobj.write(DOCTYPE)
 
14
    err = []
 
15
    def e(r):
 
16
        err.append(r.value)
 
17
    flattenString(None, page).addCallback(fobj.write).addErrback(e)
 
18
    if err:
 
19
        raise err[0]
 
20
 
 
21
 
 
22
class TemplateWriter:
 
23
    def __init__(self, filebase):
 
24
        self.base = filebase
 
25
        self.written_pages = 0
 
26
        self.total_pages = 0
 
27
        self.dry_run = False
 
28
 
 
29
    def prepOutputDirectory(self):
 
30
        if not os.path.exists(self.base):
 
31
            os.mkdir(self.base)
 
32
        shutil.copyfile(templatefile('apidocs.css'),
 
33
                        os.path.join(self.base, 'apidocs.css'))
 
34
        if self.system.options.htmlusesorttable:
 
35
            shutil.copyfile(templatefile('sorttable.js'),
 
36
                            os.path.join(self.base, 'sorttable.js'))
 
37
        if self.system.options.htmlusesplitlinks or self.system.options.htmlshortenlists:
 
38
            shutil.copyfile(templatefile('pydoctor.js'),
 
39
                            os.path.join(self.base, 'pydoctor.js'))
 
40
 
 
41
    def writeIndividualFiles(self, obs, functionpages=False):
 
42
        self.dry_run = True
 
43
        for ob in obs:
 
44
            self.writeDocsFor(ob, functionpages=functionpages)
 
45
        self.dry_run = False
 
46
        for ob in obs:
 
47
            self.writeDocsFor(ob, functionpages=functionpages)
 
48
 
 
49
    def writeModuleIndex(self, system):
 
50
        import time
 
51
        for i, pclass in enumerate(summary.summarypages):
 
52
            system.msg('html', 'starting ' + pclass.__name__ + ' ...', nonl=True)
 
53
            T = time.time()
 
54
            page = pclass(system)
 
55
            f = open(os.path.join(self.base, pclass.filename), 'w')
 
56
            flattenToFile(f, page)
 
57
            f.close()
 
58
            system.msg('html', "took %fs"%(time.time() - T), wantsnl=False)
 
59
 
 
60
    def writeDocsFor(self, ob, functionpages):
 
61
        if not ob.isVisible:
 
62
            return
 
63
        isfunc = ob.documentation_location == model.DocLocation.PARENT_PAGE
 
64
        if (isfunc and functionpages) or not isfunc:
 
65
            if self.dry_run:
 
66
                self.total_pages += 1
 
67
            else:
 
68
                f = open(os.path.join(self.base, link(ob)), 'w')
 
69
                self.writeDocsForOne(ob, f)
 
70
                f.close()
 
71
        for o in ob.orderedcontents:
 
72
            self.writeDocsFor(o, functionpages)
 
73
 
 
74
    def writeDocsForOne(self, ob, fobj):
 
75
        if not ob.isVisible:
 
76
            return
 
77
        # brrrrrrrr!
 
78
        d = pages.__dict__
 
79
        for c in ob.__class__.__mro__:
 
80
            n = c.__name__ + 'Page'
 
81
            if n in d:
 
82
                pclass = d[n]
 
83
                break
 
84
        else:
 
85
            pclass = pages.CommonPage
 
86
        self.system.msg('html', str(ob), thresh=1)
 
87
        page = pclass(ob)
 
88
        self.written_pages += 1
 
89
        self.system.progress('html', self.written_pages, self.total_pages, 'pages written')
 
90
        flattenToFile(fobj, page)