Class: Ykutils::YamlOp

Inherits:
Object
  • Object
show all
Includes:
DataStructOp, SpecFileOp
Defined in:
lib/ykutils/yamlop.rb

Direct Known Subclasses

YamlXOp

Constant Summary collapse

TEST_ID_NO_VALUE =
0
TEST_ID_LISTUP_ALL =
1

Constants included from NKFUTIL

NKFUTIL::CODE_TO_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DataStructOp

#exchange, #make_array, #make_hash, #select_array

Methods included from SpecFileOp

#check_data_complement, #check_data_complement_print_message, #dump_yaml_fileobj, #expand_data, #load_csv_file, #load_csv_file_ex, #load_plain_text_file, #load_yaml_file, #make_data_complement, #open_for_write, #parse_yaml_file, #save_yaml_file

Methods included from NKFUTIL

assoc_equal, auto_config_from_inner, auto_config_to_inner, config, convert, get, guess_encoding, #nkf_utf8_file, #puts_sj, #puts_u, set

Methods included from DebugUtils

#clear_d, #d, #d_caller, #d_exit, #d_p, #d_pp, #d_puts, #d_puts_no_empty, #debug_utils_init, #error_exit, #puts_current_method, #puts_d, #puts_no_empty, #w1_puts, #w2_puts

Constructor Details

#initialize(opts = {}, _argv = [], _debug: false) ⇒ YamlOp

Returns a new instance of YamlOp.



15
16
17
18
19
20
21
22
23
24
# File 'lib/ykutils/yamlop.rb', line 15

def initialize(opts = {}, _argv = [], _debug: false)
  @fname = nil
  @check_flag = opts["check"]
  @ifname = opts["input_file"]
  @ofname = opts["output_file"]
  @reform_flag = opts["reform"]
  @test_id = opts["test_id"]
  @valid = true
  @ptext = []
end

Instance Attribute Details

#ofnameObject

Returns the value of attribute ofname.



7
8
9
# File 'lib/ykutils/yamlop.rb', line 7

def ofname
  @ofname
end

#validObject

Returns the value of attribute valid.



7
8
9
# File 'lib/ykutils/yamlop.rb', line 7

def valid
  @valid
end

#yamlObject

Returns the value of attribute yaml.



7
8
9
# File 'lib/ykutils/yamlop.rb', line 7

def yaml
  @yaml
end

Instance Method Details

#dump_ptext_to_file(fname) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ykutils/yamlop.rb', line 173

def dump_ptext_to_file(fname)
  File.open(fname, "w") do |file|
    @ptext.each do |l|
      file.write("#{l}\n")
    end
  end
rescue YkutilsdError => e
  pp e
  pp e.backtrace
  @valid = false
end

#dump_to_file(fname) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/ykutils/yamlop.rb', line 163

def dump_to_file(fname)
  File.open(fname, "w") do |file|
    @yamlstext.dump(file)
  end
rescue YkutilsError => e
  pp e
  pp e.backtrace
  @valid = false
end

#exchange_ptext(key, value) ⇒ Object



185
186
187
188
# File 'lib/ykutils/yamlop.rb', line 185

def exchange_ptext(key, value)
  @ptext[@ptext_hash[key]["INDEX"]] = value
  @ptext_hash[key]["CONTENT"] = value
end

#execObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ykutils/yamlop.rb', line 30

def exec
  if @reform_flag
    reform
  elsif @test_id.positive?
    case @test_id
    when TEST_ID_LISTUP_ALL
      test_listup
    end
  end

  true
end

#extract_value(ary) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/ykutils/yamlop.rb', line 213

