Changeset 0d5dc05 in sasmodels for doc/genmodel.py


Ignore:
Timestamp:
Mar 31, 2019 7:33:49 AM (6 years ago)
Author:
GitHub <noreply@…>
Branches:
master
Children:
f8060c5
Parents:
be0942c (diff), 7050455 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Steve K <smk78@…> (03/31/19 07:33:49)
git-committer:
GitHub <noreply@…> (03/31/19 07:33:49)
Message:

Merge branch 'master' into ticket-1263-source-link

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/genmodel.py

    rb866abf rbe0942c  
    77import matplotlib.pyplot as plt 
    88sys.path.insert(0, os.path.abspath('..')) 
     9import sasmodels 
    910from sasmodels import generate, core 
    1011from sasmodels.direct_model import DirectModel, call_profile 
     
    127128    #print("figure saved in",path) 
    128129 
     130def copy_if_newer(src, dst): 
     131    from os.path import dirname, exists, getmtime 
     132    import shutil 
     133    if not exists(dst): 
     134        path = dirname(dst) 
     135        if not exists(path): 
     136            os.makedirs(path) 
     137        shutil.copy2(src, dst) 
     138    elif getmtime(src) > getmtime(dst): 
     139        shutil.copy2(src, dst) 
     140 
     141def link_sources(model_info): 
     142    from os.path import basename, dirname, realpath, join as joinpath 
     143 
     144    # List source files in reverse order of dependency. 
     145    model_file = basename(model_info.filename) 
     146    sources = list(reversed(model_info.source + [model_file])) 
     147 
     148    # Copy files to src dir under models directory.  Need to do this 
     149    # because sphinx can't link to an absolute path. 
     150    root = dirname(dirname(realpath(__file__))) 
     151    src = joinpath(root, "sasmodels", "models") 
     152    dst = joinpath(root, "doc", "model", "src") 
     153    for path in sources: 
     154        copy_if_newer(joinpath(src, path), joinpath(dst, path)) 
     155 
     156    # Link to local copy of the files 
     157    downloads = [":download:`%s <src/%s>`"%(path, path) for path in sources] 
     158 
     159    # Could do syntax highlighting on the model files by creating a rst file 
     160    # beside each source file named containing source file with 
     161    # 
     162    #    src/path.rst: 
     163    # 
     164    #    .. {{ path.replace('/','_') }}: 
     165    # 
     166    #    .. literalinclude:: {{ src/path }} 
     167    #        :language: {{ "python" if path.endswith('.py') else "c" }} 
     168    #        :linenos: 
     169    # 
     170    # and link to it using 
     171    # 
     172    #     colors = [":ref:`%s`"%(path.replace('/','_')) for path in sources] 
     173    # 
     174    # Probably need to dump all the rst files into an index.rst to build them. 
     175 
     176    # Link to github repo (either the tagged sasmodels version or master) 
     177    url = "https://github.com/SasView/sasmodels/blob/v%s"%sasmodels.__version__ 
     178    #url = "https://github.com/SasView/sasmodels/blob/master"%sasmodels.__version__ 
     179    links = ["`%s <%s/sasmodels/models/%s>`_"%(path, url, path) for path in sources] 
     180 
     181    sep = "\n$\\ \\star\\ $ "  # bullet 
     182    body = "\n**Source**\n" 
     183    #body += "\n" + sep.join(links) + "\n\n" 
     184    body += "\n" + sep.join(downloads) + "\n\n" 
     185    return body 
     186 
    129187def gen_docs(model_info): 
    130188    # type: (ModelInfo) -> None 
     
    150208    match = re.search(pattern, docstr.upper()) 
    151209 
     210    sources = link_sources(model_info) 
     211 
     212    insertion = captionstr + sources 
     213 
    152214    if match: 
    153215        docstr1 = docstr[:match.start()] 
    154216        docstr2 = docstr[match.start():] 
    155         docstr = docstr1 + captionstr + docstr2 
     217        docstr = docstr1 + insertion + docstr2 
    156218    else: 
    157219        print('------------------------------------------------------------------') 
    158220        print('References NOT FOUND for model: ', model_info.id) 
    159221        print('------------------------------------------------------------------') 
    160         docstr += captionstr 
    161  
    162     open(sys.argv[2],'w').write(docstr) 
     222        docstr += insertion 
     223 
     224    open(sys.argv[2], 'w').write(docstr) 
    163225 
    164226def process_model(path): 
Note: See TracChangeset for help on using the changeset viewer.