-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert.py
113 lines (74 loc) · 3.16 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright (c) 2017 Civic Knowledge. This file is licensed under the terms of the
# Revised BSD License, included in this distribution as LICENSE
"""
Functions for converting Jupyter notebooks
"""
from metatab import DEFAULT_METATAB_FILE, MetatabDoc
from metatab.cli.core import prt
from metatab.util import ensure_dir, copytree
from os.path import abspath
from os import getcwd
from rowgenerators import Url
from rowgenerators.util import fs_join as join
def convert_documentation(m):
"""Run only the document conversion portion of the notebook conversion
The final document will not be completel
"""
from .exporters import PackageExporter, DocumentationExporter
from .preprocessors import ExtractInlineMetatabDoc
from traitlets.config import Config
from .core import logger
from nbconvert.writers import FilesWriter
import nbformat
nb_path = Url(m.mt_file).parts.path
with open(nb_path) as f:
nb = nbformat.reads(f.read(), as_version=4)
doc = ExtractInlineMetatabDoc().run(nb)
package_name = doc.as_version(None).find_first_value('Root.Name')
output_dir = join(getcwd(), package_name)
de = DocumentationExporter(config=Config(), log=logger, metadata=doc_metadata(doc))
prt('Converting documentation')
output, resources = de.from_filename(nb_path)
fw = FilesWriter()
fw.build_directory = join(output_dir,'docs')
fw.write(output, resources, notebook_name='notebook')
prt("Wrote documentation to {}".format(fw.build_directory))
def convert_notebook(m):
from .core import logger
from traitlets.config import Config
from metatab.jupyter.exporters import PackageExporter, DocumentationExporter
from nbconvert.writers import FilesWriter
from os.path import normpath
from nbconvert.preprocessors.execute import CellExecutionError
prt('Convert notebook to Metatab source package')
nb_path = Url(m.mt_file).parts.path
c = Config()
pe = PackageExporter(config=c, log=logger)
prt('Running the notebook')
pe.run(nb_path)
de = DocumentationExporter(config=c, log=logger, metadata=doc_metadata(pe.doc))
prt('Exporting documentation')
output, resources = de.from_filename(nb_path)
fw = FilesWriter()
fw.build_directory = join(pe.output_dir,'docs')
fw.write(output, resources, notebook_name='notebook')
new_mt_file = join(pe.output_dir, DEFAULT_METATAB_FILE)
doc = MetatabDoc(new_mt_file)
de.update_metatab(doc, resources)
for lib_dir in pe.lib_dirs:
lib_dir = normpath(lib_dir).lstrip('./')
doc['Resources'].new_term("Root.PythonLib", lib_dir)
path = abspath(lib_dir)
dest = join(pe.output_dir, lib_dir)
ensure_dir(dest)
copytree(path, join(pe.output_dir, lib_dir))
doc.write_csv()
# Reset the input to use the new data
prt('Running with new package file: {}'.format(new_mt_file))
m.init_stage2(new_mt_file, '')
def doc_metadata(doc):
"""Create a metadata dict from a MetatabDoc, for Document conversion"""
r = doc['Root'].as_dict()
r.update(doc['Contacts'].as_dict())
r['author'] = r.get('author', r.get('creator', r.get('wrangler')))
return r