~ubuntu-branches/ubuntu/trusty/python-networkx/trusty-proposed

« back to all changes in this revision

Viewing changes to doc/make_examples_rst.py

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2009-02-28 13:36:24 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090228133624-9l5rwi1ftlzc7b0l
* Upload to unstable now that lenny is released (yay).
* Fix FTBFS with python-support 0.90.3: no longer rely on its internal
  behaviour, and xnow set tests/test.py executable right after “setup.py
  install” (Closes: #517065).
* Drop executable bits from bz2 files.
* Update Vcs-* fields: move from DPMT's svn to collab-maint's git.
* Remote DPMT from Uploaders, following Piotr Ożarowski's request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
"""
 
3
generate the rst files for the examples by iterating over the pylab examples
 
4
"""
 
5
# This code was developed from the Matplotlib gen_rst.py module
 
6
# and is distributed with the same license as Matplotlib
 
7
import os, glob
 
8
 
 
9
import os
 
10
import re
 
11
import sys
 
12
#fileList = []
 
13
#rootdir = '../../examples'
 
14
 
 
15
 
 
16
 
 
17
def out_of_date(original, derived):
 
18
    """
 
19
    Returns True if derivative is out-of-date wrt original,
 
20
    both of which are full file paths.
 
21
 
 
22
    TODO: this check isn't adequate in some cases.  Eg, if we discover
 
23
    a bug when building the examples, the original and derived
 
24
    will be unchanged but we still want to fource a rebuild.  We can
 
25
    manually remove from _static, but we may need another solution
 
26
    """
 
27
    return (not os.path.exists(derived) or
 
28
            os.stat(derived).st_mtime < os.stat(original).st_mtime)
 
29
 
 
30
def main(exampledir,sourcedir):    
 
31
 
 
32
    noplot_regex = re.compile(r"#\s*-\*-\s*noplot\s*-\*-")
 
33
 
 
34
    datad = {}
 
35
    for root, subFolders, files in os.walk(exampledir):
 
36
        for fname in files:
 
37
            if ( fname.startswith('.') or fname.startswith('#') or fname.startswith('_') or
 
38
                 fname.find('.svn')>=0 or not fname.endswith('.py') ):
 
39
                continue
 
40
 
 
41
            fullpath = os.path.join(root,fname)
 
42
            contents = file(fullpath).read()
 
43
            # indent
 
44
            relpath = os.path.split(root)[-1]
 
45
            datad.setdefault(relpath, []).append((fullpath, fname, contents))
 
46
 
 
47
    subdirs = datad.keys()
 
48
    subdirs.sort()
 
49
    output_dir=os.path.join(sourcedir,'examples')
 
50
    if not os.path.exists(output_dir):
 
51
            os.makedirs(output_dir)
 
52
    fhindex = file(os.path.join(sourcedir,'examples','index.rst'), 'w')
 
53
    fhindex.write("""\
 
54
.. _examples-index:
 
55
 
 
56
*****************
 
57
NetworkX Examples
 
58
*****************
 
59
 
 
60
.. htmlonly::
 
61
 
 
62
    :Release: |version|
 
63
    :Date: |today|
 
64
 
 
65
.. toctree::
 
66
    :maxdepth: 2
 
67
 
 
68
""")
 
69
 
 
70
    for subdir in subdirs:
 
71
        output_dir= os.path.join(sourcedir,'examples',subdir)
 
72
        if not os.path.exists(output_dir):
 
73
            os.makedirs(output_dir)
 
74
 
 
75
        static_dir = os.path.join(sourcedir, 'static', 'examples')
 
76
        if not os.path.exists(static_dir):
 
77
            os.makedirs(static_dir)
 
78
 
 
79
 
 
80
        subdirIndexFile = os.path.join(subdir, 'index.rst')
 
81
        fhsubdirIndex = file(os.path.join(output_dir,'index.rst'), 'w')
 
82
        fhindex.write('    %s\n\n'%subdirIndexFile)
 
83
        #thumbdir = '../_static/plot_directive/mpl_examples/%s/thumbnails/'%subdir
 
84
        #for thumbname in glob.glob(os.path.join(thumbdir,'*.png')):
 
85
        #    fhindex.write('    %s\n'%thumbname)
 
86
 
 
87
        fhsubdirIndex.write("""\
 
88
.. _%s-examples-index:
 
89
 
 
90
 
 
91
##############################################
 
92
%s
 
93
##############################################
 
94
 
 
95
.. htmlonly::
 
96
 
 
97
    :Release: |version|
 
98
    :Date: |today|
 
99
 
 
100
.. toctree::
 
101
    :maxdepth: 1
 
102
 
 
103
"""%(subdir, subdir.title()))
 
104
 
 
105
 
 
106
        data = datad[subdir]
 
107
        data.sort()
 
108
 
 
109
        #parts = os.path.split(static_dir)
 
110
        #thumb_dir = ('../'*(len(parts)-1)) + os.path.join(static_dir, 'thumbnails')
 
111
 
 
112
        for fullpath, fname, contents in data:
 
113
            basename, ext = os.path.splitext(fname)
 
114
            static_file = os.path.join(static_dir, fname)
 
115
            #thumbfile = os.path.join(thumb_dir, '%s.png'%basename)
 
116
            #print '    static_dir=%s, basename=%s, fullpath=%s, fname=%s, thumb_dir=%s, thumbfile=%s'%(static_dir, basename, fullpath, fname, thumb_dir, thumbfile)
 
117
 
 
118
            rstfile = '%s.rst'%basename
 
119
            outfile = os.path.join(output_dir, rstfile)
 
120
 
 
121
            fhsubdirIndex.write('    %s\n'%rstfile)
 
122
 
 
123
            if (not out_of_date(fullpath, static_file) and
 
124
                not out_of_date(fullpath, outfile)):
 
125
                continue
 
126
 
 
127
            print '%s/%s'%(subdir,fname)
 
128
 
 
129
            fhstatic = file(static_file, 'w')
 
130
            fhstatic.write(contents)
 
131
            fhstatic.close()
 
132
 
 
133
            fh = file(outfile, 'w')
 
134
            fh.write('.. _%s-%s:\n\n'%(subdir, basename))
 
135
            base=fname.partition('.')[0]
 
136
            title = '%s'%(base.replace('_',' ').title())
 
137
 
 
138
 
 
139
            #title = '<img src=%s> %s example code: %s'%(thumbfile, subdir, fname)
 
140
 
 
141
 
 
142
            fh.write(title + '\n')
 
143
            fh.write('='*len(title) + '\n\n')
 
144
 
 
145
            pngname=base+".png"
 
146
            png=os.path.join(static_dir,pngname)
 
147
            linkname = os.path.join('..', '..', 'static', 'examples')
 
148
            if os.path.exists(png):
 
149
                fh.write('.. image:: %s \n\n'%os.path.join(linkname,pngname))
 
150
            linkname = os.path.join('..', '..', '_static', 'examples')
 
151
            fh.write("[`source code <%s>`_]\n\n::\n\n" % os.path.join(linkname,fname))
 
152
 
 
153
            # indent the contents
 
154
            contents = '\n'.join(['    %s'%row.rstrip() for row in contents.split('\n')])
 
155
            fh.write(contents)
 
156
 
 
157
    #        fh.write('\n\nKeywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)')
 
158
            fh.close()
 
159
 
 
160
        fhsubdirIndex.close()
 
161
 
 
162
    fhindex.close()
 
163
 
 
164
if __name__ == '__main__':
 
165
    import sys
 
166
    main(sys.argv[1],sys.argv[2])