startask 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30dbb24b427f846ff42bd6211855e50afd63003ab67c247bae7168612742202b
4
- data.tar.gz: 9ecb8e6c9650ccf8bbd4bb4617bd57c710639e73e0f9ee6a986c7d3272425a37
3
+ metadata.gz: 8ad982911d12e9f54c2591e57aae3e8088282984540dff16709e41717cb251b7
4
+ data.tar.gz: e6cfffc5ef7f5ad1cbb9f4ebb7f75a9b36197236533d2f1221d123ce13f44388
5
5
  SHA512:
6
- metadata.gz: 06ed3157c3f971f0499ad91bfb992380cb48e00f6b6daa7ad6072099219d0e39c2ad78bf33feb3460095f79391076757f770c3fbd38e96d0bc6bea4e7930a46a
7
- data.tar.gz: 94bd4563c6b3b0ff69f0e4af49e228bffca7353eed5d94ce4667ca2ab07e511385489f934663d4b751210e3656745fa85b18fd9f3a7cfc7e223ceea22ebe8d78
6
+ metadata.gz: f731d444684c1029944c4695d802737985902420c49034ca89f2073ee9e6b397d98e04ab05cd390d06c922dba9a0bd8d47e6549651c878968eceed76589c8477
7
+ data.tar.gz: 7a5a2a533bc8d830838bac80542e9b61e52cc755ec280683374405064da0d6e8796b0fd7cdc1e5f484cb7df194028d32f57f6d797a3500d946ff5194741307f5
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/startask.rb CHANGED
@@ -6,35 +6,66 @@
6
6
  require 'kvx'
7
7
  require 'rxfhelper'
8
8
 
9
+ module RecordHelper
10
+
11
+ def generate_id()
12
+ Time.now.to_f.to_s.sub('.','-')
13
+ end
14
+
15
+ def logit(label)
16
+ @log << [label, Time.now]
17
+ #@log << [label, Time.now.strftime("%a, %d %b %Y, %H:%M%P")]
18
+ @log.last
19
+ end
20
+
21
+ end
22
+
9
23
  class ActionItem
24
+ include RecordHelper
10
25
 
11
- attr_reader :log
26
+ attr_reader :id
12
27
 
13
- def initialize(s)
28
+ def initialize(s, callback=nil, id: nil)
29
+
30
+ @id = id || generate_id()
14
31
  @title = s
15
32
  @log = []
33
+ @callback = callback
34
+
16
35
  end
17
36
 
18
37
  def done()
19
- logit :completed
38
+ logaction :completed
20
39
  end
21
40
 
22
41
  alias completed done
42
+
43
+ def find(id)
44
+ return self if id == @id
45
+ end
46
+
47
+ def log(detail: false)
48
+ detail ? @log.map {|x| [@id, x[-1], x[0], @title]} : @log
49
+ end
50
+
51
+ def logupdate(item)
52
+ @log << item
53
+ end
23
54
 
24
55
  def to_s()
25
56
  @title
26
57
  end
27
58
 
28
59
  def started()
29
- logit :started
60
+ logaction :started
30
61
  end
31
62
 
32
63
  def status()
33
- @log.last
64
+ @log.last ? @log.last : []
34
65
  end
35
66
 
36
67
  def stopped()
37
- logit :stopped
68
+ logaction :stopped
38
69
  end
39
70
 
40
71
  def status=(status)
@@ -48,25 +79,50 @@ class ActionItem
48
79
 
49
80
  end
50
81
 
82
+ def to_xml()
83
+
84
+ h = {id: @id, status: status().join(' ')}
85
+ Rexle::Element.new(:action, attributes: h, value: @title)
86
+
87
+ end
88
+
51
89
  private
52
90
 
53
- def logit(label)
54
- @log << [label, Time.now.strftime("%H:%M%P")]
91
+ def logaction(label)
92
+ r = logit label
93
+ @callback.status = r.clone.insert(1, @title) if @callback
55
94
  end
56
95
 
57
96
  end
58
97
 
59
98
  class Actions
60
99
 
100
+ attr_accessor :status
101
+
102
+ def initialize(obj=nil, callback=nil)
103
+
104
+ @a = read obj if obj.is_a? Rexle::Recordset
105
+ @callback = callback
106
+
107
+ end
108
+
109
+ def find(id)
110
+ @a.find {|x| x.find id}
111
+ end
112
+
61
113
  def import(a)
