1 | from __future__ import print_function |
---|
2 | |
---|
3 | import sys |
---|
4 | # make sure sasmodels is on the path |
---|
5 | sys.path.append('..') |
---|
6 | |
---|
7 | from os import mkdir |
---|
8 | from os.path import basename, exists, join as joinpath |
---|
9 | from sasmodels.core import load_model_definition |
---|
10 | |
---|
11 | |
---|
12 | TEMPLATE="""\ |
---|
13 | .. |
---|
14 | Generated from doc/gentoc.py -- DO NOT EDIT -- |
---|
15 | |
---|
16 | .. %(label)s: |
---|
17 | |
---|
18 | %(bar)s |
---|
19 | %(title)s |
---|
20 | %(bar)s |
---|
21 | |
---|
22 | .. toctree:: |
---|
23 | |
---|
24 | """ |
---|
25 | |
---|
26 | MODEL_TOC_PATH = "ref/models" |
---|
27 | |
---|
28 | def _make_category(category_name, label, title, parent=None): |
---|
29 | file = open(joinpath(MODEL_TOC_PATH, category_name+".rst"), "w") |
---|
30 | file.write(TEMPLATE%{'label':label, 'title':title, 'bar':'*'*len(title)}) |
---|
31 | if parent: |
---|
32 | _add_subcategory(category_name, parent) |
---|
33 | return file |
---|
34 | |
---|
35 | def _add_subcategory(category_name, parent): |
---|
36 | parent.write(" %s.rst\n"%category_name) |
---|
37 | |
---|
38 | def _add_model(file, model_name): |
---|
39 | file.write(" ../../model/%s.rst\n"%model_name) |
---|
40 | |
---|
41 | def _maybe_make_category(category, models, cat_files, model_toc): |
---|
42 | if category not in cat_files: |
---|
43 | print("Unexpected category %s containing"%category, models, file=sys.stderr) |
---|
44 | title = category.capitalize()+" Functions" |
---|
45 | cat_files[category] = _make_category(category, category, title, model_toc) |
---|
46 | |
---|
47 | def generate_toc(model_files): |
---|
48 | if not model_files: |
---|
49 | print("gentoc needs a list of model files", file=sys.stderr) |
---|
50 | |
---|
51 | # find all categories |
---|
52 | category = {} |
---|
53 | for item in model_files: |
---|
54 | # assume model is in sasmodels/models/name.py, and ignore the full path |
---|
55 | model_name = basename(item)[:-3] |
---|
56 | if model_name.startswith('_'): continue |
---|
57 | model_definition = load_model_definition(model_name) |
---|
58 | if not hasattr(model_definition, 'category'): |
---|
59 | print("Missing category for", item, file=sys.stderr) |
---|
60 | else: |
---|
61 | category.setdefault(model_definition.category,[]).append(model_name) |
---|
62 | |
---|
63 | # Check category names |
---|
64 | for k,v in category.items(): |
---|
65 | if len(v) == 1: |
---|
66 | print("Category %s contains only %s"%(k,v[0]), file=sys.stderr) |
---|
67 | |
---|
68 | # Generate category files for the table of contents. |
---|
69 | # Initially we had "shape functions" as an additional TOC level, but we |
---|
70 | # have revised it so that the individual shape categories now go at |
---|
71 | # the top level. Judicious rearrangement of comments will make the |
---|
72 | # "shape functions" level reappear. |
---|
73 | # We are forcing shape-independent, structure-factor and custom-models |
---|
74 | # to come at the end of the TOC. All other categories will come in |
---|
75 | # alphabetical order before them. |
---|
76 | |
---|
77 | if not exists(MODEL_TOC_PATH): mkdir(MODEL_TOC_PATH) |
---|
78 | model_toc = _make_category( |
---|
79 | 'index', 'Models', 'Model Functions') |
---|
80 | #shape_toc = _make_category( |
---|
81 | # 'shape', 'Shapes', 'Shape Functions', model_toc) |
---|
82 | free_toc = _make_category( |
---|
83 | 'shape-independent', 'Shape-independent', |
---|
84 | 'Shape-Independent Functions') |
---|
85 | struct_toc = _make_category( |
---|
86 | 'structure-factor', 'Structure-factor', 'Structure Factors') |
---|
87 | custom_toc = _make_category( |
---|
88 | 'custom-models', 'Custom-models', 'Custom Models') |
---|
89 | |
---|
90 | # remember to top level categories |
---|
91 | cat_files = { |
---|
92 | #'shape':shape_toc, |
---|
93 | 'shape':model_toc, |
---|
94 | 'shape-independent':free_toc, |
---|
95 | 'structure-factor': struct_toc, |
---|
96 | 'custom': custom_toc, |
---|
97 | } |
---|
98 | |
---|
99 | # Process the model lists |
---|
100 | for k,v in sorted(category.items()): |
---|
101 | if ':' in k: |
---|
102 | cat,subcat = k.split(':') |
---|
103 | _maybe_make_category(cat, v, cat_files, model_toc) |
---|
104 | cat_file = cat_files[cat] |
---|
105 | label = "-".join((cat,subcat)) |
---|
106 | filename = label |
---|
107 | title = subcat.capitalize()+" Functions" |
---|
108 | sub_toc = _make_category(filename, label, title, cat_file) |
---|
109 | for model in sorted(v): |
---|
110 | _add_model(sub_toc, model) |
---|
111 | sub_toc.close() |
---|
112 | else: |
---|
113 | _maybe_make_category(k, v, cat_files, model_toc) |
---|
114 | cat_file = cat_files[k] |
---|
115 | for model in sorted(v): |
---|
116 | _add_model(cat_file, model) |
---|
117 | |
---|
118 | #_add_subcategory('shape', model_toc) |
---|
119 | _add_subcategory('shape-independent', model_toc) |
---|
120 | _add_subcategory('structure-factor', model_toc) |
---|
121 | _add_subcategory('custom-models', model_toc) |
---|
122 | |
---|
123 | # Close the top-level category files |
---|
124 | #model_toc.close() |
---|
125 | for f in cat_files.values(): f.close() |
---|
126 | |
---|
127 | |
---|
128 | if __name__ == "__main__": |
---|
129 | generate_toc(sys.argv[1:]) |
---|