source: sasmodels/doc/gentoc.py @ f8f5a37

core_shell_microgelscostrafo411magnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since f8f5a37 was 40a87fa, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

lint and latex cleanup

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