1"""The main API for the v3 notebook format."""
2
3# Copyright (c) IPython Development Team.
4# Distributed under the terms of the Modified BSD License.
5from __future__ import annotations
6
7__all__ = [
8 "NotebookNode",
9 "new_code_cell",
10 "new_text_cell",
11 "new_notebook",
12 "new_output",
13 "new_worksheet",
14 "new_metadata",
15 "new_author",
16 "new_heading_cell",
17 "nbformat",
18 "nbformat_minor",
19 "nbformat_schema",
20 "reads_json",
21 "writes_json",
22 "read_json",
23 "write_json",
24 "to_notebook_json",
25 "reads_py",
26 "writes_py",
27 "read_py",
28 "write_py",
29 "to_notebook_py",
30 "downgrade",
31 "upgrade",
32 "parse_filename",
33]
34
35import os
36
37from .convert import downgrade, upgrade
38from .nbbase import (
39 NotebookNode,
40 nbformat,
41 nbformat_minor,
42 nbformat_schema,
43 new_author,
44 new_code_cell,
45 new_heading_cell,
46 new_metadata,
47 new_notebook,
48 new_output,
49 new_text_cell,
50 new_worksheet,
51)
52from .nbjson import reads as read_json
53from .nbjson import reads as reads_json
54from .nbjson import to_notebook as to_notebook_json
55from .nbjson import writes as write_json
56from .nbjson import writes as writes_json
57from .nbpy import reads as read_py
58from .nbpy import reads as reads_py
59from .nbpy import to_notebook as to_notebook_py
60from .nbpy import writes as write_py
61from .nbpy import writes as writes_py
62
63
64def parse_filename(fname):
65 """Parse a notebook filename.
66
67 This function takes a notebook filename and returns the notebook
68 format (json/py) and the notebook name. This logic can be
69 summarized as follows:
70
71 * notebook.ipynb -> (notebook.ipynb, notebook, json)
72 * notebook.json -> (notebook.json, notebook, json)
73 * notebook.py -> (notebook.py, notebook, py)
74 * notebook -> (notebook.ipynb, notebook, json)
75
76 Parameters
77 ----------
78 fname : unicode
79 The notebook filename. The filename can use a specific filename
80 extension (.ipynb, .json, .py) or none, in which case .ipynb will
81 be assumed.
82
83 Returns
84 -------
85 (fname, name, format) : (unicode, unicode, unicode)
86 The filename, notebook name and format.
87 """
88 basename, ext = os.path.splitext(fname) # noqa: PTH122
89 if ext in [".ipynb", ".json"]:
90 format_ = "json"
91 elif ext == ".py":
92 format_ = "py"
93 else:
94 basename = fname
95 fname = fname + ".ipynb"
96 format_ = "json"
97 return fname, basename, format_