oversetter 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d817d05d72e6b258380911749bdd4438023e47d5
4
+ data.tar.gz: 90c404428ee560aca54cc8a5ba3f477cd4375331
5
+ SHA512:
6
+ metadata.gz: d43a80b070e8090b0ccdea4a58c19fb328acb4e8a372dcbcbf9cc1e79e0f5270c77f972b5ebda55cee93b4e750677b81da5d069ede1a33e761a111ba7ad346f5
7
+ data.tar.gz: c0a2a16aef13df6df70256b3a01bc94f14ef749c44fcaf03eb9690a829ec89066ac2f0b9e86e0fd452e13a1e983e3e5d701ffb5bba4daf7390b79126563465b2
@@ -0,0 +1,5 @@
1
+ Changelog
2
+ ===
3
+
4
+ Version 1.0.0
5
+ - Glosbe, Hablaa & Yandex implemented
@@ -0,0 +1,193 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ require 'gli'
4
+ require 'httpi'
5
+ require 'oversetter/glosbe'
6
+ require 'oversetter/hablaa'
7
+ require 'oversetter/version'
8
+ require 'oversetter/yandex'
9
+ require 'xml-fu'
10
+
11
+ # The Oversetter module handles base functionality.
12
+ module Oversetter
13
+ HTTPI.log = false
14
+
15
+ # Optionally sets HTTP adapter with httpi. Supports [:httpclient,
16
+ # :curb, :em_http, :net_http_persistent, :excon, :rack]
17
+ #
18
+ # @param http [Symbol] The http adapter to use. Smart defaults.
19
+ def self.http(http)
20
+ HTTPI.adapter = http
21
+ end
22
+
23
+ # Optionally sets JSON adapter with multi_json. Supports [:oj,
24
+ # :yajl, :json_gem, :json_pure]
25
+ #
26
+ # @param mj [Symbol] The JSON adapter to use. Smart defaults.
27
+ def self.json(mj)
28
+ MultiJson.use(mj)
29
+ end
30
+
31
+ # Optionally sets XML adapter with multi_xml. Supports [:ox,
32
+ # :libxml, :nokogiri, :rexml]
33
+ #
34
+ # @param mx [Symbol] The XML adapter to use. Smart defaults.
35
+ def self.xml(mx)
36
+ MultiXml.parser = mx
37
+ end
38
+
39
+ # Prints colored element label.
40
+ #
41
+ # @param label [String] The label to print
42
+ def self.label(label)
43
+ print Rainbow('[').blue.bright
44
+ print Rainbow(label).green.bright
45
+ print Rainbow(']').blue.bright
46
+ print Rainbow('|').bright
47
+ end
48
+
49
+ # Adds an object to the outfile array.
50
+ #
51
+ # @param obj [String] The item to add
52
+ def self.tofile(obj)
53
+ if $fmt != nil
54
+ $tofile.push obj
55
+ end
56
+ end
57
+ end
58
+
59
+ include GLI::App
60
+ program_desc 'A powerful text translation tool.'
61
+ config_file '.oversetter.yml'
62
+ version Oversetter::VERSION
63
+
64
+ flag [:h, :http], :default_value => nil, :arg_name => 'string', :desc => 'HTTP adapter', :long_desc => 'httpclient, curb, em_http, net_http_persistent, excon, rack'
65
+ flag [:j, :json], :default_value => nil, :arg_name => 'string', :desc => 'JSON adapter', :long_desc => 'oj, yajl, json_gem, json_pure'
66
+ flag [:o, :out], :default_value => nil, :arg_name => 'filename', :desc => 'Output file', :long_desc => 'filename.json or filename.xml'
67
+ flag [:x, :xml], :default_value => nil, :arg_name => 'string', :desc => 'XML adapter', :long_desc => 'ox, libxml, nokogiri, rexml'
68
+ switch [:fo, :force], :default_value => false, :desc => 'Force overwrite', :long_desc => 'Overwrites existing JSON & XML files'
69
+ switch [:v, :verbose], :default_value => false, :desc => 'Prints parameters and options'
70
+
71
+ require 'oversetter/cli/glosbe'
72
+ require 'oversetter/cli/hablaa'
73
+ require 'oversetter/cli/yandex'
74
+
75
+ pre do |global, command, options, args|
76
+ # Pre logic here
77
+ # Return true to proceed; false to abort and not call the
78
+ # chosen command
79
+ # Use skips_pre before a command to skip this block
80
+ # on that command only
81
+ if global[:o] != nil
82
+ outfile = global[:o]
83
+ if outfile =~ /\w*\.json/
84
+ $fmt = :json
85
+ elsif outfile =~ /\w*\.xml/
86
+ $fmt = :xml
87
+ else
88
+ puts 'Invalid file extension.'
89
+ end
90
+ $tofile = []
91
+ end
92
+ http = global[:h]
93
+ json = global[:j]
94
+ xml = global[:x]
95
+ if http != nil
96
+ if http.class != Symbol then http = http.to_sym; end
97
+ Oversetter.http(http)
98
+ end
99
+ if json != nil
100
+ if json.class != Symbol then json = json.to_sym; end
101
+ Oversetter.json(json)
102
+ end
103
+ if xml != nil
104
+ if xml.class != Symbol then xml = xml.to_sym; end
105
+ Oversetter.xml(xml)
106
+ end
107
+ if global[:v]
108
+ Oversetter.label('Global options')
109
+ print "#{global}"
110
+ Oversetter.label('Command')
111
+ print "#{command.name}"
112
+ Oversetter.label('Command options')
113
+ print "#{options}"
114
+ Oversetter.label('Args')
115
+ print "#{args}"
116
+ Oversetter.label('Bootstrapped')
117
+ puts ''
118
+ end
119
+ true
120
+ end
121
+
122
+ post do |global, command, options, args|
123
+ # Post logic here
124
+ # Use skips_post before a command to skip this
125
+ # block on that command only
126
+ if $fmt != nil
127
+ outfile = global[:o]
128
+ if File.exist?(outfile) && global[:fo] == true
129
+ if $fmt == :json
130
+ fo = File.new(outfile, 'w+')
131
+ fo.print MultiJson.dump($tofile, :pretty => true)
132
+ fo.close
133
+ puts Rainbow("Word search was written to #{outfile}.").bright
134
+ elsif $fmt == :xml
135
+ fo = File.new(outfile, 'w+')
136
+ fo.print '<?xml version="1.0" encoding="utf-8"?>'
137
+ fo.print XmlFu.xml($tofile)
138
+ fo.close
139
+ puts Rainbow("Word search was written to #{outfile}.").bright
140
+ else
141
+ puts 'Invalid file extension.'
142
+ end
143
+ end
144
+ if File.exist?(outfile) && global[:fo] == false
145
+ puts Rainbow("#{outfile} exists. Overwrite? y/n ").bright
146
+ ans = gets
147
+ if ans =~ /y/
148
+ if $fmt == :json
149
+ fo = File.new(outfile, 'w+')
150
+ fo.print MultiJson.dump($tofile, :pretty => true)
151
+ fo.close
152
+ puts Rainbow("Word search was written to #{outfile}.").bright
153
+ elsif $fmt == :xml
154
+ fo = File.new(outfile, 'w+')
155
+ fo.print '<?xml version="1.0" encoding="utf-8"?>'
156
+ fo.print XmlFu.xml($tofile)
157
+ fo.close
158
+ puts Rainbow("Word search was written to #{outfile}.").bright
159
+ else
160
+ puts 'Invalid file extension.'
161
+ end
162
+ else
163
+ puts 'Please try again with a different filename.'
164
+ end
165
+ else
166
+ if $fmt == :json
167
+ fo = File.open(outfile, 'w+')
168
+ fo.print MultiJson.dump($tofile, :pretty => true)
169
+ fo.close
170
+ puts Rainbow("Word search was written to #{outfile}.").bright
171
+ elsif $fmt == :xml
172
+ fo = File.open(outfile, 'w+')
173
+ fo.print '<?xml version="1.0" encoding="utf-8"?>'
174
+ fo.print XmlFu.xml($tofile)
175
+ fo.close
176
+ puts Rainbow("Word search was written to #{outfile}.").bright
177
+ else
178
+ puts 'Invalid file extension.'
179
+ end
180
+ end
181
+ end
182
+ if global[:v]
183
+ Oversetter.label('Shutdown')
184
+ puts ''
185
+ end
186
+ end
187
+
188
+ on_error do |exception|
189
+ # Error logic here
190
+ # return false to skip default error handling
191
+ true
192
+ end
193
+ exit run(ARGV)
@@ -0,0 +1,193 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ require 'gli'
4
+ require 'httpi'
5
+ require 'oversetter/glosbe'
6
+ require 'oversetter/hablaa'
7
+ require 'oversetter/version'
8
+ require 'oversetter/yandex'
9
+ require 'xml-fu'
10
+
11
+ # The Oversetter module handles base functionality.
12
+ module Oversetter
13
+ HTTPI.log = false
14
+
15
+ # Optionally sets HTTP adapter with httpi. Supports [:httpclient,
16
+ # :curb, :em_http, :net_http_persistent, :excon, :rack]
17
+ #
18
+ # @param http [Symbol] The http adapter to use. Smart defaults.
19
+ def self.http(http)
20
+ HTTPI.adapter = http
21
+ end
22
+
23
+ # Optionally sets JSON adapter with multi_json. Supports [:oj,
24
+ # :yajl, :json_gem, :json_pure]
25
+ #
26
+ # @param mj [Symbol] The JSON adapter to use. Smart defaults.
27
+ def self.json(mj)
28
+ MultiJson.use(mj)
29
+ end
30
+
31
+ # Optionally sets XML adapter with multi_xml. Supports [:ox,
32
+ # :libxml, :nokogiri, :rexml]
33
+ #
34
+ # @param mx [Symbol] The XML adapter to use. Smart defaults.
35
+ def self.xml(mx)
36
+ MultiXml.parser = mx
37
+ end
38
+
39
+ # Prints colored element label.
40
+ #
41
+ # @param label [String] The label to print
42
+ def self.label(label)
43
+ print Rainbow('[').blue.bright
44
+ print Rainbow(label).green.bright
45
+ print Rainbow(']').blue.bright
46
+ print Rainbow('|').bright
47
+ end
48
+
49
+ # Adds an object to the outfile array.
50
+ #
51
+ # @param obj [String] The item to add
52
+ def self.tofile(obj)
53
+ if $fmt != nil
54
+ $tofile.push obj
55
+ end
56
+ end
57
+ end
58
+
59
+ include GLI::App
60
+ program_desc 'A powerful text translation tool.'
61
+ config_file '.oversetter.yml'
62
+ version Oversetter::VERSION
63
+
64
+ flag [:h, :http], :default_value => nil, :arg_name => 'string', :desc => 'HTTP adapter', :long_desc => 'httpclient, curb, em_http, net_http_persistent, excon, rack'
65
+ flag [:j, :json], :default_value => nil, :arg_name => 'string', :desc => 'JSON adapter', :long_desc => 'oj, yajl, json_gem, json_pure'
66
+ flag [:o, :out], :default_value => nil, :arg_name => 'filename', :desc => 'Output file', :long_desc => 'filename.json or filename.xml'
67
+ flag [:x, :xml], :default_value => nil, :arg_name => 'string', :desc => 'XML adapter', :long_desc => 'ox, libxml, nokogiri, rexml'
68
+ switch [:fo, :force], :default_value => false, :desc => 'Force overwrite', :long_desc => 'Overwrites existing JSON & XML files'
69
+ switch [:v, :verbose], :default_value => false, :desc => 'Prints parameters and options'
70
+
71
+ require 'oversetter/cli/glosbe'
72
+ require 'oversetter/cli/hablaa'
73
+ require 'oversetter/cli/yandex'
74
+
75
+ pre do |global, command, options, args|
76
+ # Pre logic here
77
+ # Return true to proceed; false to abort and not call the
78
+ # chosen command
79
+ # Use skips_pre before a command to skip this block
80
+ # on that command only
81
+ if global[:o] != nil
82
+ outfile = global[:o]
83
+ if outfile =~ /\w*\.json/
84
+ $fmt = :json
85
+ elsif outfile =~ /\w*\.xml/
86
+ $fmt = :xml
87
+ else
88
+ puts 'Invalid file extension.'
89
+ end
90
+ $tofile = []
91
+ end
92
+ http = global[:h]
93
+ json = global[:j]
94
+ xml = global[:x]
95
+ if http != nil
96
+ if http.class != Symbol then http = http.to_sym; end
97
+ Oversetter.http(http)
98
+ end
99
+ if json != nil
100
+ if json.class != Symbol then json = json.to_sym; end
101
+ Oversetter.json(json)
102
+ end
103
+ if xml != nil
104
+ if xml.class != Symbol then xml = xml.to_sym; end
105
+ Oversetter.xml(xml)
106
+ end
107
+ if global[:v]
108
+ Oversetter.label('Global options')
109
+ print "#{global}"
110
+ Oversetter.label('Command')
111
+ print "#{command.name}"
112
+ Oversetter.label('Command options')
113
+ print "#{options}"
114
+ Oversetter.label('Args')
115
+ print "#{args}"
116
+ Oversetter.label('Bootstrapped')
117
+ puts ''
118
+ end
119
+ true
120
+ end
121
+
122
+ post do |global, command, options, args|
123
+ # Post logic here
124
+ # Use skips_post before a command to skip this
125
+ # block on that command only
126
+ if $fmt != nil
127
+ outfile = global[:o]
128
+ if File.exist?(outfile) && global[:fo] == true
129
+ if $fmt == :json
130
+ fo = File.new(outfile, 'w+')
131
+ fo.print MultiJson.dump($tofile, :pretty => true)
132
+ fo.close
133
+ puts Rainbow("Word search was written to #{outfile}.").bright
134
+ elsif $fmt == :xml
135
+ fo = File.new(outfile, 'w+')
136
+ fo.print '<?xml version="1.0" encoding="utf-8"?>'
137
+ fo.print XmlFu.xml($tofile)
138
+ fo.close
139
+ puts Rainbow("Word search was written to #{outfile}.").bright
140
+ else
141
+ puts 'Invalid file extension.'
142
+ end
143
+ end
144
+ if File.exist?(outfile) && global[:fo] == false
145
+ puts Rainbow("#{outfile} exists. Overwrite? y/n ").bright
146
+ ans = gets
147
+ if ans =~ /y/
148
+ if $fmt == :json
149
+ fo = File.new(outfile, 'w+')
150
+ fo.print MultiJson.dump($tofile, :pretty => true)
151
+ fo.close
152
+ puts Rainbow("Word search was written to #{outfile}.").bright
153
+ elsif $fmt == :xml
154
+ fo = File.new(outfile, 'w+')
155
+ fo.print '<?xml version="1.0" encoding="utf-8"?>'
156
+ fo.print XmlFu.xml($tofile)
157
+ fo.close
158
+ puts Rainbow("Word search was written to #{outfile}.").bright
159
+ else
160
+ puts 'Invalid file extension.'
161
+ end
162
+ else
163
+ puts 'Please try again with a different filename.'
164
+ end
165
+ else
166
+ if $fmt == :json
167
+ fo = File.open(outfile, 'w+')
168
+ fo.print MultiJson.dump($tofile, :pretty => true)
169
+ fo.close
170
+ puts Rainbow("Word search was written to #{outfile}.").bright
171
+ elsif $fmt == :xml
172
+ fo = File.open(outfile, 'w+')
173
+ fo.print '<?xml version="1.0" encoding="utf-8"?>'
174
+ fo.print XmlFu.xml($tofile)
175
+ fo.close
176
+ puts Rainbow("Word search was written to #{outfile}.").bright
177
+ else
178
+ puts 'Invalid file extension.'
179
+ end
180
+ end
181
+ end
182
+ if global[:v]
183
+ Oversetter.label('Shutdown')
184
+ puts ''
185
+ end
186
+ end
187
+
188
+ on_error do |exception|
189
+ # Error logic here
190
+ # return false to skip default error handling
191
+ true
192
+ end
193
+ exit run(ARGV)
@@ -0,0 +1,38 @@
1
+ desc 'Glosbe operations'
2
+ command :gl do |osbe|
3
+ osbe.desc 'Translates given text'
4
+ osbe.arg_name 'word'
5
+ osbe.command :tr do |translate|
6
+ translate.flag :src, :default_value => nil, :arg_name => 'string', :desc => '3-letter ISO 693-3 source language code (Required)'
7
+ translate.flag :tar, :default_value => nil, :arg_name => 'string', :desc => '3-letter ISO 693-3 target language code (Required)'
8
+ translate.switch :auth, :default_value => false, :desc => 'Show authors'
9
+ translate.action do |global_options, options, args|
10
+ if args.length > 1
11
+ search = args.join(' ')
12
+ else
13
+ search = args[0]
14
+ end
15
+ params = { src: options[:src], tar: options[:tar], auth: options[:auth] }
16
+ tran = Oversetter::Glosbe::Translate.new
17
+ tran.get_trans(search, params)
18
+ end
19
+ end
20
+ osbe.desc 'Fetches examples'
21
+ osbe.arg_name 'word'
22
+ osbe.command :ex do |example|
23
+ example.flag :esrc, :default_value => nil, :arg_name => 'string', :desc => '3-letter ISO 693-3 source language code (Required)'
24
+ example.flag :etar, :default_value => nil, :arg_name => 'string', :desc => '3-letter ISO 693-3 target language code (Required)'
25
+ example.flag :page, :default_value => 1, :arg_name => 'integer', :desc => 'Page of results to be displayed (200 Max)'
26
+ example.flag :size, :default_value => 1, :arg_name => 'integer', :desc => 'Size of the result page (30 Max)'
27
+ example.action do |global_options, options, args|
28
+ if args.length > 1
29
+ search = args.join(' ')
30
+ else
31
+ search = args[0]
32
+ end
33
+ params = { src: options[:esrc], tar: options[:etar], page: options[:page], size: options[:size] }
34
+ ex = Oversetter::Glosbe::Example.new
35
+ ex.get_ex(search, params)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,61 @@
1
+ desc 'Hablaa operations'
2
+ command :ha do |blaa|
3
+ blaa.desc 'Lists supported languages'
4
+ blaa.arg_name 'word'
5
+ blaa.command :li do |list|
6
+ list.action do |global_options, options, args|
7
+ search, params = args[0], nil
8
+ if search == nil then search = 'blank'; end
9
+ lang = Oversetter::Hablaa::Getlangs.new
10
+ lang.get_lang(search, params)
11
+ end
12
+ end
13
+ blaa.desc 'Translates given text'
14
+ blaa.arg_name 'word'
15
+ blaa.command :tr do |translate|
16
+ translate.flag :src, :default_value => nil, :arg_name => 'string', :desc => '3-letter ISO 693-3 source language code (Required)'
17
+ translate.flag :tar, :default_value => nil, :arg_name => 'string', :desc => '3-letter ISO 693-3 source language code (Required)'
18
+ translate.action do |global_options, options, args|
19
+ if args.length > 1
20
+ search = args.join(' ')
21
+ else
22
+ search = args[0]
23
+ end
24
+ params = { src: options[:src], tar: options[:tar] }
25
+ trans = Oversetter::Hablaa::Translate.new
26
+ trans.get_trans(search, params)
27
+ end
28
+ end
29
+ blaa.desc 'Fetches examples'
30
+ blaa.arg_name 'word'
31
+ blaa.command :ex do |example|
32
+ example.flag :esrc, :default_value => nil, :arg_name => 'string', :desc => '3-letter ISO 693-3 source language code (Required)'
33
+ example.flag :etar, :default_value => nil, :arg_name => 'string', :desc => '3-letter ISO 693-3 source language code (Required)'
34
+ example.action do |global_options, options, args|
35
+ if args.length > 1
36
+ search = args.join(' ')
37
+ else
38
+ search = args[0]
39
+ end
40
+ params = { src: options[:esrc], tar: options[:etar] }
41
+ trans = Oversetter::Hablaa::Example.new
42
+ trans.get_ex(search, params)
43
+ end
44
+ end
45
+ blaa.desc 'Fetches similar translations'
46
+ blaa.arg_name 'word'
47
+ blaa.command :si do |similar|
48
+ similar.flag :ssrc, :default_value => nil, :arg_name => 'string', :desc => '3-letter ISO 693-3 source language code (Required)'
49
+ similar.flag :star, :default_value => nil, :arg_name => 'string', :desc => '3-letter ISO 693-3 source language code (Required)'
50
+ similar.action do |global_options, options, args|
51
+ if args.length > 1
52
+ search = args.join(' ')
53
+ else
54
+ search = args[0]
55
+ end
56
+ params = { src: options[:ssrc], tar: options[:star] }
57
+ trans = Oversetter::Hablaa::Similar.new
58
+ trans.get_sim(search, params)
59
+ end
60
+ end
61
+ end