Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/v2/__init__.py: 68%
28 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +0000
1"""The main API for the v2 notebook format.
3Authors:
5* Brian Granger
6"""
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# -----------------------------------------------------------------------------
15# -----------------------------------------------------------------------------
16# Imports
17# -----------------------------------------------------------------------------
19import os
21from .convert import downgrade, upgrade
22from .nbbase import (
23 NotebookNode,
24 new_author,
25 new_code_cell,
26 new_metadata,
27 new_notebook,
28 new_output,
29 new_text_cell,
30 new_worksheet,
31)
32from .nbjson import reads as read_json
33from .nbjson import reads as reads_json
34from .nbjson import to_notebook as to_notebook_json
35from .nbjson import writes as write_json
36from .nbjson import writes as writes_json
37from .nbpy import reads as read_py
38from .nbpy import reads as reads_py
39from .nbpy import to_notebook as to_notebook_py
40from .nbpy import writes as write_py
41from .nbpy import writes as writes_py
43# Implementation removed, vulnerable to DoS attacks
44from .nbxml import reads as read_xml
45from .nbxml import reads as reads_xml
46from .nbxml import to_notebook as to_notebook_xml
48# -----------------------------------------------------------------------------
49# Code
50# -----------------------------------------------------------------------------
52nbformat = 2
53nbformat_minor = 0
56def parse_filename(fname):
57 """Parse a notebook filename.
59 This function takes a notebook filename and returns the notebook
60 format (json/py) and the notebook name. This logic can be
61 summarized as follows:
63 * notebook.ipynb -> (notebook.ipynb, notebook, json)
64 * notebook.json -> (notebook.json, notebook, json)
65 * notebook.py -> (notebook.py, notebook, py)
66 * notebook -> (notebook.ipynb, notebook, json)
68 Parameters
69 ----------
70 fname : unicode
71 The notebook filename. The filename can use a specific filename
72 extention (.ipynb, .json, .py) or none, in which case .ipynb will
73 be assumed.
75 Returns
76 -------
77 (fname, name, format) : (unicode, unicode, unicode)
78 The filename, notebook name and format.
79 """
80 basename, ext = os.path.splitext(fname)
81 if ext in [".ipynb", ".json"]:
82 format_ = "json"
83 elif ext == ".py":
84 format_ = "py"
85 else:
86 basename = fname
87 fname = fname + ".ipynb"
88 format_ = "json"
89 return fname, basename, format_