libs/core: Fixed some typos in the luci.util inline documentation
[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 --- Delete all sections of a given type that match certain criteria.
40 -- @param config                UCI config
41 -- @param type                  UCI section type
42 -- @param comparator    Function that will be called for each section and
43 -- returns a boolean whether to delete the current section (optional)
44 function delete_all(config, type, comparator)
45         local del = {}
46         local function helper (section)
47                         if not comparator or comparator(section) then
48                                 table.insert(del, section[".name"])
49                         end
50         end
51         
52         foreach(config, type, helper)
53         
54         for i, j in ipairs(del) do
55                 delete(config, j)
56         end
57 end
58
59 --- Create a new section and initialize it with data.
60 -- @param config        UCI config
61 -- @param type          UCI section type
62 -- @param name          UCI section name (optional)
63 -- @param values        Table of key - value pairs to initialize the section with
64 -- @return                      Name of created section
65 function section(config, type, name, values)
66         local stat = true
67         if name then
68                 stat = set(config, name, type)
69         else
70                 name = add(config, type)
71                 stat = name and true
72         end
73         
74         if stat and values then
75                 stat = tset(config, name, values)
76         end
77         
78         return stat and name
79 end
80
81 --- Get a certain state value.
82 -- @param ...   Parameters passed to function get
83 -- @return              UCI value
84 -- @see                 get
85 function get_statevalue(...)
86         set_savedir(savedir_state)
87         local result = get(...)
88         set_savedir(savedir_default)
89         return result
90 end
91
92 --- Updated the data of a section using data from a table.
93 -- @param config        UCI config
94 -- @param section       UCI section name (optional)
95 -- @param values        Table of key - value pairs to update the section with
96 function tset(config, section, values)
97         local stat = true
98         for k, v in pairs(values) do
99                 if k:sub(1, 1) ~= "." then
100                         stat = stat and set(config, section, k, v)
101                 end
102         end
103 end
104
105
106 --- Add an anonymous section.
107 -- @class function
108 -- @name add
109 -- @param config        UCI config
110 -- @param type          UCI section type
111 -- @return                      Name of created section
112
113 --- Get a table of unsaved changes.
114 -- @class function
115 -- @name changes
116 -- @param config        UCI config
117 -- @return                      Table of changes
118
119 --- Commit unsaved changes.
120 -- @class function
121 -- @name commit
122 -- @param config        UCI config
123 -- @return                      Boolean whether operation succeeded
124 -- @see revert
125
126 --- Deletes a section or an option.
127 -- @class function
128 -- @name delete
129 -- @param config        UCI config
130 -- @param section       UCI section name
131 -- @param option        UCI option (optional)
132 -- @return                      Boolean whether operation succeeded
133
134 --- Call a function for every section of a certain type.
135 -- @class function
136 -- @name foreach
137 -- @param config        UCI config
138 -- @param type          UCI section type
139 -- @param callback      Function to be called
140 -- @return                      Boolean whether operation succeeded
141
142 --- Get a section type or an option
143 -- @class function
144 -- @name get
145 -- @param config        UCI config
146 -- @param section       UCI section name
147 -- @param option        UCI option (optional)
148 -- @return                      UCI value
149
150 --- Get all sections of a config or all values of a section.
151 -- @class function
152 -- @name get_all
153 -- @param config        UCI config
154 -- @param section       UCI section name (optional)
155 -- @return                      Table of UCI sections or table of UCI values
156
157 --- Manually load a config.
158 -- @class function
159 -- @name load
160 -- @param config        UCI config
161 -- @return                      Boolean whether operation succeeded
162 -- @see save
163 -- @see unload
164
165 --- Revert unsaved changes.
166 -- @class function
167 -- @name revert
168 -- @param config        UCI config
169 -- @return                      Boolean whether operation succeeded
170 -- @see commit
171
172 --- Saves changes made to a config to make them committable.
173 -- @class function
174 -- @name save
175 -- @param config        UCI config
176 -- @return                      Boolean whether operation succeeded
177 -- @see load
178 -- @see unload
179
180 --- Set a value or create a named section.
181 -- @class function
182 -- @name set
183 -- @param config        UCI config
184 -- @param section       UCI section name
185 -- @param option        UCI option or UCI section type
186 -- @param value         UCI value or nil if you want to create a section
187 -- @return                      Boolean whether operation succeeded
188
189 --- Set the configuration directory.
190 -- @class function
191 -- @name set_confdir
192 -- @param directory     UCI configuration directory
193 -- @return                      Boolean whether operation succeeded
194
195 --- Set the directory for uncommited changes.
196 -- @class function
197 -- @name set_savedir
198 -- @param directory     UCI changes directory
199 -- @return                      Boolean whether operation succeeded
200
201 --- Discard changes made to a config.
202 -- @class function
203 -- @name unload
204 -- @param config        UCI config
205 -- @return                      Boolean whether operation succeeded
206 -- @see load
207 -- @see save