Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/reader.py: 83%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

30 statements  

1"""API for reading notebooks of different versions""" 

2 

3# Copyright (c) IPython Development Team. 

4# Distributed under the terms of the Modified BSD License. 

5from __future__ import annotations 

6 

7import json 

8 

9from .validator import ValidationError 

10 

11 

12class NotJSONError(ValueError): 

13 """An error raised when an object is not valid JSON.""" 

14 

15 

16def parse_json(s, **kwargs): 

17 """Parse a JSON string into a dict.""" 

18 try: 

19 nb_dict = json.loads(s, **kwargs) 

20 except ValueError as e: 

21 message = f"Notebook does not appear to be JSON: {s!r}" 

22 # Limit the error message to 80 characters. Display whatever JSON will fit. 

23 if len(message) > 80: 

24 message = message[:77] + "..." 

25 raise NotJSONError(message) from e 

26 return nb_dict 

27 

28 

29# High level API 

30 

31 

32def get_version(nb): 

33 """Get the version of a notebook. 

34 

35 Parameters 

36 ---------- 

37 nb : dict 

38 NotebookNode or dict containing notebook data. 

39 

40 Returns 

41 ------- 

42 Tuple containing major (int) and minor (int) version numbers 

43 """ 

44 major = nb.get("nbformat", 1) 

45 minor = nb.get("nbformat_minor", 0) 

46 return (major, minor) 

47 

48 

49def reads(s, **kwargs): 

50 """Read a notebook from a json string and return the 

51 NotebookNode object. 

52 

53 This function properly reads notebooks of any version. No version 

54 conversion is performed. 

55 

56 Parameters 

57 ---------- 

58 s : unicode | bytes 

59 The raw string or bytes object to read the notebook from. 

60 

61 Returns 

62 ------- 

63 nb : NotebookNode 

64 The notebook that was read. 

65 

66 Raises 

67 ------ 

68 ValidationError 

69 Notebook JSON for a given version is missing an expected key and cannot be read. 

70 NBFormatError 

71 Specified major version is invalid or unsupported. 

72 """ 

73 from . import NBFormatError, versions 

74 

75 nb_dict = parse_json(s, **kwargs) 

76 (major, minor) = get_version(nb_dict) 

77 if major in versions: 

78 try: 

79 return versions[major].to_notebook_json(nb_dict, minor=minor) 

80 except AttributeError as e: 

81 msg = f"The notebook is invalid and is missing an expected key: {e}" 

82 raise ValidationError(msg) from None 

83 else: 

84 raise NBFormatError("Unsupported nbformat version %s" % major) 

85 

86 

87def read(fp, **kwargs): 

88 """Read a notebook from a file and return the NotebookNode object. 

89 

90 This function properly reads notebooks of any version. No version 

91 conversion is performed. 

92 

93 Parameters 

94 ---------- 

95 fp : file 

96 Any file-like object with a read method. 

97 

98 Returns 

99 ------- 

100 nb : NotebookNode 

101 The notebook that was read. 

102 """ 

103 return reads(fp.read(), **kwargs)