source: sasmodels/doc/gentoc.py @ eaca9eb

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since eaca9eb was 9404dd3, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

python 3.x support

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[9404dd3]1from __future__ import print_function
2
[61ba623]3import sys
4# make sure sasmodels is on the path
5sys.path.append('..')
6
7from os import mkdir
8from os.path import basename, exists, join as joinpath
9from sasmodels.core import load_model_definition
10
11
12TEMPLATE="""\
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
26MODEL_TOC_PATH = "ref/models"
27
28def _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:
[7bb290c]32        _add_subcategory(category_name, parent)
[61ba623]33    return file
34
[7bb290c]35def _add_subcategory(category_name, parent):
36    parent.write("    %s.rst\n"%category_name)
[61ba623]37
38def _add_model(file, model_name):
39    file.write("    ../../model/%s.rst\n"%model_name)
40
[7bb290c]41def _maybe_make_category(category, models, cat_files, model_toc):
42    if category not in cat_files:
[9404dd3]43        print("Unexpected category %s containing"%category, models, file=sys.stderr)
[7bb290c]44        title = category.capitalize()+" Functions"
45        cat_files[category] = _make_category(category, category, title, model_toc)
46
[61ba623]47def generate_toc(model_files):
48    if not model_files:
[9404dd3]49        print("gentoc needs a list of model files", file=sys.stderr)
[61ba623]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'):
[9404dd3]59            print("Missing category for", item, file=sys.stderr)
[61ba623]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:
[9404dd3]66            print("Category %s contains only %s"%(k,v[0]), file=sys.stderr)
[61ba623]67
[7bb290c]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.
[61ba623]76
77    if not exists(MODEL_TOC_PATH): mkdir(MODEL_TOC_PATH)
78    model_toc = _make_category(
79        'index',  'Models', 'Model Functions')
[7bb290c]80    #shape_toc = _make_category(
81    #    'shape',  'Shapes', 'Shape Functions', model_toc)
[61ba623]82    free_toc = _make_category(
83        'shape-independent',  'Shape-independent',
[7bb290c]84        'Shape-Independent Functions')
[61ba623]85    struct_toc = _make_category(
[7bb290c]86        'structure-factor',  'Structure-factor', 'Structure Factors')
[61ba623]87    custom_toc = _make_category(
[7bb290c]88        'custom-models',  'Custom-models', 'Custom Models')
[61ba623]89
90    # remember to top level categories
91    cat_files = {
[7bb290c]92        #'shape':shape_toc,
93        'shape':model_toc,
[61ba623]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(':')
[7bb290c]103            _maybe_make_category(cat, v, cat_files, model_toc)
[61ba623]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:
[7bb290c]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)
[61ba623]122
123    # Close the top-level category files
[7bb290c]124    #model_toc.close()
[61ba623]125    for f in cat_files.values(): f.close()
126
127
128if __name__ == "__main__":
129    generate_toc(sys.argv[1:])
Note: See TracBrowser for help on using the repository browser.