source: sasmodels/doc/gentoc.py @ 58c3367

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 58c3367 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
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
[5041682]9from sasmodels.core import load_model_info
[61ba623]10
[a5b8477]11try:
12    from typing import Optional, BinaryIO, List, Dict
13except ImportError:
14    pass
15else:
16    from sasmodels.modelinfo import ModelInfo
[61ba623]17
18TEMPLATE="""\
19..
20    Generated from doc/gentoc.py -- DO NOT EDIT --
21
[40a87fa]22.. _%(label)s:
[61ba623]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):
[a5b8477]35    # type: (str, str, str, Optional[BinaryIO]) -> BinaryIO
[61ba623]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:
[7bb290c]39        _add_subcategory(category_name, parent)
[61ba623]40    return file
41
[7bb290c]42def _add_subcategory(category_name, parent):
[a5b8477]43    # type: (str, BinaryIO) -> None
[7bb290c]44    parent.write("    %s.rst\n"%category_name)
[61ba623]45
46def _add_model(file, model_name):
[a5b8477]47    # type: (IO[str], str) -> None
[61ba623]48    file.write("    ../../model/%s.rst\n"%model_name)
49
[7bb290c]50def _maybe_make_category(category, models, cat_files, model_toc):
[a5b8477]51    # type: (str, List[str], Dict[str, BinaryIO], BinaryIO) -> None
[7bb290c]52    if category not in cat_files:
[9404dd3]53        print("Unexpected category %s containing"%category, models, file=sys.stderr)
[7bb290c]54        title = category.capitalize()+" Functions"
55        cat_files[category] = _make_category(category, category, title, model_toc)
56
[61ba623]57def generate_toc(model_files):
[a5b8477]58    # type: (List[str]) -> None
[61ba623]59    if not model_files:
[9404dd3]60        print("gentoc needs a list of model files", file=sys.stderr)
[61ba623]61
62    # find all categories
[a5b8477]63    category = {} # type: Dict[str, List[str]]
[61ba623]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
[5041682]68        model_info = load_model_info(model_name)
[a5b8477]69        if model_info.category is None:
[9404dd3]70            print("Missing category for", item, file=sys.stderr)
[61ba623]71        else:
[a5b8477]72            category.setdefault(model_info.category,[]).append(model_name)
[61ba623]73
74    # Check category names
75    for k,v in category.items():
76        if len(v) == 1:
[9404dd3]77            print("Category %s contains only %s"%(k,v[0]), file=sys.stderr)
[61ba623]78
[7bb290c]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.
[61ba623]87
88    if not exists(MODEL_TOC_PATH): mkdir(MODEL_TOC_PATH)
89    model_toc = _make_category(
90        'index',  'Models', 'Model Functions')
[7bb290c]91    #shape_toc = _make_category(
92    #    'shape',  'Shapes', 'Shape Functions', model_toc)
[61ba623]93    free_toc = _make_category(
94        'shape-independent',  'Shape-independent',
[7bb290c]95        'Shape-Independent Functions')
[61ba623]96    struct_toc = _make_category(
[7bb290c]97        'structure-factor',  'Structure-factor', 'Structure Factors')
[61ba623]98    custom_toc = _make_category(
[7bb290c]99        'custom-models',  'Custom-models', 'Custom Models')
[61ba623]100
101    # remember to top level categories
102    cat_files = {
[7bb290c]103        #'shape':shape_toc,
104        'shape':model_toc,
[61ba623]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(':')
[7bb290c]114            _maybe_make_category(cat, v, cat_files, model_toc)
[61ba623]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:
[7bb290c]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)
[61ba623]133
134    # Close the top-level category files
[7bb290c]135    #model_toc.close()
[61ba623]136    for f in cat_files.values(): f.close()
137
138
139if __name__ == "__main__":
[5041682]140    generate_toc(sys.argv[1:])
Note: See TracBrowser for help on using the repository browser.