Backported UVL optimizations
[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 if not nodeps then
339 local ok, err = dependencies.check( self, option )
340 if not ok then
341 option:error(err)
342 end
343 end
344 end
345
346 local ok, err = validation.check( self, option )
347 if not ok and STRICT_EXTERNAL_VALIDATORS then
348 return false, option:error(err)
349 end
350 end
351
352 return option:ok(), option:errors()
353 end
354
355 --- Find all parts of given scheme and construct validation tree.
356 -- This is normally done on demand, so you don't have to call this function
357 -- by yourself.
358 -- @param shm Name of the scheme to parse
359 -- @param alias Create an alias for the loaded scheme
360 function UVL.read_scheme( self, shm, alias )
361
362 local so = scheme( self, shm )
363 local bc = "%s/bytecode/%s.lua" %{ self.schemedir, shm }
364
365 if not fs.access(bc) then
366 local files = fs.glob(self.schemedir .. '/*/' .. shm)
367
368 if files then
369 local ok, err
370 for i, file in ipairs( files ) do
371 if not fs.access(file) then
372 return false, so:error(ERR.SME_READ(so,file))
373 end
374
375 local uci = uci.cursor( fs.dirname(file), default_savedir )
376
377 local sname = fs.basename(file)
378 local sd, err = uci:load( sname )
379
380 if not sd then
381 return false, ERR.UCILOAD(so, err)
382 end
383
384 ok, err = pcall(function()
385 uci:foreach(sname, "package", function(s)
386 self:_parse_package(so, s[".name"], s)
387 end)
388 uci:foreach(sname, "section", function(s)
389 self:_parse_section(so, s[".name"], s)
390 end)
391 uci:foreach(sname, "variable", function(s)
392 self:_parse_var(so, s[".name"], s)
393 end)
394 uci:foreach(sname, "enum", function(s)
395 self:_parse_enum(so, s[".name"], s)
396 end)
397
398 end)
399 end
400
401 if ok and alias then self.packages[alias] = self.packages[shm] end
402 return ok and self, err
403 else
404 return false, so:error(ERR.SME_FIND(so, self.schemedir))
405 end
406 else
407 local sc = loadfile(bc)
408 if sc then
409 self.packages[shm] = sc()
410 return true
411 else
412 return false, so:error(ERR.SME_READ(so,bc))
413 end
414 end
415 end
416
417 -- helper function to check for required fields
418 local function _req( t, n, c, r )
419 for i, v in ipairs(r) do
420 if not c[v] then
421 local p, o = scheme:sid(), nil
422
423 if t == TYPE_SECTION then
424 o = section( scheme, nil, p, n )
425 elseif t == TYPE_OPTION then
426 o = option( scheme, nil, p, '(nil)', n )
427 elseif t == TYPE_ENUM then
428 o = enum( scheme, nil, p, '(nil)', '(nil)', n )
429 end
430
431 return false, ERR.SME_REQFLD(o,v)
432 end
433 end
434 return true
435 end
436
437 -- helper function to validate references
438 local function _ref( c, t )
439 local r, k, n = {}
440 if c == TYPE_SECTION then
441 k = "package"
442 n = 1
443 elseif c == TYPE_OPTION then
444 k = "section"
445 n = 2
446 elseif c == TYPE_ENUM then
447 k = "variable"
448 n = 3
449 end
450
451 for o in t[k]:gmatch("[^.]+") do
452 r[#r+1] = o
453 end
454 r[1] = ( #r[1] > 0 and r[1] or scheme:sid() )
455
456 if #r ~= n then
457 return false, ERR.SME_BADREF(scheme, k)
458 end
459
460 return r
461 end
462
463 -- helper function to read bools
464 local function _bool( v )
465 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
466 end
467
468 -- Step 0: get package meta information
469 function UVL._parse_package(self, scheme, k, v)
470 local sid = scheme:sid()
471 local pkg = self.packages[sid] or {
472 ["name"] = sid;
473 ["sections"] = { };
474 ["variables"] = { };
475 }
476
477 pkg.title = v.title
478 pkg.description = v.description
479
480 self.packages[sid] = pkg
481 end
482
483 -- Step 1: get all sections
484 function UVL._parse_section(self, scheme, k, v)
485 local ok, err = _req( TYPE_SECTION, k, v, { "name", "package" } )
486 if err then error(scheme:error(err)) end
487
488 local r, err = _ref( TYPE_SECTION, v )
489 if err then error(scheme:error(err)) end
490
491 local p = self.packages[r[1]] or {
492 ["name"] = r[1];
493 ["sections"] = { };
494 ["variables"] = { };
495 }
496 p.sections[v.name] = p.sections[v.name] or { }
497 p.variables[v.name] = p.variables[v.name] or { }
498 self.packages[r[1]] = p
499
500 local s = p.sections[v.name]
501 local so = scheme:section(v.name)
502
503 for k, v2 in pairs(v) do
504 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
505 if k == "depends" then
506 s.depends = self:_read_dependency( v2, s.depends )
507 if not s.depends then
508 return false, scheme:error(
509 ERR.SME_BADDEP(so, util.serialize_data(s.depends))
510 )
511 end
512 elseif k == "dynamic" or k == "unique" or
513 k == "required" or k == "named"
514 then
515 s[k] = _bool(v2)
516 else
517 s[k] = v2
518 end
519 end
520 end
521
522 s.dynamic = s.dynamic or false
523 s.unique = s.unique or false
524 s.required = s.required or false
525 s.named = s.named or false
526 end
527
528
529 -- Step 2: get all variables
530 function UVL._parse_var(self, scheme, k, v)
531 local ok, err = _req( TYPE_OPTION, k, v, { "name", "section" } )
532 if err then error(scheme:error(err)) end
533
534 local r, err = _ref( TYPE_OPTION, v )
535 if err then error(scheme:error(err)) end
536
537 local p = self.packages[r[1]]
538 if not p then
539 error(scheme:error(
540 ERR.SME_VBADPACK({scheme:sid(), '', v.name}, r[1])
541 ))
542 end
543
544 local s = p.variables[r[2]]
545 if not s then
546 error(scheme:error(
547 ERR.SME_VBADSECT({scheme:sid(), '', v.name}, r[2])
548 ))
549 end
550
551 s[v.name] = s[v.name] or { }
552
553 local t = s[v.name]
554 local so = scheme:section(r[2])
555 local to = so:option(v.name)
556
557 for k, v2 in pairs(v) do
558 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
559 if k == "depends" then
560 t.depends = self:_read_dependency( v2, t.depends )
561 if not t.depends then
562 error(scheme:error(so:error(
563 ERR.SME_BADDEP(to, util.serialize_data(v2))
564 )))
565 end
566 elseif k == "validator" then
567 t.validators = self:_read_validator( v2, t.validators )
568 if not t.validators then
569 error(scheme:error(so:error(
570 ERR.SME_BADVAL(to, util.serialize_data(v2))
571 )))
572 end
573 elseif k == "valueof" then
574 local values, err = self:_read_reference( v2 )
575 if err then
576 error(scheme:error(so:error(
577 ERR.REFERENCE(to, util.serialize_data(v2)):child(err)
578 )))
579 end
580 t.type = "reference"
581 t.values = values
582 t.valueof = type(v2) == "table" and v2 or {v2}
583 elseif k == "required" then
584 t[k] = _bool(v2)
585 else
586 t[k] = t[k] or v2
587 end
588 end
589 end
590
591 t.type = t.type or "variable"
592 t.datatype = t.datatype or "string"
593 t.required = t.required or false
594 end
595
596 -- Step 3: get all enums
597 function UVL._parse_enum(self, scheme, k, v)
598 local ok, err = _req( TYPE_ENUM, k, v, { "value", "variable" } )
599 if err then error(scheme:error(err)) end
600
601 local r, err = _ref( TYPE_ENUM, v )
602 if err then error(scheme:error(err)) end
603
604 local p = self.packages[r[1]]
605 if not p then
606 error(scheme:error(
607 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
608 ))
609 end
610
611 local s = p.variables[r[2]]
612 if not s then
613 error(scheme:error(
614 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
615 ))
616 end
617
618 local t = s[r[3]]
619 if not t then
620 error(scheme:error(
621 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
622 ))
623 end
624
625
626 local so = scheme:section(r[2])
627 local oo = so:option(r[3])
628 local eo = oo:enum(v.value)
629
630 if t.type ~= "enum" and t.type ~= "reference" then
631 error(scheme:error(ERR.SME_EBADTYPE(eo)))
632 end
633
634 if not t.values then
635 t.values = { [v.value] = v.title or v.value }
636 else
637 t.values[v.value] = v.title or v.value
638 end
639
640 if not t.enum_depends then
641 t.enum_depends = { }
642 end
643
644 if v.default then
645 if t.default then
646 error(scheme:error(ERR.SME_EBADDEF(eo)))
647 end
648 t.default = v.value
649 end
650
651 if v.depends then
652 t.enum_depends[v.value] = self:_read_dependency(
653 v.depends, t.enum_depends[v.value]
654 )
655
656 if not t.enum_depends[v.value] then
657 error(scheme:error(so:error(oo:error(
658 ERR.SME_BADDEP(eo, util.serialize_data(v.depends))
659 ))))
660 end
661 end
662 end
663
664 -- Read a dependency specification
665 function UVL._read_dependency( self, values, deps )
666 local expr = "%$?[%w_]+"
667 if values then
668 values = ( type(values) == "table" and values or { values } )
669 for _, value in ipairs(values) do
670 local condition = { }
671 for val in value:gmatch("[^,]+") do
672 local k, e, v = val:match("%s*([%w$_.]+)%s*(=?)%s*(.*)")
673
674 if k and (
675 k:match("^"..expr.."%."..expr.."%."..expr.."$") or
676 k:match("^"..expr.."%."..expr.."$") or
677 k:match("^"..expr.."$")
678 ) then
679 condition[k] = (e == '=') and v or true
680 else
681 return nil
682 end
683 end
684
685 if not deps then
686 deps = { condition }
687 else
688 deps[#deps+1] = condition
689 end
690 end
691 end
692
693 return deps
694 end
695
696 -- Read a validator specification
697 function UVL._read_validator( self, values, validators )
698 if values then
699 values = ( type(values) == "table" and values or { values } )
700 for _, value in ipairs(values) do
701 local validator
702
703 if value:match("^exec:") then
704 validator = value:gsub("^exec:","")
705 elseif value:match("^lua:") then
706 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
707 elseif value:match("^regexp:") then
708 local pattern = value:gsub("^regexp:","")
709 validator = function( type, dtype, pack, sect, optn, ... )
710 local values = { ... }
711 for _, v in ipairs(values) do
712 local ok, match =
713 pcall( string.match, v, pattern )
714
715 if not ok then
716 return false, match
717 elseif not match then
718 return false,
719 'Value "%s" does not match pattern "%s"' % {
720 v, pattern
721 }
722 end
723 end
724 return true
725 end
726 end
727
728 if validator then
729 if not validators then
730 validators = { validator }
731 else
732 validators[#validators+1] = validator
733 end
734 else
735 return nil
736 end
737 end
738
739 return validators
740 end
741 end
742
743 -- Read a reference specification (XXX: We should validate external configs too...)
744 function UVL._read_reference( self, values )
745 local val = { }
746 values = ( type(values) == "table" and values or { values } )
747
748 for _, value in ipairs(values) do
749 local ref = util.split(value, ".")
750
751 if #ref == 2 or #ref == 3 then
752 local co = config( self, ref[1] )
753 if not co:config() then return false, co:errors() end
754
755 for k, v in pairs(co:config()) do
756 if v['.type'] == ref[2] then
757 if #ref == 2 then
758 if v['.anonymous'] == true then
759 return false, ERR.SME_INVREF('', value)
760 end
761 val[k] = k -- XXX: title/description would be nice
762 elseif v[ref[3]] then
763 val[v[ref[3]]] = v[ref[3]] -- XXX: dito
764 end
765 end
766 end
767 else
768 return false, ERR.SME_BADREF('', value)
769 end
770 end
771
772 return val, nil
773 end
774
775 -- Resolve given path
776 function UVL._resolve_function( self, value )
777 local path = util.split(value, ".")
778
779 for i=1, #path-1 do
780 local stat, mod = pcall(
781 require, table.concat(path, ".", 1, i)
782 )
783
784 if stat and mod then
785 for j=i+1, #path-1 do
786 if not type(mod) == "table" then
787 break
788 end
789 mod = mod[path[j]]
790 if not mod then
791 break
792 end
793 end
794 mod = type(mod) == "table" and mod[path[#path]] or nil
795 if type(mod) == "function" then
796 return mod
797 end
798 end
799 end
800 end
801
802
803 --- Object representation of an uvl item - base class.
804 uvlitem = util.class()
805
806 function uvlitem.cid(self)
807 if #self.cref == 1 then
808 return self.cref[1]
809 else
810 local r = { unpack(self.cref) }
811 local c = self.c
812 if c and c[r[2]] and c[r[2]]['.anonymous'] and c[r[2]]['.index'] then
813 r[2] = '@' .. c[r[2]]['.type'] ..
814 '[' .. tostring(c[r[2]]['.index']) .. ']'
815 end
816 return table.concat( r, '.' )
817 end
818 end
819
820 function uvlitem.sid(self)
821 return table.concat( self.sref, '.' )
822 end
823
824 function uvlitem.scheme(self, opt)
825 local s = self.s and self.s.packages
826 s = s and s[self.sref[1]]
827 if #self.sref == 4 or #self.sref == 3 then
828 s = s and s.variables
829 s = s and s[self.sref[2]]
830 s = s and s[self.sref[3]]
831 elseif #self.sref == 2 then
832 s = s and s.sections
833 s = s and s[self.sref[2]]
834 end
835
836 if s and opt then
837 return s[opt]
838 elseif s then
839 return s
840 end
841 end
842
843 function uvlitem.config(self, opt)
844 local c = self.c
845
846 if #self.cref >= 2 and #self.cref <= 4 then
847 c = c and self.c[self.cref[2]] or nil
848 if #self.cref >= 3 then
849 c = c and c[self.cref[3]] or nil
850 end
851 end
852
853 if c and opt then
854 return c[opt]
855 elseif c then
856 return c
857 end
858 end
859
860 function uvlitem.title(self)
861 return self:scheme() and self:scheme('title') or
862 self.cref[3] or self.cref[2] or self.cref[1]
863 end
864
865 function uvlitem.type(self)
866 if self.t == TYPE_CONFIG then
867 return 'config'
868 elseif self.t == TYPE_SECTION then
869 return 'section'
870 elseif self.t == TYPE_OPTION then
871 return 'option'
872 elseif self.t == TYPE_ENUM then
873 return 'enum'
874 end
875 end
876
877 function uvlitem.error(self, ...)
878 if not self.e then
879 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
880 self.e = errconst[#self.cref]( self )
881 end
882
883 return self.e:child( ... )
884 end
885
886 function uvlitem.errors(self)
887 return self.e
888 end
889
890 function uvlitem.ok(self)
891 return not self:errors()
892 end
893
894 function uvlitem.parent(self)
895 if self.p then
896 return self.p
897 elseif #self.cref == 3 or #self.cref == 4 then
898 return section( self.s, self.c, self.cref[1], self.cref[2] )
899 elseif #self.cref == 2 then
900 return config( self.s, self.c, self.cref[1] )
901 else
902 return nil
903 end
904 end
905
906 function uvlitem._loadconf(self, co, c)
907 co = co or self._configcache
908 if not co then
909 local err
910 co, err = uci.cursor():get_all(c)
911
912 if err then
913 self:error(ERR.UCILOAD(self, err))
914 end
915
916 self._configcache = co
917 end
918 return co
919 end
920
921
922 --- Object representation of a scheme.
923 -- @class scheme
924 -- @cstyle instance
925 -- @name luci.uvl.scheme
926
927 --- Scheme instance constructor.
928 -- @class function
929 -- @name scheme
930 -- @param scheme Scheme instance
931 -- @param co Configuration data
932 -- @param c Configuration name
933 -- @return Config instance
934 scheme = util.class(uvlitem)
935
936 function scheme.__init__(self, scheme, co, c)
937 if not c then
938 c, co = co, nil
939 end
940
941 self.cref = { c }
942 self.sref = { c }
943 self.c = self:_loadconf(co, c)
944 self.s = scheme
945 self.t = TYPE_SCHEME
946 end
947
948 --- Add an error to scheme.
949 -- @return Scheme error context
950 function scheme.error(self, ...)
951 if not self.e then self.e = ERR.SCHEME( self ) end
952 return self.e:child( ... )
953 end
954
955 --- Get an associated config object.
956 -- @return Config instance
957 function scheme.config(self)
958 local co = config( self.s, self.cref[1] )
959 co.p = self
960
961 return co
962 end
963
964 --- Get all section objects associated with this scheme.
965 -- @return Table containing all associated luci.uvl.section instances
966 function scheme.sections(self)
967 local v = { }
968 if self.s.packages[self.sref[1]].sections then
969 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
970 v[#v+1] = option(
971 self.s, self.c, self.cref[1], self.cref[2], o
972 )
973 end
974 end
975 return v
976 end
977
978 --- Get an associated section object.
979 -- @param s Section to select
980 -- @return Section instance
981 function scheme.section(self, s)
982 local so = section( self.s, self.c, self.cref[1], s )
983 so.p = self
984
985 return so
986 end
987
988
989 --- Object representation of a config.
990 -- @class config
991 -- @cstyle instance
992 -- @name luci.uvl.config
993
994 --- Config instance constructor.
995 -- @class function
996 -- @name config
997 -- @param scheme Scheme instance
998 -- @param co Configuration data
999 -- @param c Configuration name
1000 -- @return Config instance
1001 config = util.class(uvlitem)
1002
1003 function config.__init__(self, scheme, co, c)
1004 if not c then
1005 c, co = co, nil
1006 end
1007
1008 self.cref = { c }
1009 self.sref = { c }
1010 self.c = self:_loadconf(co, c)
1011 self.s = scheme
1012 self.t = TYPE_CONFIG
1013 end
1014
1015 --- Get all section objects associated with this config.
1016 -- @return Table containing all associated luci.uvl.section instances
1017 function config.sections(self)
1018 local v = { }
1019 if self.s.packages[self.sref[1]].sections then
1020 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1021 v[#v+1] = option(
1022 self.s, self.c, self.cref[1], self.cref[2], o
1023 )
1024 end
1025 end
1026 return v
1027 end
1028
1029 --- Get an associated section object.
1030 -- @param s Section to select
1031 -- @return Section instance
1032 function config.section(self, s)
1033 local so = section( self.s, self.c, self.cref[1], s )
1034 so.p = self
1035
1036 return so
1037 end
1038
1039
1040 --- Object representation of a scheme/config section.
1041 -- @class module
1042 -- @cstyle instance
1043 -- @name luci.uvl.section
1044
1045 --- Section instance constructor.
1046 -- @class function
1047 -- @name section
1048 -- @param scheme Scheme instance
1049 -- @param co Configuration data
1050 -- @param c Configuration name
1051 -- @param s Section name
1052 -- @return Section instance
1053 section = util.class(uvlitem)
1054
1055 function section.__init__(self, scheme, co, c, s)
1056 self.cref = { c, s }
1057 self.sref = { c, co and co[s] and co[s]['.type'] or s }
1058 self.c = self:_loadconf(co, c)
1059 self.s = scheme
1060 self.t = TYPE_SECTION
1061 end
1062
1063 --- Get all option objects associated with this section.
1064 -- @return Table containing all associated luci.uvl.option instances
1065 function section.variables(self)
1066 local v = { }
1067 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
1068 for o, _ in pairs(
1069 self.s.packages[self.sref[1]].variables[self.sref[2]]
1070 ) do
1071 v[#v+1] = option(
1072 self.s, self.c, self.cref[1], self.cref[2], o
1073 )
1074 end
1075 end
1076 return v
1077 end
1078
1079 --- Get an associated option object.
1080 -- @param o Option to select
1081 -- @return Option instance
1082 function section.option(self, o)
1083 local oo = option( self.s, self.c, self.cref[1], self.cref[2], o )
1084 oo.p = self
1085
1086 return oo
1087 end
1088
1089
1090 --- Object representation of a scheme/config option.
1091 -- @class module
1092 -- @cstyle instance
1093 -- @name luci.uvl.option
1094
1095 --- Section instance constructor.
1096 -- @class function
1097 -- @name option
1098 -- @param scheme Scheme instance
1099 -- @param co Configuration data
1100 -- @param c Configuration name
1101 -- @param s Section name
1102 -- @param o Option name
1103 -- @return Option instance
1104 option = util.class(uvlitem)
1105
1106 function option.__init__(self, scheme, co, c, s, o)
1107 self.cref = { c, s, o }
1108 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1109 self.c = self:_loadconf(co, c)
1110 self.s = scheme
1111 self.t = TYPE_OPTION
1112 end
1113
1114 --- Get the value of this option.
1115 -- @return The associated configuration value
1116 function option.value(self)
1117 local v = self:config() or self:scheme('default')
1118 if v and self:scheme('multival') then
1119 v = util.split( v, "%s+", nil, true )
1120 end
1121 return v
1122 end
1123
1124 --- Get the associated section information in scheme.
1125 -- @return Table containing the scheme properties
1126 function option.section(self)
1127 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1128 end
1129
1130 --- Construct an enum object instance from given or default value.
1131 -- @param v Value to select
1132 -- @return Enum instance for selected value
1133 function option.enum(self, val)
1134 return enum(
1135 self.s, self.c,
1136 self.cref[1], self.cref[2], self.cref[3],
1137 val or self:value()
1138 )
1139 end
1140
1141
1142 --- Object representation of a enum value.
1143 -- @class module
1144 -- @cstyle instance
1145 -- @name luci.uvl.enum
1146
1147 --- Section instance constructor.
1148 -- @class function
1149 -- @name enum
1150 -- @param scheme Scheme instance
1151 -- @param co Configuration data
1152 -- @param c Configuration name
1153 -- @param s Section name
1154 -- @param o Enum name
1155 -- @param v Enum value
1156 -- @return Enum value instance
1157 enum = util.class(option)
1158
1159 function enum.__init__(self, scheme, co, c, s, o, v)
1160 self.cref = { c, s, o, v }
1161 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1162 self.c = self:_loadconf(co, c)
1163 self.s = scheme
1164 self.t = TYPE_ENUM
1165 end