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