fileconv 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/Gemfile +8 -0
  4. data/Gemfile.lock +22 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +231 -0
  7. data/Rakefile +11 -0
  8. data/bin/console +14 -0
  9. data/bin/setup +8 -0
  10. data/example/csv/add_column.rb +14 -0
  11. data/example/csv/add_header.rb +14 -0
  12. data/example/csv/output/test2.txt +4 -0
  13. data/example/csv/test.csv +3 -0
  14. data/example/csv/test2.txt +4 -0
  15. data/example/csv/with_header.rb +26 -0
  16. data/example/data/list_file.rb +24 -0
  17. data/example/data/output/result.txt +3 -0
  18. data/example/data/test.data +3 -0
  19. data/example/file/output/test.data +7 -0
  20. data/example/file/read_bytes.rb +20 -0
  21. data/example/file/test.data +1 -0
  22. data/example/json/address.json +1 -0
  23. data/example/json/modify_json.rb +18 -0
  24. data/example/json/output/address.json +1 -0
  25. data/example/json/pretty_json.rb +11 -0
  26. data/example/line/add_lineno.rb +19 -0
  27. data/example/line/count_lines.rb +14 -0
  28. data/example/line/filter_lines.rb +43 -0
  29. data/example/line/input_files.rb +22 -0
  30. data/example/line/modify_new_line.rb +14 -0
  31. data/example/line/output/test1.txt +5 -0
  32. data/example/line/output/test2.txt +7 -0
  33. data/example/line/output/test3.txt +10 -0
  34. data/example/line/output/test4.txt +5 -0
  35. data/example/line/sort_lines.rb +17 -0
  36. data/example/line/test1.txt +6 -0
  37. data/example/line/test2.txt +7 -0
  38. data/example/line/test3.txt +10 -0
  39. data/example/line/test4.txt +1 -0
  40. data/example/line/uniq_line.rb +32 -0
  41. data/example/meta_convertor/csv2json.rb +53 -0
  42. data/example/meta_convertor/output/test.csv +17 -0
  43. data/example/meta_convertor/test.csv +4 -0
  44. data/fileconv.gemspec +30 -0
  45. data/lib/fileconv/base.rb +212 -0
  46. data/lib/fileconv/csv.rb +39 -0
  47. data/lib/fileconv/data.rb +7 -0
  48. data/lib/fileconv/file.rb +20 -0
  49. data/lib/fileconv/json.rb +26 -0
  50. data/lib/fileconv/line.rb +25 -0
  51. data/lib/fileconv/version.rb +3 -0
  52. data/lib/fileconv.rb +10 -0
  53. metadata +124 -0
