__init__.py 2.5 KB

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