libis-tools 1.0.5-java

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 (71) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +2 -0
  3. data/.gitignore +16 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +40 -0
  6. data/Gemfile +7 -0
  7. data/README.md +202 -0
  8. data/Rakefile +11 -0
  9. data/bin/libis_tool +5 -0
  10. data/lib/libis-tools.rb +1 -0
  11. data/lib/libis/tools.rb +25 -0
  12. data/lib/libis/tools/assert.rb +52 -0
  13. data/lib/libis/tools/checksum.rb +106 -0
  14. data/lib/libis/tools/cli/cli_helper.rb +189 -0
  15. data/lib/libis/tools/cli/reorg.rb +416 -0
  16. data/lib/libis/tools/command.rb +133 -0
  17. data/lib/libis/tools/command_line.rb +23 -0
  18. data/lib/libis/tools/config.rb +147 -0
  19. data/lib/libis/tools/config_file.rb +85 -0
  20. data/lib/libis/tools/csv.rb +38 -0
  21. data/lib/libis/tools/deep_struct.rb +71 -0
  22. data/lib/libis/tools/extend/array.rb +16 -0
  23. data/lib/libis/tools/extend/empty.rb +7 -0
  24. data/lib/libis/tools/extend/hash.rb +147 -0
  25. data/lib/libis/tools/extend/kernel.rb +25 -0
  26. data/lib/libis/tools/extend/ostruct.rb +3 -0
  27. data/lib/libis/tools/extend/roo.rb +91 -0
  28. data/lib/libis/tools/extend/string.rb +94 -0
  29. data/lib/libis/tools/extend/struct.rb +29 -0
  30. data/lib/libis/tools/extend/symbol.rb +8 -0
  31. data/lib/libis/tools/logger.rb +130 -0
  32. data/lib/libis/tools/mets_dnx.rb +61 -0
  33. data/lib/libis/tools/mets_file.rb +504 -0
  34. data/lib/libis/tools/mets_objects.rb +547 -0
  35. data/lib/libis/tools/parameter.rb +372 -0
  36. data/lib/libis/tools/spreadsheet.rb +196 -0
  37. data/lib/libis/tools/temp_file.rb +42 -0
  38. data/lib/libis/tools/thread_safe.rb +31 -0
  39. data/lib/libis/tools/version.rb +5 -0
  40. data/lib/libis/tools/xml_document.rb +583 -0
  41. data/libis-tools.gemspec +55 -0
  42. data/spec/assert_spec.rb +65 -0
  43. data/spec/checksum_spec.rb +68 -0
  44. data/spec/command_spec.rb +90 -0
  45. data/spec/config_file_spec.rb +83 -0
  46. data/spec/config_spec.rb +113 -0
  47. data/spec/csv_spec.rb +159 -0
  48. data/spec/data/test-headers.csv +2 -0
  49. data/spec/data/test-headers.tsv +2 -0
  50. data/spec/data/test-noheaders.csv +1 -0
  51. data/spec/data/test-noheaders.tsv +1 -0
  52. data/spec/data/test.data +9 -0
  53. data/spec/data/test.xlsx +0 -0
  54. data/spec/data/test.xml +8 -0
  55. data/spec/data/test.yml +2 -0
  56. data/spec/data/test_config.yml +15 -0
  57. data/spec/deep_struct_spec.rb +138 -0
  58. data/spec/logger_spec.rb +165 -0
  59. data/spec/mets_file_spec.rb +223 -0
  60. data/spec/parameter_container_spec.rb +152 -0
  61. data/spec/parameter_spec.rb +148 -0
  62. data/spec/spec_helper.rb +29 -0
  63. data/spec/spreadsheet_spec.rb +1820 -0
  64. data/spec/temp_file_spec.rb +76 -0
  65. data/spec/test.xsd +20 -0
  66. data/spec/thread_safe_spec.rb +64 -0
  67. data/spec/xmldocument_spec.rb +421 -0
  68. data/test/test_helper.rb +7 -0
  69. data/test/webservices/test_ca_item_info.rb +59 -0
  70. data/test/webservices/test_ca_search.rb +35 -0
  71. metadata +437 -0
