LRH 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/LRH.rb +308 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e4ffed3ad5dfc6317cd2622188ac955f15a12f91
4
+ data.tar.gz: eed070fa6d11c75c59acd352b367ff8bcb8c0359
5
+ SHA512:
6
+ metadata.gz: b028377d8746f7ab7c35a472a1d68c9de05bb89e58914557c6c51f5cfce192f5eb58288f8fa55020449b09e17bbe2871a9a44d889c1472301d2761d41136853d
7
+ data.tar.gz: 1683e3738a6ce7767e4481d1de688919f9e2e06f74b393583bcc67f894a922f89e30cf9e8a187b26c40f321fa9d2b68f3c19572826fc58f680b3f822bc1c9a71
data/LRH.rb ADDED
@@ -0,0 +1,308 @@
1
+ class IOAble
2
+ def initialize
3
+ @config = {}
4
+ end
5
+
6
+ def set_option(name, value)
7
+ @config[name] = value
8
+ end
9
+
10
+ def get_option(name)
11
+ return @config[name]
12
+ end
13
+
14
+ def debug!
15
+ set_option :debug, true
16
+
17
+ self
18
+ end
19
+
20
+ def debug?
21
+ return get_option(:debug)
22
+ end
23
+
24
+ def log(obj)
25
+ msg = nil
26
+ msg = "Lister: " + obj.to_s if self.is_a? Lister
27
+ msg = "Runner: " + obj.to_s if self.is_a? Runner
28
+ msg = "Harvester: " + obj.to_s if self.is_a? Harvester
29
+
30
+ puts msg
31
+ return msg
32
+ end
33
+ end
34
+
35
+ class Lister < IOAble
36
+ def initialize
37
+ super
38
+ @limit = false
39
+ end
40
+
41
+ def limit!(n)
42
+ @limit = n
43
+
44
+ self
45
+ end
46
+
47
+ def _list
48
+ limit = @limit
49
+ self.list do |e|
50
+ yield e
51
+
52
+ limit -= 1 if limit
53
+ return if limit && limit <= 0
54
+ end
55
+ end
56
+
57
+ def list
58
+ list_array.each do |e|
59
+ yield e
60
+ end
61
+
62
+ self
63
+ end
64
+
65
+ def list_array
66
+ return []
67
+ end
68
+ end
69
+
70
+ class ParallelLister < Lister
71
+ def initialize(threads=4)
72
+ super()
73
+ @threads_number = threads
74
+ @threads = []
75
+ end
76
+
77
+ def list
78
+ queue = []
79
+ done = false
80
+ Thread.start do
81
+ elements.each do |element|
82
+ puts element
83
+ while @threads.size == @threads_number
84
+ torem = []
85
+ @threads.each do |t|
86
+ torem.push t unless t.status
87
+ end
88
+ @threads -= torem
89
+ sleep 0.1
90
+ end
91
+
92
+ @threads.push Thread.start do
93
+ list_one(element) do |target|
94
+ queue.push target
95
+ end
96
+ end
97
+ end
98
+ done = true
99
+ end
100
+
101
+ while !done || queue.size > 0
102
+ while queue.size == 0
103
+ puts "Waiting..."
104
+ sleep 0.1
105
+ end
106
+
107
+ yield queue.shift
108
+ end
109
+ end
110
+
111
+ def elements
112
+ end
113
+
114
+ def list_one(element)
115
+ end
116
+ end
117
+
118
+ class Runner < IOAble
119
+ def initialize
120
+ super
121
+ end
122
+
123
+ def _run(target)
124
+ result = run(target)
125
+ if debug?
126
+ log result
127
+ end
128
+
129
+ return result
130
+ end
131
+
132
+ def _finally
133
+ finally do |to_harvest|
134
+ yield to_harvest
135
+ end
136
+ end
137
+
138
+ def finally
139
+ end
140
+
141
+ def run(target)
142
+ return nil
143
+ end
144
+ end
145
+
146
+ class Harvester < IOAble
147
+ def initialize
148
+ super
149
+ end
150
+
151
+ def _harvest(partial)
152
+ return harvest(partial)
153
+ end
154
+
155
+ def _result
156
+ return result()
157
+ end
158
+
159
+ def harvest(partial)
160
+ end
161
+
162
+ def result
163
+ return nil
164
+ end
165
+ end
166
+
167
+ class Job
168
+ def initialize(lister, runner, harvester)
169
+ @lister = lister
170
+ @runner = runner
171
+ @harvester = harvester
172
+ end
173
+
174
+ def run
175
+ @lister._list do |target|
176
+ partial = @runner._run(target)
177
+ @harvester._harvest(partial)
178
+ end
179
+
180
+ @runner._finally do |to_harvest|
181
+ @harvester._harvest(to_harvest)
182
+ end
183
+
184
+ return @harvester._result
185
+ end
186
+ end
187
+
188
+ class PartialJob < Job
189
+ def initialize(lister=NullLister.new, runner=NullRunner.new, harvester=NullHarvester.new)
190
+ super
191
+ end
192
+ end
193
+
194
+
195
+ ###########################################
196
+ # STUBS
197
+ ###########################################
198
+
199
+ class StubLister < Lister
200
+ def initialize(spec={:time => Integer})
201
+ @spec = spec
202
+ end
203
+
204
+ def rand_from_spec
205
+ hash = {}
206
+
207
+ @spec.each do |key, klass|
208
+ if [Integer, Numeric].include? klass
209
+ hash[key] = rand(10000)
210
+ elsif String == klass
211
+ hash[key] = (0...rand(100)).map { (('a'..'z').to_a + ('A'..'Z').to_a + ['!', '?', '/', '>', '<'])[rand(57)] }.join
212
+ elsif Array == klass
213
+ hash[key] = []
214
+ rand(10).times do
215
+ hash[key].push (0...rand(100)).map { (('a'..'z').to_a + ('A'..'Z').to_a + ['!', '?', '/', '>', '<'])[rand(57)] }.join
216
+ end
217
+ else
218
+ hash[key] = klass
219
+ end
220
+ end
221
+
222
+ return hash
223
+ end
224
+
225
+ def list
226
+ 10.times do |i|
227
+ yield rand_from_spec
228
+ end
229
+
230
+ self
231
+ end
232
+ end
233
+
234
+ class StubRunner < Runner
235
+ def run(target)
236
+ return target
237
+ end
238
+ end
239
+
240
+ class StubHarvester < Harvester
241
+ def initialize
242
+ @result = ""
243
+ end
244
+
245
+ def harvest(partial)
246
+ @result += partial.to_s + "\n"
247
+ end
248
+
249
+ def result
250
+ return @result
251
+ end
252
+ end
253
+
254
+ class NullLister < Lister
255
+ def list
256
+ end
257
+ end
258
+
259
+ class NullRunner < Runner
260
+ def run(target)
261
+ return nil
262
+ end
263
+ end
264
+
265
+ class NullHarvester < Harvester
266
+ def harvest(partial)
267
+ end
268
+
269
+ def result
270
+ return nil
271
+ end
272
+ end
273
+
274
+ ###########################################
275
+ # TEST RUNNER
276
+ ###########################################
277
+ class LRHTest
278
+ def self.test_job(lister, runner, harvester)
279
+ job = Job.new(lister, runner, harvester)
280
+ puts job.run
281
+ end
282
+
283
+ def self.test_lister(lister)
284
+ self.test_job(lister, StubRunner.new, StubHarvester.new)
285
+ end
286
+
287
+ def self.test_runner(runner, listerOrSpec=StubLister.new)
288
+ listerOrSpec = StubLister.new(listerOrSpec) unless listerOrSpec.is_a? Lister
289
+ self.test_job(listerOrSpec, runner, StubHarvester.new)
290
+ end
291
+
292
+ def self.test_harvester(harvester, runner=StubRunner.new, listerOrSpec=StubLister.new)
293
+ listerOrSpec = StubLister.new(listerOrSpec) unless listerOrSpec.is_a? Lister
294
+ self.test_job(listerOrSpec, runner, harvester)
295
+ end
296
+ end
297
+
298
+ def require_lister lname
299
+ "require_relative \"listers/#{lname}\""
300
+ end
301
+
302
+ def require_runner rname
303
+ "require_relative \"runners/#{rname}\""
304
+ end
305
+
306
+ def require_harvester hname
307
+ "require_relative \"harvesters/#{hname}\""
308
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: LRH
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Simone Scalabrino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Lister-Runner-Harvester framework
14
+ email: s.scalabrino9@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LRH.rb
20
+ homepage: ''
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.6.11
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Lister-Runner-Harvester framework
43
+ test_files: []