util 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 +7 -0
- data/.yardopts +1 -0
- data/CECILL-C.EN +517 -0
- data/CECILL-C.FR +521 -0
- data/LICENSES.md +21 -0
- data/README.md +325 -0
- data/lib/util.rb +8 -0
- data/lib/util/arg.rb +90 -0
- data/lib/util/communia.rb +8 -0
- data/lib/util/console_logger.rb +178 -0
- data/lib/util/i18n.rb +328 -0
- data/lib/util/lists.rb +6 -0
- data/lib/util/lists/iso639.rb +149 -0
- data/lib/util/test.rb +225 -0
- data/lib/util/yaml.rb +18 -0
- data/share/lists/iso639-3.yml +35528 -0
- data/test/unit.rb +20 -0
- data/test/unit/i18n.rb +216 -0
- data/test/unit/i18n/CamelCase/eng.yml +2 -0
- data/test/unit/i18n/CamelCase/fra.yml +2 -0
- data/test/unit/i18n/aaj.yml +2 -0
- data/test/unit/i18n/en.yml +2 -0
- data/test/unit/i18n/fra.yml +2 -0
- data/test/unit/i18n/i18n +0 -0
- data/test/unit/i18n/prv.yml +2 -0
- data/test/unit/i18n/void/fra.yml/Yes-git-I-need-this-folder-even-if-it-is-empty +0 -0
- data/test/unit/i18n//316/261/316/273/316/271/316/261/317/202/fra.yml +2 -0
- data/test/unit/lists/iso639.rb +78 -0
- data/tools/create-iso639-3.rb +235 -0
- metadata +74 -0
data/test/unit.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'util/test'
|
2
|
+
|
3
|
+
$test = Util::Testing.new
|
4
|
+
|
5
|
+
paths = [File.join(__dir__, 'unit')]
|
6
|
+
files = []
|
7
|
+
begin
|
8
|
+
path = paths.shift
|
9
|
+
Dir[File.join path, '*'].each do |f|
|
10
|
+
next if f == '.' or f == '..'
|
11
|
+
paths << f if File.directory? f
|
12
|
+
files << f if File.file? f and File.extname(f) == '.rb'
|
13
|
+
end
|
14
|
+
end until paths.empty?
|
15
|
+
|
16
|
+
files.each do |f|
|
17
|
+
require_relative f.sub(__dir__, '')[1..-1]
|
18
|
+
end
|
19
|
+
|
20
|
+
$test.run
|
data/test/unit/i18n.rb
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
$test.register 'i18n-init', [false, true] do ||
|
2
|
+
require 'util/i18n'
|
3
|
+
result = []
|
4
|
+
result << Util::I18n.initialized?
|
5
|
+
Util::I18n.init
|
6
|
+
result << Util::I18n.initialized?
|
7
|
+
end
|
8
|
+
|
9
|
+
results = [true, :eng, true, :fra, true, :prv, true, :eng]
|
10
|
+
results += [true, :zho, true, :oci, true, :sqi, true, :qqa]
|
11
|
+
results += [false, :qqa, false, :qqa]
|
12
|
+
$test.register 'i18n-default-lang', results do ||
|
13
|
+
require 'util/i18n'
|
14
|
+
[ Util::I18n.set_default_lang(:eng),
|
15
|
+
Util::I18n.default_lang,
|
16
|
+
Util::I18n.set_default_lang('fra'), # String
|
17
|
+
Util::I18n.default_lang,
|
18
|
+
Util::I18n.set_default_lang(:PrV), # Non lowercase
|
19
|
+
Util::I18n.default_lang,
|
20
|
+
Util::I18n.set_default_lang('en_US.UTF-8'), # POSIX
|
21
|
+
Util::I18n.default_lang,
|
22
|
+
Util::I18n.set_default_lang('zh-Hant-344'), # IETF
|
23
|
+
Util::I18n.default_lang,
|
24
|
+
Util::I18n.set_default_lang(:oc), # ISO 639-1
|
25
|
+
Util::I18n.default_lang,
|
26
|
+
Util::I18n.set_default_lang(:alb), # ISO 639-2
|
27
|
+
Util::I18n.default_lang,
|
28
|
+
Util::I18n.set_default_lang(:qqa), # Private use
|
29
|
+
Util::I18n.default_lang,
|
30
|
+
Util::I18n.set_default_lang(:aaj), # Non existent code
|
31
|
+
Util::I18n.default_lang,
|
32
|
+
Util::I18n.set_default_lang(:afa), # ISO 639-2 code with
|
33
|
+
Util::I18n.default_lang, # no ISO 639-3 equivalent
|
34
|
+
]
|
35
|
+
end
|
36
|
+
|
37
|
+
results = [[:set_def_unknown, :aaj], [:set_def_unknown, :afa], nil]
|
38
|
+
$test.register 'i18n-next-error', results do ||
|
39
|
+
require 'util/i18n'
|
40
|
+
Util::I18n.init # Purge all previous errors
|
41
|
+
Util::I18n.set_default_lang(:aaj)
|
42
|
+
Util::I18n.set_default_lang(:afa)
|
43
|
+
[Util::I18n.next_error, Util::I18n.next_error, Util::I18n.next_error]
|
44
|
+
end
|
45
|
+
|
46
|
+
results = ['alias', '今日は', 'la-vie-de-ma-mère', 'ça-va-oui-et-toi']
|
47
|
+
results += ['Pz8hPw==', 'hello-darkneß-m41_0-d']
|
48
|
+
results += ['estie-de-tabarnak-ISHCpyE=', '']
|
49
|
+
$test.register 'i18n-slugify', results do ||
|
50
|
+
require 'util/i18n'
|
51
|
+
[ Util::I18n.send(:slugify, 'alias'),
|
52
|
+
Util::I18n.send(:slugify, '今日は'),
|
53
|
+
Util::I18n.send(:slugify, 'La vie de ma mère'),
|
54
|
+
Util::I18n.send(:slugify, 'Ça va? Oui, et toi?'),
|
55
|
+
Util::I18n.send(:slugify, '??!?'),
|
56
|
+
Util::I18n.send(:slugify, File.join('hello', 'Darkneß', 'm41_0!d')),
|
57
|
+
Util::I18n.send(:slugify, File.join('Estie', 'de', 'tabarnak', '!!§!')),
|
58
|
+
Util::I18n.send(:slugify, ''),
|
59
|
+
]
|
60
|
+
end
|
61
|
+
|
62
|
+
results = [false, :reg_no_name, false, :reg_path_not_exist]
|
63
|
+
results += [false, :reg_path_not_exist, false, :reg_path_not_dir]
|
64
|
+
results += [false, :reg_path_not_dir, 'temp', (File::SEPARATOR + 'i18n')]
|
65
|
+
results += [true, false, true]
|
66
|
+
$test.register 'i18n-register-check-args', results do ||
|
67
|
+
require 'util/i18n'
|
68
|
+
Util::I18n.init # Purge all previous errors
|
69
|
+
res = [Util::I18n.register, Util::I18n.next_error[0]]
|
70
|
+
# No name given
|
71
|
+
base = File.dirname(File.expand_path __FILE__)
|
72
|
+
path = File.join base, 'i18n', 'CamelCase', 'temp.rb'
|
73
|
+
res += [Util::I18n.register('temp', path), Util::I18n.next_error[0]]
|
74
|
+
# Will search inside 'i18n/CamelCase/i18n', that does not exist
|
75
|
+
path = File.join base, 'i18n', 'CamelCase', 'i18n'
|
76
|
+
res += [Util::I18n.register('temp', path, false), Util::I18n.next_error[0]]
|
77
|
+
# Same with an absolute path
|
78
|
+
path = File.join base, 'i18n', 'temp.rb'
|
79
|
+
res += [Util::I18n.register('temp', path), Util::I18n.next_error[0]]
|
80
|
+
# Will search inside 'i18n/i18n', that is a file not a folder
|
81
|
+
path = File.join base, 'i18n', 'i18n'
|
82
|
+
res += [Util::I18n.register('temp', path, false), Util::I18n.next_error[0]]
|
83
|
+
# Same with an absolute path
|
84
|
+
temp = Util::I18n.send(:register_check_args, 'temp', __FILE__, true, false)
|
85
|
+
res += [temp[0], temp[1].sub(base, ''), temp[2], temp[3]]
|
86
|
+
# The normal most ordinary case
|
87
|
+
temp = Util::I18n.send(:register_check_args, 'temp', '', true, false)
|
88
|
+
if temp == false then
|
89
|
+
err_name, err_data = Util::I18n.next_error
|
90
|
+
right_err = (err_name == :reg_path_not_exist or :reg_path_not_dir)
|
91
|
+
err_data = err_data.sub(File.expand_path('.'), '')
|
92
|
+
right_path = err_data == (File::SEPARATOR + 'i18n')
|
93
|
+
res << (right_err and right_path)
|
94
|
+
else
|
95
|
+
path = temp[1].sub(File.expand_path('.'), '')
|
96
|
+
res << (temp[0] == 'temp' and path == (File::SEPARATOR + 'i18n') \
|
97
|
+
and temp[2] == true and temp[3] == false)
|
98
|
+
end # Much more difficult to check: when `path` is empty
|
99
|
+
res
|
100
|
+
end
|
101
|
+
|
102
|
+
path = File.join File.dirname(File.expand_path __FILE__), 'i18n'
|
103
|
+
results = [File.join(path, '*.yml'), 'temp']
|
104
|
+
results += [File.join(path, '*.yml'), 'temp']
|
105
|
+
results += [File.join(path, 'αλιας', '*.yml'), 'temp-αλιας']
|
106
|
+
results += [File.join(path, 'void', '*.yml'), 'temp-void']
|
107
|
+
results += [File.join(path, 'CamelCase', '*.yml'), 'temp-camelcase']
|
108
|
+
results += [File.join(path, 'void', 'fra.yml', '*.yml'), 'temp-void-fra-yml']
|
109
|
+
path = nil
|
110
|
+
$test.register 'i18n-register-get-locations', results do ||
|
111
|
+
require 'util/i18n'
|
112
|
+
path = File.join File.dirname(File.expand_path __FILE__), 'i18n'
|
113
|
+
[ Util::I18n.send(:register_get_locations, 'temp', path, false),
|
114
|
+
Util::I18n.send(:register_get_locations, 'temp', path, true)
|
115
|
+
].flatten
|
116
|
+
end
|
117
|
+
|
118
|
+
hash = { 'temp' => {} }
|
119
|
+
hash['temp'][:eng] = {
|
120
|
+
'toto' => 'tata mais en anglais',
|
121
|
+
'titi'=> 'tutu mais en anglais'
|
122
|
+
}
|
123
|
+
hash['temp'][:fra] = { 'toto' => 'tata', 'titi' => 'tutu' }
|
124
|
+
hash['temp'][:prv] = { 'toto' => 'pecaire', 'titi' => 'eh fada' }
|
125
|
+
results = [Marshal.load(Marshal.dump(hash))] # Deep copy
|
126
|
+
hash['temp'][:fra]['tété'] = 'toto'
|
127
|
+
results << Marshal.load(Marshal.dump(hash))
|
128
|
+
results += [{}, :reg_yaml_cant_open]
|
129
|
+
hash = nil
|
130
|
+
$test.register 'i18n-register-get-messages', results do ||
|
131
|
+
require 'util/i18n'
|
132
|
+
Util::I18n.init # Purge all previous errors
|
133
|
+
path = File.join File.dirname(File.expand_path __FILE__), 'i18n'
|
134
|
+
loc = [File.join(path, '*.yml'), 'temp']
|
135
|
+
messages = {}
|
136
|
+
res = [Util::I18n.send(:register_get_messages, loc, messages)]
|
137
|
+
# Normal registering
|
138
|
+
messages = { 'temp' => { :fra => { 'toto' => 'titi', 'tété' => 'toto' } } }
|
139
|
+
res << Util::I18n.send(:register_get_messages, loc, messages)
|
140
|
+
# When internationalization tokens have already been registered
|
141
|
+
loc = [File.join(path, 'void', '*.yml'), 'temp']
|
142
|
+
messages = {}
|
143
|
+
res << Util::I18n.send(:register_get_messages, loc, messages)
|
144
|
+
res << Util::I18n.next_error[0]
|
145
|
+
# 'i18n/void/fra.yml' is a directory, not a file
|
146
|
+
res
|
147
|
+
end
|
148
|
+
|
149
|
+
results = [false, :reg_yaml_cant_open, :reg_no_file]
|
150
|
+
hash = { 'temp' => {} }
|
151
|
+
hash['temp'][:eng] = {
|
152
|
+
'toto' => 'tata mais en anglais',
|
153
|
+
'titi'=> 'tutu mais en anglais'
|
154
|
+
}
|
155
|
+
hash['temp'][:fra] = { 'toto' => 'tata', 'titi' => 'tutu' }
|
156
|
+
hash['temp'][:prv] = { 'toto' => 'pecaire', 'titi' => 'eh fada' }
|
157
|
+
results += [true, Marshal.load(Marshal.dump(hash))] # Deep copy
|
158
|
+
hash['temp-αλιας'] = { :fra => hash['temp'][:fra] }
|
159
|
+
hash['temp-camelcase'] = {
|
160
|
+
:eng => hash['temp'][:eng],
|
161
|
+
:fra => { 'toto' => 'tata-bis', 'titi' => 'tutu' }
|
162
|
+
}
|
163
|
+
results += [true, Marshal.load(Marshal.dump(hash))]
|
164
|
+
hash = nil
|
165
|
+
$test.register 'i18n-register', results do ||
|
166
|
+
require 'util/i18n'
|
167
|
+
Util::I18n.init # Purge all previous errors
|
168
|
+
base = File.join File.dirname(File.expand_path __FILE__), 'i18n'
|
169
|
+
path = File.join base, 'void'
|
170
|
+
res = [Util::I18n.register('temp', path, false)]
|
171
|
+
res += [Util::I18n.next_error[0], Util::I18n.next_error[0]]
|
172
|
+
# No valid file inside 'i18n/void', because 'fra.yml' is a directory
|
173
|
+
res << Util::I18n.register('temp', __FILE__)
|
174
|
+
res << Util::I18n.instance_variable_get('@messages')
|
175
|
+
# Normal registering
|
176
|
+
Util::I18n.init # Reset the @messages variable
|
177
|
+
res << Util::I18n.register('temp', __FILE__, true, true)
|
178
|
+
res << Util::I18n.instance_variable_get('@messages')
|
179
|
+
# Registering recursively
|
180
|
+
res
|
181
|
+
end
|
182
|
+
|
183
|
+
results = ['', :messages_empty, true, :reg_yaml_cant_open]
|
184
|
+
results += ['', :msg_no_module_content, '', :msg_no_valid_lang]
|
185
|
+
results += [true, 'tata', 'pecaire', 'tata-bis', 'tata-bis']
|
186
|
+
results += ['tata-bis', '']
|
187
|
+
$test.register 'i18n-message', results do ||
|
188
|
+
require 'util/i18n'
|
189
|
+
Util::I18n.init # Reset the data and errors
|
190
|
+
Util::I18n.set_default_lang :sqi
|
191
|
+
ov, $VERBOSE = $VERBOSE, nil
|
192
|
+
Util::I18n.const_set :DEFAULT_LANG, :sqi
|
193
|
+
$VERBOSE = ov
|
194
|
+
[ Util::I18n.message('toto'), # Nothing_registered yet
|
195
|
+
Util::I18n.next_error[0],
|
196
|
+
Util::I18n.register('temp', __FILE__, true, true),
|
197
|
+
Util::I18n.next_error[0],
|
198
|
+
Util::I18n.message('toto', mod: 'camelcase'), # No such module
|
199
|
+
Util::I18n.next_error[0],
|
200
|
+
Util::I18n.message('toto', lang: :zho), # No such language, and the
|
201
|
+
Util::I18n.next_error[0], # default languages neither have messages
|
202
|
+
Util::I18n.set_default_lang(:fra),
|
203
|
+
Util::I18n.message('toto'), # Default settings
|
204
|
+
Util::I18n.message('toto', lang: :prv), # Different language
|
205
|
+
Util::I18n.message('toto', mod: 'temp-camelcase'), # Different module
|
206
|
+
Util::I18n.message('toto'), # Last used module has changed
|
207
|
+
Util::I18n.message('toto', lang: :zho), # Message not translated
|
208
|
+
Util::I18n.message('tata'), # Message does not exist
|
209
|
+
]
|
210
|
+
end
|
211
|
+
|
212
|
+
$test.register '', [] do ||
|
213
|
+
require 'util/lists/iso639'
|
214
|
+
Util::Lists::ISO639::P3.instance_variable_set '@complete', nil
|
215
|
+
# Put the ISO639 module back to its uninitialized state.
|
216
|
+
end
|
data/test/unit/i18n/i18n
ADDED
File without changes
|
File without changes
|
@@ -0,0 +1,78 @@
|
|
1
|
+
$test.register 'iso639-init', [false, true] do ||
|
2
|
+
require 'util/lists/iso639'
|
3
|
+
result = []
|
4
|
+
result << Util::Lists::ISO639::P3.initialized?
|
5
|
+
Util::Lists::ISO639::P3.init
|
6
|
+
result << Util::Lists::ISO639::P3.initialized?
|
7
|
+
end
|
8
|
+
|
9
|
+
result = [true, true, true, true, false]
|
10
|
+
$test.register 'iso639-3-exist', result do ||
|
11
|
+
require 'util/lists/iso639'
|
12
|
+
[ Util::Lists::ISO639::P3.exist?(:fra),
|
13
|
+
Util::Lists::ISO639::P3.exist?('fra'),
|
14
|
+
Util::Lists::ISO639::P3.exist?(:prv),
|
15
|
+
Util::Lists::ISO639::P3.exist?(:qst),
|
16
|
+
Util::Lists::ISO639::P3.exist?(:aaj),
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
result = [false, false, true, false, false]
|
21
|
+
$test.register 'iso639-3-deprecated', result do ||
|
22
|
+
require 'util/lists/iso639'
|
23
|
+
[ Util::Lists::ISO639::P3.deprecated?(:fra),
|
24
|
+
Util::Lists::ISO639::P3.deprecated?('fra'),
|
25
|
+
Util::Lists::ISO639::P3.deprecated?(:prv),
|
26
|
+
Util::Lists::ISO639::P3.deprecated?(:qst),
|
27
|
+
Util::Lists::ISO639::P3.deprecated?(:aaj),
|
28
|
+
]
|
29
|
+
end
|
30
|
+
|
31
|
+
result = [false, false, false, true, false]
|
32
|
+
$test.register 'iso639-3-private', result do ||
|
33
|
+
require 'util/lists/iso639'
|
34
|
+
[ Util::Lists::ISO639::P3.private?(:fra),
|
35
|
+
Util::Lists::ISO639::P3.private?('fra'),
|
36
|
+
Util::Lists::ISO639::P3.private?(:prv),
|
37
|
+
Util::Lists::ISO639::P3.private?(:qst),
|
38
|
+
Util::Lists::ISO639::P3.private?(:aaj),
|
39
|
+
]
|
40
|
+
end
|
41
|
+
|
42
|
+
result = [true, true, false, false, false]
|
43
|
+
$test.register 'iso639-3-valid', result do ||
|
44
|
+
require 'util/lists/iso639'
|
45
|
+
[ Util::Lists::ISO639::P3.valid?(:fra),
|
46
|
+
Util::Lists::ISO639::P3.valid?('fra'),
|
47
|
+
Util::Lists::ISO639::P3.valid?(:prv),
|
48
|
+
Util::Lists::ISO639::P3.valid?(:qst),
|
49
|
+
Util::Lists::ISO639::P3.valid?(:aaj),
|
50
|
+
]
|
51
|
+
end
|
52
|
+
|
53
|
+
result = [:fra, :fra, :eng, nil, nil, nil]
|
54
|
+
$test.register 'iso639-3-from1', result do ||
|
55
|
+
require 'util/lists/iso639'
|
56
|
+
class Fr; end
|
57
|
+
[ Util::Lists::ISO639::P3.from1(:fr),
|
58
|
+
Util::Lists::ISO639::P3.from1('fr'),
|
59
|
+
Util::Lists::ISO639::P3.from1(:en),
|
60
|
+
Util::Lists::ISO639::P3.from1(:qs),
|
61
|
+
Util::Lists::ISO639::P3.from1(:pr),
|
62
|
+
Util::Lists::ISO639::P3.from2(Fr),
|
63
|
+
]
|
64
|
+
end
|
65
|
+
|
66
|
+
result = [:fra, :fra, :fra, nil, nil, :qst, nil]
|
67
|
+
$test.register 'iso639-3-from2', result do ||
|
68
|
+
require 'util/lists/iso639'
|
69
|
+
class Fra; end
|
70
|
+
[ Util::Lists::ISO639::P3.from2(:fra),
|
71
|
+
Util::Lists::ISO639::P3.from2('fra'),
|
72
|
+
Util::Lists::ISO639::P3.from2(:fre),
|
73
|
+
Util::Lists::ISO639::P3.from2(:fr),
|
74
|
+
Util::Lists::ISO639::P3.from2(:prv),
|
75
|
+
Util::Lists::ISO639::P3.from2(:qst),
|
76
|
+
Util::Lists::ISO639::P3.from2(Fra),
|
77
|
+
]
|
78
|
+
end
|
@@ -0,0 +1,235 @@
|
|
1
|
+
# Generates a YAML file containing all ISO 639-3 codes and information
|
2
|
+
# about them, from the lists distributed by the SIL.
|
3
|
+
#
|
4
|
+
# See `$0 --help` for command-line options.
|
5
|
+
# See main `LICENSES` file for license information.
|
6
|
+
# @author Guillaume Lestringant
|
7
|
+
# @version 1.0
|
8
|
+
# @note This program is supposed to not raise any exception, so that
|
9
|
+
# it can be called from a script (cf. `-q` option).
|
10
|
+
require 'optparse'
|
11
|
+
require 'yaml'
|
12
|
+
|
13
|
+
def log type, msg
|
14
|
+
code = { :none => "\033[0m", :ok => "\033[32m",
|
15
|
+
:err => "\033[31m", :warn => "\033[33m" }
|
16
|
+
text = { :none => '', :ok => '', :err => '[ERROR] ', :warn => '[WARNING] ' }
|
17
|
+
|
18
|
+
unless $options[:quiet] then
|
19
|
+
$options[:stderr_log] \
|
20
|
+
? $STDERR.puts("#{code[type]}#{text[type]}#{msg}#{code[:none]}")
|
21
|
+
: puts("#{code[type]}#{text[type]}#{msg}#{code[:none]}")
|
22
|
+
end
|
23
|
+
|
24
|
+
exit 1 if type == :err
|
25
|
+
end
|
26
|
+
|
27
|
+
# Parse command-line options
|
28
|
+
###########################
|
29
|
+
|
30
|
+
$options = {
|
31
|
+
:dest => 'iso639-3.yml',
|
32
|
+
:force => false,
|
33
|
+
:quiet => false,
|
34
|
+
:src_b => 'iso639-3',
|
35
|
+
:src_d => 'iso639-3d',
|
36
|
+
:src_m => 'iso639-3m',
|
37
|
+
:stderr_log => false,
|
38
|
+
}
|
39
|
+
|
40
|
+
OptionParser.new do |opts|
|
41
|
+
DEPR_TEXT = "List of deprecated codes. [DEF: #{$options[:src_d]}(.txt)]"
|
42
|
+
DEST_TEXT = "The file that will be generated. [DEF: #{$options[:dest]}(.txt)]"
|
43
|
+
FORCE_TEXT = 'Write destination even if file already exists. [DEF: false]'
|
44
|
+
HELP_TEXT = 'Display this help message.'
|
45
|
+
MACRO_TEXT = "List of macrolanguage mappings. [DEF: #{$options[:src_m]}(.txt)]"
|
46
|
+
QUIET_TEXT = 'Do not diplay anything in terminal. [DEF: false]'
|
47
|
+
SRC_TEXT = "List of active codes. [DEF: #{$options[:src_b]}(.txt)]"
|
48
|
+
STDERR_TEXT = 'Log to STDERR instead of STDOUT. [DEF: false]'
|
49
|
+
VERSION_TEXT = 'Print program version.'
|
50
|
+
NAME = 'Create ISO 639-3'
|
51
|
+
VERSION = '1.0'
|
52
|
+
|
53
|
+
opts.banner = "Usage: #{$0} [options]"
|
54
|
+
|
55
|
+
opts.on("-d", "--depr FILE", String, DEPR_TEXT) do |f|
|
56
|
+
$options[:src_d] = f
|
57
|
+
end
|
58
|
+
|
59
|
+
opts.on("-f", "--force", FORCE_TEXT) do |f|
|
60
|
+
$options[:force] = true
|
61
|
+
end
|
62
|
+
|
63
|
+
opts.on("-h", "--help", HELP_TEXT) do |h|
|
64
|
+
puts opts
|
65
|
+
exit
|
66
|
+
end
|
67
|
+
|
68
|
+
opts.on("-l", "--log-to-stderr", STDERR_TEXT) do |f|
|
69
|
+
$options[:stderr_log] = true
|
70
|
+
end
|
71
|
+
|
72
|
+
opts.on("-m", "--macro FILE", String, MACRO_TEXT) do |f|
|
73
|
+
$options[:src_m] = f
|
74
|
+
end
|
75
|
+
|
76
|
+
opts.on("-o", "--output FILE", String, DEST_TEXT) do |f|
|
77
|
+
$options[:dest] = f
|
78
|
+
end
|
79
|
+
|
80
|
+
opts.on("-q", "--quiet", QUIET_TEXT) do |f|
|
81
|
+
$options[:quiet] = true
|
82
|
+
end
|
83
|
+
|
84
|
+
opts.on("-s", "--src FILE", String, SRC_TEXT) do |f|
|
85
|
+
$options[:src_b] = f
|
86
|
+
end
|
87
|
+
|
88
|
+
opts.on("-v", "--version", VERSION_TEXT) do |v|
|
89
|
+
puts "#{NAME} v. #{VERSION}"
|
90
|
+
exit
|
91
|
+
end
|
92
|
+
end.parse!
|
93
|
+
|
94
|
+
log :none, "#{NAME} v. #{VERSION}"
|
95
|
+
|
96
|
+
# Check all parameters
|
97
|
+
#####################
|
98
|
+
|
99
|
+
def check_src idx, default
|
100
|
+
return if File.exist? $options[idx]
|
101
|
+
|
102
|
+
log :warn, "File not found `#{$options[idx]}`."
|
103
|
+
$options[idx] = File.extname($options[idx]) == '.txt' \
|
104
|
+
? File.basename($options[idx], '.txt')
|
105
|
+
: $options[idx] + '.txt'
|
106
|
+
log :warn, "Trying `#{$options[idx]}` instead."
|
107
|
+
return if File.exist? $options[idx]
|
108
|
+
|
109
|
+
if $options[idx] == default or $options[idx] == (default + '.txt') then
|
110
|
+
log :err, 'File not found. No other possibility. Aborting.'
|
111
|
+
end
|
112
|
+
|
113
|
+
log :warn, "File not found. Reverting to default value `#{default}`."
|
114
|
+
$options[idx] = default
|
115
|
+
return if File.exist? $options[idx]
|
116
|
+
|
117
|
+
$options[idx] += '.txt'
|
118
|
+
log :warn, "File not found. Trying `#{$options[idx]}` instead."
|
119
|
+
return if File.exist? $options[idx]
|
120
|
+
|
121
|
+
log :err, 'File not found. No other possibility. Aborting.'
|
122
|
+
end
|
123
|
+
|
124
|
+
def mkdir_p dir
|
125
|
+
parent = File.dirname dir
|
126
|
+
log :err, "Root directory not found `#{dir}`." if dir == parent
|
127
|
+
mkdir_p parent unless File.exist? parent
|
128
|
+
log :err, "Not a directory `#{parent}`." unless File.directory? parent
|
129
|
+
Dir.mkdir dir
|
130
|
+
end
|
131
|
+
|
132
|
+
check_src :src_b, 'iso639-3'
|
133
|
+
check_src :src_d, 'iso639-3d'
|
134
|
+
check_src :src_m, 'iso639-3m'
|
135
|
+
|
136
|
+
if File.exist? $options[:dest] then
|
137
|
+
message = "File already exists `#{$options[:dest]}`. Use `-f` to overwrite."
|
138
|
+
log :err, message unless $options[:force]
|
139
|
+
else
|
140
|
+
dir = File.dirname $options[:dest]
|
141
|
+
mkdir_p dir unless File.exist? dir
|
142
|
+
log :err, "Not a directory `#{dir}`." unless File.directory? dir
|
143
|
+
end
|
144
|
+
|
145
|
+
log :ok, 'All parameters clear.'
|
146
|
+
|
147
|
+
# Extract the data
|
148
|
+
#################
|
149
|
+
|
150
|
+
keys = []
|
151
|
+
codes = {}
|
152
|
+
result = {}
|
153
|
+
|
154
|
+
active = File.read($options[:src_b]).lines[1..-1]
|
155
|
+
active = [] if active.nil?
|
156
|
+
active.each do |l|
|
157
|
+
id, part2b, part2t, part1, scope, type, name, comment = l.chomp.split "\t"
|
158
|
+
|
159
|
+
keys << id
|
160
|
+
id = id.to_sym
|
161
|
+
codes[id] = { :name => name }
|
162
|
+
|
163
|
+
case scope
|
164
|
+
when 'I' then codes[id][:scope] = :individual
|
165
|
+
when 'M' then codes[id][:scope] = :macro
|
166
|
+
when 'S' then codes[id][:scope] = :special
|
167
|
+
else puts "Unknown scope for #{id} : #{scope}."
|
168
|
+
end
|
169
|
+
|
170
|
+
case type
|
171
|
+
when 'A' then codes[id][:type] = :ancient
|
172
|
+
when 'C' then codes[id][:type] = :constructed
|
173
|
+
when 'E' then codes[id][:type] = :extinct
|
174
|
+
when 'H' then codes[id][:type] = :historical
|
175
|
+
when 'L' then codes[id][:type] = :living
|
176
|
+
when 'S' then codes[id][:type] = :special
|
177
|
+
else puts "Unknown type for #{id} : #{type}."
|
178
|
+
end
|
179
|
+
|
180
|
+
codes[id][:p1] = part1.to_sym unless part1.empty?
|
181
|
+
codes[id][:p2b] = part2b.to_sym unless part2b.empty?
|
182
|
+
codes[id][:p2t] = part2t.to_sym unless part2t.empty?
|
183
|
+
codes[id][:comment] = comment unless comment.nil? or comment.empty?
|
184
|
+
end
|
185
|
+
|
186
|
+
log :ok, 'Extracted active codes.'
|
187
|
+
|
188
|
+
deprecated = File.read($options[:src_d]).lines[1..-1]
|
189
|
+
deprecated = [] if deprecated.nil?
|
190
|
+
deprecated.each do |l|
|
191
|
+
id, name, reason, new, comment, date = l.chomp.split "\t"
|
192
|
+
|
193
|
+
keys << id
|
194
|
+
id = id.to_sym
|
195
|
+
codes[id] = { :deprecated => true, :name => name }
|
196
|
+
|
197
|
+
case reason
|
198
|
+
when 'C' then codes[id][:reason] = :change
|
199
|
+
when 'D' then codes[id][:reason] = :duplicate
|
200
|
+
when 'N' then codes[id][:reason] = :non_existent
|
201
|
+
when 'S' then codes[id][:reason] = :split
|
202
|
+
when 'M' then codes[id][:reason] = :merge
|
203
|
+
else puts "Unknown deprecation reason for #{id} : #{reason}."
|
204
|
+
end
|
205
|
+
|
206
|
+
codes[id][:new] = new.to_sym unless new.empty?
|
207
|
+
codes[id][:comment] = comment unless comment.empty?
|
208
|
+
codes[id][:date] = date unless date.empty? or date.empty?
|
209
|
+
end
|
210
|
+
|
211
|
+
log :ok, 'Extracted deprecated codes.'
|
212
|
+
|
213
|
+
macro = File.read($options[:src_m]).lines[1..-1]
|
214
|
+
macro = [] if macro.nil?
|
215
|
+
macro.each do |l|
|
216
|
+
macro, member, status = l.chomp.split "\t"
|
217
|
+
|
218
|
+
macro, member = macro.to_sym, member.to_sym
|
219
|
+
codes[macro][:members] = [] if codes[macro][:members].nil?
|
220
|
+
codes[macro][:members] << member
|
221
|
+
codes[member][:macro] = macro
|
222
|
+
end
|
223
|
+
|
224
|
+
log :ok, 'Extracted macrolanguage mappings.'
|
225
|
+
|
226
|
+
# Export
|
227
|
+
#######
|
228
|
+
|
229
|
+
keys.sort.each do |k|
|
230
|
+
result[k.to_sym] = codes[k.to_sym]
|
231
|
+
end
|
232
|
+
|
233
|
+
File.write $options[:dest], result.to_yaml
|
234
|
+
|
235
|
+
log :ok, 'Full success.'
|