libs/cbi: Make sure options in ListValues are unique
[project/luci.git] / libs / uvl / luasrc / uvl.lua
1 --[[
2
3 UCI Validation Layer - Main Library
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14
15 ]]--
16
17
18 --- UVL - UCI Validation Layer
19 -- @class module
20 -- @cstyle instance
21
22 module( "luci.uvl", package.seeall )
23
24 require("luci.fs")
25 require("luci.util")
26 require("luci.model.uci")
27 require("luci.uvl.errors")
28 require("luci.uvl.datatypes")
29 require("luci.uvl.validation")
30 require("luci.uvl.dependencies")
31
32
33 TYPE_SCHEME = 0x00
34 TYPE_CONFIG = 0x01
35 TYPE_SECTION = 0x02
36 TYPE_VARIABLE = 0x03
37 TYPE_OPTION = 0x04
38 TYPE_ENUM = 0x05
39
40 --- Boolean; default true;
41 -- treat sections found in config but not in scheme as error
42 STRICT_UNKNOWN_SECTIONS = true
43
44 --- Boolean; default true;
45 -- treat options found in config but not in scheme as error
46 STRICT_UNKNOWN_OPTIONS = true
47
48 --- Boolean; default true;
49 -- treat failed external validators as error
50 STRICT_EXTERNAL_VALIDATORS = true
51
52 --- Boolean; default true;
53 -- treat list values stored as options like errors
54 STRICT_LIST_TYPE = true
55
56
57 local default_schemedir = "/lib/uci/schema"
58 local default_savedir = "/tmp/.uvl"
59 local ERR = luci.uvl.errors
60
61
62 --- Object constructor
63 -- @class function
64 -- @name UVL
65 -- @param schemedir Path to the scheme directory (optional)
66 -- @return Instance object
67 UVL = luci.util.class()
68
69 function UVL.__init__( self, schemedir )
70 self.schemedir = schemedir or default_schemedir
71 self.packages = { }
72 self.beenthere = { }
73 self.uci = luci.model.uci
74 self.err = luci.uvl.errors
75 self.dep = luci.uvl.dependencies
76 self.datatypes = luci.uvl.datatypes
77 end
78
79
80 --- Parse given scheme and return the scheme tree.
81 -- @param scheme Name of the scheme to parse
82 -- @return Table containing the parsed scheme or nil on error
83 -- @return String containing the reason for errors (if any)
84 function UVL.get_scheme( self, scheme )
85 if not self.packages[scheme] then
86 local ok, err = self:read_scheme( scheme )
87 if not ok then
88 return nil, err
89 end
90 end
91 return self.packages[scheme], nil
92 end
93
94 --- Validate given configuration, section or option.
95 -- @param config Name of the configuration to validate
96 -- @param section Name of the section to validate (optional)
97 -- @param option Name of the option to validate (optional)
98 -- @return Boolean indicating whether the given config validates
99 -- @return String containing the reason for errors (if any)
100 function UVL.validate( self, config, section, option )
101 if config and section and option then
102 return self:validate_option( config, section, option )
103 elseif config and section then
104 return self:validate_section( config, section )
105 elseif config then
106 return self:validate_config( config )
107 end
108 end
109
110 --- Validate given configuration.
111 -- @param config Name of the configuration to validate
112 -- @return Boolean indicating whether the given config validates
113 -- @return String containing the reason for errors (if any)
114 function UVL.validate_config( self, config, uci )
115
116 if not self.packages[config] then
117 local ok, err = self:read_scheme(config)
118 if not ok then
119 return false, err
120 end
121 end
122
123 local co = luci.uvl.config( self, uci or config, uci and config )
124 local sc = { }
125
126 self.beenthere = { }
127
128 if not co:config() then
129 return false, co:errors()
130 end
131
132 local function _uci_foreach( type, func )
133 for k, v in pairs(co:config()) do
134 if v['.type'] == type then
135 sc[type] = sc[type] + 1
136 local ok, err = func( k, v )
137 if not ok then co:error(err) end
138 end
139 end
140 end
141
142 for k, v in pairs( self.packages[config].sections ) do
143 sc[k] = 0
144 _uci_foreach( k,
145 function(s)
146 return self:_validate_section( co:section(s) )
147 end
148 )
149 end
150
151 if STRICT_UNKNOWN_SECTIONS then
152 for k, v in pairs(co:config()) do
153 local so = co:section(k)
154 if not self.beenthere[so:cid()] then
155 co:error(ERR.SECT_UNKNOWN(so))
156 end
157 end
158 end
159
160 for _, k in ipairs(luci.util.keys(sc)) do
161 local so = co:section(k)
162 if so:scheme('required') and sc[k] == 0 then
163 co:error(ERR.SECT_REQUIRED(so))
164 elseif so:scheme('unique') and sc[k] > 1 then
165 co:error(ERR.SECT_UNIQUE(so))
166 end
167 end
168
169 return co:ok(), co:errors()
170 end
171
172 --- Validate given config section.
173 -- @param config Name of the configuration to validate
174 -- @param section Name of the section to validate
175 -- @return Boolean indicating whether the given config validates
176 -- @return String containing the reason for errors (if any)
177 function UVL.validate_section( self, config, section, uci )
178
179 if not self.packages[config] then
180 local ok, err = self:read_scheme( config )
181 if not ok then
182 return false, err
183 end
184 end
185
186 local co = luci.uvl.config( self, uci or config, uci and config )
187 local so = co:section( section )
188
189 self.beenthere = { }
190
191 if not co:config() then
192 return false, co:errors()
193 end
194
195 if so:config() then
196 return self:_validate_section( so )
197 else
198 return false, ERR.SECT_NOTFOUND(so)
199 end
200 end
201
202 --- Validate given config option.
203 -- @param config Name of the configuration to validate
204 -- @param section Name of the section to validate
205 -- @param option Name of the option to validate
206 -- @return Boolean indicating whether the given config validates
207 -- @return String containing the reason for errors (if any)
208 function UVL.validate_option( self, config, section, option, uci )
209
210 if not self.packages[config] then
211 local ok, err = self:read_scheme( config )
212 if not ok then
213 return false, err
214 end
215 end
216
217 local co = luci.uvl.config( self, uci or config, uci and config )
218 local so = co:section( section )
219 local oo = so:option( option )
220
221 if not co:config() then
222 return false, co:errors()
223 end
224
225 if so:config() and oo:config() then
226 return self:_validate_option( oo )
227 else
228 return false, ERR.OPT_NOTFOUND(oo)
229 end
230 end
231
232
233 function UVL._validate_section( self, section )
234
235 if section:config() then
236 if section:scheme('named') == true and
237 section:config('.anonymous') == true
238 then
239 return false, ERR.SECT_NAMED(section)
240 end
241
242 for _, v in ipairs(section:variables()) do
243 local ok, err = self:_validate_option( v )
244 if not ok then
245 section:error(err)
246 end
247 end
248
249 local ok, err = luci.uvl.dependencies.check( self, section )
250 if not ok then
251 section:error(err)
252 end
253 else
254 return false, ERR.SECT_NOTFOUND(section)
255 end
256
257 if STRICT_UNKNOWN_OPTIONS and not section:scheme('dynamic') then
258 for k, v in pairs(section:config()) do
259 local oo = section:option(k)
260 if k:sub(1,1) ~= "." and not self.beenthere[oo:cid()] then
261 section:error(ERR.OPT_NOTFOUND(oo))
262 end
263 end
264 end
265
266 return section:ok(), section:errors()
267 end
268
269 function UVL._validate_option( self, option, nodeps )
270
271 if not option:scheme() and not option:parent():scheme('dynamic') then
272 return false, option:error(ERR.OPT_UNKNOWN(option))
273
274 elseif option:scheme() then
275 if option:scheme('required') and not option:value() then
276 return false, option:error(ERR.OPT_REQUIRED(option))
277
278 elseif option:value() then
279 local val = option:value()
280
281 if option:scheme('type') == "reference" or
282 option:scheme('type') == "enum"
283 then
284 if not option:scheme('values') or
285 not option:scheme('values')[val]
286 then
287 return false, option:error( ERR.OPT_BADVALUE(
288 option, { val, table.concat(
289 luci.util.keys(option:scheme('values') or {}), ", "
290 ) }
291 ) )
292 end
293 elseif option:scheme('type') == "list" then
294 if type(val) ~= "table" and STRICT_LIST_TYPE then
295 return false, option:error(ERR.OPT_NOTLIST(option))
296 end
297 elseif option:scheme('datatype') then
298 local dt = option:scheme('datatype')
299
300 if self.datatypes[dt] then
301 val = ( type(val) == "table" and val or { val } )
302 for i, v in ipairs(val) do
303 if not self.datatypes[dt]( v ) then
304 return false, option:error(
305 ERR.OPT_INVVALUE(option, {v, dt})
306 )
307 end
308 end
309 else
310 return false, option:error(ERR.OPT_DATATYPE(option, dt))
311 end
312 end
313 end
314
315 if not nodeps then
316 local ok, err = luci.uvl.dependencies.check( self, option )
317 if not ok then
318 option:error(err)
319 end
320 end
321
322 local ok, err = luci.uvl.validation.check( self, option )
323 if not ok and STRICT_EXTERNAL_VALIDATORS then
324 return false, option:error(err)
325 end
326 end
327
328 return option:ok(), option:errors()
329 end
330
331 --- Find all parts of given scheme and construct validation tree.
332 -- This is normally done on demand, so you don't have to call this function
333 -- by yourself.
334 -- @param scheme Name of the scheme to parse
335 function UVL.read_scheme( self, scheme )
336
337 local so = luci.uvl.scheme( self, scheme )
338
339 local schemes = { }
340 local files = luci.fs.glob(self.schemedir .. '/*/' .. scheme)
341
342 if files then
343 for i, file in ipairs( files ) do
344 if not luci.fs.access(file) then
345 return so:error(ERR.SME_READ(so,file))
346 end
347
348 local uci = luci.model.uci.cursor( luci.fs.dirname(file), default_savedir )
349
350 local sd, err = uci:get_all( luci.fs.basename(file) )
351
352 if not sd then
353 return false, ERR.UCILOAD(so, err)
354 end
355
356 table.insert( schemes, sd )
357 end
358
359 return self:_read_scheme_parts( so, schemes )
360 else
361 return false, so:error(ERR.SME_FIND(so, self.schemedir))
362 end
363 end
364
365 -- Process all given parts and construct validation tree
366 function UVL._read_scheme_parts( self, scheme, schemes )
367
368 -- helper function to check for required fields
369 local function _req( c, t, r )
370 for i, v in ipairs(r) do
371 if not t[v] then
372 return false, ERR.SME_REQFLD({c,t}, v)
373 end
374 end
375 return true
376 end
377
378 -- helper function to validate references
379 local function _ref( c, t )
380 local k, n
381 if c == TYPE_SECTION then
382 k = "package"
383 n = 1
384 elseif c == TYPE_VARIABLE then
385 k = "section"
386 n = 2
387 elseif c == TYPE_ENUM then
388 k = "variable"
389 n = 3
390 end
391
392 local r = luci.util.split( t[k], "." )
393 r[1] = ( #r[1] > 0 and r[1] or scheme:sid() )
394
395 if #r ~= n then
396 return false, ERR.SME_BADREF(scheme, k)
397 end
398
399 return r
400 end
401
402 -- helper function to read bools
403 local function _bool( v )
404 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
405 end
406
407
408 local ok, err
409
410 -- Step 1: get all sections
411 for i, conf in ipairs( schemes ) do
412 for k, v in pairs( conf ) do
413 if v['.type'] == 'section' then
414
415 ok, err = _req( TYPE_SECTION, v, { "name", "package" } )
416 if err then return false, scheme:error(err) end
417
418 local r, err = _ref( TYPE_SECTION, v )
419 if err then return false, scheme:error(err) end
420
421 self.packages[r[1]] =
422 self.packages[r[1]] or {
423 ["name"] = r[1];
424 ["sections"] = { };
425 ["variables"] = { };
426 }
427
428 local p = self.packages[r[1]]
429 p.sections[v.name] = p.sections[v.name] or { }
430 p.variables[v.name] = p.variables[v.name] or { }
431
432 local s = p.sections[v.name]
433 local so = scheme:section(v.name)
434
435 for k, v2 in pairs(v) do
436 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
437 if k == "depends" then
438 s.depends = self:_read_dependency( v2, s.depends )
439 if not s.depends then
440 return false, scheme:error(
441 ERR.SME_BADDEP(so, luci.util.serialize_data(s.depends))
442 )
443 end
444 elseif k == "dynamic" or k == "unique" or
445 k == "required" or k == "named"
446 then
447 s[k] = _bool(v2)
448 else
449 s[k] = v2
450 end
451 end
452 end
453
454 s.dynamic = s.dynamic or false
455 s.unique = s.unique or false
456 s.required = s.required or false
457 s.named = s.named or false
458 end
459 end
460 end
461
462 -- Step 2: get all variables
463 for i, conf in ipairs( schemes ) do
464 for k, v in pairs( conf ) do
465 if v['.type'] == "variable" then
466
467 ok, err = _req( TYPE_VARIABLE, v, { "name", "section" } )
468 if err then return false, scheme:error(err) end
469
470 local r, err = _ref( TYPE_VARIABLE, v )
471 if err then return false, scheme:error(err) end
472
473 local p = self.packages[r[1]]
474 if not p then
475 return false, scheme:error(
476 ERR.SME_VBADPACK({scheme:sid(), '', v.name}, r[1])
477 )
478 end
479
480 local s = p.variables[r[2]]
481 if not s then
482 return false, scheme:error(
483 ERR.SME_VBADSECT({scheme:sid(), '', v.name}, r[2])
484 )
485 end
486
487 s[v.name] = s[v.name] or { }
488
489 local t = s[v.name]
490 local so = scheme:section(r[2])
491 local to = so:option(v.name)
492
493 for k, v2 in pairs(v) do
494 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
495 if k == "depends" then
496 t.depends = self:_read_dependency( v2, t.depends )
497 if not t.depends then
498 return false, scheme:error(so:error(
499 ERR.SME_BADDEP(to, luci.util.serialize_data(v2))
500 ))
501 end
502 elseif k == "validator" then
503 t.validators = self:_read_validator( v2, t.validators )
504 if not t.validators then
505 return false, scheme:error(so:error(
506 ERR.SME_BADVAL(to, luci.util.serialize_data(v2))
507 ))
508 end
509 elseif k == "valueof" then
510 local values, err = self:_read_reference( v2 )
511 if err then
512 return false, scheme:error(so:error(
513 ERR.REFERENCE(to, luci.util.serialize_data(v2)):child(err)
514 ))
515 end
516 t.type = "reference"
517 t.values = values
518 elseif k == "required" then
519 t[k] = _bool(v2)
520 else
521 t[k] = t[k] or v2
522 end
523 end
524 end
525
526 t.type = t.type or "variable"
527 t.datatype = t.datatype or "string"
528 t.required = t.required or false
529 end
530 end
531 end
532
533 -- Step 3: get all enums
534 for i, conf in ipairs( schemes ) do
535 for k, v in pairs( conf ) do
536 if v['.type'] == "enum" then
537
538 ok, err = _req( TYPE_ENUM, v, { "value", "variable" } )
539 if err then return false, scheme:error(err) end
540
541 local r, err = _ref( TYPE_ENUM, v )
542 if err then return false, scheme:error(err) end
543
544 local p = self.packages[r[1]]
545 if not p then
546 return false, scheme:error(
547 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
548 )
549 end
550
551 local s = p.variables[r[2]]
552 if not s then
553 return false, scheme:error(
554 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
555 )
556 end
557
558 local t = s[r[3]]
559 if not t then
560 return false, scheme:error(
561 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
562 )
563 end
564
565
566 local so = scheme:section(r[2])
567 local oo = so:option(r[3])
568 local eo = oo:enum(v.value)
569
570 if t.type ~= "enum" then
571 return false, scheme:error(ERR.SME_EBADTYPE(eo))
572 end
573
574 if not t.values then
575 t.values = { [v.value] = v.title or v.value }
576 else
577 t.values[v.value] = v.title or v.value
578 end
579
580 if not t.enum_depends then
581 t.enum_depends = { }
582 end
583
584 if v.default then
585 if t.default then
586 return false, scheme:error(ERR.SME_EBADDEF(eo))
587 end
588 t.default = v.value
589 end
590
591 if v.depends then
592 t.enum_depends[v.value] = self:_read_dependency(
593 v.depends, t.enum_depends[v.value]
594 )
595
596 if not t.enum_depends[v.value] then
597 return false, scheme:error(so:error(oo:error(
598 ERR.SME_BADDEP(eo, luci.util.serialize_data(v.depends))
599 )))
600 end
601 end
602 end
603 end
604 end
605
606 return self
607 end
608
609 -- Read a dependency specification
610 function UVL._read_dependency( self, values, deps )
611 local expr = "%$?[a-zA-Z0-9_]+"
612 if values then
613 values = ( type(values) == "table" and values or { values } )
614 for _, value in ipairs(values) do
615 local parts = luci.util.split( value, "%s*,%s*", nil, true )
616 local condition = { }
617 for i, val in ipairs(parts) do
618 local k, v = unpack(luci.util.split(val, "%s*=%s*", nil, true))
619
620 if k and (
621 k:match("^"..expr.."%."..expr.."%."..expr.."$") or
622 k:match("^"..expr.."%."..expr.."$") or
623 k:match("^"..expr.."$")
624 ) then
625 condition[k] = v or true
626 else
627 return nil
628 end
629 end
630
631 if not deps then
632 deps = { condition }
633 else
634 table.insert( deps, condition )
635 end
636 end
637 end
638
639 return deps
640 end
641
642 -- Read a validator specification
643 function UVL._read_validator( self, values, validators )
644 if values then
645 values = ( type(values) == "table" and values or { values } )
646 for _, value in ipairs(values) do
647 local validator
648
649 if value:match("^exec:") then
650 validator = value:gsub("^exec:","")
651 elseif value:match("^lua:") then
652 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
653 end
654
655 if validator then
656 if not validators then
657 validators = { validator }
658 else
659 table.insert( validators, validator )
660 end
661 else
662 return nil
663 end
664 end
665
666 return validators
667 end
668 end
669
670 -- Read a reference specification (XXX: We should validate external configs too...)
671 function UVL._read_reference( self, values )
672 local val = { }
673 values = ( type(values) == "table" and values or { values } )
674
675 for _, value in ipairs(values) do
676 local ref = luci.util.split(value, ".")
677
678 if #ref == 2 or #ref == 3 then
679 local co = luci.uvl.config( self, ref[1] )
680 if not co:config() then return false, co:errors() end
681
682 for k, v in pairs(co:config()) do
683 if v['.type'] == ref[2] then
684 if #ref == 2 then
685 if v['.anonymous'] == true then
686 return false, ERR.SME_INVREF('', value)
687 end
688 val[k] = k -- XXX: title/description would be nice
689 elseif v[ref[3]] then
690 val[v[ref[3]]] = v[ref[3]] -- XXX: dito
691 end
692 end
693 end
694 else
695 return false, ERR.SME_BADREF('', value)
696 end
697 end
698
699 return val, nil
700 end
701
702 -- Resolve given path
703 function UVL._resolve_function( self, value )
704 local path = luci.util.split(value, ".")
705
706 for i=1, #path-1 do
707 local stat, mod = luci.util.copcall(
708 require, table.concat(path, ".", 1, i)
709 )
710
711 if stat and mod then
712 for j=i+1, #path-1 do
713 if not type(mod) == "table" then
714 break
715 end
716 mod = mod[path[j]]
717 if not mod then
718 break
719 end
720 end
721 mod = type(mod) == "table" and mod[path[#path]] or nil
722 if type(mod) == "function" then
723 return mod
724 end
725 end
726 end
727 end
728
729
730 --- Object representation of an uvl item - base class.
731 uvlitem = luci.util.class()
732
733 function uvlitem.cid(self)
734 return table.concat( self.cref, '.' )
735 end
736
737 function uvlitem.sid(self)
738 return table.concat( self.sref, '.' )
739 end
740
741 function uvlitem.scheme(self, opt)
742 local s
743
744 if #self.sref == 4 or #self.sref == 3 then
745 s = self.s
746 .packages[self.sref[1]]
747 .variables[self.sref[2]][self.sref[3]]
748 elseif #self.sref == 2 then
749 s = self.s
750 .packages[self.sref[1]]
751 .sections[self.sref[2]]
752 else
753 s = self.s
754 .packages[self.sref[1]]
755 end
756
757 if s and opt then
758 return s[opt]
759 elseif s then
760 return s
761 end
762 end
763
764 function uvlitem.config(self, opt)
765 local c
766
767 if #self.cref == 4 or #self.cref == 3 then
768 c = self.c[self.cref[2]][self.cref[3]]
769 elseif #self.cref == 2 then
770 c = self.c[self.cref[2]]
771 else
772 c = self.c
773 end
774
775 if c and opt then
776 return c[opt]
777 elseif c then
778 return c
779 end
780 end
781
782 function uvlitem.title(self)
783 return self:scheme() and self:scheme('title') or
784 self.cref[3] or self.cref[2] or self.cref[1]
785 end
786
787 function uvlitem.type(self)
788 if self.t == luci.uvl.TYPE_CONFIG then
789 return 'config'
790 elseif self.t == luci.uvl.TYPE_SECTION then
791 return 'section'
792 elseif self.t == luci.uvl.TYPE_OPTION then
793 return 'option'
794 elseif self.t == luci.uvl.TYPE_ENUM then
795 return 'enum'
796 end
797 end
798
799 function uvlitem.error(self, ...)
800 if not self.e then
801 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
802 self.e = errconst[#self.cref]( self )
803 end
804
805 return self.e:child( ... )
806 end
807
808 function uvlitem.errors(self)
809 return self.e
810 end
811
812 function uvlitem.ok(self)
813 return not self:errors()
814 end
815
816 function uvlitem.parent(self)
817 if self.p then
818 return self.p
819 elseif #self.cref == 3 or #self.cref == 4 then
820 return luci.uvl.section( self.s, self.c, self.cref[1], self.cref[2] )
821 elseif #self.cref == 2 then
822 return luci.uvl.config( self.s, self.c, self.cref[1] )
823 else
824 return nil
825 end
826 end
827
828 function uvlitem._loadconf(self, co, c)
829 if not co then
830 local uci, err = luci.model.uci.cursor(), nil
831 co, err = uci:get_all(c)
832
833 if err then
834 self:error(ERR.UCILOAD(self, err))
835 end
836 end
837 return co
838 end
839
840
841 --- Object representation of a scheme.
842 -- @class scheme
843 -- @cstyle instance
844 -- @name luci.uvl.scheme
845
846 --- Scheme instance constructor.
847 -- @class function
848 -- @name scheme
849 -- @param scheme Scheme instance
850 -- @param co Configuration data
851 -- @param c Configuration name
852 -- @return Config instance
853 scheme = luci.util.class(uvlitem)
854
855 function scheme.__init__(self, scheme, co, c)
856 if not c then
857 c, co = co, nil
858 end
859
860 self.cref = { c }
861 self.sref = { c }
862 self.c = self:_loadconf(co, c)
863 self.s = scheme
864 self.t = luci.uvl.TYPE_SCHEME
865 end
866
867 --- Add an error to scheme.
868 -- @return Scheme error context
869 function scheme.error(self, ...)
870 if not self.e then self.e = ERR.SCHEME( self ) end
871 return self.e:child( ... )
872 end
873
874 --- Get an associated config object.
875 -- @return Config instance
876 function scheme.config(self)
877 local co = luci.uvl.config( self.s, self.cref[1] )
878 co.p = self
879
880 return co
881 end
882
883 --- Get all section objects associated with this scheme.
884 -- @return Table containing all associated luci.uvl.section instances
885 function scheme.sections(self)
886 local v = { }
887 if self.s.packages[self.sref[1]].sections then
888 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
889 table.insert( v, luci.uvl.option(
890 self.s, self.c, self.cref[1], self.cref[2], o
891 ) )
892 end
893 end
894 return v
895 end
896
897 --- Get an associated section object.
898 -- @param s Section to select
899 -- @return Section instance
900 function scheme.section(self, s)
901 local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
902 so.p = self
903
904 return so
905 end
906
907
908 --- Object representation of a config.
909 -- @class config
910 -- @cstyle instance
911 -- @name luci.uvl.config
912
913 --- Config instance constructor.
914 -- @class function
915 -- @name config
916 -- @param scheme Scheme instance
917 -- @param co Configuration data
918 -- @param c Configuration name
919 -- @return Config instance
920 config = luci.util.class(uvlitem)
921
922 function config.__init__(self, scheme, co, c)
923 if not c then
924 c, co = co, nil
925 end
926
927 self.cref = { c }
928 self.sref = { c }
929 self.c = self:_loadconf(co, c)
930 self.s = scheme
931 self.t = luci.uvl.TYPE_CONFIG
932 end
933
934 --- Get all section objects associated with this config.
935 -- @return Table containing all associated luci.uvl.section instances
936 function config.sections(self)
937 local v = { }
938 if self.s.packages[self.sref[1]].sections then
939 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
940 table.insert( v, luci.uvl.option(
941 self.s, self.c, self.cref[1], self.cref[2], o
942 ) )
943 end
944 end
945 return v
946 end
947
948 --- Get an associated section object.
949 -- @param s Section to select
950 -- @return Section instance
951 function config.section(self, s)
952 local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
953 so.p = self
954
955 return so
956 end
957
958
959 --- Object representation of a scheme/config section.
960 -- @class module
961 -- @cstyle instance
962 -- @name luci.uvl.section
963
964 --- Section instance constructor.
965 -- @class function
966 -- @name section
967 -- @param scheme Scheme instance
968 -- @param co Configuration data
969 -- @param c Configuration name
970 -- @param s Section name
971 -- @return Section instance
972 section = luci.util.class(uvlitem)
973
974 function section.__init__(self, scheme, co, c, s)
975 self.cref = { c, s }
976 self.sref = { c, co and co[s] and co[s]['.type'] or s }
977 self.c = self:_loadconf(co, c)
978 self.s = scheme
979 self.t = luci.uvl.TYPE_SECTION
980 end
981
982 --- Get all option objects associated with this section.
983 -- @return Table containing all associated luci.uvl.option instances
984 function section.variables(self)
985 local v = { }
986 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
987 for o, _ in pairs(
988 self.s.packages[self.sref[1]].variables[self.sref[2]]
989 ) do
990 table.insert( v, luci.uvl.option(
991 self.s, self.c, self.cref[1], self.cref[2], o
992 ) )
993 end
994 end
995 return v
996 end
997
998 --- Get an associated option object.
999 -- @param o Option to select
1000 -- @return Option instance
1001 function section.option(self, o)
1002 local oo = luci.uvl.option( self.s, self.c, self.cref[1], self.cref[2], o )
1003 oo.p = self
1004
1005 return oo
1006 end
1007
1008
1009 --- Object representation of a scheme/config option.
1010 -- @class module
1011 -- @cstyle instance
1012 -- @name luci.uvl.option
1013
1014 --- Section instance constructor.
1015 -- @class function
1016 -- @name option
1017 -- @param scheme Scheme instance
1018 -- @param co Configuration data
1019 -- @param c Configuration name
1020 -- @param s Section name
1021 -- @param o Option name
1022 -- @return Option instance
1023 option = luci.util.class(uvlitem)
1024
1025 function option.__init__(self, scheme, co, c, s, o)
1026 self.cref = { c, s, o }
1027 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1028 self.c = self:_loadconf(co, c)
1029 self.s = scheme
1030 self.t = luci.uvl.TYPE_OPTION
1031 end
1032
1033 --- Get the value of this option.
1034 -- @return The associated configuration value
1035 function option.value(self)
1036 return self:config()
1037 end
1038
1039 --- Get the associated section information in scheme.
1040 -- @return Table containing the scheme properties
1041 function option.section(self)
1042 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1043 end
1044
1045 --- Construct an enum object instance from given or default value.
1046 -- @param v Value to select
1047 -- @return Enum instance for selected value
1048 function option.enum(self, val)
1049 return enum(
1050 self.s, self.c,
1051 self.cref[1], self.cref[2], self.cref[3],
1052 val or self:value()
1053 )
1054 end
1055
1056
1057 --- Object representation of a enum value.
1058 -- @class module
1059 -- @cstyle instance
1060 -- @name luci.uvl.enum
1061
1062 --- Section instance constructor.
1063 -- @class function
1064 -- @name enum
1065 -- @param scheme Scheme instance
1066 -- @param co Configuration data
1067 -- @param c Configuration name
1068 -- @param s Section name
1069 -- @param o Enum name
1070 -- @param v Enum value
1071 -- @return Enum value instance
1072 enum = luci.util.class(option)
1073
1074 function enum.__init__(self, scheme, co, c, s, o, v)
1075 self.cref = { c, s, o, v }
1076 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1077 self.c = self:_loadconf(co, c)
1078 self.s = scheme
1079 self.t = luci.uvl.TYPE_ENUM
1080 end