|
14 | 14 | datad = {} |
15 | 15 | for root, subFolders, files in os.walk(rootdir): |
16 | 16 | for fname in files: |
17 | | - if ( fname.startswith('.') or fname.startswith('#') or |
| 17 | + if ( fname.startswith('.') or fname.startswith('#') or fname.startswith('_') or |
18 | 18 | fname.find('.svn')>=0 or not fname.endswith('.py') ): |
19 | 19 | continue |
20 | 20 |
|
21 | 21 | fullpath = os.path.join(root,fname) |
22 | 22 | contents = file(fullpath).read() |
23 | 23 | # indent |
24 | | - contents = '\n'.join([' %s'%row.rstrip() for row in contents.split('\n')]) |
25 | 24 | relpath = os.path.split(root)[-1] |
26 | 25 | datad.setdefault(relpath, []).append((fname, contents)) |
27 | 26 |
|
|
47 | 46 | """) |
48 | 47 |
|
49 | 48 | for subdir in subdirs: |
50 | | - print subdir |
51 | | - outfile = '%s.rst'%subdir |
52 | | - fh = file(outfile, 'w') |
| 49 | + if not os.path.exists(subdir): |
| 50 | + os.makedirs(subdir) |
| 51 | + |
| 52 | + static_dir = os.path.join('..', '_static', 'examples') |
| 53 | + if not os.path.exists(static_dir): |
| 54 | + os.makedirs(static_dir) |
| 55 | + |
| 56 | + static_dir = os.path.join(static_dir, subdir) |
| 57 | + if not os.path.exists(static_dir): |
| 58 | + os.makedirs(static_dir) |
| 59 | + |
| 60 | + |
| 61 | + subdirIndexFile = os.path.join(subdir, 'index.rst') |
| 62 | + fhsubdirIndex = file(subdirIndexFile, 'w') |
| 63 | + fhindex.write(' %s\n'%subdirIndexFile) |
| 64 | + |
| 65 | + |
| 66 | + fhsubdirIndex.write("""\ |
| 67 | +.. _%s-examples-index: |
| 68 | +
|
| 69 | +
|
| 70 | +############################################## |
| 71 | +%s Examples |
| 72 | +############################################## |
| 73 | +
|
| 74 | +.. htmlonly:: |
| 75 | +
|
| 76 | + :Release: |version| |
| 77 | + :Date: |today| |
| 78 | +
|
| 79 | +.. toctree:: |
| 80 | + :maxdepth: 1 |
| 81 | +
|
| 82 | +"""%(subdir, subdir)) |
53 | 83 |
|
54 | | - fhindex.write(' %s\n'%outfile) |
| 84 | + print subdir |
55 | 85 |
|
56 | | - fh.write('.. _%s-examples:\n\n'%subdir) |
57 | | - title = '%s examples'%subdir |
| 86 | + |
| 87 | + data = datad[subdir] |
| 88 | + data.sort() |
| 89 | + for fname, contents in data: |
58 | 90 |
|
59 | | - fh.write('*'*len(title) + '\n') |
60 | | - fh.write(title + '\n') |
61 | | - fh.write('*'*len(title) + '\n\n') |
| 91 | + static_file = os.path.join(static_dir, fname) |
| 92 | + fhstatic = file(static_file, 'w') |
| 93 | + fhstatic.write(contents) |
| 94 | + fhstatic.close() |
62 | 95 |
|
63 | | - for fname, contents in datad[subdir]: |
64 | | - print ' ', fname |
65 | 96 | basename, ext = os.path.splitext(fname) |
66 | | - fh.write('.. _%s-example:\n\n'%basename) |
67 | | - title = '%s example'%basename |
| 97 | + rstfile = '%s.rst'%basename |
| 98 | + outfile = os.path.join(subdir, rstfile) |
| 99 | + fhsubdirIndex.write(' %s\n'%rstfile) |
| 100 | + fh = file(outfile, 'w') |
| 101 | + fh.write('.. _%s-%s:\n\n'%(subdir, basename)) |
| 102 | + title = '%s example code: %s'%(subdir, fname) |
68 | 103 |
|
69 | 104 | fh.write(title + '\n') |
70 | 105 | fh.write('='*len(title) + '\n\n') |
71 | | - fh.write(fname + '::\n\n') |
| 106 | + |
| 107 | + |
| 108 | + print ' %s'%fname |
| 109 | + |
| 110 | + linkname = os.path.join('..', '..', '_static', 'examples', subdir, fname) |
| 111 | + fh.write('%s (`link to source <%s>`_)::\n\n'%(fname, linkname)) |
| 112 | + |
| 113 | + # indent the contents |
| 114 | + contents = '\n'.join([' %s'%row.rstrip() for row in contents.split('\n')]) |
| 115 | + |
72 | 116 | fh.write(contents) |
73 | 117 | fh.write('\n\n') |
74 | | - fh.close() |
75 | | - |
| 118 | + fh.close() |
76 | 119 |
|
| 120 | + fhsubdirIndex.close() |
77 | 121 |
|
78 | 122 | fhindex.close() |
0 commit comments