ipynbdiff 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d87c49e29a069b5064985b1b1097ac4d02c375161d9fe64df8e70f0155419220
4
- data.tar.gz: 97931332962139f7f129a546bb7a7f9b670d779dcb032b27fbb1a3faa6f7dc85
3
+ metadata.gz: 54c011ed4790dc548b33d6e4773bb9bbdaedd7b8c1ffbdbcfa5e494e5380b1a9
4
+ data.tar.gz: 70f78c722ddc432576ec52d55617e4ce5bdf29cb7ff32ad46219ac52d0d6f5d5
5
5
  SHA512:
6
- metadata.gz: 86c4be0fc53e0db2172221a17dd07e0c3f37492ffc699075b0de74dea3265feebee2207ef4e9170ead59be15c42b858918fbd2e56c7ed109cb674fbf7eb7dbbb
7
- data.tar.gz: 10fdb124114a73425edc57f5c9fb80fbd546f6b8e7d58929a327f1be268774d8409238630286aa050e41384bf53c951d2b8c67aaeb76835bc3e0f37672775d48
6
+ metadata.gz: 757f9ea284f358771835db2e27ebe90cb5c8c99336313a65d04cbd0080c0891e1c25af79740cb4d191eeafec10e66c74baceef1ba24729b6ba6f743d502564fa
7
+ data.tar.gz: 35866e056ae8f4ab043bd9cc247f6c3f8dc3b02d475e94221ca11352badf1e96cf840e8020553ffa8c32b1359f967c74ccc67e0f4abda376e8a035d027871daf
data/README.md CHANGED
@@ -32,6 +32,7 @@ Options:
32
32
  write_output_to: nil, # Pass a path to save the output to a file
33
33
  format: :text, # These are the formats Diffy accepts https://github.com/samg/diffy
34
34
  sources_are_files: FALSE, # Weather to use the from/to as string or path to a file
35
+ raise_if_invalid_notebook: FALSE, # Raises an error if the notebooks are invalid, otherwise returns nil
35
36
  transform_options: @default_transform_options, # See below for transform options
36
37
  diff_opts: {
37
38
  include_diff_info: FALSE # These are passed to Diffy https://github.com/samg/diffy
data/ipynbdiff.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'ipynbdiff'
5
- s.version = '0.3.4'
5
+ s.version = '0.3.5'
6
6
  s.summary = 'Human Readable diffs for Jupyter Notebooks'
7
7
  s.description = 'Better diff for Jupyter Notebooks by first preprocessing them and removing clutter'
8
8
  s.authors = ['Eduardo Bonet']
data/lib/ipynbdiff.rb CHANGED
@@ -15,6 +15,7 @@ module IpynbDiff
15
15
  write_output_to: nil,
16
16
  format: :text,
17
17
  sources_are_files: FALSE,
18
+ raise_if_invalid_notebook: FALSE,
18
19
  transform_options: @default_transform_options,
19
20
  diff_opts: {
20
21
  include_diff_info: FALSE
@@ -22,32 +23,36 @@ module IpynbDiff
22
23
  }.freeze
23
24
 
24
25
  def self.prepare_input(to_prepare, options)
25
- prepared = to_prepare
26
- prepared = File.read(prepared) if options[:sources_are_files]
27
- prepared = transform(prepared, options[:transform_options]) if options[:preprocess_input]
26
+ return '' unless to_prepare
28
27
 
29
- prepared
28
+ prep = to_prepare
29
+ prep = File.read(prep) if options[:sources_are_files]
30
+ prep = transform(prep, raise_errors: TRUE, options: options[:transform_options]) if options[:preprocess_input]
31
+ prep
30
32
  end
31
33
 
32
34
  def self.diff(
33
35
  from_notebook,
34
36
  to_notebook,
35
- options = {}
37
+ options = @default_diff_options
36
38
  )
37
39
  options = @default_diff_options.merge(options)
38
40
 
39
- from = from_notebook && prepare_input(from_notebook, options) || ''
40
- to = to_notebook && prepare_input(to_notebook, options) || ''
41
+ from = prepare_input(from_notebook, options)
42
+ to = prepare_input(to_notebook, options)
41
43
 
42
44
  d = Diffy::Diff.new(from, to, **options[:diff_opts]).to_s(options[:format])
43
-
44
45
  File.write(options[:write_output_to], d) if options[:write_output_to]
45
-
46
46
  d
47
+ rescue InvalidNotebookError
48
+ raise if options[:raise_if_invalid_notebook]
47
49
  end
48
50
 
49
- def self.transform(notebook, options)
51
+ def self.transform(notebook, raise_errors: FALSE, options: @default_transform_options)
50
52
  options = @default_transform_options.merge(options)
53
+
51
54
  Transformer.new(**options).transform(notebook)
55
+ rescue InvalidNotebookError
56
+ raise if raise_errors
52
57
  end
53
58
  end
data/lib/transformer.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IpynbDiff
4
+ class InvalidNotebookError < StandardError
5
+ end
6
+
4
7
  # Returns a markdown version of the Jupyter Notebook
5
8
  class Transformer
6
9
  require 'json'
@@ -14,8 +17,18 @@ module IpynbDiff
14
17
  @cell_decorator = cell_decorator
15
18
  end
16
19
 
17
- def transform(notebook)
20
+ def validate_notebook(notebook)
18
21
  notebook_json = JSON.parse(notebook)
22
+
23
+ return notebook_json if notebook_json.key?('cells') && notebook_json.key?('metadata')
24
+
25
+ raise InvalidNotebookError
26
+ rescue JSON::ParserError
27
+ raise InvalidNotebookError
28
+ end
29
+
30
+ def transform(notebook)
31
+ notebook_json = validate_notebook(notebook)
19
32
  transformed_blocks = notebook_json['cells'].map do |cell|
20
33
  decorate_cell(transform_cell(cell, notebook_json), cell)
21
34
  end
@@ -26,7 +39,7 @@ module IpynbDiff
26
39
 
27
40
  def decorate_cell(rows, cell)
28
41
  tags = cell['metadata']&.fetch('tags', [])
29
- type = cell['cell_type']
42
+ type = cell['cell_type'] || 'raw'
30
43
 
31
44
  case @cell_decorator
32
45
  when :html
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ipynbdiff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Bonet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-18 00:00:00.000000000 Z
11
+ date: 2021-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: diffy