ykutils 0.1.0 → 0.1.1

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.
@@ -1,33 +1,32 @@
1
- require 'ykutils/debugutils'
2
- require 'ykutils/stext'
3
- require 'ykutils/xlines'
4
-
5
- module Ykutils
6
- class StructuredTextForX < StructuredText
7
- include DebugUtils
8
-
9
- def initialize( debug = false )
10
- super()
11
-
12
- debug_utils_init
13
- set_debug( debug )
14
- ends
15
-
16
- def analyze( line_ary , fname = nil )
17
- lines = XLines.new( line_ary )
18
- # lines.output_f( fname )
19
-
20
- analyze_sub( lines )
21
- end
22
-
23
- def analyze_sub( lines )
24
- puts_current_method
25
-
26
- while line = lines.get_line
27
- # p line
28
- end
29
- end
30
- end
31
- end
32
- end
33
-
1
+ require "ykutils/debugutils"
2
+ require "ykutils/stext"
3
+ require "ykutils/xlines"
4
+
5
+ module Ykutils
6
+ class StructuredTextForX < StructuredText
7
+ include DebugUtils
8
+
9
+ def initialize(debug = false)
10
+ super()
11
+
12
+ debug_utils_init
13
+ set_debug(debug)
14
+ ends
15
+
16
+ def analyze(line_ary, _fname = nil)
17
+ lines = XLines.new(line_ary)
18
+ # lines.output_f( fname )
19
+
20
+ analyze_sub(lines)
21
+ end
22
+
23
+ def analyze_sub(lines)
24
+ puts_current_method
25
+
26
+ while line = lines.get_line
27
+ # p line
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,125 +1,111 @@
1
- module Ykutils
2
- module StringUtils
3
-
4
- def nil_to_nullstring( str )
5
- str ? str : ""
6
- end
7
-
8
- def get_basename_ext( it )
9
- bn0 = it.basename.to_s
10
- if RUBY_VERSION > "1.9"
11
- # bn0.encode!( "internal" )
12
- bn0.encode! "UTF-8"
13
-
14
- end
15
- list = bn0.split(".")
16
- case list.size
17
- when 0
18
- bname = ""
19
- ext = ""
20
- when 1
21
- bname = list[0]
22
- ext = ""
23
- when 2
24
- bname = list[0]
25
- ext = list[1]
26
- else
27
- bname_size = list.size - 1
28
- bname = list[0,bname_size].join('.')
29
- ext = list[-1]
30
- end
31
- [bname , ext]
32
- end
33
-
34
-
35
- def normal_string?( str )
36
- ret = false
37
- if str
38
- s = str.strip
39
- if s != ""
40
- if s =~ /^[0-9A-Za-z_:\.\/\-]+$/
41
- ret = true
42
- end
43
- end
44
- end
45
- ret
46
- end
47
-
48
- def get_normalized_string( str )
49
- ret = ""
50
- if str
51
- ret = str.strip
52
- end
53
- ret
54
- end
55
-
56
- def make_content_from_template( template_ary , tag_value_hash , separator )
57
- content_ary = []
58
-
59
- reg_hash = { }
60
- tag_value_hash.each do |k,v|
61
- reg_hash[k] = Regexp.new( separator + k + separator )
62
- end
63
-
64
- template_ary.each do |line|
65
- reg_hash.each do |k,v|
66
- line = line.sub( v , tag_value_hash[k] )
67
- end
68
- content_ary << line
69
- end
70
- content_ary
71
- end
72
-
73
- def indent(n)
74
- n = 0 unless n
75
- @indent_hash ||= { }
76
- @indent_hash[n] ||= " " * n
77
- @indent_hash[n]
78
- end
79
-
80
- def print_hier( data , level )
81
- case data.class.to_s
82
- when "Hash"
83
- data.each do |k,v|
84
- # puts "#{indent(level)}#{k}|#{v.class} #{if v.class.to_s == 'String' then v.size else '' end }"
85
- # puts "#{indent(level)}#{k}|#{v.class}"
86
- print_hier( v , level + 1 )
87
- end
88
- when "Array"
89
- data.each do |v|
90
- print_hier( v , level + 1 )
91
- end
92
- else
93
- # puts "#{indent(level)}|#{data.class}"
94
- end
95
- end
96
-
97
- def print_hier_2( data , level )
98
- case data.class.to_s
99
- when "Hash"
100
- data.each do |k,v|
101
- # puts "#{indent(level)}#{k}|#{v.class} #{if v.class.to_s == 'String' then v.size else '' end }"
102
- str = "#{indent(level)}#{k}|#{v.class}"
103
- if v.class == Array
104
- num = v.size
105
- str += ("|" + num.to_s )
106
- if num > 1
107
- str += "###################"
108
- end
109
- end
110
- # puts str
111
- if k != "group"
112
- print_hier_2( v , level + 1 )
113
- end
114
- end
115
- when "Array"
116
- data.each do |v|
117
- print_hier_2( v , level + 1 )
118
- end
119
- else
120
- # puts "#{indent(level)}|#{data.class}"
121
- end
122
- end
123
-
124
- end
125
- end
1
+ module Ykutils
2
+ module StringUtils
3
+ def nil_to_nullstring(str)
4
+ str || ""
5
+ end
6
+
7
+ def get_basename_ext(it)
8
+ bn0 = it.basename.to_s
9
+ if RUBY_VERSION > "1.9"
10
+ # bn0.encode!( "internal" )
11
+ bn0.encode! "UTF-8"
12
+ end
13
+ list = bn0.split(".")
14
+ case list.size
15
+ when 0
16
+ bname = ""
17
+ ext = ""
18
+ when 1
19
+ bname = list[0]
20
+ ext = ""
21
+ when 2
22
+ bname = list[0]
23
+ ext = list[1]
24
+ else
25
+ bname_size = list.size - 1
26
+ bname = list[0, bname_size].join(".")
27
+ ext = list[-1]
28
+ end
29
+ [bname, ext]
30
+ end
31
+
32
+ def normal_string?(str)
33
+ ret = false
34
+ if str
35
+ s = str.strip
36
+ ret = true if s != "" && (s =~ %r{^[0-9A-Za-z_:./\-]+$})
37
+ end
38
+ ret
39
+ end
40
+
41
+ def get_normalized_string(str)
42
+ ret = ""
43
+ ret = str.strip if str
44
+ ret
45
+ end
46
+
47
+ def make_content_from_template(template_ary, tag_value_hash, separator)
48
+ content_ary = []
49
+
50
+ reg_hash = {}
51
+ tag_value_hash.each do |k, _v|
52
+ reg_hash[k] = Regexp.new(separator + k + separator)
53
+ end
54
+
55
+ template_ary.each do |line|
56
+ reg_hash.each do |k, v|
57
+ line = line.sub(v, tag_value_hash[k])
58
+ end
59
+ content_ary << line
60
+ end
61
+ content_ary
62
+ end
63
+
64
+ def indent(n)
65
+ n ||= 0
66
+ @indent_hash ||= {}
67
+ @indent_hash[n] ||= " " * n
68
+ @indent_hash[n]
69
+ end
70
+
71
+ def print_hier(data, level)
72
+ case data.class.to_s
73
+ when "Hash"
74
+ data.each do |_k, v|
75
+ # puts "#{indent(level)}#{k}|#{v.class} #{if v.class.to_s == 'String' then v.size else '' end }"
76
+ # puts "#{indent(level)}#{k}|#{v.class}"
77
+ print_hier(v, level + 1)
78
+ end
79
+ when "Array"
80
+ data.each do |v|
81
+ print_hier(v, level + 1)
82
+ end
83
+ else
84
+ # puts "#{indent(level)}|#{data.class}"
85
+ end
86
+ end
87
+
88
+ def print_hier_2(data, level)
89
+ case data.class.to_s
90
+ when "Hash"
91
+ data.each do |k, v|
92
+ # puts "#{indent(level)}#{k}|#{v.class} #{if v.class.to_s == 'String' then v.size else '' end }"
93
+ str = "#{indent(level)}#{k}|#{v.class}"
94
+ if v.instance_of?(Array)
95
+ num = v.size
96
+ str += ("|" + num.to_s)
97
+ str += "###################" if num > 1
98
+ end
99
+ # puts str
100
+ print_hier_2(v, level + 1) if k != "group"
101
+ end
102
+ when "Array"
103
+ data.each do |v|
104
+ print_hier_2(v, level + 1)
105
+ end
106
+ else
107
+ # puts "#{indent(level)}|#{data.class}"
108
+ end
109
+ end
110
+ end
111
+ end
@@ -1,36 +1,33 @@
1
- require 'tsort'
2
-
3
- module Ykytils
4
- class TreeManager
5
- include TSort
6
-
7
- def initialize
8
- @table = { }
9
- end
10
-
11
- def add( parent_name , name )
12
- if parent_name
13
- parent = @table[parent_name]
14
- if parent
15
- @table[parent_name] << name
16
- else
17
- @table[parent_name] = [name]
18
- end
19
- else
20
- @table[name] = []
21
- end
22
- end
23
-
24
- def tsort_each_child(node, &block)
25
- ary = @table[node]
26
- if ary
27
- ary.each(&block)
28
- end
29
- end
30
-
31
- def tsort_each_node( &block)
32
- @table.keys.each(&block)
33
- end
34
- end
35
- end
36
-
1
+ require "tsort"
2
+
3
+ module Ykytils
4
+ class TreeManager
5
+ include TSort
6
+
7
+ def initialize
8
+ @table = {}
9
+ end
10
+
11
+ def add(parent_name, name)
12
+ if parent_name
13
+ parent = @table[parent_name]
14
+ if parent
15
+ @table[parent_name] << name
16
+ else
17
+ @table[parent_name] = [name]
18
+ end
19
+ else
20
+ @table[name] = []
21
+ end
22
+ end
23
+
24
+ def tsort_each_child(node, &block)
25
+ ary = @table[node]
26
+ ary.each(&block) if ary
27
+ end
28
+
29
+ def tsort_each_node(&block)
30
+ @table.keys.each(&block)
31
+ end
32
+ end
33
+ end
@@ -1,26 +1,21 @@
1
- require 'ykutils/treemanager'
2
-
3
- module Ykutils
4
- class TreeManager
5
- def addTag( node_name , tag_name )
6
- unless @tag
7
- @tag = { }
8
- end
9
-
10
- unless @tag[node_name]
11
- @tag[node_name] = [tag_name]
12
- else
13
- @tag[node_name] << tag_name
14
- end
15
-
16
- end
17
-
18
- def getTag( node_name)
19
- ret = nil
20
- if @tag
21
- ret = @tag[node_name]
22
- end
23
- ret
24
- end
25
- end
26
- end
1
+ require "ykutils/treemanager"
2
+
3
+ module Ykutils
4
+ class TreeManager
5
+ def addTag(node_name, tag_name)
6
+ @tag ||= {}
7
+
8
+ if @tag[node_name]
9
+ @tag[node_name] << tag_name
10
+ else
11
+ @tag[node_name] = [tag_name]
12
+ end
13
+ end
14
+
15
+ def getTag(node_name)
16
+ ret = nil
17
+ ret = @tag[node_name] if @tag
18
+ ret
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Ykutils
2
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
3
5
  end
