]> git.immae.eu Git - perso/Immae/Projets/Ruby/Monitor.git/blob - ini_read.rb
Customization of window size
[perso/Immae/Projets/Ruby/Monitor.git] / ini_read.rb
1 require 'optparse'
2 require 'ostruct'
3 require "inifile"
4
5 class OptParse
6 def parse(args)
7
8 options = OpenStruct.new()
9 options.inifile = nil
10
11 opt_parser = OptionParser.new() do |opts|
12
13 opts.banner = "Usage: monitor [options]"
14 opts.separator ""
15
16 opts.on( '-f', "-f ini_file", "chose a different initialization file") do |ini|
17 options.inifile = ini
18 end
19 end
20 opt_parser.parse!(args)
21 return options
22 end
23 end
24
25
26 class Ini_read
27 attr_reader(:global,:sections)
28 # attr_reader :global
29 # attr_accessor :global
30 def initialize()
31 options = OptParse.new().parse(ARGV)
32 find_ini(options.inifile)
33 read_ini()
34 parse_config()
35 parse_global()
36 end
37
38 def find_ini(option_inifile)
39 if(not option_inifile.nil?)
40 @inifile = option_inifile
41 elsif(ENV.has_key?('MONITOR_RC'))
42 @inifile = ENV['MONITOR_RC']
43 elsif(Process.uid == 0)
44 @inifile = "/etc/monitor.rc"
45 else
46 @inifile = ENV['HOME']+"/.monitorrc"
47 end
48 end
49
50 def read_ini()
51 @config = IniFile.load(@inifile, :default => 'global')
52 if(@config.nil?)
53 puts "Initialization file not found or not readable"
54 exit
55 end
56 end
57
58 def each_section()
59 return @sections
60 end
61
62 def parse_config()
63 @global = {}
64 @sections = {}
65 @config.each_section do |section|
66 sec = @config[section]
67 if(section == "global")
68 @global = sec
69 next
70 end
71 if(not sec.has_key?('Type'))
72 puts "Section incomplete, ignored: "+ section
73 next
74 elsif(not sec.has_key?('Command'))
75 puts "Section incomplete, ignored: "+ section
76 next
77 end
78 if(sec['Type'] == "continuous")
79 if(not sec.has_key?('Buffer'))
80 sec['Buffer'] = 1000
81 end
82 end
83 if(sec['Type'] == "oneshot")
84 if(not sec.has_key?('Periodic'))
85 sec['Periodic'] = 600
86 end
87 end
88 @sections[section] = sec
89 end
90 end
91
92 def parse_global()
93 if(@global.has_key?('list_size'))
94 @global['list_size'] = @global['list_size'].to_i
95 if(@global['list_size'] == 0)
96 @global['list_size'] = 25
97 end
98 else
99 @global['list_size'] = 25
100 end
101 end
102
103 end
104