Class: Aio::Module::OutputStyle::CompareXML

Inherits:
Aio::Module::OutputStyle show all
Defined in:
lib/modules/output/style/compare_xml.rb

Overview

<CompareModule description=‘这个模块输出为XML文件,用于和基准值比较’ author=‘Elin’> <device device_name=‘COS_1’> <cpu cmd=‘display cpu-usage’> <slot_id val=‘1’> <seonds_5 val=‘9%’> <minute_1 val=‘9%’> <minutes_5 val=‘9%’> </cpu> … </device> … </CompareModule>

Instance Attribute Summary

Attributes inherited from Aio::Module::OutputStyle

#device_manager, #module_manager, #output_file, #output_info

Instance Method Summary collapse

Methods inherited from Aio::Module::OutputStyle

#author, #description, #each_devices_with_useful, #file_suffix, #license, #platform, #set_defaults, #type

Constructor Details

#initializeCompareXML

Returns a new instance of CompareXML.



23
24
25
26
27
28
29
# File 'lib/modules/output/style/compare_xml.rb', line 23

def initialize
	super({
		:author			=> "Elin",
		:description => "这个模块输出为XML文件,用于和基准值比较。",
		:file_suffix => "xml",
	})
end

Instance Method Details

#generateObject



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
# File 'lib/modules/output/style/compare_xml.rb', line 31

def generate
	file = File.new(output_file, "w+")
	doc = REXML::Document.new
	root = doc.add_element("CompareModule",{ 
													'description' => self.output_info[:description], 
													'author' => self.output_info[:author]}
												)
	# 第一层, device 节
	each_devices_with_useful do |device_name, useful|
		device_root = root.add_element('device', {'device_name' => device_name})
		useful.each_pair do |cmd_name, info|
			info.each_pair do |c, i|
				cmd_root = device_root.add_element(c.to_s, {'cmd' => cmd_name})
				if i.class == Hash
					loop_element(device_root, cmd_root, i)
				elsif i.kind_of? String
					cmd_root.add_attribute('val', i)
				else
					# 这一段留作观察,很有可能没有作用
					puts "output/style/compare_xml.rb# add_element"
					cmd_root.add_element(c.to_s, {'val' => i.to_s})
				end
			end
		end
	end
	#doc.write(STDOUT, 2); puts
	doc.write(file, 2)
end

#loop_element(parent, son, info) ⇒ Object

迭代方法



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/modules/output/style/compare_xml.rb', line 61

def loop_element(parent, son, info)
	# 如果val为Hash,则继续循环
	info.each_pair do |key, val|
		if val.class == Hash
			grandson = son.add_element(safe(key))
			loop_element(son, grandson, val) 
		end
	end

	info.each_pair do |key, val|
		next if val.class == Hash
		son.add_element(safe(key), {'val' => val.to_s})
	end
end

#safe(key) ⇒ Object

2.路由表中以数字开头的在字符串前面加上 _i



80
81
82
83
84
85
# File 'lib/modules/output/style/compare_xml.rb', line 80

def safe(key)
	key = key.to_s
	key.gsub!('/', '__') if /\//.match(key) 
	key.insert(0, "_i") if /^\d+/.match(key)
	key
end