62
114
 
63
115
  @a = a.map do |x|
64
- x.is_a?(String) ? ActionItem.new(x) : Actions.new.import(x)
116
+ x.is_a?(String) ? ActionItem.new(x, @callback) : Actions.new(@callback).import(x)
65
117
  end
66
118
 
67
119
  return self
68
120
 
69
121
  end
122
+
123
+ def log()
124
+ @a.flat_map {|x| x.log(detail: true)}
125
+ end
70
126
 
71
127
  def [](i)
72
128
  @a[i]
@@ -75,25 +131,65 @@ class Actions
75
131
  def to_s()
76
132
  @a.each(&:to_s)
77
133
  end
134
+
135
+ def to_xml()
136
+
137
+ Rexle::Element.new( :actions, value: @a.map(&:to_xml).join)
138
+ #@a.map(&:to_xml)
139
+ end
140
+
141
+ private
142
+
143
+ def read(node)
144
+
145
+ node.map do |e|
146
+
147
+ case e.name.to_sym
148
+ when :action
149
+ ActionItem.new(e.text, id: e.attributes[:id])
150
+ when :actions
151
+ Actions.new(e.xpath('*'), callback: @callback)
152
+ end
153
+
154
+ end
155
+
156
+ end
78
157
 
79
158
  end
80
159
 
81
160
  class ResultItem
161
+ include RecordHelper
82
162
 
83
- def initialize(s)
163
+ attr_reader :id
164
+
165
+ def initialize(s, callback=nil, id: nil)
166
+ @id = id || generate_id()
84
167
  @evidence = s
168
+ @callback = callback
85
169
  end
86
170
 
87
171
  def to_s()
88
172
  @evidence
89
173
  end
90
174
 
175
+ def to_xml()
176
+
177
+ h = {id: @id}
178
+ Rexle::Element.new(:result, attributes: h, value: @evidence)
179
+
180
+ end
181
+
91
182
  end
92
183
 
93
184
  class Results
94
185
 
95
- def initialize()
96
-
186
+ def initialize(obj=nil, callback: nil)
187
+
188
+ @a = read obj if obj.is_a? Rexle::Recordset
189
+ @callback = callback
190
+
191
+ return self
192
+
97
193
  end
98
194
 
99
195
  def import(a)
@@ -121,6 +217,12 @@ class Results
121
217
  end.join("\n")
122
218
  end
123
219
 
220
+ def to_xml()
221
+
222
+ Rexle::Element.new( :results, value: @a.map(&:to_xml).join)
223
+
224
+ end
225
+
124
226
  def print_row(id, indent: 0)
125
227
 
126
228
  @a.map.with_index do |x, i|
@@ -132,6 +234,23 @@ class Results
132
234
  end.join("\n")
133
235
  end
134
236
 
237
+ private
238
+
239
+ def read(node)
240
+
241
+ node.map do |e|
242
+
243
+ case e.name.to_sym
244
+ when :result
245
+ ResultItem.new(e.text, id: e.attributes[:id])
246
+ when :results
247
+ Results.new(e.xpath('*'), callback: @callback)
248
+ end
249
+
250
+ end
251
+
252
+ end
253
+
135
254
  end
136
255
 
137
256
  # task dictionary
@@ -150,20 +269,35 @@ end
150
269
  # started (method) adds a status message to the log
151
270
 
152
271
  class StarTask
272
+ include RecordHelper
153
273
 
154
- attr_reader :situation, :task, :actions, :log, :results
274
+ attr_reader :situation, :task, :actions, :results, :id
155
275
 
156
276
  def initialize(src=nil, debug: false)
277
+
157
278
  @debug = debug
279
+ @id = generate_id()
158
280
  @log = []
159
- import src if src
281
+ @status = ''
282
+ read src if src
283
+
160
284
  end
161
285
 
162
286
  def done()
163
- logit :completed
287
+ logtask :completed
164
288
  end
165
289
 
166
290
  alias completed done
291
+
292
+ def find(id)
293
+
294
+ if @id == id then
295
+ return self
296
+ else
297
+ @actions.find id
298
+ end
299
+
300
+ end
167
301
 
168
302
  def import(raws)
169
303
 
@@ -179,7 +313,7 @@ class StarTask
179
313
 
180
314
  rawlabel, body = x.split(/\n/,2)
181
315
 
