libs/core: Reworked some basic libraries to not use package.seeall
[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 table = require "table"
29
30 local setmetatable, rawget, rawset = setmetatable, rawget, rawset
31 local error, pairs, ipairs, tostring = error, pairs, ipairs, tostring
32 local require = require
33
34 --- LuCI UCI model library.
35 module("luci.model.uci", function(m) setmetatable(m, {__index = uci}) end)
36
37 savedir_default = "/tmp/.uci"
38 confdir_default = "/etc/config"
39
40 savedir_state = "/var/state"
41
42
43 --- Applies the new config
44 -- @param config                UCI config
45 function apply(config)
46         local conf = require "luci.config"
47         return conf.uci_oncommit[config] and os.execute(conf.uci_oncommit[config])
48 end
49
50 --- Delete all sections of a given type that match certain criteria.
51 -- @param config                UCI config
52 -- @param type                  UCI section type
53 -- @param comparator    Function that will be called for each section and
54 -- returns a boolean whether to delete the current section (optional)
55 function delete_all(config, type, comparator)
56         local del = {}
57         local function helper (section)
58                         if not comparator or comparator(section) then
59                                 table.insert(del, section[".name"])
60                         end
61         end
62
63         foreach(config, type, helper)
64
65         for i, j in ipairs(del) do
66                 delete(config, j)
67         end
68 end
69
70 --- Create a new section and initialize it with data.
71 -- @param config        UCI config
72 -- @param type          UCI section type
73 -- @param name          UCI section name (optional)
74 -- @param values        Table of key - value pairs to initialize the section with
75 -- @return                      Name of created section
76 function section(config, type, name, values)
77         local stat = true
78         if name then
79                 stat = set(config, name, type)
80         else
81                 name = add(config, type)
82                 stat = name and true
83         end
84
85         if stat and values then
86                 stat = tset(config, name, values)
87         end
88
89         return stat and name
90 end
91
92 --- Savely load the configuration.
93 -- @param config        Configuration to load
94 -- @return                      Sucess status
95 -- @see                         load_state
96 -- @see                         load
97 function load_config(...)
98         set_confdir(confdir_default)
99         set_savedir(savedir_default)
100         return load(...)
101 end
102
103 --- Savely load state values.
104 -- @param config        Configuration to load
105 -- @return                      Sucess status
106 -- @see                         load_config
107 -- @see                         load
108 function load_state(config)
109         set_confdir(confdir_default)
110         set_savedir(savedir_state)
111         return load(config)
112 end
113
114 --- Save changes to config values.
115 -- @param config        Configuration to save
116 -- @return                      Sucess status
117 -- @see                         save_state
118 -- @see                         save
119 function save_config(config)
120         set_savedir(savedir_default)
121         return save(config)
122 end
123
124 --- Save changes to state values.
125 -- @param config        Configuration to save
126 -- @return                      Sucess status
127 -- @see                         save_config
128 -- @see                         save
129 function save_state(config)
130         set_savedir(savedir_state)
131         return save(config)
132 end
133
134 --- Updated the data of a section using data from a table.
135 -- @param config        UCI config
136 -- @param section       UCI section name (optional)
137 -- @param values        Table of key - value pairs to update the section with
138 function tset(config, section, values)
139         local stat = true
140         for k, v in pairs(values) do
141                 if k:sub(1, 1) ~= "." then
142                         stat = stat and set(config, section, k, v)
143                 end
144         end
145         return stat
146 end
147
148 --- Get an option or list and return values as table.
149 -- @param config        UCI config
150 -- @param section       UCI section name
151 -- @param option        UCI option
152 -- @return                      UCI value
153 function get_list(config, section, option)
154         if config and section and option then
155                 local val = get(config, section, option)
156                 return ( type(val) == "table" and val or { val } )
157         end
158         return nil
159 end
160
161 --- Set given values as list.
162 -- @param config        UCI config
163 -- @param section       UCI section name
164 -- @param option        UCI option
165 -- @param value         UCI value
166 -- @return                      Boolean whether operation succeeded
167 function set_list(config, section, option, value)
168         if config and section and option then
169                 return set(
170                         config, section, option,
171                         ( type(value) == "table" and value or { value } )
172                 )
173         end
174         return false
175 end
176
177
178 --- Add an anonymous section.
179 -- @class function
180 -- @name add
181 -- @param config        UCI config
182 -- @param type          UCI section type
183 -- @return                      Name of created section
184
185 --- Get a table of unsaved changes.
186 -- @class function
187 -- @name changes
188 -- @param config        UCI config
189 -- @return                      Table of changes
190
191 --- Commit unsaved changes.
192 -- @class function
193 -- @name commit
194 -- @param config        UCI config
195 -- @return                      Boolean whether operation succeeded
196 -- @see revert
197
198 --- Deletes a section or an option.
199 -- @class function
200 -- @name delete
201 -- @param config        UCI config
202 -- @param section       UCI section name
203 -- @param option        UCI option (optional)
204 -- @return                      Boolean whether operation succeeded
205
206 --- Call a function for every section of a certain type.
207 -- @class function
208 -- @name foreach
209 -- @param config        UCI config
210 -- @param type          UCI section type
211 -- @param callback      Function to be called
212 -- @return                      Boolean whether operation succeeded
213
214 --- Get a section type or an option
215 -- @class function
216 -- @name get
217 -- @param config        UCI config
218 -- @param section       UCI section name
219 -- @param option        UCI option (optional)
220 -- @return                      UCI value
221
222 --- Get all sections of a config or all values of a section.
223 -- @class function
224 -- @name get_all
225 -- @param config        UCI config
226 -- @param section       UCI section name (optional)
227 -- @return                      Table of UCI sections or table of UCI values
228
229 --- Manually load a config.
230 -- Warning: This function is unsave! You should use load_config or load_state if possible.
231 -- @class function
232 -- @name load
233 -- @param config        UCI config
234 -- @return                      Boolean whether operation succeeded
235 -- @see load_config
236 -- @see load_state
237 -- @see save
238 -- @see unload
239
240 --- Revert unsaved changes.
241 -- @class function
242 -- @name revert
243 -- @param config        UCI config
244 -- @return                      Boolean whether operation succeeded
245 -- @see commit
246
247 --- Saves changes made to a config to make them committable.
248 -- @class function
249 -- @name save
250 -- @param config        UCI config
251 -- @return                      Boolean whether operation succeeded
252 -- @see load
253 -- @see unload
254
255 --- Set a value or create a named section.
256 -- @class function
257 -- @name set
258 -- @param config        UCI config
259 -- @param section       UCI section name
260 -- @param option        UCI option or UCI section type
261 -- @param value         UCI value or nil if you want to create a section
262 -- @return                      Boolean whether operation succeeded
263
264 --- Set the configuration directory.
265 -- @class function
266 -- @name set_confdir
267 -- @param directory     UCI configuration directory
268 -- @return                      Boolean whether operation succeeded
269
270 --- Set the directory for uncommited changes.
271 -- @class function
272 -- @name set_savedir
273 -- @param directory     UCI changes directory
274 -- @return                      Boolean whether operation succeeded
275
276 --- Discard changes made to a config.
277 -- @class function
278 -- @name unload
279 -- @param config        UCI config
280 -- @return                      Boolean whether operation succeeded
281 -- @see load
282 -- @see save