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