2 LuCI - UCI utilities for model classes
4 Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
20 local assert, pairs, type = assert, pairs, type
21 local utl = require "luci.util"
23 module "luci.model.uci.bind"
27 function bind.__init__(self, config, cursor)
28 assert(config, "call to bind() without config file")
33 function bind.init(self, cursor)
34 assert(cursor, "call to init() without uci cursor")
38 function bind.section(self, stype)
39 local x = utl.class(bsection)
40 x.__init__ = function(inst, sid)
41 assert(self.uci:get(self.cfg, sid) == stype,
42 "attempt to instantiate bsection(%q) of wrong type, expected %q"
52 function bind.usection(self, stype)
53 local x = utl.class(bsection)
54 x.__init__ = function(inst)
62 function bind.list(self, list, add, rem)
65 if type(list) == "string" then
67 for item in list:gmatch("%S+") do
71 elseif type(list) == "table" then
73 for _, item in pairs(list) do
78 if add then lookup[add] = true end
79 if rem then lookup[rem] = nil end
81 return utl.keys(lookup)
84 function bind.bool(self, v)
85 return ( v == "1" or v == "true" or v == "yes" or v == "on" )
89 bsection = utl.class()
91 function bsection.uciop(self, op, ...)
92 assert(self.bind and self.bind.uci,
93 "attempt to use unitialized binding")
96 return self.bind.uci[op](self.bind.uci, self.bind.cfg, ...)
102 function bsection.get(self, k, c)
104 if type(c) == "string" then
105 v = self:uciop("get", c, k)
107 self:uciop("foreach", self.stype,
109 if type(c) == "table" then
111 for ck, cv in pairs(c) do
112 if s[ck] ~= cv then return true end
126 function bsection.set(self, k, v, c)
128 if type(c) == "string" then
129 if type(v) == "table" and #v == 0 then
130 stat = self:uciop("delete", c, k)
132 stat = self:uciop("set", c, k, v)
135 self:uciop("foreach", self.stype,
137 if type(c) == "table" then
139 for ck, cv in pairs(c) do
140 if s[ck] ~= cv then return true end
143 stat = self:uciop("set", c, k, v)
150 function bsection.property(self, k, n)
151 self[n or k] = function(c, val)
153 return c:get(k, c.sid)
155 return c:set(k, val, c.sid)
160 function bsection.property_bool(self, k, n)
161 self[n or k] = function(c, val)
163 return self.bind:bool(c:get(k, c.sid))
165 return c:set(k, self.bind:bool(val) and "1" or "0", c.sid)