3 UCI Validation Layer - Main Library
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
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
11 http://www.apache.org/licenses/LICENSE-2.0
18 --- UVL - UCI Validation Layer
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"
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
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"
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
45 --- Boolean; default true;
46 -- treat sections found in config but not in scheme as error
47 STRICT_UNKNOWN_SECTIONS = true
49 --- Boolean; default true;
50 -- treat options found in config but not in scheme as error
51 STRICT_UNKNOWN_OPTIONS = true
53 --- Boolean; default true;
54 -- treat failed external validators as error
55 STRICT_EXTERNAL_VALIDATORS = true
57 --- Boolean; default true;
58 -- treat list values stored as options like errors
59 STRICT_LIST_TYPE = true
62 local default_schemedir = "/lib/uci/schema"
63 local default_savedir = "/tmp/.uvl"
66 --- Object constructor
69 -- @param schemedir Path to the scheme directory (optional)
70 -- @return Instance object
73 function UVL.__init__( self, schemedir, configdir )
74 self.schemedir = schemedir or default_schemedir
75 self.configdir = configdir
81 self.dep = dependencies
82 self.datatypes = datatypes
86 --- Parse given scheme and return the scheme tree.
87 -- @param scheme Name of the scheme to parse
88 -- @return Table containing the parsed scheme or nil on error
89 -- @return String containing the reason for errors (if any)
90 function UVL.get_scheme( self, scheme )
91 if not self.packages[scheme] then
92 local ok, err = self:read_scheme( scheme )
97 return self.packages[scheme], nil
100 --- Validate given configuration, section or option.
101 -- @param config Name of the configuration to validate
102 -- @param section Name of the section to validate (optional)
103 -- @param option Name of the option to validate (optional)
104 -- @return Boolean indicating whether the given config validates
105 -- @return String containing the reason for errors (if any)
106 function UVL.validate( self, config, section, option )
107 if config and section and option then
108 return self:validate_option( config, section, option )
109 elseif config and section then
110 return self:validate_section( config, section )
112 return self:validate_config( config )
116 --- Validate given configuration.
117 -- @param cfg Name of the configuration to validate
118 -- @return Boolean indicating whether the given config validates
119 -- @return String containing the reason for errors (if any)
120 function UVL.validate_config( self, cfg, uci )
122 if not self.packages[cfg] then
123 local ok, err = self:read_scheme(cfg)
129 local co = config( self, uci or cfg, uci and cfg )
135 if not co:config() then
136 return false, co:errors()
139 local function _uci_foreach( type, func )
140 for k, v in pairs(co:config()) do
141 if v['.type'] == type then
142 sc[type] = sc[type] + 1
143 local ok, err = func( k, v )
144 if not ok then co:error(err) end
149 for k, v in pairs( self.packages[cfg].sections ) do
153 return self:_validate_section( co:section(s) )
158 if STRICT_UNKNOWN_SECTIONS then
159 for k, v in pairs(co:config()) do
160 local so = co:section(k)
161 if not self.beenthere[so:cid()] then
162 co:error(ERR.SECT_UNKNOWN(so))
167 for _, k in ipairs(util.keys(sc)) do
168 local so = co:section(k)
169 if so:scheme('required') and sc[k] == 0 then
170 co:error(ERR.SECT_REQUIRED(so))
171 elseif so:scheme('unique') and sc[k] > 1 then
172 co:error(ERR.SECT_UNIQUE(so))
176 return co:ok(), co:errors()
179 --- Validate given config section.
180 -- @param config Name of the configuration to validate
181 -- @param section Name of the section to validate
182 -- @return Boolean indicating whether the given config validates
183 -- @return String containing the reason for errors (if any)
184 function UVL.validate_section( self, cfg, section, uci )
186 if not self.packages[cfg] then
187 local ok, err = self:read_scheme( cfg )
193 local co = config( self, uci or cfg, uci and cfg )
194 local so = co:section( section )
199 if not co:config() then
200 return false, co:errors()
204 return self:_validate_section( so )
206 return false, ERR.SECT_NOTFOUND(so)
210 --- Validate given config option.
211 -- @param config Name of the configuration to validate
212 -- @param section Name of the section to validate
213 -- @param option Name of the option to validate
214 -- @return Boolean indicating whether the given config validates
215 -- @return String containing the reason for errors (if any)
216 function UVL.validate_option( self, cfg, section, option, uci )
218 if not self.packages[cfg] then
219 local ok, err = self:read_scheme( cfg )
225 local co = config( self, uci or cfg, uci and cfg )
226 local so = co:section( section )
227 local oo = so:option( option )
229 if not co:config() then
230 return false, co:errors()
233 if so:config() and oo:config() then
234 return self:_validate_option( oo )
236 return false, ERR.OPT_NOTFOUND(oo)
241 function UVL._validate_section( self, section )
243 self.beenthere[section:cid()] = true
245 if section:config() then
246 if section:scheme('named') == true and
247 section:config('.anonymous') == true
249 return false, ERR.SECT_NAMED(section)
252 for _, v in ipairs(section:variables()) do
253 local ok, err = self:_validate_option( v )
255 v:scheme('required') or v:scheme('type') == "enum" or (
256 not err:is(ERR.ERR_DEP_NOTEQUAL) and
257 not err:is(ERR.ERR_DEP_NOVALUE)
264 local ok, err = dependencies.check( self, section )
269 return false, ERR.SECT_NOTFOUND(section)
272 if STRICT_UNKNOWN_OPTIONS and not section:scheme('dynamic') then
273 for k, v in pairs(section:config()) do
274 local oo = section:option(k)
275 if k:sub(1,1) ~= "." and not self.beenthere[oo:cid()] then
276 section:error(ERR.OPT_UNKNOWN(oo))
281 return section:ok(), section:errors()
284 function UVL._validate_option( self, option, nodeps )
286 self.beenthere[option:cid()] = true
288 if not option:scheme() and not option:parent():scheme('dynamic') then
289 if STRICT_UNKNOWN_OPTIONS then
290 return false, option:error(ERR.OPT_UNKNOWN(option))
295 elseif option:scheme() then
297 local ok, err = dependencies.check( self, option )
300 ERR.ERR_OPT_REQUIRED,
301 ERR.ERR_DEP_NOTEQUAL,
305 return false, option:errors()
312 if option:scheme('required') and not option:value() then
313 return false, option:error(ERR.OPT_REQUIRED(option))
315 elseif option:value() then
316 local val = option:value()
318 if option:scheme('type') == "reference" or
319 option:scheme('type') == "enum"
321 local scheme_values = option:scheme('values') or { }
322 local config_values = ( type(val) == "table" and val or { val } )
323 for _, v in ipairs(config_values) do
324 if not scheme_values[v] then
325 return false, option:error( ERR.OPT_BADVALUE(
326 option, { v, util.serialize_data(
327 util.keys(scheme_values)
332 elseif option:scheme('type') == "list" then
333 if type(val) ~= "table" and STRICT_LIST_TYPE then
334 return false, option:error(ERR.OPT_NOTLIST(option))
338 if option:scheme('datatype') then
339 local dt = option:scheme('datatype')
341 if self.datatypes[dt] then
342 val = ( type(val) == "table" and val or { val } )
343 for i, v in ipairs(val) do
344 if not self.datatypes[dt]( v ) then
345 return false, option:error(
346 ERR.OPT_INVVALUE(option, { v, dt })
351 return false, option:error(ERR.OPT_DATATYPE(option, dt))
355 val = ( type(val) == "table" and val or { val } )
356 for _, v in ipairs(val) do
357 if option:scheme('minlength') then
358 if #v < option:scheme('minlength') then
359 return false, option:error(ERR.OPT_RANGE(option))
363 if option:scheme('maxlength') then
364 if #v > option:scheme('maxlength') then
365 return false, option:error(ERR.OPT_RANGE(option))
369 local w = tonumber(v)
371 if option:scheme('minimum') then
372 if not w or w < option:scheme('minimum') then
373 return false, option:error(ERR.OPT_RANGE(option))
377 if option:scheme('maximum') then
378 if not w or w > option:scheme('maximum') then
379 return false, option:error(ERR.OPT_RANGE(option))
385 local ok, err = validation.check( self, option )
386 if not ok and STRICT_EXTERNAL_VALIDATORS then
387 return false, option:error(err)
391 return option:ok(), option:errors()
394 --- Find all parts of given scheme and construct validation tree.
395 -- This is normally done on demand, so you don't have to call this function
397 -- @param shm Name of the scheme to parse
398 -- @param alias Create an alias for the loaded scheme
399 function UVL.read_scheme( self, shm, alias )
401 local so = scheme( self, shm )
402 local bc = "%s/bytecode/%s.lua" %{ self.schemedir, shm }
404 if not fs.access(bc) then
405 local files = fs.glob(self.schemedir .. '/*/' .. shm)
409 for i, file in ipairs( files ) do
410 if not fs.access(file) then
411 return false, so:error(ERR.SME_READ(so,file))
414 local uci = uci.cursor( fs.dirname(file), default_savedir )
416 local sname = fs.basename(file)
417 local sd, err = uci:load( sname )
420 return false, ERR.UCILOAD(so, err)
423 ok, err = pcall(function()
424 uci:foreach(sname, "package", function(s)
425 self:_parse_package(so, s[".name"], s)
427 uci:foreach(sname, "section", function(s)
428 self:_parse_section(so, s[".name"], s)
430 uci:foreach(sname, "variable", function(s)
431 self:_parse_var(so, s[".name"], s)
433 uci:foreach(sname, "enum", function(s)
434 self:_parse_enum(so, s[".name"], s)
440 if ok and alias then self.packages[alias] = self.packages[shm] end
441 return ok and self, err
443 return false, so:error(ERR.SME_FIND(so, self.schemedir))
446 local sc = loadfile(bc)
448 self.packages[shm] = sc()
451 return false, so:error(ERR.SME_READ(so,bc))
456 -- helper function to check for required fields
457 local function _req( t, n, c, r )
458 for i, v in ipairs(r) do
460 local p, o = scheme:sid(), nil
462 if t == TYPE_SECTION then
463 o = section( scheme, nil, p, n )
464 elseif t == TYPE_OPTION then
465 o = option( scheme, nil, p, '(nil)', n )
466 elseif t == TYPE_ENUM then
467 o = enum( scheme, nil, p, '(nil)', '(nil)', n )
470 return false, ERR.SME_REQFLD(o,v)
476 -- helper function to validate references
477 local function _ref( c, t )
479 if c == TYPE_SECTION then
482 elseif c == TYPE_OPTION then
485 elseif c == TYPE_ENUM then
490 for o in t[k]:gmatch("[^.]+") do
493 r[1] = ( #r[1] > 0 and r[1] or scheme:sid() )
496 return false, ERR.SME_BADREF(scheme, k)
502 -- helper function to read bools
503 local function _bool( v )
504 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
507 -- Step 0: get package meta information
508 function UVL._parse_package(self, scheme, k, v)
509 local sid = scheme:sid()
510 local pkg = self.packages[sid] or {
517 pkg.description = v.description
519 self.packages[sid] = pkg
522 -- Step 1: get all sections
523 function UVL._parse_section(self, scheme, k, v)
524 local ok, err = _req( TYPE_SECTION, k, v, { "name", "package" } )
525 if err then error(scheme:error(err)) end
527 local r, err = _ref( TYPE_SECTION, v )
528 if err then error(scheme:error(err)) end
530 local p = self.packages[r[1]] or {
535 p.sections[v.name] = p.sections[v.name] or { }
536 p.variables[v.name] = p.variables[v.name] or { }
537 self.packages[r[1]] = p
539 local s = p.sections[v.name]
540 local so = scheme:section(v.name)
542 for k, v2 in pairs(v) do
543 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
544 if k == "depends" then
545 s.depends = self:_read_dependency( v2, s.depends )
546 if not s.depends then
547 return false, scheme:error(
548 ERR.SME_BADDEP(so, util.serialize_data(s.depends))
551 elseif k == "dynamic" or k == "unique" or
552 k == "required" or k == "named"
561 s.dynamic = s.dynamic or false
562 s.unique = s.unique or false
563 s.required = s.required or false
564 s.named = s.named or false
567 -- Step 2: get all variables
568 function UVL._parse_var(self, scheme, k, v)
569 local ok, err = _req( TYPE_OPTION, k, v, { "name", "section" } )
570 if err then error(scheme:error(err)) end
572 local r, err = _ref( TYPE_OPTION, v )
573 if err then error(scheme:error(err)) end
575 local p = self.packages[r[1]]
578 ERR.SME_VBADPACK({scheme:sid(), '', v.name}, r[1])
582 local s = p.variables[r[2]]
585 ERR.SME_VBADSECT({scheme:sid(), '', v.name}, r[2])
589 s[v.name] = s[v.name] or { }
592 local so = scheme:section(r[2])
593 local to = so:option(v.name)
595 for k, v2 in pairs(v) do
596 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
597 if k == "depends" then
598 t.depends = self:_read_dependency( v2, t.depends )
599 if not t.depends then
600 error(scheme:error(so:error(
601 ERR.SME_BADDEP(to, util.serialize_data(v2))
604 elseif k == "validator" then
605 t.validators = self:_read_validator( v2, t.validators )
606 if not t.validators then
607 error(scheme:error(so:error(
608 ERR.SME_BADVAL(to, util.serialize_data(v2))
611 elseif k == "valueof" then
612 local values, err = self:_read_reference( v2 )
614 error(scheme:error(so:error(
615 ERR.REFERENCE(to, util.serialize_data(v2)):child(err)
620 t.valueof = type(v2) == "table" and v2 or {v2}
621 elseif k == "required" then
623 elseif k == "minlength" or k == "maxlength" or
624 k == "minimum" or k == "maximum"
633 t.type = t.type or "variable"
634 t.datatype = t.datatype or "string"
635 t.required = t.required or false
638 -- Step 3: get all enums
639 function UVL._parse_enum(self, scheme, k, v)
640 local ok, err = _req( TYPE_ENUM, k, v, { "value", "variable" } )
641 if err then error(scheme:error(err)) end
643 local r, err = _ref( TYPE_ENUM, v )
644 if err then error(scheme:error(err)) end
646 local p = self.packages[r[1]]
649 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
653 local s = p.variables[r[2]]
656 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
663 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
668 local so = scheme:section(r[2])
669 local oo = so:option(r[3])
670 local eo = oo:enum(v.value)
672 if t.type ~= "enum" and t.type ~= "reference" then
673 error(scheme:error(ERR.SME_EBADTYPE(eo)))
677 t.values = { [v.value] = v.title or v.value }
678 t.valuelist = { {value = v.value, title = v.title} }
680 t.values[v.value] = v.title or v.value
681 t.valuelist[#t.valuelist + 1] = {value = v.value, title = v.title}
684 if not t.enum_depends then
690 error(scheme:error(ERR.SME_EBADDEF(eo)))
696 t.enum_depends[v.value] = self:_read_dependency(
697 v.depends, t.enum_depends[v.value]
700 if not t.enum_depends[v.value] then
701 error(scheme:error(so:error(oo:error(
702 ERR.SME_BADDEP(eo, util.serialize_data(v.depends))
708 -- Read a dependency specification
709 function UVL._read_dependency( self, values, deps )
710 local expr = "%$?[%w_]+"
712 values = ( type(values) == "table" and values or { values } )
713 for _, value in ipairs(values) do
714 local condition = { }
715 for val in value:gmatch("[^,]+") do
716 local k, e, v = val:match("%s*([%w$_.]+)%s*(=?)%s*(.*)")
719 k:match("^"..expr.."%."..expr.."%."..expr.."$") or
720 k:match("^"..expr.."%."..expr.."$") or
721 k:match("^"..expr.."$")
723 condition[k] = (e == '=') and v or true
732 deps[#deps+1] = condition
740 -- Read a validator specification
741 function UVL._read_validator( self, values, validators )
743 values = ( type(values) == "table" and values or { values } )
744 for _, value in ipairs(values) do
747 if value:match("^exec:") then
748 validator = value:gsub("^exec:","")
749 elseif value:match("^lua:") then
750 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
751 elseif value:match("^regexp:") then
752 local pattern = value:gsub("^regexp:","")
753 validator = function( type, dtype, pack, sect, optn, ... )
754 local values = { ... }
755 for _, v in ipairs(values) do
757 pcall( string.match, v, pattern )
761 elseif not match then
763 'Value "%s" does not match pattern "%s"' % {
773 if not validators then
774 validators = { validator }
776 validators[#validators+1] = validator
787 -- Read a reference specification (XXX: We should validate external configs too...)
788 function UVL._read_reference( self, values )
790 values = ( type(values) == "table" and values or { values } )
792 for _, value in ipairs(values) do
793 local ref = util.split(value, ".")
795 if #ref == 2 or #ref == 3 then
796 local co = config( self, ref[1] )
797 if not co:config() then return false, co:errors() end
799 for k, v in pairs(co:config()) do
800 if v['.type'] == ref[2] then
802 if v['.anonymous'] == true then
803 return false, ERR.SME_INVREF('', value)
805 val[k] = k -- XXX: title/description would be nice
806 elseif v[ref[3]] then
807 val[v[ref[3]]] = v[ref[3]] -- XXX: dito
812 return false, ERR.SME_BADREF('', value)
819 -- Resolve given path
820 function UVL._resolve_function( self, value )
821 local path = util.split(value, ".")
824 local stat, mod = pcall(
825 require, table.concat(path, ".", 1, i)
829 for j=i+1, #path-1 do
830 if not type(mod) == "table" then
838 mod = type(mod) == "table" and mod[path[#path]] or nil
839 if type(mod) == "function" then
847 --- Object representation of an uvl item - base class.
848 uvlitem = util.class()
850 function uvlitem.cid(self)
851 if #self.cref == 1 then
854 local r = { unpack(self.cref) }
856 if c and c[r[2]] and c[r[2]]['.anonymous'] and c[r[2]]['.index'] then
857 r[2] = '@' .. c[r[2]]['.type'] ..
858 '[' .. tostring(c[r[2]]['.index']) .. ']'
860 return table.concat( r, '.' )
864 function uvlitem.sid(self)
865 return table.concat( self.sref, '.' )
868 function uvlitem.scheme(self, opt)
869 local s = self.s and self.s.packages
870 s = s and s[self.sref[1]]
871 if #self.sref == 4 or #self.sref == 3 then
872 s = s and s.variables
873 s = s and s[self.sref[2]]
874 s = s and s[self.sref[3]]
875 elseif #self.sref == 2 then
877 s = s and s[self.sref[2]]
887 function uvlitem.config(self, opt)
890 if #self.cref >= 2 and #self.cref <= 4 then
891 c = c and self.c[self.cref[2]] or nil
892 if #self.cref >= 3 then
893 c = c and c[self.cref[3]] or nil
904 function uvlitem.title(self)
905 return self:scheme() and self:scheme('title') or
906 self.cref[3] or self.cref[2] or self.cref[1]
909 function uvlitem.type(self)
910 if self.t == TYPE_CONFIG then
912 elseif self.t == TYPE_SECTION then
914 elseif self.t == TYPE_OPTION then
916 elseif self.t == TYPE_ENUM then
921 function uvlitem.error(self, ...)
923 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
924 self.e = errconst[#self.cref]( self )
927 return self.e:child( ... )
930 function uvlitem.errors(self)
934 function uvlitem.ok(self)
935 return not self:errors()
938 function uvlitem.parent(self)
941 elseif #self.cref == 3 or #self.cref == 4 then
942 return section( self.s, self.c, self.cref[1], self.cref[2] )
943 elseif #self.cref == 2 then
944 return config( self.s, self.c, self.cref[1] )
950 function uvlitem._loadconf(self, co, c, configdir)
951 co = co or self._configcache
954 co, err = uci.cursor(configdir):get_all(c)
957 self:error(ERR.UCILOAD(self, err))
960 self._configcache = co
966 --- Object representation of a scheme.
969 -- @name luci.uvl.scheme
971 --- Scheme instance constructor.
974 -- @param scheme Scheme instance
975 -- @param co Configuration data
976 -- @param c Configuration name
977 -- @return Config instance
978 scheme = util.class(uvlitem)
980 function scheme.__init__(self, scheme, co, c)
987 self.c = self:_loadconf(co, c, scheme.configdir)
992 --- Add an error to scheme.
993 -- @return Scheme error context
994 function scheme.error(self, ...)
995 if not self.e then self.e = ERR.SCHEME( self ) end
996 return self.e:child( ... )
999 --- Get an associated config object.
1000 -- @return Config instance
1001 function scheme.config(self)
1002 local co = config( self.s, self.cref[1] )
1008 --- Get all section objects associated with this scheme.
1009 -- @return Table containing all associated luci.uvl.section instances
1010 function scheme.sections(self)
1012 if self.s.packages[self.sref[1]].sections then
1013 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1015 self.s, self.c, self.cref[1], self.cref[2], o
1022 --- Get an associated section object.
1023 -- @param s Section to select
1024 -- @return Section instance
1025 function scheme.section(self, s)
1026 local so = section( self.s, self.c, self.cref[1], s )
1033 --- Object representation of a config.
1036 -- @name luci.uvl.config
1038 --- Config instance constructor.
1041 -- @param scheme Scheme instance
1042 -- @param co Configuration data
1043 -- @param c Configuration name
1044 -- @return Config instance
1045 config = util.class(uvlitem)
1047 function config.__init__(self, scheme, co, c)
1053 self.c = self:_loadconf(co, c, scheme.configdir)
1055 self.t = TYPE_CONFIG
1058 --- Get all section objects associated with this config.
1059 -- @return Table containing all associated luci.uvl.section instances
1060 function config.sections(self)
1062 if self.s.packages[self.sref[1]].sections then
1063 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1065 self.s, self.c, self.cref[1], self.cref[2], o
1072 --- Get an associated section object.
1073 -- @param s Section to select
1074 -- @return Section instance
1075 function config.section(self, s)
1076 local so = section( self.s, self.c, self.cref[1], s )
1083 --- Object representation of a scheme/config section.
1086 -- @name luci.uvl.section
1088 --- Section instance constructor.
1091 -- @param scheme Scheme instance
1092 -- @param co Configuration data
1093 -- @param c Configuration name
1094 -- @param s Section name
1095 -- @return Section instance
1096 section = util.class(uvlitem)
1098 function section.__init__(self, scheme, co, c, s)
1099 self.cref = { c, s }
1100 self.sref = { c, co and co[s] and co[s]['.type'] or s }
1101 self.c = self:_loadconf(co, c, scheme.configdir)
1103 self.t = TYPE_SECTION
1106 --- Get all option objects associated with this section.
1107 -- @return Table containing all associated luci.uvl.option instances
1108 function section.variables(self)
1110 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
1112 self.s.packages[self.sref[1]].variables[self.sref[2]]
1115 self.s, self.c, self.cref[1], self.cref[2], o
1122 --- Get an associated option object.
1123 -- @param o Option to select
1124 -- @return Option instance
1125 function section.option(self, o)
1126 local oo = option( self.s, self.c, self.cref[1], self.cref[2], o )
1133 --- Object representation of a scheme/config option.
1136 -- @name luci.uvl.option
1138 --- Section instance constructor.
1141 -- @param scheme Scheme instance
1142 -- @param co Configuration data
1143 -- @param c Configuration name
1144 -- @param s Section name
1145 -- @param o Option name
1146 -- @return Option instance
1147 option = util.class(uvlitem)
1149 function option.__init__(self, scheme, co, c, s, o)
1150 self.cref = { c, s, o }
1151 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1152 self.c = self:_loadconf(co, c, scheme.configdir)
1154 self.t = TYPE_OPTION
1157 --- Get the value of this option.
1158 -- @return The associated configuration value
1159 function option.value(self)
1160 local v = self:config() or self:scheme('default')
1161 if v and self:scheme('multival') then
1162 v = util.split( v, "%s+", nil, true )
1167 --- Get the associated section information in scheme.
1168 -- @return Table containing the scheme properties
1169 function option.section(self)
1170 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1173 --- Construct an enum object instance from given or default value.
1174 -- @param v Value to select
1175 -- @return Enum instance for selected value
1176 function option.enum(self, val)
1179 self.cref[1], self.cref[2], self.cref[3],
1185 --- Object representation of a enum value.
1188 -- @name luci.uvl.enum
1190 --- Section instance constructor.
1193 -- @param scheme Scheme instance
1194 -- @param co Configuration data
1195 -- @param c Configuration name
1196 -- @param s Section name
1197 -- @param o Enum name
1198 -- @param v Enum value
1199 -- @return Enum value instance
1200 enum = util.class(option)
1202 function enum.__init__(self, scheme, co, c, s, o, v)
1203 self.cref = { c, s, o, v }
1204 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1205 self.c = self:_loadconf(co, c, scheme.configdir)