182
- [rawlabel.downcase.gsub(/\s+/, '_').to_sym,
316
+ [rawlabel.rstrip.downcase.gsub(/\s+/, '_').to_sym,
183
317
  body.strip.gsub(/^(\s*)[\*\+] /,'\1')]
184
318
 
185
319
  end.to_h
@@ -200,30 +334,108 @@ class StarTask
200
334
  kvx = Kvx.new obj
201
335
  @situation = kvx.situation
202
336
  @task = kvx.task
203
- @actions = Actions.new.import(kvx.action[:items])
204
- @results = Results.new.import(kvx.result[:items])
337
+ @actions = Actions.new(self).import(kvx.action[:items])
338
+ @results = Results.new(self).import(kvx.result[:items])
205
339
 
206
340
  end
341
+
342
+ def log(detail: false)
343
+ #todo
344
+ detail ? @log.map {|x| [@id, x[-1], x[0]]} : @log
345
+ end
346
+
347
+ def logupdate(item)
348
+ @log << item
349
+ end
207
350
 
208
351
  # adds a status message to the log
209
352
  #
210
353
  def started()
211
- logit :started
354
+ logtask :started
212
355
  end
213
356
 
214
357
  def status()
215
- @log.last
358
+ @status
216
359
  end
217
360
 
218
- def stopped()
219
- logit :stopped
361
+ def status=(s)
362
+ @status = s
220
363
  end
221
364
 
365
+ def stopped()
366
+ logtask :stopped
367
+ end
368
+
369
+ def to_xml()
370
+
371
+ situation = Rexle::Element.new( :situation, value: @situation)
372
+ task = Rexle::Element.new( :task, value: @task)
373
+
374
+ doc = Rexle.new()
375
+ doc.add Rexle::Element.new(:star)
376
+ doc.root.attributes[:id] = @id
377
+ doc.root.add situation
378
+ doc.root.add task
379
+ doc.root.add @actions.to_xml
380
+ doc.root.add @results.to_xml
381
+
382
+ logx = log(detail: true) + @actions.log
383
+
384
+ lognode = Rexle::Element.new(:log)
385
+
386
+ logx.sort_by {|x| x[1]}.reverse.each do |x|
387
+
388
+ attr = {id: x[0], timestamp: x[1]}
389
+ val = "[%s]" % x[2]
390
+ val += ' ' + x[3] if x[3]
391
+
392
+ lognode.add Rexle::Element.new( :li, attributes: attr, value: val)
393
+ end
394
+
395
+ doc.root.add lognode
396
+ doc.root.xml pretty: true
397
+
398
+ end
399
+
222
400
  private
223
401
 
224
- def logit(label)
225
- @log << [label, Time.now.strftime("%H:%M%P")]
226
- return label
402
+ def logtask(label)
403
+
404
+ r = logit label
405
+ @status = r
406
+
407
+ end
408
+
409
+ def read(obj)
410
+
411
+ s, _ = RXFHelper.read obj
412
+ doc = Rexle.new(s)
413
+ @id = doc.root.attributes[:id]
414
+ @situation = doc.root.element('situation').text
415
+ @task = doc.root.element('task').text
416
+ actions = doc.root.xpath('actions/*')
417
+ @actions = Actions.new actions
418
+ results = doc.root.xpath('results/*')
419
+ @results = Results.new results
420
+
421
+ log = doc.root.xpath('log/*')
422
+
423
+ log.reverse.each do |x|
424
+
425
+ id = x.attributes[:id]
426
+ found = find id
427
+
428
+ if found then
429
+ d = Time.parse(x.attributes[:timestamp])
430
+ puts 'x.text: ' + x.text.inspect if @debug
431
+ status = x.text[/^\[([^\]]+)/,1]
432
+ found.logupdate([status.to_sym, d])
433
+ end
434
+
435
+
436
+ end
437
+
438
+ doc.xml pretty: true
227
439
  end
228
440
 
229
441
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: startask
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -36,7 +36,7 @@ cert_chain:
36
36
  BWN+6F7HF8UfTUHWCQqk9FoCAYtbfQWLUqZwWN+dLzn1CPK0yoPjuSR59/HShliV
37
37
  IUEz9QfhClpwEVu+leXBe7RwfxADwAWYEfI=
38
38
  -----END CERTIFICATE-----
39
- date: 2022-09-28 00:00:00.000000000 Z
39
+ date: 2022-10-01 00:00:00.000000000 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rxfhelper
metadata.gz.sig CHANGED
Binary file