##// END OF EJS Templates
API docs gardening
Thomas Kluyver -
Show More
@@ -1,146 +1,150 b''
1 """The IPython notebook format
1 """The IPython notebook format
2
2
3 Use this module to read or write notebook files as particular nbformat versions.
3 Use this module to read or write notebook files as particular nbformat versions.
4 """
4 """
5
5
6 # Copyright (c) IPython Development Team.
6 # Copyright (c) IPython Development Team.
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8
8
9 from IPython.utils.log import get_logger
9 from IPython.utils.log import get_logger
10
10
11 from . import v1
11 from . import v1
12 from . import v2
12 from . import v2
13 from . import v3
13 from . import v3
14 from . import v4
14 from . import v4
15
15
16 __all__ = ['versions', 'validate', 'ValidationError', 'convert', 'from_dict',
17 'NotebookNode', 'current_nbformat', 'current_nbformat_minor',
18 'NBFormatError', 'NO_CONVERT', 'reads', 'read', 'writes', 'write']
19
16 versions = {
20 versions = {
17 1: v1,
21 1: v1,
18 2: v2,
22 2: v2,
19 3: v3,
23 3: v3,
20 4: v4,
24 4: v4,
21 }
25 }
22
26
23 from .validator import validate, ValidationError
27 from .validator import validate, ValidationError
24 from .converter import convert
28 from .converter import convert
25 from . import reader
29 from . import reader
26 from .notebooknode import from_dict, NotebookNode
30 from .notebooknode import from_dict, NotebookNode
27
31
28 from .v4 import (
32 from .v4 import (
29 nbformat as current_nbformat,
33 nbformat as current_nbformat,
30 nbformat_minor as current_nbformat_minor,
34 nbformat_minor as current_nbformat_minor,
31 )
35 )
32
36
33 class NBFormatError(ValueError):
37 class NBFormatError(ValueError):
34 pass
38 pass
35
39
36 # no-conversion singleton
40 # no-conversion singleton
37 NO_CONVERT = object()
41 NO_CONVERT = object()
38
42
39 def reads(s, as_version, **kwargs):
43 def reads(s, as_version, **kwargs):
40 """Read a notebook from a string and return the NotebookNode object as the given version.
44 """Read a notebook from a string and return the NotebookNode object as the given version.
41
45
42 The string can contain a notebook of any version.
46 The string can contain a notebook of any version.
43 The notebook will be returned `as_version`, converting, if necessary.
47 The notebook will be returned `as_version`, converting, if necessary.
44
48
45 Notebook format errors will be logged.
49 Notebook format errors will be logged.
46
50
47 Parameters
51 Parameters
48 ----------
52 ----------
49 s : unicode
53 s : unicode
50 The raw unicode string to read the notebook from.
54 The raw unicode string to read the notebook from.
51 as_version : int
55 as_version : int
52 The version of the notebook format to return.
56 The version of the notebook format to return.
53 The notebook will be converted, if necessary.
57 The notebook will be converted, if necessary.
54 Pass nbformat.NO_CONVERT to prevent conversion.
58 Pass nbformat.NO_CONVERT to prevent conversion.
55
59
56 Returns
60 Returns
57 -------
61 -------
58 nb : NotebookNode
62 nb : NotebookNode
59 The notebook that was read.
63 The notebook that was read.
60 """
64 """
61 nb = reader.reads(s, **kwargs)
65 nb = reader.reads(s, **kwargs)
62 if as_version is not NO_CONVERT:
66 if as_version is not NO_CONVERT:
63 nb = convert(nb, as_version)
67 nb = convert(nb, as_version)
64 try:
68 try:
65 validate(nb)
69 validate(nb)
66 except ValidationError as e:
70 except ValidationError as e:
67 get_logger().error("Notebook JSON is invalid: %s", e)
71 get_logger().error("Notebook JSON is invalid: %s", e)
68 return nb
72 return nb
69
73
70
74
71 def writes(nb, version=NO_CONVERT, **kwargs):
75 def writes(nb, version=NO_CONVERT, **kwargs):
72 """Write a notebook to a string in a given format in the given nbformat version.
76 """Write a notebook to a string in a given format in the given nbformat version.
73
77
74 Any notebook format errors will be logged.
78 Any notebook format errors will be logged.
75
79
76 Parameters
80 Parameters
77 ----------
81 ----------
78 nb : NotebookNode
82 nb : NotebookNode
79 The notebook to write.
83 The notebook to write.
80 version : int, optional
84 version : int, optional
81 The nbformat version to write.
85 The nbformat version to write.
82 If unspecified, or specified as nbformat.NO_CONVERT,
86 If unspecified, or specified as nbformat.NO_CONVERT,
83 the notebook's own version will be used and no conversion performed.
87 the notebook's own version will be used and no conversion performed.
84
88
85 Returns
89 Returns
86 -------
90 -------
87 s : unicode
91 s : unicode
88 The notebook as a JSON string.
92 The notebook as a JSON string.
89 """
93 """
90 if version is not NO_CONVERT:
94 if version is not NO_CONVERT:
91 nb = convert(nb, version)
95 nb = convert(nb, version)
92 else:
96 else:
93 version, _ = reader.get_version(nb)
97 version, _ = reader.get_version(nb)
94 try:
98 try:
95 validate(nb)
99 validate(nb)
96 except ValidationError as e:
100 except ValidationError as e:
97 get_logger().error("Notebook JSON is invalid: %s", e)
101 get_logger().error("Notebook JSON is invalid: %s", e)
98 return versions[version].writes_json(nb, **kwargs)
102 return versions[version].writes_json(nb, **kwargs)
99
103
100
104
101 def read(fp, as_version, **kwargs):
105 def read(fp, as_version, **kwargs):
102 """Read a notebook from a file as a NotebookNode of the given version.
106 """Read a notebook from a file as a NotebookNode of the given version.
103
107
104 The string can contain a notebook of any version.
108 The string can contain a notebook of any version.
105 The notebook will be returned `as_version`, converting, if necessary.
109 The notebook will be returned `as_version`, converting, if necessary.
106
110
107 Notebook format errors will be logged.
111 Notebook format errors will be logged.
108
112
109 Parameters
113 Parameters
110 ----------
114 ----------
111 fp : file
115 fp : file
112 Any file-like object with a read method.
116 Any file-like object with a read method.
113 as_version: int
117 as_version: int
114 The version of the notebook format to return.
118 The version of the notebook format to return.
115 The notebook will be converted, if necessary.
119 The notebook will be converted, if necessary.
116 Pass nbformat.NO_CONVERT to prevent conversion.
120 Pass nbformat.NO_CONVERT to prevent conversion.
117
121
118 Returns
122 Returns
119 -------
123 -------
120 nb : NotebookNode
124 nb : NotebookNode
121 The notebook that was read.
125 The notebook that was read.
122 """
126 """
123 return reads(fp.read(), as_version, **kwargs)
127 return reads(fp.read(), as_version, **kwargs)
124
128
125
129
126 def write(nb, fp, version=NO_CONVERT, **kwargs):
130 def write(nb, fp, version=NO_CONVERT, **kwargs):
127 """Write a notebook to a file in a given nbformat version.
131 """Write a notebook to a file in a given nbformat version.
128
132
129 The file-like object must accept unicode input.
133 The file-like object must accept unicode input.
130
134
131 Parameters
135 Parameters
132 ----------
136 ----------
133 nb : NotebookNode
137 nb : NotebookNode
134 The notebook to write.
138 The notebook to write.
135 fp : file
139 fp : file
136 Any file-like object with a write method that accepts unicode.
140 Any file-like object with a write method that accepts unicode.
137 version : int, optional
141 version : int, optional
138 The nbformat version to write.
142 The nbformat version to write.
139 If nb is not this version, it will be converted.
143 If nb is not this version, it will be converted.
140 If unspecified, or specified as nbformat.NO_CONVERT,
144 If unspecified, or specified as nbformat.NO_CONVERT,
141 the notebook's own version will be used and no conversion performed.
145 the notebook's own version will be used and no conversion performed.
142 """
146 """
143 s = writes(nb, version, **kwargs)
147 s = writes(nb, version, **kwargs)
144 if isinstance(s, bytes):
148 if isinstance(s, bytes):
145 s = s.decode('utf8')
149 s = s.decode('utf8')
146 return fp.write(s)
150 return fp.write(s)
@@ -1,77 +1,70 b''
1 """The main API for the v3 notebook format.
1 """The main API for the v3 notebook format.
2
3 Authors:
4
5 * Brian Granger
6 """
2 """
7
3
8 #-----------------------------------------------------------------------------
4 # Copyright (c) IPython Development Team.
9 # Copyright (C) 2008-2011 The IPython Development Team
5 # Distributed under the terms of the Modified BSD License.
10 #
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
14
6
15 #-----------------------------------------------------------------------------
7 __all__ = ['NotebookNode', 'new_code_cell', 'new_text_cell', 'new_notebook',
16 # Imports
8 'new_output', 'new_worksheet', 'new_metadata', 'new_author',
17 #-----------------------------------------------------------------------------
9 'new_heading_cell', 'nbformat', 'nbformat_minor', 'nbformat_schema',
10 'reads_json', 'writes_json', 'read_json', 'write_json',
11 'to_notebook_json', 'reads_py', 'writes_py', 'read_py', 'write_py',
12 'to_notebook_py', 'downgrade', 'upgrade', 'parse_filename'
13 ]
18
14
19 import os
15 import os
20
16
21 from .nbbase import (
17 from .nbbase import (
22 NotebookNode,
18 NotebookNode,
23 new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
19 new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
24 new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor,
20 new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor,
25 nbformat_schema
21 nbformat_schema
26 )
22 )
27
23
28 from .nbjson import reads as reads_json, writes as writes_json
24 from .nbjson import reads as reads_json, writes as writes_json
29 from .nbjson import reads as read_json, writes as write_json
25 from .nbjson import reads as read_json, writes as write_json
30 from .nbjson import to_notebook as to_notebook_json
26 from .nbjson import to_notebook as to_notebook_json
31
27
32 from .nbpy import reads as reads_py, writes as writes_py
28 from .nbpy import reads as reads_py, writes as writes_py
33 from .nbpy import reads as read_py, writes as write_py
29 from .nbpy import reads as read_py, writes as write_py
34 from .nbpy import to_notebook as to_notebook_py
30 from .nbpy import to_notebook as to_notebook_py
35
31
36 from .convert import downgrade, upgrade
32 from .convert import downgrade, upgrade
37
33
38 #-----------------------------------------------------------------------------
39 # Code
40 #-----------------------------------------------------------------------------
41
34
42 def parse_filename(fname):
35 def parse_filename(fname):
43 """Parse a notebook filename.
36 """Parse a notebook filename.
44
37
45 This function takes a notebook filename and returns the notebook
38 This function takes a notebook filename and returns the notebook
46 format (json/py) and the notebook name. This logic can be
39 format (json/py) and the notebook name. This logic can be
47 summarized as follows:
40 summarized as follows:
48
41
49 * notebook.ipynb -> (notebook.ipynb, notebook, json)
42 * notebook.ipynb -> (notebook.ipynb, notebook, json)
50 * notebook.json -> (notebook.json, notebook, json)
43 * notebook.json -> (notebook.json, notebook, json)
51 * notebook.py -> (notebook.py, notebook, py)
44 * notebook.py -> (notebook.py, notebook, py)
52 * notebook -> (notebook.ipynb, notebook, json)
45 * notebook -> (notebook.ipynb, notebook, json)
53
46
54 Parameters
47 Parameters
55 ----------
48 ----------
56 fname : unicode
49 fname : unicode
57 The notebook filename. The filename can use a specific filename
50 The notebook filename. The filename can use a specific filename
58 extention (.ipynb, .json, .py) or none, in which case .ipynb will
51 extention (.ipynb, .json, .py) or none, in which case .ipynb will
59 be assumed.
52 be assumed.
60
53
61 Returns
54 Returns
62 -------
55 -------
63 (fname, name, format) : (unicode, unicode, unicode)
56 (fname, name, format) : (unicode, unicode, unicode)
64 The filename, notebook name and format.
57 The filename, notebook name and format.
65 """
58 """
66 basename, ext = os.path.splitext(fname)
59 basename, ext = os.path.splitext(fname)
67 if ext == u'.ipynb':
60 if ext == u'.ipynb':
68 format = u'json'
61 format = u'json'
69 elif ext == u'.json':
62 elif ext == u'.json':
70 format = u'json'
63 format = u'json'
71 elif ext == u'.py':
64 elif ext == u'.py':
72 format = u'py'
65 format = u'py'
73 else:
66 else:
74 basename = fname
67 basename = fname
75 fname = fname + u'.ipynb'
68 fname = fname + u'.ipynb'
76 format = u'json'
69 format = u'json'
77 return fname, basename, format
70 return fname, basename, format
@@ -1,19 +1,23 b''
1 """The main API for the v4 notebook format."""
1 """The main API for the v4 notebook format."""
2
2
3 # Copyright (c) IPython Development Team.
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 __all__ = ['nbformat', 'nbformat_minor', 'nbformat_schema', 'new_code_cell',
7 'new_markdown_cell', 'new_notebook', 'new_output', 'output_from_msg',
8 'reads', 'writes', 'to_notebook', 'downgrade', 'upgrade']
9
6 from .nbbase import (
10 from .nbbase import (
7 nbformat, nbformat_minor, nbformat_schema,
11 nbformat, nbformat_minor, nbformat_schema,
8 new_code_cell, new_markdown_cell, new_notebook,
12 new_code_cell, new_markdown_cell, new_notebook,
9 new_output, output_from_msg,
13 new_output, output_from_msg,
10 )
14 )
11
15
12 from .nbjson import reads, writes, to_notebook
16 from .nbjson import reads, writes, to_notebook
13 reads_json = reads
17 reads_json = reads
14 writes_json = writes
18 writes_json = writes
15 to_notebook_json = to_notebook
19 to_notebook_json = to_notebook
16
20
17 from .convert import downgrade, upgrade
21 from .convert import downgrade, upgrade
18
22
19
23
@@ -1,70 +1,79 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 """Script to auto-generate our API docs.
2 """Script to auto-generate our API docs.
3 """
3 """
4 # stdlib imports
4 # stdlib imports
5 import os
5 import os
6 import sys
6 import sys
7
7
8 # local imports
8 # local imports
9 sys.path.append(os.path.abspath('sphinxext'))
9 sys.path.append(os.path.abspath('sphinxext'))
10 from apigen import ApiDocWriter
10 from apigen import ApiDocWriter
11
11
12 #*****************************************************************************
12 #*****************************************************************************
13 if __name__ == '__main__':
13 if __name__ == '__main__':
14 pjoin = os.path.join
14 pjoin = os.path.join
15 package = 'IPython'
15 package = 'IPython'
16 outdir = pjoin('source','api','generated')
16 outdir = pjoin('source','api','generated')
17 docwriter = ApiDocWriter(package,rst_extension='.rst')
17 docwriter = ApiDocWriter(package,rst_extension='.rst')
18 # You have to escape the . here because . is a special char for regexps.
18 # You have to escape the . here because . is a special char for regexps.
19 # You must do make clean if you change this!
19 # You must do make clean if you change this!
20 docwriter.package_skip_patterns += [r'\.external$',
20 docwriter.package_skip_patterns += [r'\.external$',
21 # Extensions are documented elsewhere.
21 # Extensions are documented elsewhere.
22 r'\.extensions',
22 r'\.extensions',
23 r'\.config\.profile',
23 r'\.config\.profile',
24 # These should be accessed via nbformat.current
24 # Old nbformat versions
25 r'\.nbformat\.v\d+',
25 r'\.nbformat\.v[1-2]',
26 # Public API for this is in kernel.zmq.eventloops
26 # Public API for this is in kernel.zmq.eventloops
27 r'\.kernel\.zmq\.gui',
27 r'\.kernel\.zmq\.gui',
28 # Magics are documented separately
28 # Magics are documented separately
29 r'\.core\.magics',
29 r'\.core\.magics',
30 ]
30 ]
31
31
32 # The inputhook* modules often cause problems on import, such as trying to
32 # The inputhook* modules often cause problems on import, such as trying to
33 # load incompatible Qt bindings. It's easiest to leave them all out. The
33 # load incompatible Qt bindings. It's easiest to leave them all out. The
34 # main API is in the inputhook module, which is documented.
34 # main API is in the inputhook module, which is documented.
35 docwriter.module_skip_patterns += [ r'\.lib\.inputhook.+',
35 docwriter.module_skip_patterns += [ r'\.lib\.inputhook.+',
36 r'\.ipdoctest',
36 r'\.ipdoctest',
37 r'\.testing\.plugin',
37 r'\.testing\.plugin',
38 # This just prints a deprecation msg:
38 # This just prints a deprecation msg:
39 r'\.frontend$',
39 r'\.frontend$',
40 # Deprecated:
40 # Deprecated:
41 r'\.core\.magics\.deprecated',
41 r'\.core\.magics\.deprecated',
42 # We document this manually.
42 # We document this manually.
43 r'\.utils\.py3compat',
43 r'\.utils\.py3compat',
44 # These are exposed by nbformat.current
44 # These are exposed by nbformat
45 r'\.nbformat\.convert',
45 r'\.nbformat\.convert',
46 r'\.nbformat\.validator',
46 r'\.nbformat\.validator',
47 r'\.nbformat\.notebooknode',
48 # Deprecated
49 r'\.nbformat\.current',
50 # Exposed by nbformat.vN
51 r'\.nbformat\.v[3-4]\.nbbase',
47 # These are exposed in display
52 # These are exposed in display
48 r'\.core\.display',
53 r'\.core\.display',
49 r'\.lib\.display',
54 r'\.lib\.display',
50 # This isn't actually a module
55 # This isn't actually a module
51 r'\.html\.tasks',
56 r'\.html\.tasks',
57 # This is private
58 r'\.html\.allow76'
52 ]
59 ]
53
60
54 # These modules import functions and classes from other places to expose
61 # These modules import functions and classes from other places to expose
55 # them as part of the public API. They must have __all__ defined. The
62 # them as part of the public API. They must have __all__ defined. The
56 # non-API modules they import from should be excluded by the skip patterns
63 # non-API modules they import from should be excluded by the skip patterns
57 # above.
64 # above.
58 docwriter.names_from__all__.update({
65 docwriter.names_from__all__.update({
59 'IPython.nbformat.current',
66 'IPython.nbformat',
67 'IPython.nbformat.v3',
68 'IPython.nbformat.v4',
60 'IPython.display',
69 'IPython.display',
61 })
70 })
62
71
63 # Now, generate the outputs
72 # Now, generate the outputs
64 docwriter.write_api_docs(outdir)
73 docwriter.write_api_docs(outdir)
65 # Write index with .txt extension - we can include it, but Sphinx won't try
74 # Write index with .txt extension - we can include it, but Sphinx won't try
66 # to compile it
75 # to compile it
67 docwriter.write_index(outdir, 'gen.txt',
76 docwriter.write_index(outdir, 'gen.txt',
68 relative_to = pjoin('source','api')
77 relative_to = pjoin('source','api')
69 )
78 )
70 print ('%d files written' % len(docwriter.written_modules))
79 print ('%d files written' % len(docwriter.written_modules))
General Comments 0
You need to be logged in to leave comments. Login now