@@ -1,9 +1,8 @@
1
- require 'ykutils/debugutils'
2
- require 'ykutils/lines'
3
-
4
- module Ykutils
5
- class XLines < Lines
6
- include DebugUtils
7
-
8
- end
9
- end
1
+ require "ykutils/debugutils"
2
+ require "ykutils/lines"
3
+
4
+ module Ykutils
5
+ class XLines < Lines
6
+ include DebugUtils
7
+ end
8
+ end
@@ -0,0 +1,227 @@
1
+ require "ykutils/specfileop"
2
+ require "ykutils/stext"
3
+ require "ykutils/datastructop"
4
+
5
+ module Ykutils
6
+ class YamlOp
7
+ attr_accessor :valid, :yaml, :ofname
8
+
9
+ include SpecFileOp
10
+ include DataStructOp
11
+
12
+ TEST_ID_NO_VALUE = 0
13
+ TEST_ID_LISTUP_ALL = 1
14
+
15
+ def initialize(opts = {}, _argv = [], _debug = false)
16
+ @fname = nil
17
+ @check_flag = opts["check"]
18
+ @ifname = opts["input_file"]
19
+ @ofname = opts["output_file"]
20
+ @reform_flag = opts["reform"]
21
+ @test_id = opts["test_id"]
22
+ @valid = true
23
+ @ptext = []
24
+ end
25
+
26
+ def valid?
27
+ @valid
28
+ end
29
+
30
+ def exec
31
+ if @reform_flag
32
+ reform
33
+ elsif @test_id > 0
34
+ case @test_id
35
+ when TEST_ID_LISTUP_ALL
36
+ test_listup
37
+ end
38
+ end
39
+
40
+ true
41
+ end
42
+
43
+ def test_listup
44
+ import(@ifname)
45
+ puts "###listup_all"
46
+ ary = listup_all
47
+ ary.each do |it|
48
+ puts(it)
49
+ end
50
+
51
+ puts "###listup_host"
52
+ ary = listup_host
53
+ ary.each do |it|
54
+ puts(it)
55
+ end
56
+
57
+ puts "###listup_domain"
58
+ ary = listup_domain
59
+ ary.each do |it|
60
+ puts(it)
61
+ end
62
+ end
63
+
64
+ def load_yaml(fname)
65
+ @yaml = load_yaml_file(fname)
66
+ end
67
+
68
+ def load_yaml_ifname
69
+ @yaml = load_yaml_file(@ifname)
70
+ end
71
+
72
+ def load(fname)
73
+ ret = false
74
+ if File.file?(fname)
75
+ @fname = fname
76
+ @pstext = StructuredTextForAccount.new
77
+ # @pstext.load_analyze( fname , "C-#{fname}-#{Time.now.to_i}.txt" )
78
+ @pstext.load_analyze(fname)
79
+ @yaml = load_yaml_file(fname)
80
+ ret = true
81
+ end
82
+
83
+ ret
84
+ end
85
+
86
+ def yaml2stext(yaml = @yaml)
87
+ # begin
88
+ yaml_str = YAML.dump(yaml)
89
+ # puts yaml_str
90
+ # rescue => ex
91
+ # pp ex
92
+ # pp caller(0)
93
+ # exit 1000
94
+ # end
95
+ yaml_ary = yaml_str.split("\n")
96
+
97
+ @yamlstext = StructuredTextForAccount.new
98
+ @yamlstext.analyze(yaml_ary)
99
+ end
100
+
101
+ def sort
102
+ @yamlstext.sort_by(@pstext)
103
+ end
104
+
105
+ def import(fname)
106
+ ret = false
107
+ if File.file?(fname) and File.readable?(fname)
108
+ @ptext = load_plain_text_file(fname).collect { |it| it.chomp }
109
+ ret = false
110
+ end
111
+ ret
112
+ end
113
+
114
+ def importex(fname)
115
+ ret = import(fname)
116
+ if ret
117
+ @ptext_hash = {}
118
+ i = 0
119
+ @ptext_hash = make_hash(@ptext, 0) do |l, i|
120
+ if l !~ /^\s/ and l !~ /^==/
121
+ key, vale = l.split(":")
122
+ [key, { "CONTENT" => l, "INDEX" => i }]
123
+ end
124
+ end
125
+ ret = false
126
+ end
127
+ ret
128
+ end
129
+
130
+ def listup_all
131
+ @ptext.select { |l| l !~ /^\s/ and l !~ /^#/ }
132
+ end
133
+
134
+ def listup_host
135
+ @ptext.select { |l| l !~ /^\s/ and l !~ /^=/ and l !~ /^#/ }
136
+ end
137
+
138
+ def listup_domain
139
+ @ptext.select { |l| l =~ /^==/ }
140
+ end
141
+
142
+ def output_yaml(obj, fname)
143
+ File.open(fname, "w") do |file|
144
+ YAML.dump(obj, file)
145
+ end
146
+ rescue StandardError => e
147
+ pp e
148
+ pp e.backtrace
149
+ @valid = false
150
+ end
151
+
152
+ def update_yaml(yaml)
153
+ buf = File.read(@ifname)
154
+ File.write(@ofname, buf)
155
+ File.open(@ifname, "w") do |file|
156
+ YAML.dump(yaml, file)
157
+ end
158
+ rescue StandardError => e
159
+ pp e
160
+ pp e.backtrace
161
+ @valid = false
162
+ end
163
+
164
+ def dump_to_file(fname)
165
+ File.open(fname, "w") do |file|
166
+ @yamlstext.dump(file)
167
+ end
168
+ rescue StandardError => e
169
+ pp e
170
+ pp e.backtrace
171
+ @valid = false
172
+ end
173
+
174
+ def dump_ptext_to_file(fname)
175
+ file = File.open(fname, "w") do |file|
176
+ @ptext.each do |l|
177
+ file.write(l + "\n")
178
+ end
179
+ end
180
+ rescue StandardError => e
181
+ pp e
182
+ pp e.backtrace
183
+ @valid = false
184
+ end
185
+
186
+ def exchange_ptext(key, value)
187
+ @ptext[@ptext_hash[key]["INDEX"]] = value
188
+ @ptext_hash[key]["CONTENT"] = value
189
+ end
190
+
191
+ def reform
192
+ ret = importex(@ifname)
193
+ if ret
194
+ ary = listup_host.collect do |it|
195
+ key, value = it.split(":")
196
+ key
197
+ end
198
+ str = ary.join(" , ")
199
+ new_host_list = "=-HOST_LIST: [#{str}]"
200
+ ary2 = listup_domain.collect do |it|
201
+ key, value = it.split(":")
202
+ key
203
+ end
204
+ str2 = ary2.join(" , ")
205
+ new_domain_list = "=-DOMAIN_LIST: [#{str2}]"
206
+ pp "==new_host_list"
207
+ pp new_host_list
208
+ exchange_ptext("=-HOST_LIST", new_host_list)
209
+ exchange_ptext("=-DOMAIN_LIST", new_domain_list)
210
+ dump_ptext_to_file(@ofname)
211
+ end
212
+ end
213
+
214
+ def extract_value(ary)
215
+ ary.collect do |x|
216
+ x2 = x
217
+ x.scan(/{([^}]+)}/).each do |y|
218
+ y.each do |z|
219
+ re = Regexp.new(%({#{z}}))
220
+ x2 = x2.gsub(re, @yaml[z])
221
+ end
222
+ end
223
+ x2
224
+ end
225
+ end
226
+ end
227
+ end