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 "nixio.fs"
23 local uci = require "luci.model.uci"
24 local util = require "luci.util"
25 local nutil = require "nixio.util"
26 local table = require "table"
27 local string = require "string"
29 local require, pcall, ipairs, pairs = require, pcall, ipairs, pairs
30 local type, error, tonumber, tostring = type, error, tonumber, tostring
31 local unpack, loadfile, collectgarbage = unpack, loadfile, collectgarbage
35 local ERR = require "luci.uvl.errors"
36 local datatypes = require "luci.uvl.datatypes"
37 local validation = require "luci.uvl.validation"
38 local dependencies = require "luci.uvl.dependencies"
40 local TYPE_SCHEME = 0x00
41 local TYPE_CONFIG = 0x01
42 local TYPE_SECTION = 0x02
43 local TYPE_OPTION = 0x03
44 local TYPE_ENUM = 0x04
46 local PAT_EXPR1 = "^%$?[%w_]+$"
47 local PAT_EXPR2 = "^%$?[%w_]+%.%$?[%w_]+$"
48 local PAT_EXPR3 = "^%$?[%w_]+%.%$?[%w_]+%.%$?[%w_]+$"
50 --- Boolean; default true;
51 -- treat sections found in config but not in scheme as error
52 STRICT_UNKNOWN_SECTIONS = true
54 --- Boolean; default true;
55 -- treat options found in config but not in scheme as error
56 STRICT_UNKNOWN_OPTIONS = true
58 --- Boolean; default true;
59 -- treat failed external validators as error
60 STRICT_EXTERNAL_VALIDATORS = true
62 --- Boolean; default true;
63 -- treat list values stored as options like errors
64 STRICT_LIST_TYPE = true
67 local default_schemedir = "/lib/uci/schema"
68 local default_savedir = "/tmp/.uvl"
71 --- Object constructor
74 -- @param schemedir Path to the scheme directory (optional)
75 -- @param configdir Override config directory (optional)
76 -- @return Instance object
79 function UVL.__init__( self, schemedir, configdir )
80 self.schemedir = schemedir or default_schemedir
81 self.configdir = configdir
87 self.dep = dependencies
88 self.datatypes = datatypes
92 --- Parse given scheme and return the scheme tree.
93 -- @param scheme Name of the scheme to parse
94 -- @return Table containing the parsed scheme or nil on error
95 -- @return String containing the reason for errors (if any)
96 function UVL.get_scheme( self, scheme )
97 if not self.packages[scheme] then
98 local ok, err = self:read_scheme( scheme )
103 return self.packages[scheme], nil
106 --- Validate given configuration, section or option.
107 -- @param config Name of the configuration to validate
108 -- @param section Name of the section to validate (optional)
109 -- @param option Name of the option to validate (optional)
110 -- @return Boolean indicating whether the given config validates
111 -- @return String containing the reason for errors (if any)
112 function UVL.validate( self, config, section, option )
113 if config and section and option then
114 return self:validate_option( config, section, option )
115 elseif config and section then
116 return self:validate_section( config, section )
118 return self:validate_config( config )
122 --- Validate given configuration.
123 -- @param cfg Name of the configuration to validate
124 -- @return Boolean indicating whether the given config validates
125 -- @return String containing the reason for errors (if any)
126 function UVL.validate_config( self, cfg, uci )
128 if not self.packages[cfg] then
129 local ok, err = self:read_scheme(cfg)
135 local co = config( self, uci or cfg, uci and cfg )
141 if not co:config() then
142 return false, co:errors()
145 local function _uci_foreach( type, func )
146 for k, v in pairs(co:config()) do
147 if v['.type'] == type then
148 sc[type] = sc[type] + 1
149 local ok, err = func( k, v )
150 if not ok then co:error(err) end
155 for k, v in pairs( self.packages[cfg].sections ) do
159 return self:_validate_section( co:section(s) )
164 if STRICT_UNKNOWN_SECTIONS then
165 for k, v in pairs(co:config()) do
166 local so = co:section(k)
167 if not self.beenthere[so:cid()] then
168 co:error(ERR.SECT_UNKNOWN(so))
173 for _, k in ipairs(util.keys(sc)) do
174 local so = co:section(k)
175 if so:scheme('required') and sc[k] == 0 then
176 co:error(ERR.SECT_REQUIRED(so))
177 elseif so:scheme('unique') and sc[k] > 1 then
178 co:error(ERR.SECT_UNIQUE(so))
182 return co:ok(), co:errors()
185 --- Validate given config section.
186 -- @param config Name of the configuration to validate
187 -- @param section Name of the section to validate
188 -- @return Boolean indicating whether the given config validates
189 -- @return String containing the reason for errors (if any)
190 function UVL.validate_section( self, cfg, section, uci )
192 if not self.packages[cfg] then
193 local ok, err = self:read_scheme( cfg )
199 local co = config( self, uci or cfg, uci and cfg )
200 local so = co:section( section )
205 if not co:config() then
206 return false, co:errors()
210 return self:_validate_section( so )
212 return false, ERR.SECT_NOTFOUND(so)
216 --- Validate given config option.
217 -- @param config Name of the configuration to validate
218 -- @param section Name of the section to validate
219 -- @param option Name of the option to validate
220 -- @return Boolean indicating whether the given config validates
221 -- @return String containing the reason for errors (if any)
222 function UVL.validate_option( self, cfg, section, option, uci )
224 if not self.packages[cfg] then
225 local ok, err = self:read_scheme( cfg )
231 local co = config( self, uci or cfg, uci and cfg )
232 local so = co:section( section )
233 local oo = so:option( option )
235 if not co:config() then
236 return false, co:errors()
239 if so:config() and oo:config() then
240 return self:_validate_option( oo )
242 return false, ERR.OPT_NOTFOUND(oo)
247 function UVL._validate_section( self, section )
249 self.beenthere[section:cid()] = true
251 if section:config() then
252 if section:scheme('named') == true and
253 section:config('.anonymous') == true
255 return false, ERR.SECT_NAMED(section)
258 for _, v in ipairs(section:variables()) do
259 local ok, err = self:_validate_option( v )
261 v:scheme('required') or v:scheme('type') == "enum" or (
262 not err:is(ERR.ERR_DEP_NOTEQUAL) and
263 not err:is(ERR.ERR_DEP_NOVALUE)
270 local ok, err = dependencies.check( self, section )
275 return false, ERR.SECT_NOTFOUND(section)
278 if STRICT_UNKNOWN_OPTIONS and not section:scheme('dynamic') then
279 for k, v in pairs(section:config()) do
280 local oo = section:option(k)
281 if k:byte(1) == 46 and not self.beenthere[oo:cid()] then
282 section:error(ERR.OPT_UNKNOWN(oo))
287 return section:ok(), section:errors()
290 function UVL._validate_option( self, option, nodeps )
292 self.beenthere[option:cid()] = true
294 if not option:scheme() and not option:parent():scheme('dynamic') then
295 if STRICT_UNKNOWN_OPTIONS then
296 return false, option:error(ERR.OPT_UNKNOWN(option))
301 elseif option:scheme() then
303 local ok, err = dependencies.check( self, option )
306 ERR.ERR_OPT_REQUIRED,
307 ERR.ERR_DEP_NOTEQUAL,
311 return false, option:errors()
318 if option:scheme('required') and not option:value() then
319 return false, option:error(ERR.OPT_REQUIRED(option))
321 elseif option:value() then
322 local val = option:value()
324 if option:scheme('type') == "reference" or
325 option:scheme('type') == "enum"
327 local scheme_values = option:scheme('values') or { }
328 local config_values = ( type(val) == "table" and val or { val } )
329 for _, v in ipairs(config_values) do
330 if not scheme_values[v] then
331 return false, option:error( ERR.OPT_BADVALUE(
332 option, { v, util.serialize_data(
333 util.keys(scheme_values)
338 elseif option:scheme('type') == "list" then
339 if type(val) ~= "table" and STRICT_LIST_TYPE then
340 return false, option:error(ERR.OPT_NOTLIST(option))
344 if option:scheme('datatype') then
345 local dt = option:scheme('datatype')
347 if self.datatypes[dt] then
348 val = ( type(val) == "table" and val or { val } )
349 for i, v in ipairs(val) do
350 if not self.datatypes[dt]( v ) then
351 return false, option:error(
352 ERR.OPT_INVVALUE(option, { v, dt })
357 return false, option:error(ERR.OPT_DATATYPE(option, dt))
361 val = ( type(val) == "table" and val or { val } )
362 for _, v in ipairs(val) do
363 if option:scheme('minlength') then
364 if #v < option:scheme('minlength') then
365 return false, option:error(ERR.OPT_RANGE(option))
369 if option:scheme('maxlength') then
370 if #v > option:scheme('maxlength') then
371 return false, option:error(ERR.OPT_RANGE(option))
375 local w = tonumber(v)
377 if option:scheme('minimum') then
378 if not w or w < option:scheme('minimum') then
379 return false, option:error(ERR.OPT_RANGE(option))
383 if option:scheme('maximum') then
384 if not w or w > option:scheme('maximum') then
385 return false, option:error(ERR.OPT_RANGE(option))
391 local ok, err = validation.check( self, option )
392 if not ok and STRICT_EXTERNAL_VALIDATORS then
393 return false, option:error(err)
397 return option:ok(), option:errors()
400 --- Find all parts of given scheme and construct validation tree.
401 -- This is normally done on demand, so you don't have to call this function
403 -- @param shm Name of the scheme to parse
404 -- @param alias Create an alias for the loaded scheme
405 function UVL.read_scheme( self, shm, alias )
407 local so = scheme( self, shm )
408 local bc = "%s/bytecode/%s.lua" %{ self.schemedir, shm }
410 if not fs.access(bc) then
411 local files = nutil.consume((fs.glob(self.schemedir .. '/*/' .. shm)))
415 for _, file in ipairs(files) do
416 if not fs.access(file) then
417 return false, so:error(ERR.SME_READ(so,file))
420 local uci = uci.cursor( fs.dirname(file), default_savedir )
422 local sname = fs.basename(file)
423 local sd, err = uci:load( sname )
426 return false, ERR.UCILOAD(so, err)
429 ok, err = pcall(function()
430 uci:foreach(sname, "package", function(s)
431 self:_parse_package(so, s[".name"], s)
433 uci:foreach(sname, "section", function(s)
434 self:_parse_section(so, s[".name"], s)
436 uci:foreach(sname, "variable", function(s)
437 self:_parse_var(so, s[".name"], s)
439 uci:foreach(sname, "enum", function(s)
440 self:_parse_enum(so, s[".name"], s)
446 if ok and alias then self.packages[alias] = self.packages[shm] end
447 return ok and self, err
449 return false, so:error(ERR.SME_FIND(so, self.schemedir))
452 local sc = loadfile(bc)
454 self.packages[shm] = sc()
457 return false, so:error(ERR.SME_READ(so,bc))
462 -- helper function to check for required fields
463 local function _req( t, n, c, r )
464 for i, v in ipairs(r) do
466 local p, o = scheme:sid(), nil
468 if t == TYPE_SECTION then
469 o = section( scheme, nil, p, n )
470 elseif t == TYPE_OPTION then
471 o = option( scheme, nil, p, '(nil)', n )
472 elseif t == TYPE_ENUM then
473 o = enum( scheme, nil, p, '(nil)', '(nil)', n )
476 return false, ERR.SME_REQFLD(o,v)
482 -- helper function to validate references
483 local function _ref( c, t )
485 if c == TYPE_SECTION then
488 elseif c == TYPE_OPTION then
491 elseif c == TYPE_ENUM then
496 for o in t[k]:gmatch("[^.]+") do
499 r[1] = ( #r[1] > 0 and r[1] or scheme:sid() )
502 return false, ERR.SME_BADREF(scheme, k)
508 -- helper function to read bools
509 local function _bool( v )
510 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
513 -- Step 0: get package meta information
514 function UVL._parse_package(self, scheme, k, v)
515 local sid = scheme:sid()
516 local pkg = self.packages[sid] or {
523 pkg.description = v.description
525 self.packages[sid] = pkg
528 -- Step 1: get all sections
529 function UVL._parse_section(self, scheme, k, v)
530 local ok, err = _req( TYPE_SECTION, k, v, { "name", "package" } )
531 if err then error(scheme:error(err)) end
533 local r, err = _ref( TYPE_SECTION, v )
534 if err then error(scheme:error(err)) end
536 local p = self.packages[r[1]] or {
541 p.sections[v.name] = p.sections[v.name] or { }
542 p.variables[v.name] = p.variables[v.name] or { }
543 self.packages[r[1]] = p
545 local s = p.sections[v.name]
546 local so = scheme:section(v.name)
548 for k, v2 in pairs(v) do
549 if k ~= "name" and k ~= "package" and k:byte(1) == 46 then
550 if k == "depends" then
551 s.depends = self:_read_dependency( v2, s.depends )
552 if not s.depends then
553 return false, scheme:error(
554 ERR.SME_BADDEP(so, util.serialize_data(s.depends))
557 elseif k == "dynamic" or k == "unique" or
558 k == "required" or k == "named"
567 s.dynamic = s.dynamic or false
568 s.unique = s.unique or false
569 s.required = s.required or false
570 s.named = s.named or false
573 -- Step 2: get all variables
574 function UVL._parse_var(self, scheme, k, v)
575 local ok, err = _req( TYPE_OPTION, k, v, { "name", "section" } )
576 if err then error(scheme:error(err)) end
578 local r, err = _ref( TYPE_OPTION, v )
579 if err then error(scheme:error(err)) end
581 local p = self.packages[r[1]]
584 ERR.SME_VBADPACK({scheme:sid(), '', v.name}, r[1])
588 local s = p.variables[r[2]]
591 ERR.SME_VBADSECT({scheme:sid(), '', v.name}, r[2])
595 s[v.name] = s[v.name] or { }
598 local so = scheme:section(r[2])
599 local to = so:option(v.name)
601 for k, v2 in pairs(v) do
602 if k ~= "name" and k ~= "section" and k:byte(1) == 46 then
603 if k == "depends" then
604 t.depends = self:_read_dependency( v2, t.depends )
605 if not t.depends then
606 error(scheme:error(so:error(
607 ERR.SME_BADDEP(to, util.serialize_data(v2))
610 elseif k == "validator" then
611 t.validators = self:_read_validator( v2, t.validators )
612 if not t.validators then
613 error(scheme:error(so:error(
614 ERR.SME_BADVAL(to, util.serialize_data(v2))
617 elseif k == "valueof" then
618 local values, err = self:_read_reference( v2 )
620 error(scheme:error(so:error(
621 ERR.REFERENCE(to, util.serialize_data(v2)):child(err)
626 t.valueof = type(v2) == "table" and v2 or {v2}
627 elseif k == "required" then
629 elseif k == "minlength" or k == "maxlength" or
630 k == "minimum" or k == "maximum"
639 t.type = t.type or "variable"
640 t.datatype = t.datatype or "string"
641 t.required = t.required or false
644 -- Step 3: get all enums
645 function UVL._parse_enum(self, scheme, k, v)
646 local ok, err = _req( TYPE_ENUM, k, v, { "value", "variable" } )
647 if err then error(scheme:error(err)) end
649 local r, err = _ref( TYPE_ENUM, v )
650 if err then error(scheme:error(err)) end
652 local p = self.packages[r[1]]
655 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
659 local s = p.variables[r[2]]
662 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
669 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
674 local so = scheme:section(r[2])
675 local oo = so:option(r[3])
676 local eo = oo:enum(v.value)
678 if t.type ~= "enum" and t.type ~= "reference" then
679 error(scheme:error(ERR.SME_EBADTYPE(eo)))
683 t.values = { [v.value] = v.title or v.value }
684 t.valuelist = { {value = v.value, title = v.title} }
686 t.values[v.value] = v.title or v.value
687 t.valuelist[#t.valuelist + 1] = {value = v.value, title = v.title}
690 if not t.enum_depends then
696 error(scheme:error(ERR.SME_EBADDEF(eo)))
702 t.enum_depends[v.value] = self:_read_dependency(
703 v.depends, t.enum_depends[v.value]
706 if not t.enum_depends[v.value] then
707 error(scheme:error(so:error(oo:error(
708 ERR.SME_BADDEP(eo, util.serialize_data(v.depends))
714 -- Read a dependency specification
715 function UVL._read_dependency( self, values, deps )
716 local expr = "%$?[%w_]+"
718 values = ( type(values) == "table" and values or { values } )
719 for _, value in ipairs(values) do
720 local condition = { }
721 for val in value:gmatch("[^,]+") do
722 local k, e, v = val:match("%s*([%w$_.]+)%s*(=?)%s*(.*)")
725 k:match(PAT_EXPR1) or k:match(PAT_EXPR2) or k:match(PAT_EXPR3)
727 condition[k] = (e == '=') and v or true
736 deps[#deps+1] = condition
744 -- Read a validator specification
745 function UVL._read_validator( self, values, validators )
747 values = ( type(values) == "table" and values or { values } )
748 for _, value in ipairs(values) do
751 if value:match("^exec:") then
752 validator = value:gsub("^exec:","")
753 elseif value:match("^lua:") then
754 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
755 elseif value:match("^regexp:") then
756 local pattern = value:gsub("^regexp:","")
757 validator = function( type, dtype, pack, sect, optn, arg1, arg2, arg3, arg4, arg5 )
758 local values = { arg1, arg2, arg3, arg4, arg5 }
759 for _, v in ipairs(values) do
761 pcall( string.match, v, pattern )
765 elseif not match then
767 'Value "%s" does not match pattern "%s"' % {
777 if not validators then
778 validators = { validator }
780 validators[#validators+1] = validator
791 -- Read a reference specification (XXX: We should validate external configs too...)
792 function UVL._read_reference( self, values )
794 values = ( type(values) == "table" and values or { values } )
796 for _, value in ipairs(values) do
797 local ref = util.split(value, ".")
799 if #ref == 2 or #ref == 3 then
800 local co = config( self, ref[1] )
801 if not co:config() then return false, co:errors() end
803 for k, v in pairs(co:config()) do
804 if v['.type'] == ref[2] then
806 if v['.anonymous'] == true then
807 return false, ERR.SME_INVREF('', value)
809 val[k] = k -- XXX: title/description would be nice
810 elseif v[ref[3]] then
811 val[v[ref[3]]] = v[ref[3]] -- XXX: dito
816 return false, ERR.SME_BADREF('', value)
823 -- Resolve given path
824 function UVL._resolve_function( self, value )
825 local path = util.split(value, ".")
828 local stat, mod = pcall(
829 require, table.concat(path, ".", 1, i)
833 for j=i+1, #path-1 do
834 if not type(mod) == "table" then
842 mod = type(mod) == "table" and mod[path[#path]] or nil
843 if type(mod) == "function" then
851 --- Object representation of an uvl item - base class.
852 uvlitem = util.class()
854 function uvlitem.cid(self)
855 if #self.cref == 1 then
858 local r = { unpack(self.cref) }
860 if c and c[r[2]] and c[r[2]]['.anonymous'] and c[r[2]]['.index'] then
861 r[2] = '@' .. c[r[2]]['.type'] ..
862 '[' .. tostring(c[r[2]]['.index']) .. ']'
864 return table.concat( r, '.' )
868 function uvlitem.sid(self)
869 return table.concat( self.sref, '.' )
872 function uvlitem.scheme(self, opt)
873 local s = self.s and self.s.packages
874 s = s and s[self.sref[1]]
875 if #self.sref == 4 or #self.sref == 3 then
876 s = s and s.variables
877 s = s and s[self.sref[2]]
878 s = s and s[self.sref[3]]
879 elseif #self.sref == 2 then
881 s = s and s[self.sref[2]]
891 function uvlitem.config(self, opt)
894 if #self.cref >= 2 and #self.cref <= 4 then
895 c = c and self.c[self.cref[2]] or nil
896 if #self.cref >= 3 then
897 c = c and c[self.cref[3]] or nil
908 function uvlitem.title(self)
909 return self:scheme() and self:scheme('title') or
910 self.cref[3] or self.cref[2] or self.cref[1]
913 function uvlitem.type(self)
914 if self.t == TYPE_CONFIG then
916 elseif self.t == TYPE_SECTION then
918 elseif self.t == TYPE_OPTION then
920 elseif self.t == TYPE_ENUM then
925 function uvlitem.error(self, arg1, arg2, arg3, arg4, arg5)
927 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
928 self.e = errconst[#self.cref]( self )
931 return self.e:child( arg1, arg2, arg3, arg4, arg5 )
934 function uvlitem.errors(self)
938 function uvlitem.ok(self)
939 return not self:errors()
942 function uvlitem.parent(self)
945 elseif #self.cref == 3 or #self.cref == 4 then
946 return section( self.s, self.c, self.cref[1], self.cref[2] )
947 elseif #self.cref == 2 then
948 return config( self.s, self.c, self.cref[1] )
954 function uvlitem._loadconf(self, co, c, configdir)
955 co = co or self._configcache
958 co, err = uci.cursor(configdir):get_all(c)
961 self:error(ERR.UCILOAD(self, err))
964 self._configcache = co
970 --- Object representation of a scheme.
973 -- @name luci.uvl.scheme
975 --- Scheme instance constructor.
978 -- @param scheme Scheme instance
979 -- @param co Configuration data
980 -- @param c Configuration name
981 -- @return Config instance
982 scheme = util.class(uvlitem)
984 function scheme.__init__(self, scheme, co, c)
991 self.c = self:_loadconf(co, c, scheme.configdir)
996 --- Add an error to scheme.
997 -- @return Scheme error context
998 function scheme.error(self, arg1, arg2, arg3, arg4, arg5)
999 if not self.e then self.e = ERR.SCHEME( self ) end
1000 return self.e:child( arg1, arg2, arg3, arg4, arg5 )
1003 --- Get an associated config object.
1004 -- @return Config instance
1005 function scheme.config(self)
1006 local co = config( self.s, self.cref[1] )
1012 --- Get all section objects associated with this scheme.
1013 -- @return Table containing all associated luci.uvl.section instances
1014 function scheme.sections(self)
1016 if self.s.packages[self.sref[1]].sections then
1017 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1019 self.s, self.c, self.cref[1], self.cref[2], o
1026 --- Get an associated section object.
1027 -- @param s Section to select
1028 -- @return Section instance
1029 function scheme.section(self, s)
1030 local so = section( self.s, self.c, self.cref[1], s )
1037 --- Object representation of a config.
1040 -- @name luci.uvl.config
1042 --- Config instance constructor.
1045 -- @param scheme Scheme instance
1046 -- @param co Configuration data
1047 -- @param c Configuration name
1048 -- @return Config instance
1049 config = util.class(uvlitem)
1051 function config.__init__(self, scheme, co, c)
1057 self.c = self:_loadconf(co, c, scheme.configdir)
1059 self.t = TYPE_CONFIG
1062 --- Get all section objects associated with this config.
1063 -- @return Table containing all associated luci.uvl.section instances
1064 function config.sections(self)
1066 if self.s.packages[self.sref[1]].sections then
1067 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1069 self.s, self.c, self.cref[1], self.cref[2], o
1076 --- Get an associated section object.
1077 -- @param s Section to select
1078 -- @return Section instance
1079 function config.section(self, s)
1080 local so = section( self.s, self.c, self.cref[1], s )
1087 --- Object representation of a scheme/config section.
1090 -- @name luci.uvl.section
1092 --- Section instance constructor.
1095 -- @param scheme Scheme instance
1096 -- @param co Configuration data
1097 -- @param c Configuration name
1098 -- @param s Section name
1099 -- @return Section instance
1100 section = util.class(uvlitem)
1102 function section.__init__(self, scheme, co, c, s)
1103 self.cref = { c, s }
1104 self.sref = { c, co and co[s] and co[s]['.type'] or s }
1105 self.c = self:_loadconf(co, c, scheme.configdir)
1107 self.t = TYPE_SECTION
1110 --- Get all option objects associated with this section.
1111 -- @return Table containing all associated luci.uvl.option instances
1112 function section.variables(self)
1114 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
1116 self.s.packages[self.sref[1]].variables[self.sref[2]]
1119 self.s, self.c, self.cref[1], self.cref[2], o
1126 --- Get an associated option object.
1127 -- @param o Option to select
1128 -- @return Option instance
1129 function section.option(self, o)
1130 local oo = option( self.s, self.c, self.cref[1], self.cref[2], o )
1137 --- Object representation of a scheme/config option.
1140 -- @name luci.uvl.option
1142 --- Section instance constructor.
1145 -- @param scheme Scheme instance
1146 -- @param co Configuration data
1147 -- @param c Configuration name
1148 -- @param s Section name
1149 -- @param o Option name
1150 -- @return Option instance
1151 option = util.class(uvlitem)
1153 function option.__init__(self, scheme, co, c, s, o)
1154 self.cref = { c, s, o }
1155 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1156 self.c = self:_loadconf(co, c, scheme.configdir)
1158 self.t = TYPE_OPTION
1161 --- Get the value of this option.
1162 -- @return The associated configuration value
1163 function option.value(self)
1164 local v = self:config() or self:scheme('default')
1165 if v and self:scheme('multival') then
1166 v = util.split( v, "%s+", nil, true )
1171 --- Get the associated section information in scheme.
1172 -- @return Table containing the scheme properties
1173 function option.section(self)
1174 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1177 --- Construct an enum object instance from given or default value.
1178 -- @param v Value to select
1179 -- @return Enum instance for selected value
1180 function option.enum(self, val)
1183 self.cref[1], self.cref[2], self.cref[3],
1189 --- Object representation of a enum value.
1192 -- @name luci.uvl.enum
1194 --- Section instance constructor.
1197 -- @param scheme Scheme instance
1198 -- @param co Configuration data
1199 -- @param c Configuration name
1200 -- @param s Section name
1201 -- @param o Enum name
1202 -- @param v Enum value
1203 -- @return Enum value instance
1204 enum = util.class(option)
1206 function enum.__init__(self, scheme, co, c, s, o, v)
1207 self.cref = { c, s, o, v }
1208 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1209 self.c = self:_loadconf(co, c, scheme.configdir)