Class: Aio::Module::InputStyle::CompareXML
- Inherits:
-
Aio::Module::InputStyle
- Object
- Aio::Module::InputStyle
- Aio::Module::InputStyle::CompareXML
- Includes:
- Aio::Module, Ui::Verbose, REXML
- Defined in:
- lib/modules/input/style/compare_xml.rb
Instance Attribute Summary
Attributes inherited from Aio::Module::InputStyle
#ext_info, #input_file, #input_info
Instance Method Summary collapse
-
#convert(e) ⇒ Object
转换恢复会出错字符串.
-
#initialize ⇒ CompareXML
constructor
A new instance of CompareXML.
- #parse ⇒ Object
- #to_h(xml, hash) ⇒ Object
Methods included from Ui::Verbose
#clear_line, #print_error, #print_good, #progress_bar
Methods inherited from Aio::Module::InputStyle
#author, #check_file, #description, #file_suffix, #file_suffix?, #license, #pass_file, #pass_file?, #platform, #set_defaults, #type
Constructor Details
#initialize ⇒ CompareXML
Returns a new instance of CompareXML.
11 12 13 14 15 16 17 18 19 |
# File 'lib/modules/input/style/compare_xml.rb', line 11 def initialize super({ :author => "Elin", :description => "这个模块用于分析由output/out_compare_xml模块生成的xml文件", :platform => "all", :file_suffix => Regexp.new('\.xml$'), :pass_file => [], }) end |
Instance Method Details
#convert(e) ⇒ Object
转换恢复会出错字符串
83 84 85 86 87 88 |
# File 'lib/modules/input/style/compare_xml.rb', line 83 def convert(e) #/__/.match(e) ? e.gsub!('__', '/').to_sym : e.to_sym e.gsub!('__', '/') if /__/.match(e) e.gsub!(/^_i/, '') if /^_i/.match(e) e.to_sym end |
#parse ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/modules/input/style/compare_xml.rb', line 21 def parse # dir 为 Pathname 类 print_good "正在打开文件 #{self.input_file}" file = File.open(self.input_file) print_good "打开文件完成" print_good "正在载入到XML模块..." atime = Time.now doc = Document.new(file) root = doc.root print_good "载入完成。载入用时 #{format("%.2fs", Time.now.to_f - atime.to_f)}" device_hash = {} index = 0 total = root.elements.size print_good "开始比对分析..." # 按每台设备解析XML root.elements.each do |device| index += 1 info = {} device_name = device.attributes['device_name'] device_hash[device_name] = info # 按照每个cmd解析XML device.elements.each do |cmd| i = {} ch = cmd.name cmd_name = cmd.attributes['cmd'] # 当同一个cmd有两个symbol时 if info[cmd_name].nil? info[cmd_name] = {ch.to_sym => i} else info[cmd_name][ch.to_sym] = i end # 进度条 (total, index, device_name) # 还有一种情况是当<attr cmd='show' val='var'/> # 此时就可以直接将val填入 attr_val = cmd.attributes['val'] attr_val ? info[cmd_name][ch.to_sym] = attr_val : to_h(cmd, i) end end clear_line return device_hash end |
#to_h(xml, hash) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/modules/input/style/compare_xml.rb', line 70 def to_h(xml, hash) xml.elements.each do |e| if e.has_elements? son = {} hash[convert(e.name)] = son to_h(e, son) else hash[convert(e.name)] = e.attributes['val'] end end end |