11 Copyright 2008 Steven Barth <steven@midlink.org>
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
17 http://www.apache.org/licenses/LICENSE-2.0
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.
26 local uci = require("uci")
27 local util = require("luci.util")
28 local setmetatable = setmetatable
32 local tostring = tostring
34 module("luci.model.uci", function(m) setmetatable(m, {__index = uci}) end)
37 local sections_mt = {}
40 savedir_default = "/tmp/.uci"
41 confdir_default = "/etc/config"
43 savedir_state = "/var/state"
46 setmetatable(config, configs_mt)
49 function configs_mt.__index(self, key)
50 local node = rawget(self, key)
52 if not uci.load(key) then
57 setmetatable(node, sections_mt)
58 rawset(self, key, node)
62 function configs_mt.__newindex()
63 error("invalid operation")
68 function sections_mt.__index(self, key)
69 local node = rawget(self, key)
72 node[".conf"] = self[".name"]
74 node[".type"] = uci.get(self[".name"], key)
75 setmetatable(node, options_mt)
76 rawset(self, key, node)
80 function sections_mt.__newindex(self, key, value)
82 if uci.delete(self[".name"], key) then
83 rawset(self, key, nil)
85 error("unable to delete section")
88 key = uci.add(self[".name"], tostring(value))
90 rawset(self, "", self[key])
92 error("unable to create section")
95 if not uci.set(self[".name"], key, value) then
96 error("unable to create section")
103 function options_mt.__index(self, key)
104 return uci.get(self[".conf"], self[".name"], key)
106 function options_mt.__newindex(self, key, value)
108 if not uci.delete(self[".conf"], self[".name"], key) then
109 error("unable to delete option")
112 if not uci.set(self[".conf"], self[".name"], key, tostring(value)) then
113 error("unable to write option")