24dc3a25d2996d0e7aff470d4846526d15ebbd8f
[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 ERR = luci.uvl.errors
59
60
61 --- Object constructor
62 -- @class function
63 -- @name UVL
64 -- @param schemedir Path to the scheme directory (optional)
65 -- @return Instance object
66 UVL = luci.util.class()
67
68 function UVL.__init__( self, schemedir )
69 self.schemedir = schemedir or default_schemedir
70 self.packages = { }
71 self.beenthere = { }
72 self.uci = luci.model.uci
73 self.err = luci.uvl.errors
74 self.dep = luci.uvl.dependencies
75 self.datatypes = luci.uvl.datatypes
76 end
77
78
79 --- Parse given scheme and return the scheme tree.
80 -- @param scheme Name of the scheme to parse
81 -- @return Table containing the parsed scheme or nil on error
82 -- @return String containing the reason for errors (if any)
83 function UVL.get_scheme( self, scheme )
84 if not self.packages[scheme] then
85 local ok, err = self:read_scheme( scheme )
86 if not ok then
87 return nil, err
88 end
89 end
90 return self.packages[scheme], nil
91 end
92
93 --- Validate given configuration, section or option.
94 -- @param config Name of the configuration to validate
95 -- @param section Name of the section to validate (optional)
96 -- @param option Name of the option to validate (optional)
97 -- @return Boolean indicating whether the given config validates
98 -- @return String containing the reason for errors (if any)
99 function UVL.validate( self, config, section, option )
100 if config and section and option then
101 return self:validate_option( config, section, option )
102 elseif config and section then
103 return self:validate_section( config, section )
104 elseif config then
105 return self:validate_config( config )
106 end
107 end
108
109 --- Validate given configuration.
110 -- @param config Name of the configuration to validate
111 -- @return Boolean indicating whether the given config validates
112 -- @return String containing the reason for errors (if any)
113 function UVL.validate_config( self, config, uci )
114
115 if not self.packages[config] then
116 local ok, err = self:read_scheme(config)
117 if not ok then
118 return false, err
119 end
120 end
121
122 local co = luci.uvl.config( self, uci or config, uci and config )
123 local sc = { }
124
125 self.beenthere = { }
126
127 if not co:config() then
128 return false, ERR.UCILOAD(co)
129 end
130
131 local function _uci_foreach( type, func )
132 for k, v in pairs(co:config()) do
133 if v['.type'] == type then
134 sc[type] = sc[type] + 1
135 local ok, err = func( k, v )
136 if not ok then co:error(err) end
137 end
138 end
139 end
140
141 for k, v in pairs( self.packages[config].sections ) do
142 sc[k] = 0
143 _uci_foreach( k,
144 function(s)
145 return self:_validate_section( co:section(s) )
146 end
147 )
148 end
149
150 if STRICT_UNKNOWN_SECTIONS then
151 for k, v in pairs(co:config()) do
152 local so = co:section(k)
153 if not self.beenthere[so:cid()] then
154 co:error(ERR.SECT_UNKNOWN(so))
155 end
156 end
157 end
158
159 for _, k in ipairs(luci.util.keys(sc)) do
160 local so = co:section(k)
161 if so:scheme('required') and sc[k] == 0 then
162 co:error(ERR.SECT_REQUIRED(so))
163 elseif so:scheme('unique') and sc[k] > 1 then
164 co:error(ERR.SECT_UNIQUE(so))
165 end
166 end
167
168 return co:ok(), co:errors()
169 end
170
171 --- Validate given config section.
172 -- @param config Name of the configuration to validate
173 -- @param section Name of the section to validate
174 -- @return Boolean indicating whether the given config validates
175 -- @return String containing the reason for errors (if any)
176 function UVL.validate_section( self, config, section, uci )
177
178 if not self.packages[config] then
179 local ok, err = self:read_scheme( config )
180 if not ok then
181 return false, err
182 end
183 end
184
185 local co = luci.uvl.config( self, uci or config, uci and config )
186 local so = co:section( section )
187
188 self.beenthere = { }
189
190 if not co:config() then
191 return false, ERR.UCILOAD(co)
192 end
193
194 if so:config() then
195 return self:_validate_section( so )
196 else
197 return false, ERR.SECT_NOTFOUND(so)
198 end
199 end
200
201 --- Validate given config option.
202 -- @param config Name of the configuration to validate
203 -- @param section Name of the section to validate
204 -- @param option Name of the option to validate
205 -- @return Boolean indicating whether the given config validates
206 -- @return String containing the reason for errors (if any)
207 function UVL.validate_option( self, config, section, option, uci )
208
209 if not self.packages[config] then
210 local ok, err = self:read_scheme( config )
211 if not ok then
212 return false, err
213 end
214 end
215
216 local co = luci.uvl.config( self, uci or config, uci and config )
217 local so = co:section( section )
218 local oo = so:option( option )
219
220 if not co then
221 return false, oerr:child(ERR.UCILOAD(config))
222 end
223
224 if so:config() and oo:config() then
225 return self:_validate_option( oo )
226 else
227 return false, ERR.OPT_NOTFOUND(oo)
228 end
229 end
230
231
232 function UVL._validate_section( self, section )
233
234 if section:config() then
235 if section:scheme('named') == true and
236 section:config('.anonymous') == true
237 then
238 return false, ERR.SECT_NAMED(section)
239 end
240
241 for _, v in ipairs(section:variables()) do
242 local ok, err = self:_validate_option( v )
243 if not ok then
244 section:error(err)
245 end
246 end
247
248 local ok, err = luci.uvl.dependencies.check( self, section )
249 if not ok then
250 section:error(err)
251 end
252 else
253 return false, ERR.SECT_NOTFOUND(section)
254 end
255
256 if STRICT_UNKNOWN_OPTIONS and not section:scheme('dynamic') then
257 for k, v in pairs(section:config()) do
258 local oo = section:option(k)
259 if k:sub(1,1) ~= "." and not self.beenthere[oo:cid()] then
260 section:error(ERR.OPT_NOTFOUND(oo))
261 end
262 end
263 end
264
265 return section:ok(), section:errors()
266 end
267
268 function UVL._validate_option( self, option, nodeps )
269
270 if not option:scheme() and not option:parent():scheme('dynamic') then
271 return false, option:error(ERR.OPT_UNKNOWN(option))
272
273 elseif option:scheme() then
274 if option:scheme('required') and not option:value() then
275 return false, option:error(ERR.OPT_REQUIRED(option))
276
277 elseif option:value() then
278 local val = option:value()
279
280 if option:scheme('type') == "reference" or
281 option:scheme('type') == "enum"
282 then
283 if not option:scheme('values') or
284 not option:scheme('values')[val]
285 then
286 return false, option:error( ERR.OPT_BADVALUE(
287 option, { val, table.concat(
288 luci.util.keys(option:scheme('values') or {}), ", "
289 ) }
290 ) )
291 end
292 elseif option:scheme('type') == "list" then
293 if type(val) ~= "table" and STRICT_LIST_TYPE then
294 return false, option:error(ERR.OPT_NOTLIST(option))
295 end
296 elseif option:scheme('datatype') then
297 local dt = option:scheme('datatype')
298
299 if self.datatypes[dt] then
300 val = ( type(val) == "table" and val or { val } )
301 for i, v in ipairs(val) do
302 if not self.datatypes[dt]( v ) then
303 return false, option:error(
304 ERR.OPT_INVVALUE(option, {v, dt})
305 )
306 end
307 end
308 else
309 return false, option:error(ERR.OPT_DATATYPE(option, dt))
310 end
311 end
312 end
313
314 if not nodeps then
315 local ok, err = luci.uvl.dependencies.check( self, option )
316 if not ok then
317 option:error(err)
318 end
319 end
320
321 local ok, err = luci.uvl.validation.check( self, option )
322 if not ok and STRICT_EXTERNAL_VALIDATORS then
323 return false, option:error(err)
324 end
325 end
326
327 return option:ok(), option:errors()
328 end
329
330 --- Find all parts of given scheme and construct validation tree.
331 -- This is normally done on demand, so you don't have to call this function
332 -- by yourself.
333 -- @param scheme Name of the scheme to parse
334 function UVL.read_scheme( self, scheme )
335
336 local so = luci.uvl.scheme( self, scheme )
337
338 local schemes = { }
339 local files = luci.fs.glob(self.schemedir .. '/*/' .. scheme)
340
341 if files then
342 for i, file in ipairs( files ) do
343 if not luci.fs.access(file) then
344 return so:error(ERR.SME_READ(so,file))
345 end
346
347 local uci = luci.model.uci.cursor()
348 uci:set_confdir( luci.fs.dirname(file) )
349
350 local sd = uci:get_all( luci.fs.basename(file) )
351
352 if not sd then
353 return false, ERR.UCILOAD(so)
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, ERR.UCILOAD(ref[1]) 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
829 --- Object representation of a scheme.
830 -- @class scheme
831 -- @cstyle instance
832 -- @name luci.uvl.scheme
833
834 --- Scheme instance constructor.
835 -- @class function
836 -- @name scheme
837 -- @param scheme Scheme instance
838 -- @param co Configuration data
839 -- @param c Configuration name
840 -- @return Config instance
841 scheme = luci.util.class(uvlitem)
842
843 function scheme.__init__(self, scheme, co, c)
844 if not c then
845 c, co = co, nil
846 end
847
848 if not co then
849 local uci = luci.model.uci.cursor()
850 co = uci:get_all(c)
851 end
852
853 self.cref = { c }
854 self.sref = { c }
855 self.c = co
856 self.s = scheme
857 self.t = luci.uvl.TYPE_SCHEME
858 end
859
860 --- Add an error to scheme.
861 -- @return Scheme error context
862 function scheme.error(self, ...)
863 if not self.e then self.e = ERR.SCHEME( self ) end
864 return self.e:child( ... )
865 end
866
867 --- Get an associated config object.
868 -- @return Config instance
869 function scheme.config(self)
870 local co = luci.uvl.config( self.s, self.cref[1] )
871 co.p = self
872
873 return co
874 end
875
876 --- Get all section objects associated with this scheme.
877 -- @return Table containing all associated luci.uvl.section instances
878 function scheme.sections(self)
879 local v = { }
880 if self.s.packages[self.sref[1]].sections then
881 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
882 table.insert( v, luci.uvl.option(
883 self.s, self.c, self.cref[1], self.cref[2], o
884 ) )
885 end
886 end
887 return v
888 end
889
890 --- Get an associated section object.
891 -- @param s Section to select
892 -- @return Section instance
893 function scheme.section(self, s)
894 local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
895 so.p = self
896
897 return so
898 end
899
900
901 --- Object representation of a config.
902 -- @class config
903 -- @cstyle instance
904 -- @name luci.uvl.config
905
906 --- Config instance constructor.
907 -- @class function
908 -- @name config
909 -- @param scheme Scheme instance
910 -- @param co Configuration data
911 -- @param c Configuration name
912 -- @return Config instance
913 config = luci.util.class(uvlitem)
914
915 function config.__init__(self, scheme, co, c)
916 if not c then
917 c, co = co, nil
918 end
919
920 if not co then
921 local uci = luci.model.uci.cursor()
922 co = uci:get_all(c)
923 end
924
925 self.cref = { c }
926 self.sref = { c }
927 self.c = co
928 self.s = scheme
929 self.t = luci.uvl.TYPE_CONFIG
930 end
931
932 --- Get all section objects associated with this config.
933 -- @return Table containing all associated luci.uvl.section instances
934 function config.sections(self)
935 local v = { }
936 if self.s.packages[self.sref[1]].sections then
937 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
938 table.insert( v, luci.uvl.option(
939 self.s, self.c, self.cref[1], self.cref[2], o
940 ) )
941 end
942 end
943 return v
944 end
945
946 --- Get an associated section object.
947 -- @param s Section to select
948 -- @return Section instance
949 function config.section(self, s)
950 local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
951 so.p = self
952
953 return so
954 end
955
956
957 --- Object representation of a scheme/config section.
958 -- @class module
959 -- @cstyle instance
960 -- @name luci.uvl.section
961
962 --- Section instance constructor.
963 -- @class function
964 -- @name section
965 -- @param scheme Scheme instance
966 -- @param co Configuration data
967 -- @param c Configuration name
968 -- @param s Section name
969 -- @return Section instance
970 section = luci.util.class(uvlitem)
971
972 function section.__init__(self, scheme, co, c, s)
973 self.cref = { c, s }
974 self.sref = { c, co and co[s] and co[s]['.type'] or s }
975 self.c = co
976 self.s = scheme
977 self.t = luci.uvl.TYPE_SECTION
978 end
979
980 --- Get all option objects associated with this section.
981 -- @return Table containing all associated luci.uvl.option instances
982 function section.variables(self)
983 local v = { }
984 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
985 for o, _ in pairs(
986 self.s.packages[self.sref[1]].variables[self.sref[2]]
987 ) do
988 table.insert( v, luci.uvl.option(
989 self.s, self.c, self.cref[1], self.cref[2], o
990 ) )
991 end
992 end
993 return v
994 end
995
996 --- Get an associated option object.
997 -- @param o Option to select
998 -- @return Option instance
999 function section.option(self, o)
1000 local oo = luci.uvl.option( self.s, self.c, self.cref[1], self.cref[2], o )
1001 oo.p = self
1002
1003 return oo
1004 end
1005
1006
1007 --- Object representation of a scheme/config option.
1008 -- @class module
1009 -- @cstyle instance
1010 -- @name luci.uvl.option
1011
1012 --- Section instance constructor.
1013 -- @class function
1014 -- @name option
1015 -- @param scheme Scheme instance
1016 -- @param co Configuration data
1017 -- @param c Configuration name
1018 -- @param s Section name
1019 -- @param o Option name
1020 -- @return Option instance
1021 option = luci.util.class(uvlitem)
1022
1023 function option.__init__(self, scheme, co, c, s, o)
1024 self.cref = { c, s, o }
1025 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1026 self.c = co
1027 self.s = scheme
1028 self.t = luci.uvl.TYPE_OPTION
1029 end
1030
1031 --- Get the value of this option.
1032 -- @return The associated configuration value
1033 function option.value(self)
1034 return self:config()
1035 end
1036
1037 --- Get the associated section information in scheme.
1038 -- @return Table containing the scheme properties
1039 function option.section(self)
1040 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1041 end
1042
1043 --- Construct an enum object instance from given or default value.
1044 -- @param v Value to select
1045 -- @return Enum instance for selected value
1046 function option.enum(self, val)
1047 return enum(
1048 self.s, self.c,
1049 self.cref[1], self.cref[2], self.cref[3],
1050 val or self:value()
1051 )
1052 end
1053
1054
1055 --- Object representation of a enum value.
1056 -- @class module
1057 -- @cstyle instance
1058 -- @name luci.uvl.enum
1059
1060 --- Section instance constructor.
1061 -- @class function
1062 -- @name enum
1063 -- @param scheme Scheme instance
1064 -- @param co Configuration data
1065 -- @param c Configuration name
1066 -- @param s Section name
1067 -- @param o Enum name
1068 -- @param v Enum value
1069 -- @return Enum value instance
1070 enum = luci.util.class(option)
1071
1072 function enum.__init__(self, scheme, co, c, s, o, v)
1073 self.cref = { c, s, o, v }
1074 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1075 self.c = co
1076 self.s = scheme
1077 self.t = luci.uvl.TYPE_ENUM
1078 end