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