ipynbdiff 0.4.1 → 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitlab-ci.yml +25 -24
- data/ipynbdiff.gemspec +4 -1
- data/lib/ipynb_symbol_map.rb +66 -16
- data/lib/output_transformer.rb +10 -7
- data/lib/symbolized_markdown_helper.rb +4 -4
- data/lib/transformed_notebook.rb +3 -3
- data/lib/transformer.rb +7 -12
- data/lib/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef1021a6b462ff49958069aea089dabfacd7ded6858bfc49c581dfcc03330405
|
4
|
+
data.tar.gz: 93ecedada6270a3f217349ef073b8f52950793e2f2197d2a0ca3254a5c2acd02
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29cedcb6c846d5d320425e9edf0f5f1a30bbbcdbcfaeb648b6568a6c884d60120c40926a8b30632eeff7d5892654c8b0ebe0433e9a8f03d14d2b8e6ad407ce4d
|
7
|
+
data.tar.gz: 85b7b9e9b3390f1484426bdb75143aebbf1f7bd2083b0c939df7d4e1e83a43f9346231c25f925ef9187ce54612ab7d202327f197c0cdb8916457913d226c9807
|
data/.gitlab-ci.yml
CHANGED
@@ -1,42 +1,43 @@
|
|
1
|
+
# You can override the included template(s) by including variable overrides
|
2
|
+
# SAST customization: https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings
|
3
|
+
# Secret Detection customization: https://docs.gitlab.com/ee/user/application_security/secret_detection/#customizing-settings
|
4
|
+
# Dependency Scanning customization: https://docs.gitlab.com/ee/user/application_security/dependency_scanning/#customizing-the-dependency-scanning-settings
|
5
|
+
# Note that environment variables can be set in several places
|
6
|
+
# See https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence
|
1
7
|
image: ruby:2.7
|
2
|
-
|
3
8
|
stages:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
9
|
+
- test
|
10
|
+
- build
|
11
|
+
- rubygems
|
8
12
|
specs:
|
9
13
|
stage: test
|
10
14
|
script:
|
11
|
-
|
12
|
-
|
13
|
-
|
15
|
+
- bundle install
|
16
|
+
- bundle exec rspec
|
14
17
|
build-gem:
|
15
18
|
stage: build
|
16
19
|
script:
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
+
- bundle install
|
21
|
+
- cat .VERSION.TMPL | sed s/GEM_VERSION/0.0.0/ > lib/version.rb
|
22
|
+
- gem build ipynbdiff.gemspec
|
20
23
|
artifacts:
|
21
24
|
paths:
|
22
25
|
- ipynbdiff-0.0.0.gem
|
23
26
|
needs:
|
24
|
-
|
25
|
-
|
27
|
+
- specs
|
26
28
|
deploy-gem:
|
27
29
|
stage: rubygems
|
28
30
|
script:
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
- bundle install
|
32
|
+
- cat .VERSION.TMPL | sed s/GEM_VERSION/$CI_COMMIT_TAG/ > lib/version.rb
|
33
|
+
- gem build ipynbdiff.gemspec
|
34
|
+
- gem push ipynbdiff-$CI_COMMIT_TAG.gem
|
33
35
|
only:
|
34
|
-
|
36
|
+
- tags
|
35
37
|
except:
|
36
|
-
|
37
|
-
needs:
|
38
|
-
|
38
|
+
- branches
|
39
|
+
needs:
|
40
|
+
- build-gem
|
39
41
|
when: manual
|
40
|
-
|
41
|
-
|
42
|
-
|
42
|
+
include:
|
43
|
+
- template: Security/Dependency-Scanning.gitlab-ci.yml
|
data/ipynbdiff.gemspec
CHANGED
data/lib/ipynb_symbol_map.rb
CHANGED
@@ -7,8 +7,8 @@ module IpynbDiff
|
|
7
7
|
# Creates a symbol map for a ipynb file (JSON format)
|
8
8
|
class IpynbSymbolMap
|
9
9
|
class << self
|
10
|
-
def parse(notebook)
|
11
|
-
IpynbSymbolMap.new(notebook).parse('')
|
10
|
+
def parse(notebook, objects_to_ignore = [])
|
11
|
+
IpynbSymbolMap.new(notebook, objects_to_ignore).parse('')
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -18,11 +18,12 @@ module IpynbDiff
|
|
18
18
|
|
19
19
|
VALUE_STOPPERS = [',', '[', ']', '{', '}', *WHITESPACE_CHARS].freeze
|
20
20
|
|
21
|
-
def initialize(notebook)
|
21
|
+
def initialize(notebook, objects_to_ignore = [])
|
22
22
|
@chars = notebook.chars
|
23
23
|
@current_line = 0
|
24
24
|
@char_idx = 0
|
25
25
|
@results = {}
|
26
|
+
@objects_to_ignore = objects_to_ignore
|
26
27
|
end
|
27
28
|
|
28
29
|
def parse(prefix = '.')
|
@@ -69,11 +70,7 @@ module IpynbDiff
|
|
69
70
|
loop do
|
70
71
|
break if skip_beginning('}')
|
71
72
|
|
72
|
-
prop_name = parse_string
|
73
|
-
|
74
|
-
new_prefix = "#{prefix}.#{prop_name}"
|
75
|
-
|
76
|
-
add_result(new_prefix, current_line)
|
73
|
+
prop_name = parse_string(return_value: true)
|
77
74
|
|
78
75
|
next_and_skip_whitespaces
|
79
76
|
|
@@ -81,24 +78,34 @@ module IpynbDiff
|
|
81
78
|
|
82
79
|
next_and_skip_whitespaces
|
83
80
|
|
84
|
-
|
81
|
+
if @objects_to_ignore.include? prop_name
|
82
|
+
skip
|
83
|
+
else
|
84
|
+
new_prefix = "#{prefix}.#{prop_name}"
|
85
|
+
|
86
|
+
add_result(new_prefix, current_line)
|
87
|
+
|
88
|
+
parse(new_prefix)
|
89
|
+
end
|
85
90
|
end
|
86
91
|
end
|
87
92
|
|
88
|
-
def parse_string
|
89
|
-
value = ''
|
90
|
-
prev_char = nil
|
91
|
-
|
93
|
+
def parse_string(return_value: false)
|
92
94
|
current_should_be '"'
|
95
|
+
init_idx = @char_idx
|
93
96
|
|
94
97
|
loop do
|
95
98
|
increment_char_index
|
96
|
-
break if (c = current_char) == '"' && prev_char != '\\'
|
97
99
|
|
98
|
-
|
100
|
+
break if @char_idx > @chars.length
|
101
|
+
|
102
|
+
if current_char == '"' && prev_char != '\\'
|
103
|
+
init_idx += 1
|
104
|
+
break
|
105
|
+
end
|
99
106
|
end
|
100
107
|
|
101
|
-
|
108
|
+
@chars[init_idx...@char_idx].join if return_value
|
102
109
|
end
|
103
110
|
|
104
111
|
def add_result(key, line_number)
|
@@ -129,6 +136,10 @@ module IpynbDiff
|
|
129
136
|
@chars[@char_idx]
|
130
137
|
end
|
131
138
|
|
139
|
+
def prev_char
|
140
|
+
@chars[@char_idx - 1]
|
141
|
+
end
|
142
|
+
|
132
143
|
def current_should_be(another_char)
|
133
144
|
raise InvalidTokenError unless current_char == another_char
|
134
145
|
end
|
@@ -137,6 +148,45 @@ module IpynbDiff
|
|
137
148
|
@current_line += 1 if current_char == "\n"
|
138
149
|
end
|
139
150
|
|
151
|
+
def skip
|
152
|
+
skip_whitespaces
|
153
|
+
|
154
|
+
if (c = current_char) == '"'
|
155
|
+
parse_string
|
156
|
+
elsif c == '['
|
157
|
+
skip_array
|
158
|
+
elsif c == '{'
|
159
|
+
skip_object
|
160
|
+
else
|
161
|
+
parse_value
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def skip_array
|
166
|
+
loop do
|
167
|
+
break if skip_beginning(']')
|
168
|
+
|
169
|
+
skip
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def skip_object
|
174
|
+
|
175
|
+
loop do
|
176
|
+
break if skip_beginning('}')
|
177
|
+
|
178
|
+
parse_string
|
179
|
+
|
180
|
+
next_and_skip_whitespaces
|
181
|
+
|
182
|
+
current_should_be ':'
|
183
|
+
|
184
|
+
next_and_skip_whitespaces
|
185
|
+
|
186
|
+
skip
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
140
190
|
def skip_beginning(closing_char)
|
141
191
|
|
142
192
|
check_for_new_line
|
data/lib/output_transformer.rb
CHANGED
@@ -8,7 +8,8 @@ module IpynbDiff
|
|
8
8
|
|
9
9
|
ORDERED_KEYS = {
|
10
10
|
'execute_result' => %w[image/png image/svg+xml image/jpeg text/markdown text/latex text/plain],
|
11
|
-
'display_data' => %w[image/png image/svg+xml image/jpeg text/markdown text/latex]
|
11
|
+
'display_data' => %w[image/png image/svg+xml image/jpeg text/markdown text/latex],
|
12
|
+
'stream' => %w[text]
|
12
13
|
}.freeze
|
13
14
|
|
14
15
|
def transform(output, symbol)
|
@@ -17,15 +18,17 @@ module IpynbDiff
|
|
17
18
|
transform_error(output['traceback'], symbol / 'traceback')
|
18
19
|
when 'execute_result', 'display_data'
|
19
20
|
transform_non_error(ORDERED_KEYS[output_type], output['data'], symbol / 'data')
|
21
|
+
when 'stream'
|
22
|
+
transform_element('text', output['text'], symbol)
|
20
23
|
end
|
21
24
|
|
22
|
-
decorate_output(transformed, output, symbol)
|
25
|
+
transformed ? decorate_output(transformed, output, symbol) : []
|
23
26
|
end
|
24
27
|
|
25
28
|
def decorate_output(output_rows, output, symbol)
|
26
29
|
[
|
27
30
|
_,
|
28
|
-
symbol, %(%%%% Output: #{output['output_type']}),
|
31
|
+
_(symbol, %(%%%% Output: #{output['output_type']})),
|
29
32
|
_,
|
30
33
|
*output_rows
|
31
34
|
]
|
@@ -34,7 +37,7 @@ module IpynbDiff
|
|
34
37
|
def transform_error(traceback, symbol)
|
35
38
|
traceback.map.with_index do |t, idx|
|
36
39
|
t.split("\n").map do |l|
|
37
|
-
|
40
|
+
_(symbol / idx, l.gsub(/\[[0-9][0-9;]*m/, '').sub("\u001B", ' ').gsub(/\u001B/, '').rstrip)
|
38
41
|
end
|
39
42
|
end
|
40
43
|
end
|
@@ -52,13 +55,13 @@ module IpynbDiff
|
|
52
55
|
transform_image(output_type, output_element, new_symbol)
|
53
56
|
when 'image/svg+xml'
|
54
57
|
transform_svg(output_element, new_symbol)
|
55
|
-
when 'text/markdown', 'text/latex', 'text/plain'
|
58
|
+
when 'text/markdown', 'text/latex', 'text/plain', 'text'
|
56
59
|
transform_text(output_element, new_symbol)
|
57
60
|
end
|
58
61
|
end
|
59
62
|
|
60
63
|
def transform_image(image_type, image_content, symbol)
|
61
|
-
|
64
|
+
_(symbol, " ![](data:#{image_type};base64,#{image_content.gsub("\n", '')})")
|
62
65
|
end
|
63
66
|
|
64
67
|
def transform_svg(image_content, symbol)
|
@@ -66,7 +69,7 @@ module IpynbDiff
|
|
66
69
|
|
67
70
|
single_line = lines.map(&:strip).join.gsub(/\s+/, ' ')
|
68
71
|
|
69
|
-
|
72
|
+
_(symbol, " ![](data:image/svg+xml;utf8,#{single_line})")
|
70
73
|
end
|
71
74
|
|
72
75
|
def transform_text(text_content, symbol)
|
@@ -4,8 +4,8 @@ module IpynbDiff
|
|
4
4
|
# Helper functions
|
5
5
|
module SymbolizedMarkdownHelper
|
6
6
|
|
7
|
-
def _(content = '')
|
8
|
-
|
7
|
+
def _(symbol = nil, content = '')
|
8
|
+
{ symbol: symbol, content: content }
|
9
9
|
end
|
10
10
|
|
11
11
|
def array_if_not_array(thing)
|
@@ -14,9 +14,9 @@ module IpynbDiff
|
|
14
14
|
|
15
15
|
def symbolize_array(symbol, content, &block)
|
16
16
|
if content.is_a?(Array)
|
17
|
-
content.map.with_index { |l, idx|
|
17
|
+
content.map.with_index { |l, idx| _(symbol / idx, block.call(l)) }
|
18
18
|
else
|
19
|
-
|
19
|
+
_(symbol, content)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
data/lib/transformed_notebook.rb
CHANGED
@@ -11,9 +11,9 @@ module IpynbDiff
|
|
11
11
|
|
12
12
|
private
|
13
13
|
|
14
|
-
def initialize(lines = [],
|
15
|
-
@blocks = lines.
|
16
|
-
{ content: line, source_symbol: symbol, source_line: symbol && symbol_map[symbol] }
|
14
|
+
def initialize(lines = [], symbol_map = {})
|
15
|
+
@blocks = lines.map do |line|
|
16
|
+
{ content: line[:content], source_symbol: (symbol = line[:symbol]), source_line: symbol && symbol_map[symbol] }
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
data/lib/transformer.rb
CHANGED
@@ -15,6 +15,7 @@ module IpynbDiff
|
|
15
15
|
include SymbolizedMarkdownHelper
|
16
16
|
|
17
17
|
@include_frontmatter = true
|
18
|
+
@objects_to_ignore = ['application/javascript', 'application/vnd.holoviews_load.v0+json']
|
18
19
|
|
19
20
|
def initialize(include_frontmatter: true)
|
20
21
|
@include_frontmatter = include_frontmatter
|
@@ -38,13 +39,7 @@ module IpynbDiff
|
|
38
39
|
transformed = transform_document(notebook_json)
|
39
40
|
symbol_map = IpynbSymbolMap.parse(notebook)
|
40
41
|
|
41
|
-
|
42
|
-
transformed.partition.each_with_index { |_el, i| i.even? }
|
43
|
-
else
|
44
|
-
[[], []]
|
45
|
-
end
|
46
|
-
|
47
|
-
TransformedNotebook.new(lines, symbols, symbol_map)
|
42
|
+
TransformedNotebook.new(transformed, symbol_map)
|
48
43
|
end
|
49
44
|
|
50
45
|
def transform_document(notebook)
|
@@ -63,9 +58,9 @@ module IpynbDiff
|
|
63
58
|
type = cell['cell_type'] || 'raw'
|
64
59
|
|
65
60
|
[
|
66
|
-
symbol, %(%% Cell type:#{type} id:#{cell['id']} tags:#{tags&.join(',')}),
|
61
|
+
_(symbol, %(%% Cell type:#{type} id:#{cell['id']} tags:#{tags&.join(',')})),
|
67
62
|
_,
|
68
|
-
|
63
|
+
rows,
|
69
64
|
_
|
70
65
|
]
|
71
66
|
end
|
@@ -76,9 +71,9 @@ module IpynbDiff
|
|
76
71
|
|
77
72
|
def transform_code_cell(cell, notebook, symbol)
|
78
73
|
[
|
79
|
-
symbol / 'source', %(``` #{notebook.dig('metadata', 'kernelspec', 'language') || ''}),
|
74
|
+
_(symbol / 'source', %(``` #{notebook.dig('metadata', 'kernelspec', 'language') || ''})),
|
80
75
|
symbolize_array(symbol / 'source', cell['source'], &:rstrip),
|
81
|
-
_('```'),
|
76
|
+
_(nil, '```'),
|
82
77
|
cell['outputs'].map.with_index do |output, idx|
|
83
78
|
@output_transformer.transform(output, symbol / ['outputs', idx])
|
84
79
|
end
|
@@ -99,7 +94,7 @@ module IpynbDiff
|
|
99
94
|
}
|
100
95
|
}.to_yaml
|
101
96
|
|
102
|
-
as_yaml.split("\n").map { |l| _(l) }.append(_('---'), _)
|
97
|
+
as_yaml.split("\n").map { |l| _(nil, l) }.append(_(nil, '---'), _)
|
103
98
|
end
|
104
99
|
end
|
105
100
|
end
|
data/lib/version.rb
CHANGED
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.4.
|
4
|
+
version: 0.4.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eduardo Bonet
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: diffy
|