-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathS10config.lua
104 lines (97 loc) · 2.32 KB
/
S10config.lua
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
config={}
function config.parse(filename,cb,param)
local f=file.open(filename)
if (f == nil) then
print("Failed to open",filename)
return false
end
while(true) do
line=f:readline()
if (line == nil) then
break
end
line=line:sub(1,-2)
local data={line=line,param=param}
if (not line:match('^%s*-')) then
k,v,c=line:match('^%s*([^ ]+)%s*=%s*([^ ]+)%s*(--.*)')
if (k ~= nil and v~= nil) then
data.key=k
data.rawvalue=v
data.comment=c
c=c:sub(3):match("^%s*(.-)%s*$")
if ((v:sub(1,1) == "'" or v:sub(1,1) == '"') and v:sub(-1,-1) == v:sub(1,1)) then
v=v:sub(2,-2)
if (c == '') then
c='string'
end
end
data.type=c
data.value=v
end
end
if (cb(data) == false) then
print("cb failed")
return false
end
if (tmr.wdclr) then
tmr.wdclr()
end
end
f:close()
print("Success")
return true
end
function config.update_cp(from,tmp,to,changes)
local f=file.open(tmp,'w')
if (f == nil) then
print("failed to open",tmp)
return false
end
local ret=config.parse(from,function(data)
local s=data.param
if (data.key) then
if (s[data.key]) then
data.value=s[data.key]
end
data.rawvalue=(data.type == 'string' or data.type == 'password') and '"'..data.value..'"' or data.value
data.line=data.key..'='..tostring(data.rawvalue)..(data.comment ~= '' and ' '..data.comment or '')
end
return f:write(data.line .. '\n')
end,changes)
f:close()
if (ret) then
file.remove(to)
ret=file.rename(tmp,to)
end
return(ret)
end
function config.move_to_old()
file.remove('config_old.lua')
return file.rename('config.lua','config_old.lua')
end
function config.update_template(template,changes)
local ret=config.move_to_old()
if (ret) then
ret=config.update_cp(template,'config.tmp','config.lua',changes)
end
return ret
end
function config.update(changes)
return config.update_template('config_old.lua',changes)
end
function config.save()
return config.update_template('config_default.lua',_G)
end
dofile "board.lua"
dofile "calibration.lua"
if (not file.exists('config.lua')) then
print("Initializing config.lua from config_default.lua")
config.update_cp('config_default.lua','config.tmp','config.lua',{})
end
dofile "config.lua"
if (enable_osprint ~= nil) then
node.osprint(enable_osprint)
end
if (timezone ~= nil) then
time.settimezone(timezone)
end