source: sasmodels/doc/gentoc.py @ 5b5ea20

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 5b5ea20 was 990d8df, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

move polydispersity and resolution docs into sasmodels; write installation instructions

  • Property mode set to 100644
File size: 4.7 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
[8ae8532]18TEMPLATE = """\
[61ba623]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
[8ae8532]32MODEL_TOC_PATH = "guide/models"
[61ba623]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]
[8ae8532]67        if model_name.startswith('_'):
68            continue
[5041682]69        model_info = load_model_info(model_name)
[a5b8477]70        if model_info.category is None:
[9404dd3]71            print("Missing category for", item, file=sys.stderr)
[61ba623]72        else:
[8ae8532]73            category.setdefault(model_info.category, []).append(model_name)
[61ba623]74
75    # Check category names
[8ae8532]76    for k, v in category.items():
[61ba623]77        if len(v) == 1:
[8ae8532]78            print("Category %s contains only %s"%(k, v[0]), file=sys.stderr)
[61ba623]79
[7bb290c]80    # Generate category files for the table of contents.
81    # Initially we had "shape functions" as an additional TOC level, but we
82    # have revised it so that the individual shape categories now go at
83    # the top level.  Judicious rearrangement of comments will make the
84    # "shape functions" level reappear.
85    # We are forcing shape-independent, structure-factor and custom-models
86    # to come at the end of the TOC.  All other categories will come in
87    # alphabetical order before them.
[61ba623]88
[8ae8532]89    if not exists(MODEL_TOC_PATH):
90        mkdir(MODEL_TOC_PATH)
[61ba623]91    model_toc = _make_category(
[8ae8532]92        'index', 'Models', 'Model Functions')
[7bb290c]93    #shape_toc = _make_category(
94    #    'shape',  'Shapes', 'Shape Functions', model_toc)
[61ba623]95    free_toc = _make_category(
[8ae8532]96        'shape-independent', 'Shape-independent',
[7bb290c]97        'Shape-Independent Functions')
[61ba623]98    struct_toc = _make_category(
[8ae8532]99        'structure-factor', 'Structure-factor', 'Structure Factors')
[990d8df]100    #custom_toc = _make_category(
101    #    'custom-models', 'Custom-models', 'Custom Models')
[61ba623]102
103    # remember to top level categories
104    cat_files = {
[7bb290c]105        #'shape':shape_toc,
106        'shape':model_toc,
[61ba623]107        'shape-independent':free_toc,
108        'structure-factor': struct_toc,
[990d8df]109        #'custom': custom_toc,
[61ba623]110        }
111
112    # Process the model lists
[8ae8532]113    for k, v in sorted(category.items()):
[61ba623]114        if ':' in k:
[8ae8532]115            cat, subcat = k.split(':')
[7bb290c]116            _maybe_make_category(cat, v, cat_files, model_toc)
[61ba623]117            cat_file = cat_files[cat]
[8ae8532]118            label = "-".join((cat, subcat))
[61ba623]119            filename = label
[8ae8532]120            title = subcat.capitalize() + " Functions"
[61ba623]121            sub_toc = _make_category(filename, label, title, cat_file)
122            for model in sorted(v):
123                _add_model(sub_toc, model)
124            sub_toc.close()
125        else:
[7bb290c]126            _maybe_make_category(k, v, cat_files, model_toc)
127            cat_file = cat_files[k]
128            for model in sorted(v):
129                _add_model(cat_file, model)
130
131    #_add_subcategory('shape', model_toc)
132    _add_subcategory('shape-independent', model_toc)
133    _add_subcategory('structure-factor', model_toc)
[8ae8532]134    #_add_subcategory('custom-models', model_toc)
[61ba623]135
136    # Close the top-level category files
[7bb290c]137    #model_toc.close()
[8ae8532]138    for f in cat_files.values():
139        f.close()
[61ba623]140
141
142if __name__ == "__main__":
[5041682]143    generate_toc(sys.argv[1:])
Note: See TracBrowser for help on using the repository browser.