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