* Fixed typos
[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, rawget, rawset = setmetatable, rawget, rawset
29 local error, pairs, ipairs, tostring = error, pairs, ipairs, tostring
30
31 module("luci.model.uci", function(m) setmetatable(m, {__index = uci}) end)
32
33 savedir_default = "/tmp/.uci"
34 confdir_default = "/etc/config"
35
36 savedir_state = "/var/state"
37
38 function delete_all(config, type, comparator)
39         local del = {}
40         
41         foreach(config, type,
42                 function (section)
43                         if not comparator or comparator(section) then
44                                 table.insert(del, section[".name"])
45                         end
46                 end)
47                 
48         for i, j in ipairs(del) do
49                 uci.delete(config, j)
50         end
51 end
52
53 function section(config, type, name, values)
54         local stat = true
55         if name then
56                 stat = set(config, name, type)
57         else
58                 name = add(config, type)
59                 stat = name and true
60         end
61         
62         if stat and values then
63                 stat = tset(config, name, values)
64         end
65         
66         return stat and name
67 end
68
69 function tset(config, section, values)
70         local stat = true
71         for k, v in pairs(values) do
72                 if k:sub(1, 1) ~= "." then
73                         stat = stat and set(config, section, k, v)
74                 end
75         end
76 end