__init__.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.
  5. __all__ = ['NotebookNode', 'new_code_cell', 'new_text_cell', 'new_notebook',
  6. 'new_output', 'new_worksheet', 'new_metadata', 'new_author',
  7. 'new_heading_cell', 'nbformat', 'nbformat_minor', 'nbformat_schema',
  8. 'reads_json', 'writes_json', 'read_json', 'write_json',
  9. 'to_notebook_json', 'reads_py', 'writes_py', 'read_py', 'write_py',
  10. 'to_notebook_py', 'downgrade', 'upgrade', 'parse_filename'
  11. ]
  12. import os
  13. from .nbbase import (
  14. NotebookNode,
  15. new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
  16. new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor,
  17. nbformat_schema
  18. )
  19. from .nbjson import reads as reads_json, writes as writes_json
  20. from .nbjson import reads as read_json, writes as write_json
  21. from .nbjson import to_notebook as to_notebook_json
  22. from .nbpy import reads as reads_py, writes as writes_py
  23. from .nbpy import reads as read_py, writes as write_py
  24. from .nbpy import to_notebook as to_notebook_py
  25. from .convert import downgrade, upgrade
  26. def parse_filename(fname):
  27. """Parse a notebook filename.
  28. This function takes a notebook filename and returns the notebook
  29. format (json/py) and the notebook name. This logic can be
  30. summarized as follows:
  31. * notebook.ipynb -> (notebook.ipynb, notebook, json)
  32. * notebook.json -> (notebook.json, notebook, json)
  33. * notebook.py -> (notebook.py, notebook, py)
  34. * notebook -> (notebook.ipynb, notebook, json)
  35. Parameters
  36. ----------
  37. fname : unicode
  38. The notebook filename. The filename can use a specific filename
  39. extention (.ipynb, .json, .py) or none, in which case .ipynb will
  40. be assumed.
  41. Returns
  42. -------
  43. (fname, name, format) : (unicode, unicode, unicode)
  44. The filename, notebook name and format.
  45. """
  46. basename, ext = os.path.splitext(fname)
  47. if ext == u'.ipynb':
  48. format = u'json'
  49. elif ext == u'.json':
  50. format = u'json'
  51. elif ext == u'.py':
  52. format = u'py'
  53. else:
  54. basename = fname
  55. fname = fname + u'.ipynb'
  56. format = u'json'
  57. return fname, basename, format