ykutils 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,462 @@
1
+ # -*- coding utf-8 -*-
2
+
3
+ require 'ykutils/debugutils'
4
+ require 'ykutils/lines'
5
+ require 'ykutils/specfileop'
6
+ require 'datastructop'
7
+ require 'filex'
8
+
9
+ module Ykutils
10
+
11
+ class StructuredText
12
+ attr_reader :main_ary , :main_hash , :item_ary
13
+
14
+ include SpecFileOp
15
+ include DataStructOp
16
+ include DebugUtils
17
+
18
+ def initialize( debug = false )
19
+ @text = nil
20
+ @fname = nil
21
+ @text_ary = []
22
+
23
+ debug_utils_init
24
+ set_debug( debug )
25
+ end
26
+
27
+ def load_analyze( fname , subfname = nil )
28
+ # puts_current_method
29
+ # puts "-0 fname=#{fname}"
30
+
31
+ @fname = fname
32
+ @text_ary = load(fname)
33
+ # @text_ary.each do |x| puts x end
34
+ analyze(@text_ary , subfname )
35
+ end
36
+
37
+ def load( fname )
38
+ load_plain_text_file( fname ).collect { |it| it.chomp }
39
+ end
40
+
41
+ def analyze( line_ary , subfname = nil )
42
+
43
+ end
44
+
45
+ def dump_to_file( fname )
46
+ begin
47
+ file = FileX.open( fname , 'w'){ |file|
48
+ @text_ary.each do |l|
49
+ file.write( l + "\n" )
50
+ end
51
+ }
52
+ rescue => ex
53
+ pp ex
54
+ pp ex.backtrace
55
+ @valid = false
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ class StructuredTextForFtpOp < StructuredText
62
+ def analyze( line_ary , fname = nil )
63
+ lines = FtpOpLines.new( line_ary )
64
+ lines.output_f( fname )
65
+
66
+ analyze_sub( lines )
67
+ end
68
+ end
69
+
70
+ class StructuredTextForAccount < StructuredText
71
+ attr_reader :main_host_ary , :main_host_hash , :main_domain_ary , :main_domain_hash , :main_sep_ary , :main_sep_hash , :main_host_item_ary
72
+
73
+ def initialize
74
+ @main_host_item_ary = []
75
+
76
+ @main_sep_ary = []
77
+ @main_sep_hash = { }
78
+
79
+ @main_host_ary = []
80
+ @main_host_hash = { }
81
+ 1
82
+ @main_domain_ary = []
83
+ @main_domain_hash = { }
84
+ end
85
+
86
+ def analyze( line_ary , fname = nil )
87
+ lines = AccountLines.new( line_ary )
88
+ lines.output_f( fname )
89
+
90
+ analyze_sub( lines )
91
+ end
92
+
93
+ def analyze_sub( lines )
94
+ host = StructuredTextForAccountOneLayerHost.new
95
+ domain = StructuredTextForAccountOneLayerDomain.new
96
+
97
+ while line = lines.get_line
98
+ case line["STATUS"]
99
+ when AccountLines::HOST_ACCOUNT_START , AccountLines::HOST_ACCOUNT , AccountLines::HOST_ACCOUNT_END
100
+ if line["CONTENT"]
101
+ key , value = line["CONTENT"].split(":")
102
+ end
103
+ key.strip! if key
104
+ value.strip! if value
105
+ host.analyze( line["STATUS"] , line , key , value , @main_host_ary, @main_host_hash )
106
+ when AccountLines::DOMAIN_ACCOUNT_START , AccountLines::DOMAIN_ACCOUNT , AccountLines::DOMAIN_ACCOUNT_END
107
+ if line["CONTENT"]
108
+ key , value = line["CONTENT"].split(":")
109
+ end
110
+ key.strip! if key
111
+ value.strip! if value
112
+ domain.analyze( line["STATUS"] , line , key , value , @main_domain_ary, @main_domain_hash )
113
+ when AccountLines::SEPARATOR
114
+ if line["CONTENT"]
115
+ key , value = line["CONTENT"].split(":")
116
+ end
117
+
118
+ @main_sep_ary << key
119
+ @main_sep_hash[key] = { "TITLE" => key }
120
+
121
+ else
122
+ puts( "Error" )
123
+ puts( line["CONTENT"] )
124
+ end
125
+ end
126
+ @main_host_item_ary |= host.item_ary
127
+ end
128
+
129
+ def sort_by( other )
130
+ diff_main_host_ary = @main_host_ary - other.main_host_ary
131
+ diff_main_domain_ary = @main_domain_ary - other.main_domain_ary
132
+
133
+ n_main_sep_ary = []
134
+ n_main_sep_hash = { }
135
+ n_main_host_ary = []
136
+ n_main_host_hash = { }
137
+ n_main_domain_ary = []
138
+ n_main_domain_hash = { }
139
+
140
+ if other.main_sep_ary.size > 0
141
+ @main_sep_ary |= other.main_sep_ary
142
+ @main_sep_hash |= n_main_sep_hash
143
+ end
144
+
145
+ main_host_item_ary = other.main_host_item_ary | @main_host_item_ary
146
+
147
+ other.main_host_ary.each do |it|
148
+ sort_by_sub(it , @main_host_hash , main_host_item_ary, n_main_host_hash , n_main_host_ary )
149
+ end
150
+
151
+ diff_main_host_ary.each do |it|
152
+ sort_by_sub(it , @main_host_hash , main_host_item_ary , n_main_host_hash, n_main_host_ary )
153
+ end
154
+
155
+ other.main_domain_ary.each do |it|
156
+ sort_by_sub_for_array(it , @main_domain_hash , n_main_domain_hash, n_main_domain_ary )
157
+ end
158
+
159
+ diff_main_domain_ary.each do |it|
160
+ sort_by_sub_for_array(it , @main_domain_hash , n_main_domain_hash, n_main_domain_ary )
161
+ end
162
+
163
+ @main_host_ary = n_main_host_ary
164
+ @main_host_hash = n_main_host_hash
165
+ @main_domain_ary = n_main_domain_ary
166
+ @main_domain_hash = n_main_domain_hash
167
+ end
168
+
169
+ def sort_by_sub(it , item_hash , item_ary , n_hash , n_ary )
170
+ h = item_hash[it]
171
+ if h
172
+ n_ary << it
173
+ title = h["TITLE"]
174
+ hash = h["CONTENT"]
175
+ ary = []
176
+ if hash
177
+ item_ary.each do |ai|
178
+ v = hash[ai]
179
+ if v
180
+ ary << ai
181
+ end
182
+ end
183
+ end
184
+ n_hash[it] = { "TITLE" => title , "META" => ary , "CONTENT" => hash }
185
+ end
186
+ end
187
+
188
+ def sort_by_sub_for_array(it , item_hash , n_hash , n_ary )
189
+ h = item_hash[it]
190
+ if h
191
+ n_ary << it
192
+ title = h["TITLE"]
193
+ ary = h["META"]
194
+ hash = h["CONTENT"]
195
+
196
+ n_hash[it] = { "TITLE" => title , "META" => ary , "CONTENT" => hash }
197
+ end
198
+ end
199
+
200
+ def dump( file = nil )
201
+ dump_ary = []
202
+ dump_ary += dump_sub( @main_sep_ary, @main_sep_hash )
203
+ dump_ary += dump_sub( @main_host_ary, @main_host_hash )
204
+ dump_ary += dump_sub_for_array( @main_domain_ary, @main_domain_hash )
205
+
206
+ str = dump_ary.join("\n")
207
+
208
+ if file
209
+ begin
210
+ file.write( str )
211
+ file.write( "\n" )
212
+ rescue => ex
213
+ pp ex
214
+ pp ex.backtrace
215
+ @valid = false
216
+ end
217
+ end
218
+ str
219
+ end
220
+
221
+ def dump_sub( ary , hash )
222
+ dump_ary = []
223
+ ary.each do |it|
224
+ h = hash[it]
225
+ unless h
226
+ next
227
+ end
228
+ item_ary = h["META"]
229
+ title = h["TITLE"]
230
+ item_hash = h["CONTENT"]
231
+ dump_ary << title
232
+
233
+ if item_ary
234
+ item_ary.each do |subit|
235
+ if item_hash[subit]
236
+ dump_ary << item_hash[subit]
237
+ end
238
+ end
239
+ end
240
+ end
241
+ dump_ary
242
+ end
243
+
244
+ def dump_sub_for_array( ary , hash )
245
+ dump_ary = []
246
+ ary.each do |it|
247
+ dump_ary += hash[it]["CONTENT"]
248
+ end
249
+ dump_ary
250
+ end
251
+
252
+ end
253
+
254
+
255
+ class StructuredTextForAccountOneLayer
256
+ attr_accessor :item_ary
257
+
258
+ def initialize
259
+ @item_ary = []
260
+ @ary = []
261
+ @hash = { }
262
+ @title = ""
263
+ end
264
+ end
265
+
266
+ class StructuredTextForAccountOneLayerDomain < StructuredTextForAccountOneLayer
267
+ def initialize
268
+ super
269
+ @line_ary = []
270
+ end
271
+
272
+ def analyze( status , line , key , value , main_line_ary , main_line_hash )
273
+ case status
274
+ when AccountLines::DOMAIN_ACCOUNT_START
275
+ key.sub!( /^==/ , "" )
276
+ @title = line["CONTENT"]
277
+ @line_ary << line["CONTENT"]
278
+ when AccountLines::DOMAIN_ACCOUNT
279
+ @line_ary << line["CONTENT"]
280
+ when AccountLines::DOMAIN_ACCOUNT_END
281
+ main_line_ary << @title
282
+ main_line_hash[@title] = { "TITLE" => @title , "CONTENT" => @line_ary }
283
+ @title = ""
284
+ @line_ary = []
285
+ else
286
+ end
287
+ end
288
+ end
289
+
290
+ class StructuredTextForAccountOneLayerHost < StructuredTextForAccountOneLayer
291
+
292
+ def initialize
293
+ super
294
+ end
295
+
296
+ def analyze( status , line , key , value , main_ary , main_hash )
297
+ case status
298
+ when AccountLines::HOST_ACCOUNT_START
299
+ @hash[key] = line["CONTENT"]
300
+ @title = line["CONTENT"]
301
+ when AccountLines::HOST_ACCOUNT
302
+ @ary << key
303
+ @hash[key] = line["CONTENT"]
304
+ when AccountLines::HOST_ACCOUNT_END
305
+ main_ary << @title
306
+ main_hash[@title] = { "TITLE" => @title , "CONTENT" => @hash , "META" => @ary }
307
+
308
+ @item_ary |= @ary
309
+ @ary = []
310
+ @hash = { }
311
+ @title = ""
312
+ else
313
+ end
314
+ end
315
+
316
+ end
317
+
318
+ class StructuredTextForSimple < StructuredText
319
+ class Item
320
+ attr_reader :name, :contents
321
+ def initialize( name )
322
+ @name = name
323
+ @contents = []
324
+ end
325
+
326
+ def add( content )
327
+ @contents << content
328
+ end
329
+ end
330
+
331
+ def initialize
332
+ @item_ary = {}
333
+ end
334
+
335
+ def get_event
336
+ value = nil
337
+ line = @line_ary.shift
338
+
339
+ if line
340
+ content = line.strip
341
+ if content.length == 0
342
+ ret = :EMPTY_LINE
343
+ value = ""
344
+ else
345
+ if content =~ /^\-(.*)/
346
+ title = $1
347
+ if title.length > 0
348
+ ret = :TITLE_LINE
349
+ value = title
350
+ else
351
+ ret = :EMPTY_TITLE_LINE
352
+ value = ""
353
+ end
354
+ else
355
+ ret = :NON_EMPTY_LINE
356
+ value = content
357
+ end
358
+ end
359
+ else
360
+ ret = :EOF
361
+ end
362
+
363
+ [ret,value]
364
+ end
365
+
366
+ def make_next_state_table
367
+ @next_state = {}
368
+ @next_state[:NONE] = {}
369
+ @next_state[:NONE][:EMPTY_LINE] = :NONE
370
+ @next_state[:NONE][:TITLE_LINE] = :ITEM
371
+ @next_state[:NONE][:EMPTY_TITLE_LINE] = :END
372
+ @next_state[:NONE][:NON_EMPTY_LINE] = :BAD
373
+ @next_state[:ITEM] = {}
374
+ @next_state[:ITEM][:EMPTY_LINE] = :ITEM
375
+ @next_state[:ITEM][:TITLE_LINE] = :ITEM
376
+ @next_state[:ITEM][:EMPTY_TITLE_LINE] = :END
377
+ @next_state[:ITEM][:NON_EMPTY_LINE] = :ITEM
378
+ @next_state[:END] = {}
379
+ @next_state[:END][:EMPTY_LINE] = :END
380
+ @next_state[:END][:TITLE_LINE] = :BAD
381
+ @next_state[:END][:EMPTY_TITLE_LINE] = :BAD
382
+ @next_state[:END][:NON_EMPTY_LINE] = :END
383
+ end
384
+
385
+ def get_next_state( state , event )
386
+ if state == :BAD
387
+ :BAD
388
+ else
389
+ @next_state[state][event]
390
+ end
391
+ end
392
+
393
+ def analyze( line_ary , subfname = nil )
394
+ @line_ary = line_ary
395
+ make_next_state_table
396
+ # :NONE
397
+ # :ITEM
398
+ # :END
399
+ # :BAD
400
+
401
+ # :EMPTY_LINE
402
+ # :TITLE_LINE
403
+ # :EMPTY_TITLE_LINE
404
+ # :NON_EMPTY_LINE
405
+ state = :NONE
406
+ event = get_event
407
+
408
+ @item_ary = []
409
+ item = nil
410
+ while state != :BAD and event[0] != :EOF
411
+ case state
412
+ when :NONE
413
+ case event[0]
414
+ when :EMPTY_LINE
415
+ #
416
+ when :TITLE_LINE
417
+ @item_ary << (item = Item.new( event[1] ))
418
+ when :EMPTY_TITLE_LINE
419
+ @item_ary << (item = Item.new( "" ))
420
+ when :NON_EMPTY_LINE
421
+ item.add( event[1] )
422
+ else
423
+ #
424
+ end
425
+ when :ITEM
426
+ case event[0]
427
+ when :EMPTY_LINE
428
+ #
429
+ when :TITLE_LINE
430
+ @item_ary << (item = Item.new( event[1] ))
431
+ when :EMPTY_TITLE_LINE
432
+ @item_ary << (item = Item.new( "" ))
433
+ when :NON_EMPTY_LINE
434
+ item.add( event[1] )
435
+ else
436
+ #
437
+ end
438
+ else
439
+ case event[0]
440
+ when :EMPTY_LINE
441
+ #
442
+ when :TITLE_LINE
443
+ #
444
+ when :EMPTY_TITLE_LINE
445
+ #
446
+ when :NON_EMPTY_LINE
447
+ item.add( event[1] )
448
+ else
449
+ #
450
+ end
451
+ end
452
+
453
+ state = get_next_state( state , event[0] )
454
+ event = get_event
455
+ end
456
+
457
+ @item_ary
458
+ end
459
+
460
+ end
461
+
462
+ end