-
Notifications
You must be signed in to change notification settings - Fork 1
/
setting.rb
150 lines (127 loc) · 3.74 KB
/
setting.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class Setting < Struct.new(:api_key, :api_secret, :token_key, :token_secret ,
:server,
:size,
:model,
:time,
:exposure_time,
:white_balance,
:f_number,
:ISO,
:GPS,
:focal_length)
VERSION = 2.0
OLD_KEYS = %W(APIKEY APISECRET TOKENKEY TOKENSECRET)
attr_reader :filename
def initialize(autoload = true)
@filename = ENV["SETTING_FILE"] || "setting.db"
return unless autoload
wizard unless File.exists? @filename
self.read
wizard unless self.check?
end
# Modify setting in an intaractive way.
def wizard
puts "Please entry below informations,"
print "API key [#{self.api_key}]: "
value = gets.chomp
self.api_key = value unless value.empty?
print "API sercet key [#{self.api_secret}]: "
value = gets.chomp
self.api_secret = value unless value.empty?
print "Token key [#{self.token_key}]: "
value = gets.chomp
self.token_key = value unless value.empty?
print "Token secret key [#{self.token_secret}]: "
value = gets.chomp
self.token_secret = value unless value.empty?
print "Want to open the server?(y/n)[#{self.server}]: "
value = gets.chomp
self.server = tr(value)
print "Want to show picture's size?(y/n)[#{self.size}]: "
value = gets.chomp
self.size = tr(value)
print "Want to show picture's model?(y/n)[#{self.model}]: "
value = gets.chomp
self.model = tr(value)
print "Want to show picture's time and date?(y/n)[#{self.time}]: "
value = gets.chomp
self.time = tr(value)
print "Want to show picture's exposure time?(y/n)[#{self.exposure_time}]: "
value = gets.chomp
self.exposure_time = tr(value)
print "Want to show picture's white balance?(y/n)[#{self.white_balance}]: "
value = gets.chomp
self.white_balance = tr(value)
print "Want to show picture's f number?(y/n)[#{self.f_number}]: "
value = gets.chomp
self.f_number = tr(value)
print "Want to show picture's focal length?(y/n)[#{self.focal_length}]: "
value = gets.chomp
self.focal_length = tr(value)
print "Want to show picture's ISO?(y/n)[#{self.ISO}]: "
value = gets.chomp
self.ISO = tr(value)
print "Want to show picture's GPS data?(y/n)[#{self.GPS}]: "
value = gets.chomp
self.GPS = tr(value)
self.write!
puts self.to_s
end
REGEXP_SETTING_PAIR = /^(?<key>\w+)\s*=\s*(?<bracket>'|")(?<value>[[:word:]]*)\k<bracket>$/
def read
puts "Read setting..."
IO.readlines(@filename).each do |line|
REGEXP_SETTING_PAIR.match line.chomp do |md|
key = md[:key].to_sym
value = md[:value]
self[key] = value if self.members.include? key
puts "WARNING! Found old option: #{md[:key]}. You may run setting_upgrade.rb before starting ke__ri robot." if OLD_KEYS.include? md[:key]
end
end
puts "done"
end
def write!
File.open @filename, "w" do |io|
io.puts %(# Notice! Do NOT add option which is not effective. They will be cleared with next wizard.)
io.puts %(# Generated by v#{VERSION}.)
self.members.each do |key|
io.puts %(#{key} = "#{self[key]}")
end
end
end
def check?
self.members.each do |key|
next unless self[key].nil? or self[key].empty?
puts %(Missing necessary key: #{key})
return false
end
true
end
def set(key,value)
begin
self[key] = value
log %(#{Time.now.to_s} [EVENT] The setting #{key} has change value = #{self[key]})
rescue
log %(#{Time.now.to_s} [ERROR] Set the setting value has error: #{$!.to_s})
end
end
def list
self.members.each do |key|
puts %(#{key} = "#{self[key]}")
end
end
def to_s
result = "{\n"
self.members.each do |key|
result << %(\t#{key} => #{self[key]}\n)
end
result << "}"
end
def tr(t)
if t =~ /[nN][oO]?/
return false
else
return true
end
end
end