def route_examples(): links = hfn.render_table( [[name, hfn.atag(url_for("route_query", pred=pred, root=root) + (args[0] if args else ''), f'../query/{pred}/{root}{args[0] if args else ""}')] for name, pred, root, *args in examples], 'Root class', '../query/{predicate-curie}/{root-curie}?direction=INCOMING&depth=10&branch=master&local=false', halign='left') flinks = hfn.render_table( [[name, hfn.atag(url_for("route_filequery", pred=pred, root=root, file=file) + (args[0] if args else ''), f'../query/{pred}/{root}/{file}{args[0] if args else ""}')] for name, pred, root, file, *args in file_examples], 'Root class', '../query/{predicate-curie}/{root-curie}/{ontology-filepath}?direction=INCOMING&depth=10&branch=master&restriction=false', halign='left') dlinks = hfn.render_table( [[name, hfn.atag(url_for("route_dynamic", path=path) + (querystring if querystring else ''), f'../query/dynamic/{path}{querystring if querystring else ""}')] for name, path, querystring in dynamic_examples], 'Root class', '../query/dynamic/{path}?direction=OUTGOING&dynamic=query&args=here', halign='left') return hfn.htmldoc(links, flinks, dlinks, title='Example hierarchy queries')
def tag_row(row: list, url: url_for = None, tier_level: int = 0) -> list: ''' Tag each element in the row; atag the curies & ptag everything else ''' tagged_row = [] spaces = nbsp * 8 * tier_level if not row: return row if not isinstance(row, list): row = [row] for i, element in enumerate(row): if i > 0: spaces = '' try: oid = OntId(element) # TODO: should this have spaces? tagged_curie = atag(oid.iri, oid.curie) tagged_row.append(tagged_curie) except: if url: tagged_row.append(ptag(spaces + atag(url, element))) else: tagged_row.append(spaces + element) return tagged_row
def route_(): d = url_for('route_docs') e = url_for('route_examples') i = url_for('route_import_chain') return hfn.htmldoc(hfn.atag(d, 'Docs'), '<br>', hfn.atag(e, 'Examples'), '<br>', hfn.atag(i, 'Import chain'), title='NIF ontology hierarchies')
def route_dynamic(path): args = dict(request.args) if 'direction' in args: direction = args.pop('direction') else: direction = 'OUTGOING' # should always be outgoing here since we can't specify? if 'format' in args: format_ = args.pop('format') else: format_ = None try: j = sgd.dispatch(path, **args) except rHTTPError as e: log.exception(e) abort(e.response.status_code) # DO NOT PASS ALONG THE MESSAGE except ValueError as e: log.exception(e) abort(404) if j is None or 'edges' not in j or not j['edges']: log.error(pformat(j)) log.debug(sgd._last_url) return abort(400) prov = [ hfn.titletag(f'Dynamic query result for {path}'), f'<meta name="date" content="{UTCNOWISO()}">', f'<link rel="http://www.w3.org/ns/prov#wasGeneratedBy" href="{wgb}">', '<meta name="representation" content="SciGraph">', f'<link rel="http://www.w3.org/ns/prov#wasDerivedFrom" href="{sgd._last_url}">' ] kwargs = {'json': cleanBad(j), 'html_head': prov} tree, extras = creatTree(*Query(None, None, direction, None), **kwargs) #print(extras.hierarhcy) #print(tree) if format_ is not None: if format_ == 'table': #breakpoint() def nowrap(class_, tag=''): return (f'{tag}.{class_}' '{ white-space: nowrap; }') ots = [ OntTerm(n) for n in flatten_tree(extras.hierarchy) if 'CYCLE' not in n ] #rows = [[ot.label, ot.asId().atag(), ot.definition] for ot in ots] rows = [[ot.label, hfn.atag(ot.iri, ot.curie), ot.definition] for ot in ots] return hfn.htmldoc(hfn.render_table(rows, 'label', 'curie', 'definition'), styles=(hfn.table_style, nowrap('col-label', 'td'))) return hfn.htmldoc(extras.html, other=prov, styles=hfn.tree_styles)
def test_htmldoc(self): doc = hfn.htmldoc(hfn.atag('https://example.com/'), title='test page', metas=({ 'test': 'meta' }, ), styles=(hfn.monospace_body_style, ), scripts=('console.log("lol")', )) assert doc == doc_expect
def route_reports(): report_names = ( 'completeness', 'size', 'filetypes', 'pathids', 'keywords', 'subjects', 'errors', 'terms', ) report_links = [ atag(url_for(f'route_reports_{rn}', ext=None), rn) + '<br>\n' for rn in report_names ] return htmldoc('Reports<br>\n', *report_links, title='Reports')
def route_sparc_dynamic(path): args = dict(request.args) if 'direction' in args: direction = args.pop('direction') else: direction = 'OUTGOING' # should always be outgoing here since we can't specify? if 'format' in args: format_ = args.pop('format') else: format_ = None j = data_sgd.dispatch(path, **args) #breakpoint() if not j['edges']: log.error(pprint(j)) return abort(400) kwargs = {'json': j} tree, extras = creatTree(*Query(None, None, direction, None), **kwargs) #print(extras.hierarhcy) print(tree) if format_ is not None: if format_ == 'table': #breakpoint() def nowrap(class_, tag=''): return (f'{tag}.{class_}' '{ white-space: nowrap; }') ots = [ OntTerm(n) for n in flatten_tree(extras.hierarchy) if 'CYCLE' not in n ] #rows = [[ot.label, ot.asId().atag(), ot.definition] for ot in ots] rows = [[ot.label, hfn.atag(ot.iri, ot.curie), ot.definition] for ot in ots] return htmldoc(hfn.render_table(rows, 'label', 'curie', 'definition'), styles=(hfn.table_style, nowrap('col-label', 'td'))) return htmldoc(extras.html, styles=hfn.tree_styles)
def default(self): out_path = self.options.out_path BUILD = self.options.BUILD glb = Path(auth.get_path('git-local-base')) theme_repo = glb / 'org-html-themes' theme = theme_repo / 'setup/theme-readtheorg-local.setup' prepare_paths(BUILD, out_path, theme_repo, theme) doc_config = self._doc_config names = tuple(doc_config['repos']) + tuple( self.options.repo) # TODO fetch if missing ? repo_paths = [(glb / name).resolve() for name in names] repos = [p.repo for p in repo_paths] skip_folders = doc_config.get('skip-folders', tuple()) rskip = doc_config.get('skip', {}) # TODO move this into run_all docstring_kwargs = makeDocstrings(BUILD, repo_paths, skip_folders, rskip) wd_docs_kwargs = [docstring_kwargs] if self.options.docstring_only: [ kwargs.update({'theme': theme}) for _, _, kwargs in wd_docs_kwargs ] outname, rendered = render_docs(wd_docs_kwargs, out_path, titles=None, n_jobs=1, debug=self.options.debug)[0] if not outname.parent.exists(): outname.parent.mkdir(parents=True) with open(outname.as_posix(), 'wt') as f: f.write(rendered) return et = tuple() wd_docs_kwargs += [ (rp, rp / f, makeKwargs(rp, f)) for rp in repo_paths for f in rp.repo.git.ls_files().split('\n') if Path(f).suffix in suffixFuncs and only(rp, f) and noneMembers( f, *skip_folders) and f not in rskip.get(rp.name, et) ] [kwargs.update({'theme': theme}) for _, _, kwargs in wd_docs_kwargs] if self.options.spell: spell((f.as_posix() for _, f, _ in wd_docs_kwargs)) return titles = doc_config['titles'] outname_rendered = render_docs(wd_docs_kwargs, out_path, titles, self.options.jobs, debug=self.options.debug) index = [ f'<b class="{heading}">{heading}</b>' for heading in doc_config['index'] ] _NOTITLE = object() for outname, rendered in outname_rendered: apath = outname.relative_to(self.options.out_path) title = titles.get(apath.as_posix(), _NOTITLE) # TODO parse out/add titles if title is not None: value = (hfn.atag(apath) if title is _NOTITLE else hfn.atag( apath, title)) index.append(value) if not outname.parent.exists(): outname.parent.mkdir(parents=True) with open(outname.as_posix(), 'wt') as f: f.write(rendered) lt = list(titles) def title_key(a): title = a.split('"')[1] if title not in lt: msg = (f'{title} missing from {self.options.config}') raise ValueError(msg) return lt.index(title) index_body = '<br>\n'.join(['<h1>Documentation Index</h1>'] + sorted(index, key=title_key)) with open((out_path / 'index.html').as_posix(), 'wt') as f: f.write(hfn.htmldoc(index_body, title=doc_config['title']))
def sparc_dynamic(data_sgd, data_sgc, path, wgb, process=lambda coll, blob: blob): args = dict(request.args) if 'direction' in args: direction = args.pop('direction') else: direction = 'OUTGOING' # should always be outgoing here since we can't specify? if 'format' in args: format_ = args.pop('format') else: format_ = None if 'apinat' in path: # FIXME bad hardcoded hack _old_get = data_sgd._get try: data_sgd._get = data_sgd._normal_get j = data_sgd.dispatch(path, **args) except ValueError as e: log.exception(e) abort(404) except rHTTPError as e: log.exception(e) abort(e.response.status_code) # DO NOT PASS ALONG THE MESSAGE finally: data_sgd._get = _old_get else: try: j = data_sgd.dispatch(path, **args) except rHTTPError as e: log.exception(e) abort(e.response.status_code) # DO NOT PASS ALONG THE MESSAGE except ValueError as e: log.exception(e) abort(404) j = process(collapse_apinat, j) if j is None or 'edges' not in j: log.error(pformat(j)) return abort(400) elif not j['edges']: return node_list(j['nodes']) # FIXME ... really should error? if path.endswith('housing-lyphs'): # FIXME hack root = 'NLX:154731' #direction = 'INCOMING' else: root = None prov = [ hfn.titletag(f'Dynamic query result for {path}'), f'<meta name="date" content="{UTCNOWISO()}">', f'<link rel="http://www.w3.org/ns/prov#wasGeneratedBy" href="{wgb}">', '<meta name="representation" content="SciGraph">', ('<link rel="http://www.w3.org/ns/prov#wasDerivedFrom" ' f'href="{data_sgd._last_url}">')] kwargs = {'json': cleanBad(j), 'html_head': prov, 'prefixes': data_sgc.getCuries(), # FIXME efficiency } tree, extras = creatTree(*Query(root, None, direction, None), **kwargs) #print(extras.hierarhcy) #print(tree) if format_ is not None: if format_ == 'table': #breakpoint() def nowrap(class_, tag=''): return (f'{tag}.{class_}' '{ white-space: nowrap; }') ots = [OntTerm(n) for n in flatten_tree(extras.hierarchy) if 'CYCLE' not in n] #rows = [[ot.label, ot.asId().atag(), ot.definition] for ot in ots] rows = [[ot.label, hfn.atag(ot.iri, ot.curie), ot.definition] for ot in ots] return hfn.htmldoc(hfn.render_table(rows, 'label', 'curie', 'definition'), styles=(hfn.table_style, nowrap('col-label', 'td'))) return hfn.htmldoc(extras.html, other=prov, styles=hfn.tree_styles)
def node_list(nodes): log.debug(pformat(nodes)) # FIXME why the heck are these coming in as lbl now?!?! s = sorted([(n['lbl'], hfn.atag(n['id'], n['lbl'], new_tab=True)) for n in nodes]) return '<br>\n'.join([at for _, at in s])
def atag(self): return hfn.atag(self.hyperlink, self.value)
def main(): from docopt import docopt args = docopt(__doc__) patch_theme_setup(theme) BUILD = working_dir / 'doc_build' if not BUILD.exists(): BUILD.mkdir() docs_dir = BUILD / 'docs' if not docs_dir.exists(): docs_dir.mkdir() theme_styles_dir = theme_repo / 'styles' doc_styles_dir = docs_dir / 'styles' if doc_styles_dir.exists(): shutil.rmtree(doc_styles_dir) shutil.copytree(theme_styles_dir, doc_styles_dir) docstring_kwargs = docstrings() wd_docs_kwargs = [docstring_kwargs] if args['--docstring-only']: outname, rendered = render_docs(wd_docs_kwargs, BUILD, 1)[0] if not outname.parent.exists(): outname.parent.mkdir(parents=True) with open(outname.as_posix(), 'wt') as f: f.write(rendered) return repos = (Repo(Path(devconfig.ontology_local_repo).resolve().as_posix()), Repo(working_dir.as_posix()), *(Repo(Path(devconfig.git_local_base, repo_name).as_posix()) for repo_name in ('ontquery', 'sparc-curation'))) skip_folders = 'notebook-testing', 'complete', 'ilxutils', 'librdflib' rskip = { 'pyontutils': ( 'docs/NeuronLangExample.ipynb', # exact skip due to moving file 'ilxutils/ilx-playground.ipynb'), 'sparc-curation': ('README.md', ), } et = tuple() # TODO move this into run_all #wd_docs_kwargs = [(Path(repo.working_dir).resolve(), wd_docs_kwargs += [ (Path(repo.working_dir).resolve(), Path(repo.working_dir, f).resolve(), makeKwargs(repo, f)) for repo in repos for f in repo.git.ls_files().split('\n') if Path(f).suffix in suffixFuncs #and Path(repo.working_dir).name == 'NIF-Ontology' and f == 'README.md' # DEBUG #and Path(repo.working_dir).name == 'pyontutils' and f == 'README.md' # DEBUG #and Path(repo.working_dir).name == 'sparc-curation' and f == 'docs/setup.org' # DEBUG and noneMembers(f, *skip_folders) and f not in rskip.get( Path(repo.working_dir).name, et) ] # doesn't work because read-from-minibuffer cannot block #compile_org_forever = ['emacs', '-q', '-l', #Path(devconfig.git_local_base, #'orgstrap/init.el').resolve().as_posix(), #'--batch', '-f', 'compile-org-forever'] #org_compile_process = subprocess.Popen(compile_org_forever, #stdin=subprocess.PIPE, #stdout=subprocess.PIPE, #stderr=subprocess.PIPE) if args['--spell']: spell((f.as_posix() for _, f, _ in wd_docs_kwargs)) return outname_rendered = render_docs(wd_docs_kwargs, BUILD, int(args['--jobs'])) titles = { 'Components': 'Components', 'NIF-Ontology/README.html': 'Introduction to the NIF Ontology', # 'ontquery/README.html': 'Introduction to ontquery', 'pyontutils/README.html': 'Introduction to pyontutils', 'pyontutils/nifstd/README.html': 'Introduction to nifstd-tools', 'pyontutils/neurondm/README.html': 'Introduction to neurondm', 'pyontutils/ilxutils/README.html': 'Introduction to ilxutils', 'Developer docs': 'Developer docs', 'NIF-Ontology/docs/processes.html': 'Ontology development processes (START HERE!)', # HOWTO 'NIF-Ontology/docs/development-setup.html': 'Ontology development setup', # HOWTO 'sparc-curation/docs/setup.html': 'Developer and curator setup (broader scope but extremely detailed)', 'NIF-Ontology/docs/import-chain.html': 'Ontology import chain', # Documentation 'pyontutils/nifstd/resolver/README.html': 'Ontology resolver setup', 'pyontutils/nifstd/scigraph/README.html': 'Ontology SciGraph setup', 'sparc-curation/resources/scigraph/README.html': 'SPARC SciGraph setup', 'pyontutils/docstrings.html': 'Command line programs', 'NIF-Ontology/docs/external-sources.html': 'External sources for the ontology', # Other 'ontquery/docs/interlex-client.html': 'InterLex client library doccumentation', 'Contributing': 'Contributing', 'pyontutils/nifstd/development/README.html': 'Contributing to the ontology', 'pyontutils/nifstd/development/community/README.html': 'Contributing term lists to the ontology', 'pyontutils/neurondm/neurondm/models/README.html': 'Contributing neuron terminology to the ontology', 'Ontology content': 'Ontology content', 'NIF-Ontology/docs/brain-regions.html': 'Parcellation schemes', # Ontology Content 'pyontutils/nifstd/development/methods/README.html': 'Methods and techniques', # Ontology content 'NIF-Ontology/docs/Neurons.html': 'Neuron Lang overview', 'pyontutils/neurondm/docs/NeuronLangExample.html': 'Neuron Lang examples', 'pyontutils/neurondm/docs/neurons_notebook.html': 'Neuron Lang setup', 'Specifications': 'Specifications', 'NIF-Ontology/docs/interlex-spec.html': 'InterLex specification', # Documentation 'pyontutils/ttlser/docs/ttlser.html': 'Deterministic turtle specification', 'Other': 'Other', 'pyontutils/htmlfn/README.html': 'htmlfn readme', 'pyontutils/ttlser/README.html': 'ttlser readme', 'sparc-curation/docs/background.html': '', # present but not visibly listed } titles_sparc = { # TODO abstract this out ... 'Background': 'Background', 'sparc-curation/docs/background.html': 'SPARC curation background', 'Other':'Other', 'sparc-curation/README.html': 'sparc-curation readme', } index = [ '<b class="Components">Components</b>', '<b class="Developer docs">Developer docs</b>', '<b class="Contributing">Contributing</b>', '<b class="Ontology content">Ontology content</b>', '<b class="Specifications">Specifications</b>', '<b class="Other">Other</b>', ] for outname, rendered in outname_rendered: apath = outname.relative_to(BUILD / 'docs') title = titles.get(apath.as_posix(), None) # TODO parse out/add titles value = atag(apath) if title is None else atag(apath, title) index.append(value) if not outname.parent.exists(): outname.parent.mkdir(parents=True) with open(outname.as_posix(), 'wt') as f: f.write(rendered) lt = list(titles) def title_key(a): return lt.index(a.split('"')[1]) index_body = '<br>\n'.join(['<h1>Documentation Index</h1>'] + sorted(index, key=title_key)) with open((BUILD / 'docs/index.html').as_posix(), 'wt') as f: f.write(htmldoc(index_body, title='NIF Ontology documentation index'))