source: sasmodels/doc/gentoc.py @ dc056b9

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since dc056b9 was 61ba623, checked in by Paul Kienzle <pkienzle@…>, 10 years ago

redo model table of contents to use doc category from model

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[61ba623]1import sys
2# make sure sasmodels is on the path
3sys.path.append('..')
4
5from os import mkdir
6from os.path import basename, exists, join as joinpath
7from sasmodels.core import load_model_definition
8
9
10TEMPLATE="""\
11..
12    Generated from doc/gentoc.py -- DO NOT EDIT --
13
14.. %(label)s:
15
16%(bar)s
17%(title)s
18%(bar)s
19
20.. toctree::
21
22"""
23
24MODEL_TOC_PATH = "ref/models"
25
26def _make_category(category_name, label, title, parent=None):
27    file = open(joinpath(MODEL_TOC_PATH, category_name+".rst"), "w")
28    file.write(TEMPLATE%{'label':label, 'title':title, 'bar':'*'*len(title)})
29    if parent:
30        _add_subcategory(parent, category_name)
31    return file
32
33def _add_subcategory(file, category_name):
34    file.write("    %s.rst\n"%category_name)
35
36def _add_model(file, model_name):
37    file.write("    ../../model/%s.rst\n"%model_name)
38
39def generate_toc(model_files):
40    if not model_files:
41        print >>sys.stderr, "gentoc needs a list of model files"
42
43    # find all categories
44    category = {}
45    for item in model_files:
46        # assume model is in sasmodels/models/name.py, and ignore the full path
47        model_name = basename(item)[:-3]
48        if model_name.startswith('_'): continue
49        model_definition = load_model_definition(model_name)
50        if not hasattr(model_definition, 'category'):
51            print >>sys.stderr, "Missing category for",item
52        else:
53            category.setdefault(model_definition.category,[]).append(model_name)
54
55    # Check category names
56    for k,v in category.items():
57        if len(v) == 1:
58            print >>sys.stderr, "Category %s contains only %s"%(k,v[0])
59
60    # Generate category files
61    # Assume that top-level groups are:
62    #    shape, shape-independent, structure-factor and custom
63    # Supports a two-level category structure.
64
65    if not exists(MODEL_TOC_PATH): mkdir(MODEL_TOC_PATH)
66    model_toc = _make_category(
67        'index',  'Models', 'Model Functions')
68    shape_toc = _make_category(
69        'shape',  'Shapes', 'Shape Functions', model_toc)
70    free_toc = _make_category(
71        'shape-independent',  'Shape-independent',
72        'Shape-Independent Functions', model_toc)
73    struct_toc = _make_category(
74        'structure-factor',  'Structure-factor', 'Structure Factors', model_toc)
75    custom_toc = _make_category(
76        'custom-models',  'Custom-models', 'Custom Models', model_toc)
77    model_toc.close()
78
79    # remember to top level categories
80    cat_files = {
81        'shape':shape_toc,
82        'shape-independent':free_toc,
83        'structure-factor': struct_toc,
84        'custom': custom_toc,
85        }
86
87    # Process the model lists
88    for k,v in sorted(category.items()):
89        if ':' in k:
90            cat,subcat = k.split(':')
91            cat_file = cat_files[cat]
92            label = "-".join((cat,subcat))
93            filename = label
94            title = subcat.capitalize()+" Functions"
95            sub_toc = _make_category(filename, label, title, cat_file)
96            for model in sorted(v):
97                _add_model(sub_toc, model)
98            sub_toc.close()
99        else:
100            if k not in cat_files:
101                print >>sys.stderr, "Unknown category %s containing"%cat, v
102            else:
103                cat_file = cat_files[k]
104                for model in sorted(v):
105                    _add_model(cat_file, model)
106
107    # Close the top-level category files
108    for f in cat_files.values(): f.close()
109
110
111if __name__ == "__main__":
112    generate_toc(sys.argv[1:])
Note: See TracBrowser for help on using the repository browser.