@@ -0,0 +1,55 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'libis/tools/version'
7
+ require 'date'
8
+
9
+ Gem::Specification.new do |spec|
10
+ spec.name = 'libis-tools'
11
+ spec.version = ::Libis::Tools::VERSION
12
+ spec.date = Date.today.to_s
13
+
14
+ spec.summary = %q{LIBIS toolbox.}
15
+ spec.description = %q{Some tool classes for other LIBIS gems.}
16
+
17
+ spec.authors = ['Kris Dekeyser']
18
+ spec.email = ['kris.dekeyser@libis.be']
19
+ spec.homepage = 'https://github.com/Kris-LIBIS/LIBIS_Tools'
20
+ spec.license = 'MIT'
21
+
22
+ spec.platform = Gem::Platform::JAVA if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
23
+
24
+ spec.files = `git ls-files -z`.split("\x0")
25
+ spec.bindir = 'bin'
26
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
27
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
28
+
29
+ spec.require_paths = ['lib']
30
+ spec.has_rdoc = 'yard'
31
+
32
+ spec.add_development_dependency 'bundler', '> 1.6'
33
+ spec.add_development_dependency 'rake', '~> 10.3'
34
+ spec.add_development_dependency 'rspec', '~> 3.1'
35
+ spec.add_development_dependency 'term-ansicolor', '~> 1.6'
36
+ spec.add_development_dependency 'equivalent-xml', '~> 0.5'
37
+ spec.add_development_dependency 'awesome_print', '~> 1.6'
38
+
39
+ spec.add_runtime_dependency 'backports', '~> 3.6'
40
+ spec.add_runtime_dependency 'nokogiri', '~> 1.6'
41
+ spec.add_runtime_dependency 'gyoku', '~> 1.3'
42
+ spec.add_runtime_dependency 'nori', '~> 2.6'
43
+ spec.add_runtime_dependency 'recursive-open-struct', '~> 1.0'
44
+ spec.add_runtime_dependency 'parslet', '~> 1.7'
45
+ spec.add_runtime_dependency 'simple_xlsx_reader', '~> 1.0'
46
+ spec.add_runtime_dependency 'logging', '~> 2.0'
47
+ spec.add_runtime_dependency 'concurrent-ruby', '~> 1.0'
48
+ spec.add_runtime_dependency 'yard', '~> 0.9.11'
49
+ spec.add_runtime_dependency 'roo', '~> 2.5'
50
+ spec.add_runtime_dependency 'roo-xls', '~> 1.0'
51
+ spec.add_runtime_dependency 'tty-prompt'
52
+ spec.add_runtime_dependency 'tty-config'
53
+ spec.add_runtime_dependency 'thor'
54
+
55
+ end
@@ -0,0 +1,65 @@
1
+ # encoding: utf-8
2
+ require_relative 'spec_helper'
3
+ require 'libis/tools/assert'
4
+
5
+ describe 'Assert' do
6
+
7
+ before :example do
8
+ $DEBUG = true
9
+ end
10
+
11
+ after :example do
12
+ $DEBUG = false
13
+ end
14
+
15
+ it 'should throw an assert exception when statement is false' do
16
+
17
+ message = 'Testing the assert with false.'
18
+ expect {
19
+ assert(false, message)
20
+ }.to raise_error(AssertionFailure, message)
21
+
22
+ end
23
+
24
+ it 'should only throw an assert in debug mode' do
25
+
26
+ $DEBUG = false
27
+ expect {
28
+ assert(false, 'Testing the assert with false.')
29
+ }.to_not raise_error
30
+
31
+ end
32
+
33
+ it 'should not throw an assert if statement is true' do
34
+
35
+ expect {
36
+ assert(true, 'Testing the assert with true.')
37
+ }.to_not raise_error
38
+
39
+ end
40
+
41
+ it 'should throw an assert on nil' do
42
+
43
+ message = 'Testing the assert with nil.'
44
+ expect {
45
+ assert(nil, message)
46
+ }.to raise_error(AssertionFailure, message)
47
+ end
48
+
49
+ it 'should check the given block if present' do
50
+
51
+ message = 'block test'
52
+ expect {
53
+ assert(message) do
54
+ false
55
+ end
56
+ }.to raise_error(AssertionFailure, message)
57
+
58
+ expect {
59
+ assert(message) do
60
+ true
61
+ end
62
+ }.to_not raise_error
63
+
64
+ end
65
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+ require_relative 'spec_helper'
3
+ require 'libis/tools/checksum'
4
+
5
+ describe 'Checksum' do
6
+
7
+ def hex2base64(hexdigest)
8
+ [[hexdigest].pack('H*')].pack('m0')
9
+ end
10
+
11
+ def hex2string(hexdigest)
12
+ [hexdigest].pack('H*')
13
+ end
14
+
15
+ it 'should not know how to calculate ABC checksum' do
16
+
17
+ checksum_type = :ABC
18
+
19
+ expect {
20
+ ::Libis::Tools::Checksum.hexdigest('abc', checksum_type)
21
+ }.to raise_error(RuntimeError, "Checksum type 'ABC' not supported.")
22
+
23
+ end
24
+
25
+ let(:filename) { File.join(File.dirname(__FILE__), 'data', 'test.data') }
26
+
27
+ CHECKSUM_RESULTS = {
28
+ MD5: 'fe249d8dd45a39793f315fb0734ffe2c',
29
+ SHA1: 'e8f322d186699807a98a0cefb5015acf1554f954',
30
+ SHA256: '2a742e643dd79427738bdc0ebd0d2837f998fe2101a964c2d5014905d331bbc4',
31
+ SHA384: '71083b74394f49db6149ad9147103f7693ec823183750ce32a2215bbd7ee5e75212e2d794243c7e76c7318a4ddcf9a56',
32
+ SHA512: '10964f5272729c2670ccad67754284fb06cca1387270c184c2edbcd032700548297916c8e109a10e019c25b86c646e95a3456c465f83d571502889f97b483e6f'
33
+ }
34
+
35
+ # noinspection RubyResolve
36
+ unless defined? JRUBY_VERSION
37
+ CHECKSUM_RESULTS[:RMD160] = '17c9eaad9ccbaad0e030c2c5d60fd9d58255cc39'
38
+ end
39
+
40
+ filename = File.join(File.dirname(__FILE__), 'data', 'test.data')
41
+ file = File.absolute_path(filename)
42
+ string = File.read(filename)
43
+ SUBJECTS = {
44
+ string: string,
45
+ file: file
46
+ }
47
+
48
+ CHECKSUM_RESULTS.each do |checksum_type, digest|
49
+
50
+ SUBJECTS.each do |subject_type, subject|
51
+
52
+ it "should calculate #{checksum_type} from #{subject_type}" do
53
+ expect(::Libis::Tools::Checksum.hexdigest(subject, checksum_type)).to eq digest
54
+ expect(::Libis::Tools::Checksum.base64digest(subject, checksum_type)).to eq hex2base64(digest)
55
+ expect(::Libis::Tools::Checksum.digest(subject, checksum_type)).to eq hex2string(digest)
56
+
57
+ checksum = ::Libis::Tools::Checksum.new(checksum_type)
58
+
59
+ expect(checksum.hexdigest(subject)).to eq digest
60
+ expect(checksum.base64digest(subject)).to eq hex2base64(digest)
61
+ expect(checksum.digest(subject)).to eq hex2string(digest)
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,90 @@
1
+ # encoding: utf-8
2
+ require_relative 'spec_helper'
3
+ require 'libis/tools/command'
4
+
5
+ describe 'Command' do
6
+
7
+ let!(:dir) { Dir.pwd }
8
+ let(:entries) {
9
+ Dir.entries('.').inject([]) do |array, value|
10
+ array << value unless File.directory?(value)
11
+ array
12
+ end.sort
13
+ }
14
+
15
+ before(:each) { Dir.chdir(File.join(File.dirname(__FILE__),'data')) }
16
+ after(:each) { Dir.chdir(dir) }
17
+
18
+ it 'should run ls command' do
19
+
20
+ result = Libis::Tools::Command.run('ls')
21
+
22
+ output = result[:out].map {|x| x.split(/\s+/)}.flatten.compact
23
+ expect(output.size).to eq entries.size
24
+ expect(output.sort).to match entries
25
+ expect(result[:err]).to eq []
26
+ expect(result[:status]).to eq 0
27
+
28
+ end
29
+
30
+ it 'should run ls command with an option' do
31
+
32
+ result = Libis::Tools::Command.run('ls', '-1')
33
+
34
+ output = result[:out]
35
+ puts output.sort
36
+ expect(output.size).to eq entries.size
37
+ expect(output.sort).to match entries
38
+ expect(result[:err]).to eq []
39
+ expect(result[:status]).to eq 0
40
+
41
+ end
42
+
43
+ it 'should run ls command with multiple options' do
44
+
45
+ result = Libis::Tools::Command.run('ls', '-1', '-a', '-p')
46
+
47
+ output = result[:out]
48
+ expect(output.size).to eq entries.size + 2
49
+ expect(output[0]).to eq './'
50
+ expect(output[1]).to eq '../'
51
+ expect(output[2..-1].sort).to match entries
52
+ expect(result[:err]).to eq []
53
+ expect(result[:status]).to eq 0
54
+
55
+ end
56
+
57
+ it 'should capture error output and status' do
58
+
59
+ result = Libis::Tools::Command.run('ls', 'abc')
60
+ expect(result[:out]).to eq []
61
+ expect(result[:err].size).to eq 1
62
+ expect(result[:err][0]).to match /ls: cannot access '?abc'?: No such file or directory/
63
+ expect(result[:status]).to eq 2
64
+
65
+ end
66
+
67
+ it 'should allow to supply input data' do
68
+
69
+ result = Libis::Tools::Command.run('cat', stdin_data: "FooBar", timeout: 1)
70
+ expect(result[:out]).to eq ['FooBar']
71
+ expect(result[:err].size).to eq 0
72
+ expect(result[:status]).to eq 0
73
+
74
+ end
75
+
76
+ it 'should not timeout if command finishes' do
77
+
78
+ result = Libis::Tools::Command.run('cat', stdin_data: "FooBar", timeout: 1)
79
+ expect(result[:timeout]).to be_falsey
80
+
81
+ end
82
+
83
+ it 'should timeout if command hangs' do
84
+
85
+ result = Libis::Tools::Command.run('ls', '-aRlp', '/', timeout: 1)
86
+ expect(result[:timeout]).to be_truthy
87
+
88
+ end
89
+
90
+ end
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+ require_relative 'spec_helper'
3
+ require 'libis/tools/config_file'
4
+
5
+ describe ::Libis::Tools::ConfigFile do
6
+
7
+ let!(:test_file) { File.join(File.dirname(__FILE__), 'data', 'test_config.yml') }
8
+
9
+ let(:hash) {
10
+ {
11
+ a: {x: 0, y: 0, z: 0},
12
+ b: {x: 10, y: -5, z: 2.5},
13
+ c: [
14
+ [
15
+ {a: [{a1: 1, a2: 2}]},
16
+ ]
17
+ ]
18
+ }
19
+ }
20
+
21
+ context 'after default initialization' do
22
+
23
+ it 'has empty hash' do
24
+ # noinspection RubyResolve
25
+ expect(subject.to_hash).to be_empty
26
+ end
27
+
28
+ it 'loads from file' do
29
+ subject << test_file
30
+ expect(subject.to_hash).to eq hash
31
+ end
32
+
33
+ it 'loads a hash' do
34
+ subject << hash
35
+ expect(subject.to_hash).to eq hash
36
+ end
37
+
38
+ it 'allows to change sub-hash' do
39
+ subject << hash
40
+ # noinspection RubyResolve
41
+ subject.b.v = 1
42
+ hash[:b][:v] = 1
43
+ expect(subject.to_hash).to eq hash
44
+ end
45
+
46
+ it 'allows to change hash in array' do
47
+ subject << hash
48
+ # noinspection RubyResolve
49
+ subject.c[0][0].a[0].v = 1
50
+ hash[:c][0][0][:a][0][:v] = 1
51
+ expect(subject.to_hash).to eq hash
52
+ end
53
+
54
+ end
55
+
56
+ context 'initialization with hash' do
57
+ subject { ::Libis::Tools::ConfigFile.new hash }
58
+
59
+ it 'has hash' do
60
+ expect(subject.to_hash).to eq hash
61
+ end
62
+
63
+ end
64
+
65
+ context 'initialization with file' do
66
+ subject { ::Libis::Tools::ConfigFile.new test_file }
67
+
68
+ it 'has hash' do
69
+ expect(subject.to_hash).to eq hash
70
+ end
71
+
72
+ end
73
+
74
+ context 'initialization with hash from YAML file' do
75
+ subject { ::Libis::Tools::ConfigFile.new YAML.load_file(test_file)}
76
+
77
+ it 'has hash' do
78
+ expect(subject.to_hash).to eq hash
79
+ end
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,113 @@
1
+ # encoding: utf-8
2
+ require_relative 'spec_helper'
3
+ require 'libis/tools/config'
4
+
5
+ describe 'Config' do
6
+
7
+ before(:all) do
8
+ ::Libis::Tools::Config << {appname: 'LIBIS Default'}
9
+ end
10
+
11
+ subject(:config) { ::Libis::Tools::Config.clear! }
12
+
13
+ it 'has defaults set' do
14
+ expect(config.logger).to be_a ::Logging::Logger
15
+ end
16
+
17
+ it 'clears all values' do
18
+ # noinspection RubyResolve
19
+ config.test_value = 5
20
+ config.logger.level = :FATAL
21
+ # noinspection RubyResolve
22
+ expect(config.test_value).to be 5
23
+ expect(config.logger.level).to be ::Logging::level_num(:FATAL)
24
+
25
+ config.clear!
26
+ # noinspection RubyResolve
27
+ expect(config.test_value).to be_nil
28
+ end
29
+
30
+ context 'adding values with setters' do
31
+
32
+ it 'by symbol' do
33
+ config[:test_value] = 5
34
+ expect(config['test_value']).to be 5
35
+ end
36
+
37
+ it 'by name' do
38
+ config['test_value'] = 6
39
+ # noinspection RubyResolve
40
+ expect(config.test_value).to be 6
41
+ end
42
+
43
+ it 'by method' do
44
+ # noinspection RubyResolve
45
+ config.test_value = 7
46
+ expect(config[:test_value]).to be 7
47
+ end
48
+
49
+ it 'allows to set on instance' do
50
+ config.instance[:test_value] = :abc
51
+ # noinspection RubyResolve
52
+ expect(config.test_value).to be :abc
53
+ end
54
+
55
+ it 'allows to set on class' do
56
+ # noinspection RubyResolve
57
+ config.test_value = :def
58
+ expect(config.instance[:test_value]).to be :def
59
+ end
60
+
61
+ end
62
+
63
+ context 'loading from file' do
64
+
65
+ let(:test_file) { File.join(File.dirname(__FILE__), 'data', 'test.yml') }
66
+ subject(:config) {
67
+ Libis::Tools::Config.clear! << test_file
68
+ }
69
+
70
+ it 'has configuration parameters set' do
71
+ expect(config.process).to eq 'Test Configuration'
72
+ end
73
+
74
+ it 'resets only values loaded from file' do
75
+ config[:process] = 'foo'
76
+ config[:bar] = 'qux'
77
+
78
+ expect(config.process).to eq 'foo'
79
+ expect(config.bar).to eq 'qux'
80
+
81
+ config.reload
82
+ expect(config.process).to eq 'Test Configuration'
83
+ expect(config.bar).to eq 'qux'
84
+ end
85
+
86
+ it 'resets all values' do
87
+ config[:process] = 'foo'
88
+ config[:bar] = 'qux'
89
+
90
+ expect(config.process).to eq 'foo'
91
+ expect(config.bar).to eq 'qux'
92
+
93
+ config.reload!
94
+ expect(config.process).to eq 'Test Configuration'
95
+ expect(config.bar).to be_nil
96
+ end
97
+
98
+ it 'clears all values' do
99
+ config[:process] = 'foo'
100
+ config[:bar] = 'qux'
101
+
102
+ expect(config.process).to eq 'foo'
103
+ expect(config.bar).to eq 'qux'
104
+
105
+ config.clear!
106
+ expect(config.process).to be_nil
107
+ expect(config.bar).to be_nil
108
+ # noinspection RubyResolve
109
+ expect(config.to_h).to be_empty
110
+ end
111
+
112
+ end
113
+ end