fileconv 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +34 -20
- data/example/line/input_files.rb +1 -1
- data/lib/fileconv/base.rb +40 -29
- data/lib/fileconv/version.rb +1 -1
- metadata +1 -9
- data/example/csv/output/test2.txt +0 -4
- data/example/file/output/test.data +0 -7
- data/example/json/output/address.json +0 -1
- data/example/line/output/test1.txt +0 -5
- data/example/line/output/test2.txt +0 -7
- data/example/line/output/test3.txt +0 -10
- data/example/line/output/test4.txt +0 -5
- data/example/meta_convertor/output/test.csv +0 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1b8f07c030f514fbed40b42a39c737579023a907f7e636d3fd37714017e80bc5
|
|
4
|
+
data.tar.gz: d0115ed9453377e2cda186096c55380031550906bded00c8e5fe86422e005d72
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 141302bfc7a4cc4a8b1bcb8fc6f97fb99064f1930621647602bc0f79c6db9fae6d5b13443b7880d31df614ce1dec27f17d70d90a3983d911efb4b8c69b1c5265
|
|
7
|
+
data.tar.gz: d420660694a1ec760285f38470e0b64cf4312b7fadb6edaef0791c63a503b165e150110300572989b75ecffb5d39b545bc5f96b232a5313707feafddafaceb62
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Fileconv
|
|
2
2
|
|
|
3
|
-
Extensible multi-file convertor. Simple text file, CSV file, JSON file binary file and so on.
|
|
3
|
+
Extensible multi-file convertor. Simple text file, CSV file, JSON file, binary file and so on.
|
|
4
4
|
|
|
5
5
|
`fileconv` gem is a simple to use and extensible library to convert multi-file format. You can extend your class with `MetaConvertor` and convert files into various format.
|
|
6
6
|
|
|
@@ -25,7 +25,7 @@ Or install it yourself as:
|
|
|
25
25
|
You have to do a few things to build a Convertor with `fileconv` gem.
|
|
26
26
|
|
|
27
27
|
- include `MetaConvertor`(e.g. `Fileconv::Line`) into your object,
|
|
28
|
-
- add
|
|
28
|
+
- add some hooks(e.g. `input_ext`) if you need.
|
|
29
29
|
|
|
30
30
|
Lets start with a simple example. It will be a convertor for simple text files.
|
|
31
31
|
|
|
@@ -89,22 +89,16 @@ the convertor convert it into:
|
|
|
89
89
|
3: 333
|
|
90
90
|
```
|
|
91
91
|
|
|
92
|
-
### Variables
|
|
93
|
-
|
|
94
|
-
|variable|scope|descripton|
|
|
95
|
-
|---|---|---|
|
|
96
|
-
|acc|file|accumulator for a file|
|
|
97
|
-
|@meta|convertor|meta data for the convertor|
|
|
98
|
-
|@opts|convertor|options for the convertor(`#conv` can receive options.)|
|
|
99
|
-
|
|
100
92
|
### Convertor Hooks
|
|
101
93
|
|
|
94
|
+
You can overwrite convertor hooks if you need. The default action is that a converter copies all files in the current directory to an “output” directory. The convertor make the "output" directory in the current directory if it doesn't exist.
|
|
95
|
+
|
|
102
96
|
|hook|default|description|
|
|
103
97
|
|---|---|---|
|
|
104
98
|
|input_dir|"."(current directory)|input Directory|
|
|
105
99
|
|input_ext|`nil` (all files)|input extension|
|
|
106
100
|
|output_dir|"output"|output directory|
|
|
107
|
-
|input_files
|
|
101
|
+
|input_files(files)|`files`|input files|
|
|
108
102
|
|init_conv|`nil`|init convertor hook|
|
|
109
103
|
|init_acc(acc)|`nil`|init accumulator hook|
|
|
110
104
|
|read_file(filename, acc)|`nil` (use default reader)|read file hook|
|
|
@@ -114,10 +108,28 @@ the convertor convert it into:
|
|
|
114
108
|
|result_filename|"result.txt"|result filename|
|
|
115
109
|
|conv_result|`nil`|conversion result|
|
|
116
110
|
|
|
111
|
+
Input files is selected by `input_dir` and `input_ext`. you can overwrite it with `#input_files` hooks.
|
|
112
|
+
|
|
113
|
+
The most commonly used hooks are:
|
|
114
|
+
|
|
115
|
+
- `#input_ext`
|
|
116
|
+
- `#convert_line`
|
|
117
|
+
- `#convert_file`
|
|
118
|
+
- `#conv_result`
|
|
119
|
+
|
|
120
|
+
### Convertor Variables
|
|
121
|
+
|
|
122
|
+
You can use several convertor variables. Convertor variables are `Hash` type and have a specific scope. `acc` has single file scope, which means that it is initialized for each file. `@meta` and `@opts` is valid in a convertor. `@opts` variable is set by `#conv` method that takes user options. you can use `@meta` variable for any purpose.
|
|
123
|
+
|
|
124
|
+
|variable|scope|descripton|
|
|
125
|
+
|---|---|---|
|
|
126
|
+
|acc|file|accumulator for a file|
|
|
127
|
+
|@meta|convertor|meta data for the convertor|
|
|
128
|
+
|@opts|convertor|options for the convertor|
|
|
117
129
|
|
|
118
|
-
### Default
|
|
130
|
+
### Default MetaConvertors
|
|
119
131
|
|
|
120
|
-
`fileconv` gem have several default
|
|
132
|
+
`fileconv` gem have several default MetaConvertors. MetaConverters are mainly intended to be included by a converter.
|
|
121
133
|
|
|
122
134
|
|MetaConvertor|mode|description|
|
|
123
135
|
|---|---|---|
|
|
@@ -127,21 +139,21 @@ the convertor convert it into:
|
|
|
127
139
|
|File|File|`File` convertor|
|
|
128
140
|
|JSON|File|JSON convertor|
|
|
129
141
|
|
|
130
|
-
|
|
142
|
+
Convertors(includes MetaConvertors) can be divided into two modes.
|
|
131
143
|
|
|
132
144
|
- Line Mode
|
|
133
|
-
- `#convert_line` hooks is called
|
|
145
|
+
- `#convert_line` hooks is called.
|
|
134
146
|
- e.g. `Line`, `CSV`
|
|
135
147
|
- File Mode
|
|
136
|
-
- `#convert_line` hooks is not called
|
|
148
|
+
- `#convert_line` hooks is not called.
|
|
137
149
|
- e.g. `Data`, `File`, `JSON`
|
|
138
150
|
|
|
139
|
-
Let's see a JSON
|
|
151
|
+
Let's see a JSON MetaConvertor usage.
|
|
140
152
|
|
|
141
153
|
```ruby
|
|
142
154
|
require 'fileconv'
|
|
143
155
|
|
|
144
|
-
class
|
|
156
|
+
class ModifyJSONConvertor
|
|
145
157
|
include Fileconv::JSON
|
|
146
158
|
|
|
147
159
|
def input_ext
|
|
@@ -156,7 +168,7 @@ class ModifyJSON
|
|
|
156
168
|
end
|
|
157
169
|
end
|
|
158
170
|
|
|
159
|
-
|
|
171
|
+
ModifyJSONConvertor.new.conv
|
|
160
172
|
```
|
|
161
173
|
|
|
162
174
|
original file (`address.json`) :
|
|
@@ -203,10 +215,12 @@ module Fileconv
|
|
|
203
215
|
end
|
|
204
216
|
```
|
|
205
217
|
|
|
206
|
-
|
|
218
|
+
MetaConvertors can use below hooks:
|
|
207
219
|
|
|
208
220
|
- pre_init_conv
|
|
209
221
|
- post_init_conv
|
|
222
|
+
- pre_input_files
|
|
223
|
+
- post_input_files
|
|
210
224
|
- pre_init_acc
|
|
211
225
|
- post_init_acc
|
|
212
226
|
- pre_convert_file
|
data/example/line/input_files.rb
CHANGED
data/lib/fileconv/base.rb
CHANGED
|
@@ -1,46 +1,56 @@
|
|
|
1
1
|
module Fileconv
|
|
2
2
|
module MetaConvertor
|
|
3
|
-
# Pre
|
|
3
|
+
# Pre-hook for {Convertor#init_conv}
|
|
4
4
|
def pre_init_conv
|
|
5
5
|
end
|
|
6
6
|
|
|
7
|
-
# Post
|
|
7
|
+
# Post-hook for {Convertor#init_conv}
|
|
8
8
|
def post_init_conv
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
# Pre
|
|
11
|
+
# Pre-hook for {Convertor#input_files}
|
|
12
|
+
def pre_input_files(files)
|
|
13
|
+
files
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Post-hook for {Convertor#input_files}
|
|
17
|
+
def post_input_files(files)
|
|
18
|
+
files
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Pre-hook for {Convertor#init_acc}
|
|
12
22
|
def pre_init_acc(acc)
|
|
13
23
|
end
|
|
14
24
|
|
|
15
|
-
# Post
|
|
25
|
+
# Post-hook for {Convertor#init_acc}
|
|
16
26
|
def post_init_acc(acc)
|
|
17
27
|
end
|
|
18
28
|
|
|
19
|
-
# Pre
|
|
29
|
+
# Pre-hook for {Convertor#convert_file}
|
|
20
30
|
def pre_convert_file(file, acc)
|
|
21
31
|
file
|
|
22
32
|
end
|
|
23
33
|
|
|
24
|
-
# Pre
|
|
34
|
+
# Pre-hook for {Convertor#convert_line}
|
|
25
35
|
def pre_convert_line(line, acc)
|
|
26
36
|
line
|
|
27
37
|
end
|
|
28
38
|
|
|
29
|
-
# Post
|
|
39
|
+
# Post-hook for {Convertor#convert_line}
|
|
30
40
|
def post_convert_line(line, acc)
|
|
31
41
|
line
|
|
32
42
|
end
|
|
33
43
|
|
|
34
|
-
# Pre
|
|
44
|
+
# Pre-hook for {Convertor#convert_file}
|
|
35
45
|
def post_convert_file(file, acc)
|
|
36
46
|
file
|
|
37
47
|
end
|
|
38
48
|
|
|
39
|
-
# Pre
|
|
49
|
+
# Pre-hook for {Convertor#conv_result}
|
|
40
50
|
def pre_conv_result
|
|
41
51
|
end
|
|
42
52
|
|
|
43
|
-
# Post
|
|
53
|
+
# Post-hook for {Convertor#conv_result}
|
|
44
54
|
def post_conv_result(result)
|
|
45
55
|
result
|
|
46
56
|
end
|
|
@@ -67,10 +77,11 @@ module Fileconv
|
|
|
67
77
|
end
|
|
68
78
|
|
|
69
79
|
# Input files
|
|
70
|
-
# @
|
|
71
|
-
# @
|
|
72
|
-
# @note
|
|
73
|
-
def input_files
|
|
80
|
+
# @param [Array<string>] files input files
|
|
81
|
+
# @return [Array<String>]
|
|
82
|
+
# @note you can overwrite default input files
|
|
83
|
+
def input_files(files)
|
|
84
|
+
files
|
|
74
85
|
end
|
|
75
86
|
|
|
76
87
|
# Initialize the convertor
|
|
@@ -151,28 +162,28 @@ module Fileconv
|
|
|
151
162
|
raw = ::File.read(filename, @opts[:read_file_opts]) unless raw
|
|
152
163
|
end
|
|
153
164
|
|
|
154
|
-
|
|
165
|
+
converted_file = pre_convert_file(raw, acc)
|
|
155
166
|
if @opts[:line_mode]
|
|
156
167
|
# Line mode
|
|
157
|
-
|
|
168
|
+
converted_file = process_line(converted_file, acc)
|
|
158
169
|
end
|
|
159
170
|
|
|
160
|
-
converted_file = convert_file(
|
|
161
|
-
|
|
162
|
-
if
|
|
171
|
+
converted_file = convert_file(converted_file, acc)
|
|
172
|
+
converted_file = post_convert_file(converted_file, acc)
|
|
173
|
+
if converted_file
|
|
163
174
|
out = output_filename(::File.basename(filename), acc)
|
|
164
175
|
Dir.mkdir output_dir if !Dir.exists? output_dir
|
|
165
|
-
::File.write(::File.join(output_dir, out),
|
|
176
|
+
::File.write(::File.join(output_dir, out), converted_file, @opts[:read_file_opts])
|
|
166
177
|
end
|
|
167
178
|
end
|
|
168
179
|
|
|
169
180
|
def process_line(raw_lines, acc)
|
|
170
181
|
lines = []
|
|
171
182
|
raw_lines.each do |line|
|
|
172
|
-
|
|
173
|
-
converted_line = convert_line(
|
|
174
|
-
|
|
175
|
-
lines.push *
|
|
183
|
+
converted_line = pre_convert_line(line, acc)
|
|
184
|
+
converted_line = convert_line(converted_line, acc)
|
|
185
|
+
converted_line = post_convert_line(converted_line, acc)
|
|
186
|
+
lines.push *converted_line
|
|
176
187
|
end
|
|
177
188
|
lines
|
|
178
189
|
end
|
|
@@ -188,11 +199,11 @@ module Fileconv
|
|
|
188
199
|
init_conv()
|
|
189
200
|
post_init_conv()
|
|
190
201
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
202
|
+
glob = input_ext ? "*." + input_ext : "*"
|
|
203
|
+
files = Dir.glob(::File.join(input_dir, glob))
|
|
204
|
+
files = pre_input_files(files)
|
|
205
|
+
files = input_files(files)
|
|
206
|
+
files = post_input_files(files)
|
|
196
207
|
|
|
197
208
|
files.each do |filename|
|
|
198
209
|
process_file(filename)
|
data/lib/fileconv/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: fileconv
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- hinastory
|
|
@@ -55,29 +55,22 @@ files:
|
|
|
55
55
|
- bin/setup
|
|
56
56
|
- example/csv/add_column.rb
|
|
57
57
|
- example/csv/add_header.rb
|
|
58
|
-
- example/csv/output/test2.txt
|
|
59
58
|
- example/csv/test.csv
|
|
60
59
|
- example/csv/test2.txt
|
|
61
60
|
- example/csv/with_header.rb
|
|
62
61
|
- example/data/list_file.rb
|
|
63
62
|
- example/data/output/result.txt
|
|
64
63
|
- example/data/test.data
|
|
65
|
-
- example/file/output/test.data
|
|
66
64
|
- example/file/read_bytes.rb
|
|
67
65
|
- example/file/test.data
|
|
68
66
|
- example/json/address.json
|
|
69
67
|
- example/json/modify_json.rb
|
|
70
|
-
- example/json/output/address.json
|
|
71
68
|
- example/json/pretty_json.rb
|
|
72
69
|
- example/line/add_lineno.rb
|
|
73
70
|
- example/line/count_lines.rb
|
|
74
71
|
- example/line/filter_lines.rb
|
|
75
72
|
- example/line/input_files.rb
|
|
76
73
|
- 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
74
|
- example/line/sort_lines.rb
|
|
82
75
|
- example/line/test1.txt
|
|
83
76
|
- example/line/test2.txt
|
|
@@ -85,7 +78,6 @@ files:
|
|
|
85
78
|
- example/line/test4.txt
|
|
86
79
|
- example/line/uniq_line.rb
|
|
87
80
|
- example/meta_convertor/csv2json.rb
|
|
88
|
-
- example/meta_convertor/output/test.csv
|
|
89
81
|
- example/meta_convertor/test.csv
|
|
90
82
|
- fileconv.gemspec
|
|
91
83
|
- lib/fileconv.rb
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
[{"name":"Mike","Age":"21","country":"USA"},{"name":"Jon","Age":"33","country":"USA"}]
|