again
[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 = unpack
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 end
338
339 if not nodeps then
340 local ok, err = dependencies.check( self, option )
341 if not ok then
342 option:error(err)
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 elseif k == "required" then
583 t[k] = _bool(v2)
584 else
585 t[k] = t[k] or v2
586 end
587 end
588 end
589
590 t.type = t.type or "variable"
591 t.datatype = t.datatype or "string"
592 t.required = t.required or false
593 end
594
595 -- Step 3: get all enums
596 function UVL._parse_enum(self, scheme, k, v)
597 local ok, err = _req( TYPE_ENUM, k, v, { "value", "variable" } )
598 if err then error(scheme:error(err)) end
599
600 local r, err = _ref( TYPE_ENUM, v )
601 if err then error(scheme:error(err)) end
602
603 local p = self.packages[r[1]]
604 if not p then
605 error(scheme:error(
606 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
607 ))
608 end
609
610 local s = p.variables[r[2]]
611 if not s then
612 error(scheme:error(
613 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
614 ))
615 end
616
617 local t = s[r[3]]
618 if not t then
619 error(scheme:error(
620 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
621 ))
622 end
623
624
625 local so = scheme:section(r[2])
626 local oo = so:option(r[3])
627 local eo = oo:enum(v.value)
628
629 if t.type ~= "enum" and t.type ~= "reference" then
630 error(scheme:error(ERR.SME_EBADTYPE(eo)))
631 end
632
633 if not t.values then
634 t.values = { [v.value] = v.title or v.value }
635 else
636 t.values[v.value] = v.title or v.value
637 end
638
639 if not t.enum_depends then
640 t.enum_depends = { }
641 end
642
643 if v.default then
644 if t.default then
645 error(scheme:error(ERR.SME_EBADDEF(eo)))
646 end
647 t.default = v.value
648 end
649
650 if v.depends then
651 t.enum_depends[v.value] = self:_read_dependency(
652 v.depends, t.enum_depends[v.value]
653 )
654
655 if not t.enum_depends[v.value] then
656 error(scheme:error(so:error(oo:error(
657 ERR.SME_BADDEP(eo, util.serialize_data(v.depends))
658 ))))
659 end
660 end
661 end
662
663 -- Read a dependency specification
664 function UVL._read_dependency( self, values, deps )
665 local expr = "%$?[a-zA-Z0-9_]+"
666 if values then
667 values = ( type(values) == "table" and values or { values } )
668 for _, value in ipairs(values) do
669 local condition = { }
670 for val in value:gmatch("[^%s,]+") do
671 local k, v = val:match("([^%s]+)%s*=*%s*([^%s]*)")
672
673 if k and (
674 k:match("^"..expr.."%."..expr.."%."..expr.."$") or
675 k:match("^"..expr.."%."..expr.."$") or
676 k:match("^"..expr.."$")
677 ) then
678 condition[k] = v or true
679 else
680 return nil
681 end
682 end
683
684 if not deps then
685 deps = { condition }
686 else
687 deps[#deps+1] = condition
688 end
689 end
690 end
691
692 return deps
693 end
694
695 -- Read a validator specification
696 function UVL._read_validator( self, values, validators )
697 if values then
698 values = ( type(values) == "table" and values or { values } )
699 for _, value in ipairs(values) do
700 local validator
701
702 if value:match("^exec:") then
703 validator = value:gsub("^exec:","")
704 elseif value:match("^lua:") then
705 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
706 elseif value:match("^regexp:") then
707 local pattern = value:gsub("^regexp:","")
708 validator = function( type, dtype, pack, sect, optn, ... )
709 local values = { ... }
710 for _, v in ipairs(values) do
711 local ok, match =
712 pcall( string.match, v, pattern )
713
714 if not ok then
715 return false, match
716 elseif not match then
717 return false,
718 'Value "%s" does not match pattern "%s"' % {
719 v, pattern
720 }
721 end
722 end
723 return true
724 end
725 end
726
727 if validator then
728 if not validators then
729 validators = { validator }
730 else
731 validators[#validators+1] = validator
732 end
733 else
734 return nil
735 end
736 end
737
738 return validators
739 end
740 end
741
742 -- Read a reference specification (XXX: We should validate external configs too...)
743 function UVL._read_reference( self, values )
744 local val = { }
745 values = ( type(values) == "table" and values or { values } )
746
747 for _, value in ipairs(values) do
748 local ref = util.split(value, ".")
749
750 if #ref == 2 or #ref == 3 then
751 local co = config( self, ref[1] )
752 if not co:config() then return false, co:errors() end
753
754 for k, v in pairs(co:config()) do
755 if v['.type'] == ref[2] then
756 if #ref == 2 then
757 if v['.anonymous'] == true then
758 return false, ERR.SME_INVREF('', value)
759 end
760 val[k] = k -- XXX: title/description would be nice
761 elseif v[ref[3]] then
762 val[v[ref[3]]] = v[ref[3]] -- XXX: dito
763 end
764 end
765 end
766 else
767 return false, ERR.SME_BADREF('', value)
768 end
769 end
770
771 return val, nil
772 end
773
774 -- Resolve given path
775 function UVL._resolve_function( self, value )
776 local path = util.split(value, ".")
777
778 for i=1, #path-1 do
779 local stat, mod = pcall(
780 require, table.concat(path, ".", 1, i)
781 )
782
783 if stat and mod then
784 for j=i+1, #path-1 do
785 if not type(mod) == "table" then
786 break
787 end
788 mod = mod[path[j]]
789 if not mod then
790 break
791 end
792 end
793 mod = type(mod) == "table" and mod[path[#path]] or nil
794 if type(mod) == "function" then
795 return mod
796 end
797 end
798 end
799 end
800
801
802 --- Object representation of an uvl item - base class.
803 uvlitem = util.class()
804
805 function uvlitem.cid(self)
806 if #self.cref == 1 then
807 return self.cref[1]
808 else
809 local r = { unpack(self.cref) }
810 local c = self.c
811 if c and c[r[2]] and c[r[2]]['.anonymous'] and c[r[2]]['.index'] then
812 r[2] = '@' .. c[r[2]]['.type'] ..
813 '[' .. tostring(c[r[2]]['.index']) .. ']'
814 end
815 return table.concat( r, '.' )
816 end
817 end
818
819 function uvlitem.sid(self)
820 return table.concat( self.sref, '.' )
821 end
822
823 function uvlitem.scheme(self, opt)
824 local s = self.s and self.s.packages
825 s = s and s[self.sref[1]]
826 if #self.sref == 4 or #self.sref == 3 then
827 s = s and s.variables
828 s = s and s[self.sref[2]]
829 s = s and s[self.sref[3]]
830 elseif #self.sref == 2 then
831 s = s and s.sections
832 s = s and s[self.sref[2]]
833 end
834
835 if s and opt then
836 return s[opt]
837 elseif s then
838 return s
839 end
840 end
841
842 function uvlitem.config(self, opt)
843 local c = self.c
844
845 if #self.cref >= 2 and #self.cref <= 4 then
846 c = c and self.c[self.cref[2]] or nil
847 if #self.cref >= 3 then
848 c = c and c[self.cref[3]] or nil
849 end
850 end
851
852 if c and opt then
853 return c[opt]
854 elseif c then
855 return c
856 end
857 end
858
859 function uvlitem.title(self)
860 return self:scheme() and self:scheme('title') or
861 self.cref[3] or self.cref[2] or self.cref[1]
862 end
863
864 function uvlitem.type(self)
865 if self.t == TYPE_CONFIG then
866 return 'config'
867 elseif self.t == TYPE_SECTION then
868 return 'section'
869 elseif self.t == TYPE_OPTION then
870 return 'option'
871 elseif self.t == TYPE_ENUM then
872 return 'enum'
873 end
874 end
875
876 function uvlitem.error(self, ...)
877 if not self.e then
878 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
879 self.e = errconst[#self.cref]( self )
880 end
881
882 return self.e:child( ... )
883 end
884
885 function uvlitem.errors(self)
886 return self.e
887 end
888
889 function uvlitem.ok(self)
890 return not self:errors()
891 end
892
893 function uvlitem.parent(self)
894 if self.p then
895 return self.p
896 elseif #self.cref == 3 or #self.cref == 4 then
897 return section( self.s, self.c, self.cref[1], self.cref[2] )
898 elseif #self.cref == 2 then
899 return config( self.s, self.c, self.cref[1] )
900 else
901 return nil
902 end
903 end
904
905 function uvlitem._loadconf(self, co, c)
906 co = co or self._configcache
907 if not co then
908 local err
909 co, err = uci.cursor():get_all(c)
910
911 if err then
912 self:error(ERR.UCILOAD(self, err))
913 end
914
915 self._configcache = co
916 end
917 return co
918 end
919
920
921 --- Object representation of a scheme.
922 -- @class scheme
923 -- @cstyle instance
924 -- @name luci.uvl.scheme
925
926 --- Scheme instance constructor.
927 -- @class function
928 -- @name scheme
929 -- @param scheme Scheme instance
930 -- @param co Configuration data
931 -- @param c Configuration name
932 -- @return Config instance
933 scheme = util.class(uvlitem)
934
935 function scheme.__init__(self, scheme, co, c)
936 if not c then
937 c, co = co, nil
938 end
939
940 self.cref = { c }
941 self.sref = { c }
942 self.c = self:_loadconf(co, c)
943 self.s = scheme
944 self.t = TYPE_SCHEME
945 end
946
947 --- Add an error to scheme.
948 -- @return Scheme error context
949 function scheme.error(self, ...)
950 if not self.e then self.e = ERR.SCHEME( self ) end
951 return self.e:child( ... )
952 end
953
954 --- Get an associated config object.
955 -- @return Config instance
956 function scheme.config(self)
957 local co = config( self.s, self.cref[1] )
958 co.p = self
959
960 return co
961 end
962
963 --- Get all section objects associated with this scheme.
964 -- @return Table containing all associated luci.uvl.section instances
965 function scheme.sections(self)
966 local v = { }
967 if self.s.packages[self.sref[1]].sections then
968 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
969 v[#v+1] = option(
970 self.s, self.c, self.cref[1], self.cref[2], o
971 )
972 end
973 end
974 return v
975 end
976
977 --- Get an associated section object.
978 -- @param s Section to select
979 -- @return Section instance
980 function scheme.section(self, s)
981 local so = section( self.s, self.c, self.cref[1], s )
982 so.p = self
983
984 return so
985 end
986
987
988 --- Object representation of a config.
989 -- @class config
990 -- @cstyle instance
991 -- @name luci.uvl.config
992
993 --- Config instance constructor.
994 -- @class function
995 -- @name config
996 -- @param scheme Scheme instance
997 -- @param co Configuration data
998 -- @param c Configuration name
999 -- @return Config instance
1000 config = util.class(uvlitem)
1001
1002 function config.__init__(self, scheme, co, c)
1003 if not c then
1004 c, co = co, nil
1005 end
1006
1007 self.cref = { c }
1008 self.sref = { c }
1009 self.c = self:_loadconf(co, c)
1010 self.s = scheme
1011 self.t = TYPE_CONFIG
1012 end
1013
1014 --- Get all section objects associated with this config.
1015 -- @return Table containing all associated luci.uvl.section instances
1016 function config.sections(self)
1017 local v = { }
1018 if self.s.packages[self.sref[1]].sections then
1019 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1020 v[#v+1] = option(
1021 self.s, self.c, self.cref[1], self.cref[2], o
1022 )
1023 end
1024 end
1025 return v
1026 end
1027
1028 --- Get an associated section object.
1029 -- @param s Section to select
1030 -- @return Section instance
1031 function config.section(self, s)
1032 local so = section( self.s, self.c, self.cref[1], s )
1033 so.p = self
1034
1035 return so
1036 end
1037
1038
1039 --- Object representation of a scheme/config section.
1040 -- @class module
1041 -- @cstyle instance
1042 -- @name luci.uvl.section
1043
1044 --- Section instance constructor.
1045 -- @class function
1046 -- @name section
1047 -- @param scheme Scheme instance
1048 -- @param co Configuration data
1049 -- @param c Configuration name
1050 -- @param s Section name
1051 -- @return Section instance
1052 section = util.class(uvlitem)
1053
1054 function section.__init__(self, scheme, co, c, s)
1055 self.cref = { c, s }
1056 self.sref = { c, co and co[s] and co[s]['.type'] or s }
1057 self.c = self:_loadconf(co, c)
1058 self.s = scheme
1059 self.t = TYPE_SECTION
1060 end
1061
1062 --- Get all option objects associated with this section.
1063 -- @return Table containing all associated luci.uvl.option instances
1064 function section.variables(self)
1065 local v = { }
1066 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
1067 for o, _ in pairs(
1068 self.s.packages[self.sref[1]].variables[self.sref[2]]
1069 ) do
1070 v[#v+1] = option(
1071 self.s, self.c, self.cref[1], self.cref[2], o
1072 )
1073 end
1074 end
1075 return v
1076 end
1077
1078 --- Get an associated option object.
1079 -- @param o Option to select
1080 -- @return Option instance
1081 function section.option(self, o)
1082 local oo = option( self.s, self.c, self.cref[1], self.cref[2], o )
1083 oo.p = self
1084
1085 return oo
1086 end
1087
1088
1089 --- Object representation of a scheme/config option.
1090 -- @class module
1091 -- @cstyle instance
1092 -- @name luci.uvl.option
1093
1094 --- Section instance constructor.
1095 -- @class function
1096 -- @name option
1097 -- @param scheme Scheme instance
1098 -- @param co Configuration data
1099 -- @param c Configuration name
1100 -- @param s Section name
1101 -- @param o Option name
1102 -- @return Option instance
1103 option = util.class(uvlitem)
1104
1105 function option.__init__(self, scheme, co, c, s, o)
1106 self.cref = { c, s, o }
1107 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1108 self.c = self:_loadconf(co, c)
1109 self.s = scheme
1110 self.t = TYPE_OPTION
1111 end
1112
1113 --- Get the value of this option.
1114 -- @return The associated configuration value
1115 function option.value(self)
1116 local v = self:config() or self:scheme('default')
1117 if v and self:scheme('multival') then
1118 v = util.split( v, "%s+", nil, true )
1119 end
1120 return v
1121 end
1122
1123 --- Get the associated section information in scheme.
1124 -- @return Table containing the scheme properties
1125 function option.section(self)
1126 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1127 end
1128
1129 --- Construct an enum object instance from given or default value.
1130 -- @param v Value to select
1131 -- @return Enum instance for selected value
1132 function option.enum(self, val)
1133 return enum(
1134 self.s, self.c,
1135 self.cref[1], self.cref[2], self.cref[3],
1136 val or self:value()
1137 )
1138 end
1139
1140
1141 --- Object representation of a enum value.
1142 -- @class module
1143 -- @cstyle instance
1144 -- @name luci.uvl.enum
1145
1146 --- Section instance constructor.
1147 -- @class function
1148 -- @name enum
1149 -- @param scheme Scheme instance
1150 -- @param co Configuration data
1151 -- @param c Configuration name
1152 -- @param s Section name
1153 -- @param o Enum name
1154 -- @param v Enum value
1155 -- @return Enum value instance
1156 enum = util.class(option)
1157
1158 function enum.__init__(self, scheme, co, c, s, o, v)
1159 self.cref = { c, s, o, v }
1160 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1161 self.c = self:_loadconf(co, c)
1162 self.s = scheme
1163 self.t = TYPE_ENUM
1164 end