def extract_value(ary)
  ary.collect do |x|
    x2 = x
    x.scan(/{([^}]+)}/).each do |y|
      y.each do |z|
        re = Regexp.new(%({#{z}}))
        x2 = x2.gsub(re, @yaml[z])
      end
    end
    x2
  end
end

#import(fname) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/ykutils/yamlop.rb', line 105

def import(fname)
  ret = false
  if File.file?(fname) && File.readable?(fname)
    @ptext = load_plain_text_file(fname).collect(&:chomp)
    ret = false
  end
  ret
end

#importex(fname) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ykutils/yamlop.rb', line 114

def importex(fname)
  ret = import(fname)
  if ret
    @ptext_hash = {}
    @ptext_hash = make_hash(@ptext, 0) do |l, i|
      if l !~ (/^\s/) && l !~ (/^==/)
        key, = l.split(":")
        [key, { "CONTENT" => l, "INDEX" => i }]
      end
    end
    ret = false
  end
  ret
end

#listup_allObject



129
130
131
# File 'lib/ykutils/yamlop.rb', line 129

def listup_all
  @ptext.select { |l| l !~ /^\s/ and l !~ /^#/ }
end

#listup_domainObject



137
138
139
# File 'lib/ykutils/yamlop.rb', line 137

def listup_domain
  @ptext.grep(/^==/)
end

#listup_hostObject



133
134
135
# File 'lib/ykutils/yamlop.rb', line 133

def listup_host
  @ptext.select { |l| l !~ /^\s/ and l !~ /^=/ and l !~ /^#/ }
end

#load(fname) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ykutils/yamlop.rb', line 72

def load(fname)
  ret = false
  if File.file?(fname)
    @fname = fname
    @pstext = StructuredTextForAccount.new
    #    @pstext.load_analyze( fname , "C-#{fname}-#{Time.now.to_i}.txt" )
    @pstext.load_analyze(fname)
    @yaml = load_yaml_file(fname)
    ret = true
  end

  ret
end

#load_yaml(fname) ⇒ Object



64
65
66
# File 'lib/ykutils/yamlop.rb', line 64

def load_yaml(fname)
  @yaml = load_yaml_file(fname)
end

#load_yaml_ifnameObject



68
69
70
# File 'lib/ykutils/yamlop.rb', line 68

def load_yaml_ifname
  @yaml = load_yaml_file(@ifname)
end

#output_yaml(obj, fname) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/ykutils/yamlop.rb', line 141

def output_yaml(obj, fname)
  File.open(fname, "w") do |file|
    YAML.dump(obj, file)
  end
rescue YkutilsError => e
  pp e
  pp e.backtrace
  @valid = false
end

#reformObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/ykutils/yamlop.rb', line 190

def reform
  ret = importex(@ifname)
  return unless ret

  ary = listup_host.collect do |it|
    key, = it.split(":")
    key
  end
  str = ary.join(" , ")
  new_host_list = "=-HOST_LIST: [#{str}]"
  ary2 = listup_domain.collect do |it|
    key, = it.split(":")
    key
  end
  str2 = ary2.join(" , ")
  new_domain_list = "=-DOMAIN_LIST: [#{str2}]"
  pp "==new_host_list"
  pp new_host_list
  exchange_ptext("=-HOST_LIST", new_host_list)
  exchange_ptext("=-DOMAIN_LIST", new_domain_list)
  dump_ptext_to_file(@ofname)
end

#sortObject



101
102
103
# File 'lib/ykutils/yamlop.rb', line 101

def sort
  @yamlstext.sort_by(@pstext)
end

#test_listupObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ykutils/yamlop.rb', line 43

def test_listup
  import(@ifname)
  puts "###listup_all"
  ary = listup_all
  ary.each do |it|
    puts(it)
  end

  puts "###listup_host"
  ary = listup_host
  ary.each do |it|
    puts(it)
  end

  puts "###listup_domain"
  ary = listup_domain
  ary.each do |it|
    puts(it)
  end
end

#update_yaml(yaml) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ykutils/yamlop.rb', line 151

def update_yaml(yaml)
  buf = File.read(@ifname)
  File.write(@ofname, buf)
  File.open(@ifname, "w") do |file|
    YAML.dump(yaml, file)
  end
rescue StandardError => e
  pp e
  pp e.backtrace
  @valid = false
end

#valid?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ykutils/yamlop.rb', line 26

def valid?
  @valid
end

#yaml2stext(yaml = @yaml) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ykutils/yamlop.rb', line 86

def yaml2stext(yaml = @yaml)
  #    begin
  yaml_str = YAML.dump(yaml)
  #    puts yaml_str
  #     rescue => ex
  #       pp ex
  #       pp caller(0)
  #       exit 1000
  #     end
  yaml_ary = yaml_str.split("\n")

  @yamlstext = StructuredTextForAccount.new
  @yamlstext.analyze(yaml_ary)
end