59a9a2c27f97d7fb8cdbeca667b65cc97cdeb99f
[project/luci.git] / libs / uci / luasrc / model / uci.lua
1 --[[
2 LuCI - UCI mpdel
3
4 Description:
5 Generalized UCI model
6
7 FileId:
8 $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at 
16
17         http://www.apache.org/licenses/LICENSE-2.0 
18
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24
25 ]]--
26 local uci  = require("uci")
27 local util = require("luci.util")
28 local setmetatable = setmetatable
29 local rawget = rawget
30 local rawset = rawset
31 local error = error
32 local tostring = tostring
33
34 module("luci.model.uci", function(m) setmetatable(m, {__index = uci}) end)
35
36 local configs_mt = {}
37 local sections_mt = {}
38 local options_mt = {}
39
40 savedir_default = "/tmp/.uci"
41 confdir_default = "/etc/config"
42
43 savedir_state = "/var/state"
44
45 config = {}
46 setmetatable(config, configs_mt)
47
48 -- Level 1 (configs)
49 function configs_mt.__index(self, key)
50         local node = rawget(self, key)
51         if not node then
52                 if not uci.load(key) then
53                         return nil
54                 end
55                 node = {}
56                 node[".name"] = key
57                 setmetatable(node, sections_mt)
58                 rawset(self, key, node)
59         end
60         return node
61 end
62 function configs_mt.__newindex()
63         error("invalid operation")
64 end
65
66
67 -- Level 2 (sections)
68 function sections_mt.__index(self, key)
69         local node = rawget(self, key)
70         if not node then
71                 node = {}
72                 node[".conf"] = self[".name"]
73                 node[".name"] = key
74                 node[".type"] = uci.get(self[".name"], key)
75                 setmetatable(node, options_mt)
76                 rawset(self, key, node)
77         end
78         return node
79 end
80 function sections_mt.__newindex(self, key, value)
81         if not value then
82                 if uci.delete(self[".name"], key) then
83                         rawset(self, key, nil)
84                 else
85                         error("unable to delete section")
86                 end
87         elseif key == "" then
88                 key = uci.add(self[".name"], tostring(value))
89                 if key then
90                         rawset(self, "", self[key])
91                 else
92                         error("unable to create section")
93                 end 
94         else
95                 if not uci.set(self[".name"], key, value) then
96                         error("unable to create section")
97                 end
98         end
99 end
100
101
102 -- Level 3 (options)
103 function options_mt.__index(self, key)
104         return uci.get(self[".conf"], self[".name"], key)
105 end
106 function options_mt.__newindex(self, key, value)
107         if not value then
108                 if not uci.delete(self[".conf"], self[".name"], key) then
109                         error("unable to delete option")
110                 end
111         else
112                 if not uci.set(self[".conf"], self[".name"], key, tostring(value)) then
113                         error("unable to write option")
114                 end
115         end
116 end