__init__.py 2.7 KB

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