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

30 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-01 06:54 +0000

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. 

5 

6import json 

7 

8from .validator import ValidationError 

9 

10 

11class NotJSONError(ValueError): 

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

13 

14 pass 

15 

16 

17def parse_json(s, **kwargs): 

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

19 try: 

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

21 except ValueError as e: 

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

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

24 if len(message) > 80: # noqa 

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

26 raise NotJSONError(message) from e 

27 return nb_dict 

28 

29 

30# High level API 

31 

32 

33def get_version(nb): 

34 """Get the version of a notebook. 

35 

36 Parameters 

37 ---------- 

38 nb : dict 

39 NotebookNode or dict containing notebook data. 

40 

41 Returns 

42 ------- 

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

44 """ 

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

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

47 return (major, minor) 

48 

49 

50def reads(s, **kwargs): 

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

52 NotebookNode object. 

53 

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

55 conversion is performed. 

56 

57 Parameters 

58 ---------- 

59 s : unicode | bytes 

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

61 

62 Returns 

63 ------- 

64 nb : NotebookNode 

65 The notebook that was read. 

66 

67 Raises 

68 ------ 

69 ValidationError 

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

71 NBFormatError 

72 Specified major version is invalid or unsupported. 

73 """ 

74 from . import NBFormatError, versions 

75 

76 nb_dict = parse_json(s, **kwargs) 

77 (major, minor) = get_version(nb_dict) 

78 if major in versions: 

79 try: 

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

81 except AttributeError as e: 

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

83 raise ValidationError(msg) from None 

84 else: 

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

86 

87 

88def read(fp, **kwargs): 

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

90 

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

92 conversion is performed. 

93 

94 Parameters 

95 ---------- 

96 fp : file 

97 Any file-like object with a read method. 

98 

99 Returns 

100 ------- 

101 nb : NotebookNode 

102 The notebook that was read. 

103 """ 

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