@@ -0,0 +1,17 @@
1
+ require 'fileconv'
2
+
3
+ class SortLines
4
+ include Fileconv::Line
5
+ def input_ext
6
+ "txt"
7
+ end
8
+
9
+ def convert_file(data, acc)
10
+ if @opts[:debug]
11
+ p acc
12
+ end
13
+ data.sort
14
+ end
15
+ end
16
+
17
+ SortLines.new.conv(debug: true)
@@ -0,0 +1,6 @@
1
+ aaa
2
+ bbb
3
+ ccc
4
+ ddd
5
+ 111
6
+
@@ -0,0 +1,7 @@
1
+ aaaa
2
+ 126
3
+ pppppp
4
+ rrrr
5
+ 2323
6
+ ddds
7
+ aaaa
@@ -0,0 +1,10 @@
1
+ sssgg
2
+ ggg
3
+ ggg
4
+ sssgg
5
+ ttt
6
+ 2281
7
+ kkk
8
+ sssgg
9
+ 6656
10
+ ddd
@@ -0,0 +1 @@
1
+ ccc
@@ -0,0 +1,32 @@
1
+ require 'fileconv'
2
+
3
+ class UniqLine
4
+ include Fileconv::Line
5
+ def input_ext
6
+ "txt"
7
+ end
8
+
9
+ def init_acc(acc)
10
+ acc[:lines] = {}
11
+ acc[:dup_num] = 0
12
+ end
13
+
14
+ def convert_line(line, acc)
15
+ if acc[:lines][line]
16
+ acc[:dup_num] += 1
17
+ else
18
+ acc[:lines][line] = true
19
+ line
20
+ end
21
+ end
22
+
23
+ def convert_file(data, acc)
24
+ if @opts[:debug]
25
+ puts "#{acc[:orig_filename]} dup num= #{acc[:dup_num]}"
26
+ end
27
+
28
+ data
29
+ end
30
+ end
31
+
32
+ UniqLine.new.conv(debug: true)
@@ -0,0 +1,53 @@
1
+ require 'fileconv'
2
+ require 'csv'
3
+ require 'json'
4
+
5
+ module Fileconv
6
+ module CSV2JSON
7
+ include Fileconv::Base
8
+
9
+ def pre_init_conv
10
+ @opts[:line_mode] = true
11
+ @opts[:read_csv_opts] ||= {}
12
+ @opts[:write_csv_opts] ||= {}
13
+ @opts[:read_csv_opts][:headers] = true
14
+ @opts[:read_csv_opts][:write_headers] = true
15
+ end
16
+
17
+ def pre_convert_file(data, acc)
18
+ ::CSV.parse(data, @opts[:read_csv_opts])
19
+ end
20
+
21
+ def post_convert_line(line, acc)
22
+ return unless line
23
+
24
+ if line.is_a? ::CSV::Row
25
+ [line]
26
+ elsif line[0].is_a? Array
27
+ line
28
+ else
29
+ [line]
30
+ end
31
+ end
32
+
33
+ def post_convert_file(rows, acc)
34
+ return unless rows
35
+ obj = rows.map{|e| e.to_hash}
36
+ if @opts[:pretty_json]
37
+ ::JSON.pretty_generate(obj, @opts[:write_json_opts])
38
+ else
39
+ ::JSON.generate(obj, @opts[:write_json_opts])
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ class TestCSV2JSON
46
+ include Fileconv::CSV2JSON
47
+
48
+ def input_ext
49
+ "csv"
50
+ end
51
+ end
52
+
53
+ TestCSV2JSON.new.conv(pretty_json: true)
@@ -0,0 +1,17 @@
1
+ [
2
+ {
3
+ "Name": "Taro",
4
+ "Age": "22",
5
+ "Check": "true"
6
+ },
7
+ {
8
+ "Name": "Hanako",
9
+ "Age": "18",
10
+ "Check": "false"
11
+ },
12
+ {
13
+ "Name": "Hideki",
14
+ "Age": "6",
15
+ "Check": "false"
16
+ }
17
+ ]
@@ -0,0 +1,4 @@
1
+ Name,Age,Check
2
+ Taro,22,true
3
+ Hanako,18,false
4
+ Hideki,6,false
data/fileconv.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "fileconv/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "fileconv"
7
+ spec.version = Fileconv::VERSION
8
+ spec.authors = ["hinastory"]
9
+ spec.email = ["1696779+hinastory@users.noreply.github.com"]
10
+
11
+ spec.summary = %q{Extensible multi-file convertor.}
12
+ spec.description = %q{Extensible multi-file convertor.}
13
+ spec.homepage = "https://github.com/hinastory/fileconv"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/hinastory/fileconv"
18
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+
26
+ spec.require_paths = ["lib"]
27
+ spec.required_ruby_version = ">= 2.3.0"
28
+ spec.add_development_dependency "bundler", "~> 2.0"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ end
@@ -0,0 +1,212 @@
1
+ module Fileconv
2
+ module MetaConvertor
3
+ # Pre hook for {Convertor#init_conv}
4
+ def pre_init_conv
5
+ end
6
+
7
+ # Post hook for {Convertor#init_conv}
8
+ def post_init_conv
9
+ end
10
+
11
+ # Pre hook for {Convertor#init_acc}
12
+ def pre_init_acc(acc)
13
+ end
14
+
15
+ # Post hook for {Convertor#init_acc}
16
+ def post_init_acc(acc)
17
+ end
18
+
19
+ # Pre hook for {Convertor#convert_file}
20
+ def pre_convert_file(file, acc)
21
+ file
22
+ end
23
+
24
+ # Pre hook for {Convertor#convert_line}
25
+ def pre_convert_line(line, acc)
26
+ line
27
+ end
28
+
29
+ # Post hook for {Convertor#convert_line}
30
+ def post_convert_line(line, acc)
31
+ line
32
+ end
33
+
34
+ # Pre hook for {Convertor#convert_file}
35
+ def post_convert_file(file, acc)
36
+ file
37
+ end
38
+
39
+ # Pre hook for {Convertor#conv_result}
40
+ def pre_conv_result
41
+ end
42
+
43
+ # Post hook for {Convertor#conv_result}
44
+ def post_conv_result(result)
45
+ result
46
+ end
47
+ end
48
+
49
+ module Convertor
50
+ # Input Directory
51
+ # @return [String]
52
+ def input_dir
53
+ @opts[:input_dir] || "."
54
+ end
55
+
56
+ # File extention for input file
57
+ # @return [String,nil]
58
+ # @note target all files if this method return nil
59
+ def input_ext
60
+ @opts[:input_ext]
61
+ end
62
+
63
+ # Output Directory
64
+ # @return [String]
65
+ def output_dir
66
+ @opts[:output_dir] || "output"
67
+ end
68
+
69
+ # Input files
70
+ # @return [Array<String>,nil]
71
+ # @note ignore {#input_dir} if this method return non-nil
72
+ # @note select files by {#input_dir} and {#input_ext} if this method return nil
73
+ def input_files
74
+ end
75
+
76
+ # Initialize the convertor
77
+ # @return [void]
78
+ def init_conv
79
+ end
80
+
81
+ # Init a accumlator
82
+ # @param [Hash] acc the accumulator for this file
83
+ # @return [void]
84
+ def init_acc(acc)
85
+ end
86
+
87
+ # Read a file
88
+ # @param [Hash] acc the accumulator for this file
89
+ # @return [Object,nil]
90
+ # @note use default reader if this method return nil
91
+ def read_file(filename, acc)
92
+ end
93
+
94
+ # Convert a Line
95
+ # @param [Object] line the line object
96
+ # @param [Hash] acc the accumulator for this file
97
+ # @return [Object, Array, nil] the converted line
98
+ # @note add the line object add all lines if this method return Array and don't add it if this method return nil
99
+ def convert_line(line, acc)
100
+ line
101
+ end
102
+
103
+ # Convert a File
104
+ # @param [Object] file the file object
105
+ # @param [Hash] acc the accumulator for this file
106
+ # @return [String, nil]
107
+ # @note output the string to a file({#output_filename}) and don't create file if this method return nil
108
+ def convert_file(file, acc)
109
+ file
110
+ end
111
+
112
+ # File name for the output
113
+ # @param [String] filename original base file name
114
+ # @param [Hash] acc the accumulator for this file
115
+ # @return [String] the output file name
116
+ def output_filename(filename, acc)
117
+ filename
118
+ end
119
+
120
+ # File name for the result
121
+ # @return [String] the result file name
122
+ def result_filename
123
+ @opts[:result_file] || "result.txt"
124
+ end
125
+
126
+ # Conversion result
127
+ # @return [String, nil] the result
128
+ # @note output the string result to a file({#result_filename}) and don't create file if this method return nil(default)
129
+ # @note you can create result data using @meta or anather instance variable you create
130
+ def conv_result
131
+ end
132
+ end
133
+
134
+ module Base
135
+ include MetaConvertor
136
+ include Convertor
137
+
138
+ def process_file(filename)
139
+ if FileTest.directory? filename
140
+ return
141
+ end
142
+
143
+ acc = {orig_filename: filename}
144
+ pre_init_acc(acc)
145
+ init_acc(acc)
146
+ post_init_acc(acc)
147
+
148
+ raw = nil
149
+ unless @opts[:disable_read_file]
150
+ raw = read_file(filename, acc)
151
+ raw = ::File.read(filename, @opts[:read_file_opts]) unless raw
152
+ end
153
+
154
+ pre_converted_file = pre_convert_file(raw, acc)
155
+ if @opts[:line_mode]
156
+ # Line mode
157
+ pre_converted_file = process_line(pre_converted_file, acc)
158
+ end
159
+
160
+ converted_file = convert_file(pre_converted_file, acc)
161
+ post_converted_file = post_convert_file(converted_file, acc)
162
+ if post_converted_file
163
+ out = output_filename(::File.basename(filename), acc)
164
+ Dir.mkdir output_dir if !Dir.exists? output_dir
165
+ ::File.write(::File.join(output_dir, out), post_converted_file, @opts[:read_file_opts])
166
+ end
167
+ end
168
+
169
+ def process_line(raw_lines, acc)
170
+ lines = []
171
+ raw_lines.each do |line|
172
+ pre_converted_line = pre_convert_line(line, acc)
173
+ converted_line = convert_line(pre_converted_line, acc)
174
+ post_converted_line = post_convert_line(converted_line, acc)
175
+ lines.push *post_converted_line
176
+ end
177
+ lines
178
+ end
179
+
180
+ # Run file conversion
181
+ def conv(**opts)
182
+ @meta = {}
183
+ @opts = opts
184
+ @opts[:read_file_opts] ||= {}
185
+ @opts[:write_file_opts] ||= {}
186
+
187
+ pre_init_conv()
188
+ init_conv()
189
+ post_init_conv()
190
+
191
+ files = input_files()
192
+ unless files
193
+ glob = input_ext ? "*." + input_ext : "*"
194
+ files = Dir.glob(::File.join(input_dir, glob))
195
+ end
196
+
197
+ files.each do |filename|
198
+ process_file(filename)
199
+ end
200
+
201
+ pre_conv_result
202
+ result = conv_result
203
+ post_result = post_conv_result(result)
204
+ if post_result
205
+ Dir.mkdir output_dir if !Dir.exists? output_dir
206
+ ::File.write(::File.join(output_dir, result_filename), post_result, @opts[:write_file_opts])
207
+ end
208
+ end
209
+
210
+ private :process_file, :process_line
211
+ end
212
+ end
@@ -0,0 +1,39 @@
1
+ require 'fileconv/base'
2
+ require 'csv'
3
+
4
+ module Fileconv
5
+ module CSV
6
+ include Fileconv::Base
7
+
8
+ def pre_init_conv
9
+ @opts[:line_mode] = true
10
+ @opts[:read_csv_opts] ||= {}
11
+ @opts[:write_csv_opts] ||= {}
12
+ end
13
+
14
+ def pre_convert_file(data, acc)
15
+ ::CSV.parse(data, @opts[:read_csv_opts])
16
+ end
17
+
18
+ def post_convert_line(line, acc)
19
+ return unless line
20
+
21
+ if line.is_a? ::CSV::Row
22
+ [line]
23
+ elsif line[0].is_a? Array
24
+ line
25
+ else
26
+ [line]
27
+ end
28
+ end
29
+
30
+ def post_convert_file(rows, acc)
31
+ return unless rows
32
+ ::CSV.generate("", @opts[:write_csv_opts]) do |csv|
33
+ rows.each do |row|
34
+ csv << row
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ require 'fileconv/base'
2
+
3
+ module Fileconv
4
+ module Data
5
+ include Fileconv::Base
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ require 'fileconv/base'
2
+
3
+ module Fileconv
4
+ module File
5
+ include Fileconv::Base
6
+
7
+ def pre_init_conv
8
+ @opts[:disable_read_file] = true
9
+ end
10
+
11
+ def pre_convert_file(data, acc)
12
+ acc[:opend_file] = ::File.open(acc[:orig_filename], @opts[:read_file_opts])
13
+ end
14
+
15
+ def post_convert_file(data, acc)
16
+ acc[:opend_file].close
17
+ data
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ require 'fileconv/base'
2
+ require 'json'
3
+
4
+ module Fileconv
5
+ module JSON
6
+ include Fileconv::Base
7
+
8
+ def pre_init_conv
9
+ @opts[:read_json_opts] ||= {}
10
+ @opts[:write_json_opts] ||= {}
11
+ end
12
+
13
+ def pre_convert_file(data, acc)
14
+ ::JSON.parse(data, @opts[:read_json_opts])
15
+ end
16
+
17
+ def post_convert_file(obj, acc)
18
+ return unless obj
19
+ if @opts[:pretty_json]
20
+ ::JSON.pretty_generate(obj, @opts[:write_json_opts])
21
+ else
22
+ ::JSON.generate(obj, @opts[:write_json_opts])
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ require 'fileconv/base'
2
+
3
+ module Fileconv
4
+ module Line
5
+ include Fileconv::Base
6
+
7
+ def pre_init_conv
8
+ @opts[:line_mode] = true
9
+ end
10
+
11
+ def pre_convert_file(data, acc)
12
+ unless @opts[:new_line]
13
+ data =~ /\R/
14
+ @opts[:new_line] = $1
15
+ @opts[:new_line] ||= "\n"
16
+ end
17
+
18
+ data.split(/\R/)
19
+ end
20
+
21
+ def post_convert_file(data, acc)
22
+ return data.join(@opts[:new_line]) if data
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Fileconv
2
+ VERSION = "0.1.0"
3
+ end
data/lib/fileconv.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "fileconv/version"
2
+
3
+ module Fileconv
4
+ autoload :Base, 'fileconv/base'
5
+ autoload :Line, 'fileconv/line'
6
+ autoload :CSV, 'fileconv/csv'
7
+ autoload :Data, 'fileconv/data'
8
+ autoload :File, 'fileconv/file'
9
+ autoload :JSON, 'fileconv/json'
10
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fileconv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hinastory
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Extensible multi-file convertor.
42
+ email:
43
+ - 1696779+hinastory@users.noreply.github.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - example/csv/add_column.rb
57
+ - example/csv/add_header.rb
58
+ - example/csv/output/test2.txt
59
+ - example/csv/test.csv
60
+ - example/csv/test2.txt
61
+ - example/csv/with_header.rb
62
+ - example/data/list_file.rb
63
+ - example/data/output/result.txt
64
+ - example/data/test.data
65
+ - example/file/output/test.data
66
+ - example/file/read_bytes.rb
67
+ - example/file/test.data
68
+ - example/json/address.json
69
+ - example/json/modify_json.rb
70
+ - example/json/output/address.json
71
+ - example/json/pretty_json.rb
72
+ - example/line/add_lineno.rb
73
+ - example/line/count_lines.rb
74
+ - example/line/filter_lines.rb
75
+ - example/line/input_files.rb
76
+ - example/line/modify_new_line.rb
77
+ - example/line/output/test1.txt
78
+ - example/line/output/test2.txt
79
+ - example/line/output/test3.txt
80
+ - example/line/output/test4.txt
81
+ - example/line/sort_lines.rb
82
+ - example/line/test1.txt
83
+ - example/line/test2.txt
84
+ - example/line/test3.txt
85
+ - example/line/test4.txt
86
+ - example/line/uniq_line.rb
87
+ - example/meta_convertor/csv2json.rb
88
+ - example/meta_convertor/output/test.csv
89
+ - example/meta_convertor/test.csv
90
+ - fileconv.gemspec
91
+ - lib/fileconv.rb
92
+ - lib/fileconv/base.rb
93
+ - lib/fileconv/csv.rb
94
+ - lib/fileconv/data.rb
95
+ - lib/fileconv/file.rb
96
+ - lib/fileconv/json.rb
97
+ - lib/fileconv/line.rb
98
+ - lib/fileconv/version.rb
99
+ homepage: https://github.com/hinastory/fileconv
100
+ licenses:
101
+ - MIT
102
+ metadata:
103
+ homepage_uri: https://github.com/hinastory/fileconv
104
+ source_code_uri: https://github.com/hinastory/fileconv
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 2.3.0
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubygems_version: 3.0.6
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Extensible multi-file convertor.
124
+ test_files: []