Debian Codegraph @

python/doc/source/rstprocess.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2017 Garth N. Wells
3 #
4 # This file is part of DOLFIN.
5 #
6 # DOLFIN is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # DOLFIN 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 Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
18
19 import sys
20 import os
21 import shutil
22
23 sys.path.append('../../../utils/pylit/')
24 try:
25 import pylit
26 except ImportError:
27 raise ImportError("Unable to import pylit module")
28
29
30 def process():
31 """Copy demo rst files (C++ and Python) from the DOLFIN source tree
32 into the demo source tree, and process file with pylit
33
34 """
35
36 # Check that we can find pylint.py for converting foo.py.rst to
37 # foo.py
38 pylit_parser = "../../../utils/pylit/pylit.py"
39 if os.path.isfile(pylit_parser):
40 pass
41 else:
42 raise RuntimeError("Cannot find pylit.py")
43
44 # Directories to scan
45 subdirs = ["../../demo/documented"]
46
47 # Iterate over subdirectories containing demos
48 for subdir in subdirs:
49
50 # Get list of demos (demo name , subdirectory)
51 demos = [(dI, os.path.join(subdir, dI)) for dI in os.listdir(subdir) if os.path.isdir(os.path.join(subdir, dI))]
52
53 # Iterate over demos
54 for demo, path in demos:
55
56 # Make demo doc directory
57 demo_dir = os.path.join('./demos/', demo)
58 if not os.path.exists(demo_dir):
59 os.makedirs(demo_dir)
60
61 #for f in rst_files_common:
62 # shutil.copy(os.path.join(path, f), demo_dir)
63
64 # Build list of rst and png files in demo source directory
65 rst_files = [f for f in os.listdir(path) if os.path.splitext(f)[1] == ".rst" ]
66 other_files = [f for f in os.listdir(path) if os.path.splitext(f)[1] in (".png", ".gif", ".py", ".gz")]
67
68 # Create directory in documentation tree for demo
69 demo_dir = os.path.join('./demos/', demo)
70 if not os.path.exists(demo_dir):
71 os.makedirs(demo_dir)
72
73 # Copy .png and .py files into documentation demo directory
74 for f in other_files:
75 shutil.copy(os.path.join(path, f), demo_dir)
76
77 # Copy rst files into documentation demo directory
78 for f in rst_files:
79 shutil.copy(os.path.join(path, f), demo_dir)
80
81 # Copy rst files into documentation demo directory and
82 # process with Pylit
83 for f in rst_files:
84 shutil.copy(os.path.join(path, f), demo_dir)
85
86 # Run pylit on py.rst files (files with 'double
87 # extensions')
88 if os.path.splitext(os.path.splitext(f)[0])[1] in (".py"):
89 rst_file = os.path.join(demo_dir, f)
90 pylit.main([rst_file])
91
92
93 if __name__ == "__main__":
94 process()