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 = unpack, loadfile
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 --- Boolean; default true;
47 -- treat sections found in config but not in scheme as error
48 STRICT_UNKNOWN_SECTIONS = true
50 --- Boolean; default true;
51 -- treat options found in config but not in scheme as error
52 STRICT_UNKNOWN_OPTIONS = true
54 --- Boolean; default true;
55 -- treat failed external validators as error
56 STRICT_EXTERNAL_VALIDATORS = true
58 --- Boolean; default true;
59 -- treat list values stored as options like errors
60 STRICT_LIST_TYPE = true
63 local default_schemedir = "/lib/uci/schema"
64 local default_savedir = "/tmp/.uvl"
67 --- Object constructor
70 -- @param schemedir Path to the scheme directory (optional)
71 -- @param configdir Override config directory (optional)
72 -- @return Instance object
75 function UVL.__init__( self, schemedir, configdir )
76 self.schemedir = schemedir or default_schemedir
77 self.configdir = configdir
83 self.dep = dependencies
84 self.datatypes = datatypes
88 --- Parse given scheme and return the scheme tree.
89 -- @param scheme Name of the scheme to parse
90 -- @return Table containing the parsed scheme or nil on error
91 -- @return String containing the reason for errors (if any)
92 function UVL.get_scheme( self, scheme )
93 if not self.packages[scheme] then
94 local ok, err = self:read_scheme( scheme )
99 return self.packages[scheme], nil
102 --- Validate given configuration, section or option.
103 -- @param config Name of the configuration to validate
104 -- @param section Name of the section to validate (optional)
105 -- @param option Name of the option to validate (optional)
106 -- @return Boolean indicating whether the given config validates
107 -- @return String containing the reason for errors (if any)
108 function UVL.validate( self, config, section, option )
109 if config and section and option then
110 return self:validate_option( config, section, option )
111 elseif config and section then
112 return self:validate_section( config, section )
114 return self:validate_config( config )
118 --- Validate given configuration.
119 -- @param cfg Name of the configuration to validate
120 -- @return Boolean indicating whether the given config validates
121 -- @return String containing the reason for errors (if any)
122 function UVL.validate_config( self, cfg, uci )
124 if not self.packages[cfg] then
125 local ok, err = self:read_scheme(cfg)
131 local co = config( self, uci or cfg, uci and cfg )
137 if not co:config() then
138 return false, co:errors()
141 local function _uci_foreach( type, func )
142 for k, v in pairs(co:config()) do
143 if v['.type'] == type then
144 sc[type] = sc[type] + 1
145 local ok, err = func( k, v )
146 if not ok then co:error(err) end
151 for k, v in pairs( self.packages[cfg].sections ) do
155 return self:_validate_section( co:section(s) )
160 if STRICT_UNKNOWN_SECTIONS then
161 for k, v in pairs(co:config()) do
162 local so = co:section(k)
163 if not self.beenthere[so:cid()] then
164 co:error(ERR.SECT_UNKNOWN(so))
169 for _, k in ipairs(util.keys(sc)) do
170 local so = co:section(k)
171 if so:scheme('required') and sc[k] == 0 then
172 co:error(ERR.SECT_REQUIRED(so))
173 elseif so:scheme('unique') and sc[k] > 1 then
174 co:error(ERR.SECT_UNIQUE(so))
178 return co:ok(), co:errors()
181 --- Validate given config section.
182 -- @param config Name of the configuration to validate
183 -- @param section Name of the section to validate
184 -- @return Boolean indicating whether the given config validates
185 -- @return String containing the reason for errors (if any)
186 function UVL.validate_section( self, cfg, section, uci )
188 if not self.packages[cfg] then
189 local ok, err = self:read_scheme( cfg )
195 local co = config( self, uci or cfg, uci and cfg )
196 local so = co:section( section )
201 if not co:config() then
202 return false, co:errors()
206 return self:_validate_section( so )
208 return false, ERR.SECT_NOTFOUND(so)
212 --- Validate given config option.
213 -- @param config Name of the configuration to validate
214 -- @param section Name of the section to validate
215 -- @param option Name of the option to validate
216 -- @return Boolean indicating whether the given config validates
217 -- @return String containing the reason for errors (if any)
218 function UVL.validate_option( self, cfg, section, option, uci )
220 if not self.packages[cfg] then
221 local ok, err = self:read_scheme( cfg )
227 local co = config( self, uci or cfg, uci and cfg )
228 local so = co:section( section )
229 local oo = so:option( option )
231 if not co:config() then
232 return false, co:errors()
235 if so:config() and oo:config() then
236 return self:_validate_option( oo )
238 return false, ERR.OPT_NOTFOUND(oo)
243 function UVL._validate_section( self, section )
245 self.beenthere[section:cid()] = true
247 if section:config() then
248 if section:scheme('named') == true and
249 section:config('.anonymous') == true
251 return false, ERR.SECT_NAMED(section)
254 for _, v in ipairs(section:variables()) do
255 local ok, err = self:_validate_option( v )
257 v:scheme('required') or v:scheme('type') == "enum" or (
258 not err:is(ERR.ERR_DEP_NOTEQUAL) and
259 not err:is(ERR.ERR_DEP_NOVALUE)
266 local ok, err = dependencies.check( self, section )
271 return false, ERR.SECT_NOTFOUND(section)
274 if STRICT_UNKNOWN_OPTIONS and not section:scheme('dynamic') then
275 for k, v in pairs(section:config()) do
276 local oo = section:option(k)
277 if k:sub(1,1) ~= "." and not self.beenthere[oo:cid()] then
278 section:error(ERR.OPT_UNKNOWN(oo))
283 return section:ok(), section:errors()
286 function UVL._validate_option( self, option, nodeps )
288 self.beenthere[option:cid()] = true
290 if not option:scheme() and not option:parent():scheme('dynamic') then
291 if STRICT_UNKNOWN_OPTIONS then
292 return false, option:error(ERR.OPT_UNKNOWN(option))
297 elseif option:scheme() then
299 local ok, err = dependencies.check( self, option )
302 ERR.ERR_OPT_REQUIRED,
303 ERR.ERR_DEP_NOTEQUAL,
307 return false, option:errors()
314 if option:scheme('required') and not option:value() then
315 return false, option:error(ERR.OPT_REQUIRED(option))
317 elseif option:value() then
318 local val = option:value()
320 if option:scheme('type') == "reference" or
321 option:scheme('type') == "enum"
323 local scheme_values = option:scheme('values') or { }
324 local config_values = ( type(val) == "table" and val or { val } )
325 for _, v in ipairs(config_values) do
326 if not scheme_values[v] then
327 return false, option:error( ERR.OPT_BADVALUE(
328 option, { v, util.serialize_data(
329 util.keys(scheme_values)
334 elseif option:scheme('type') == "list" then
335 if type(val) ~= "table" and STRICT_LIST_TYPE then
336 return false, option:error(ERR.OPT_NOTLIST(option))
340 if option:scheme('datatype') then
341 local dt = option:scheme('datatype')
343 if self.datatypes[dt] then
344 val = ( type(val) == "table" and val or { val } )
345 for i, v in ipairs(val) do
346 if not self.datatypes[dt]( v ) then
347 return false, option:error(
348 ERR.OPT_INVVALUE(option, { v, dt })
353 return false, option:error(ERR.OPT_DATATYPE(option, dt))
357 val = ( type(val) == "table" and val or { val } )
358 for _, v in ipairs(val) do
359 if option:scheme('minlength') then
360 if #v < option:scheme('minlength') then
361 return false, option:error(ERR.OPT_RANGE(option))
365 if option:scheme('maxlength') then
366 if #v > option:scheme('maxlength') then
367 return false, option:error(ERR.OPT_RANGE(option))
371 local w = tonumber(v)
373 if option:scheme('minimum') then
374 if not w or w < option:scheme('minimum') then
375 return false, option:error(ERR.OPT_RANGE(option))
379 if option:scheme('maximum') then
380 if not w or w > option:scheme('maximum') then
381 return false, option:error(ERR.OPT_RANGE(option))
387 local ok, err = validation.check( self, option )
388 if not ok and STRICT_EXTERNAL_VALIDATORS then
389 return false, option:error(err)
393 return option:ok(), option:errors()
396 --- Find all parts of given scheme and construct validation tree.
397 -- This is normally done on demand, so you don't have to call this function
399 -- @param shm Name of the scheme to parse
400 -- @param alias Create an alias for the loaded scheme
401 function UVL.read_scheme( self, shm, alias )
403 local so = scheme( self, shm )
404 local bc = "%s/bytecode/%s.lua" %{ self.schemedir, shm }
406 if not fs.access(bc) then
407 local files = nutil.consume((fs.glob(self.schemedir .. '/*/' .. shm)))
411 for _, file in ipairs(files) do
412 if not fs.access(file) then
413 return false, so:error(ERR.SME_READ(so,file))
416 local uci = uci.cursor( fs.dirname(file), default_savedir )
418 local sname = fs.basename(file)
419 local sd, err = uci:load( sname )
422 return false, ERR.UCILOAD(so, err)
425 ok, err = pcall(function()
426 uci:foreach(sname, "package", function(s)
427 self:_parse_package(so, s[".name"], s)
429 uci:foreach(sname, "section", function(s)
430 self:_parse_section(so, s[".name"], s)
432 uci:foreach(sname, "variable", function(s)
433 self:_parse_var(so, s[".name"], s)
435 uci:foreach(sname, "enum", function(s)
436 self:_parse_enum(so, s[".name"], s)
442 if ok and alias then self.packages[alias] = self.packages[shm] end
443 return ok and self, err
445 return false, so:error(ERR.SME_FIND(so, self.schemedir))
448 local sc = loadfile(bc)
450 self.packages[shm] = sc()
453 return false, so:error(ERR.SME_READ(so,bc))
458 -- helper function to check for required fields
459 local function _req( t, n, c, r )
460 for i, v in ipairs(r) do
462 local p, o = scheme:sid(), nil
464 if t == TYPE_SECTION then
465 o = section( scheme, nil, p, n )
466 elseif t == TYPE_OPTION then
467 o = option( scheme, nil, p, '(nil)', n )
468 elseif t == TYPE_ENUM then
469 o = enum( scheme, nil, p, '(nil)', '(nil)', n )
472 return false, ERR.SME_REQFLD(o,v)
478 -- helper function to validate references
479 local function _ref( c, t )
481 if c == TYPE_SECTION then
484 elseif c == TYPE_OPTION then
487 elseif c == TYPE_ENUM then
492 for o in t[k]:gmatch("[^.]+") do
495 r[1] = ( #r[1] > 0 and r[1] or scheme:sid() )
498 return false, ERR.SME_BADREF(scheme, k)
504 -- helper function to read bools
505 local function _bool( v )
506 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
509 -- Step 0: get package meta information
510 function UVL._parse_package(self, scheme, k, v)
511 local sid = scheme:sid()
512 local pkg = self.packages[sid] or {
519 pkg.description = v.description
521 self.packages[sid] = pkg
524 -- Step 1: get all sections
525 function UVL._parse_section(self, scheme, k, v)
526 local ok, err = _req( TYPE_SECTION, k, v, { "name", "package" } )
527 if err then error(scheme:error(err)) end
529 local r, err = _ref( TYPE_SECTION, v )
530 if err then error(scheme:error(err)) end
532 local p = self.packages[r[1]] or {
537 p.sections[v.name] = p.sections[v.name] or { }
538 p.variables[v.name] = p.variables[v.name] or { }
539 self.packages[r[1]] = p
541 local s = p.sections[v.name]
542 local so = scheme:section(v.name)
544 for k, v2 in pairs(v) do
545 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
546 if k == "depends" then
547 s.depends = self:_read_dependency( v2, s.depends )
548 if not s.depends then
549 return false, scheme:error(
550 ERR.SME_BADDEP(so, util.serialize_data(s.depends))
553 elseif k == "dynamic" or k == "unique" or
554 k == "required" or k == "named"
563 s.dynamic = s.dynamic or false
564 s.unique = s.unique or false
565 s.required = s.required or false
566 s.named = s.named or false
569 -- Step 2: get all variables
570 function UVL._parse_var(self, scheme, k, v)
571 local ok, err = _req( TYPE_OPTION, k, v, { "name", "section" } )
572 if err then error(scheme:error(err)) end
574 local r, err = _ref( TYPE_OPTION, v )
575 if err then error(scheme:error(err)) end
577 local p = self.packages[r[1]]
580 ERR.SME_VBADPACK({scheme:sid(), '', v.name}, r[1])
584 local s = p.variables[r[2]]
587 ERR.SME_VBADSECT({scheme:sid(), '', v.name}, r[2])
591 s[v.name] = s[v.name] or { }
594 local so = scheme:section(r[2])
595 local to = so:option(v.name)
597 for k, v2 in pairs(v) do
598 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
599 if k == "depends" then
600 t.depends = self:_read_dependency( v2, t.depends )
601 if not t.depends then
602 error(scheme:error(so:error(
603 ERR.SME_BADDEP(to, util.serialize_data(v2))
606 elseif k == "validator" then
607 t.validators = self:_read_validator( v2, t.validators )
608 if not t.validators then
609 error(scheme:error(so:error(
610 ERR.SME_BADVAL(to, util.serialize_data(v2))
613 elseif k == "valueof" then
614 local values, err = self:_read_reference( v2 )
616 error(scheme:error(so:error(
617 ERR.REFERENCE(to, util.serialize_data(v2)):child(err)
622 t.valueof = type(v2) == "table" and v2 or {v2}
623 elseif k == "required" then
625 elseif k == "minlength" or k == "maxlength" or
626 k == "minimum" or k == "maximum"
635 t.type = t.type or "variable"
636 t.datatype = t.datatype or "string"
637 t.required = t.required or false
640 -- Step 3: get all enums
641 function UVL._parse_enum(self, scheme, k, v)
642 local ok, err = _req( TYPE_ENUM, k, v, { "value", "variable" } )
643 if err then error(scheme:error(err)) end
645 local r, err = _ref( TYPE_ENUM, v )
646 if err then error(scheme:error(err)) end
648 local p = self.packages[r[1]]
651 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
655 local s = p.variables[r[2]]
658 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
665 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
670 local so = scheme:section(r[2])
671 local oo = so:option(r[3])
672 local eo = oo:enum(v.value)
674 if t.type ~= "enum" and t.type ~= "reference" then
675 error(scheme:error(ERR.SME_EBADTYPE(eo)))
679 t.values = { [v.value] = v.title or v.value }
680 t.valuelist = { {value = v.value, title = v.title} }
682 t.values[v.value] = v.title or v.value
683 t.valuelist[#t.valuelist + 1] = {value = v.value, title = v.title}
686 if not t.enum_depends then
692 error(scheme:error(ERR.SME_EBADDEF(eo)))
698 t.enum_depends[v.value] = self:_read_dependency(
699 v.depends, t.enum_depends[v.value]
702 if not t.enum_depends[v.value] then
703 error(scheme:error(so:error(oo:error(
704 ERR.SME_BADDEP(eo, util.serialize_data(v.depends))
710 -- Read a dependency specification
711 function UVL._read_dependency( self, values, deps )
712 local expr = "%$?[%w_]+"
714 values = ( type(values) == "table" and values or { values } )
715 for _, value in ipairs(values) do
716 local condition = { }
717 for val in value:gmatch("[^,]+") do
718 local k, e, v = val:match("%s*([%w$_.]+)%s*(=?)%s*(.*)")
721 k:match("^"..expr.."%."..expr.."%."..expr.."$") or
722 k:match("^"..expr.."%."..expr.."$") or
723 k:match("^"..expr.."$")
725 condition[k] = (e == '=') and v or true
734 deps[#deps+1] = condition
742 -- Read a validator specification
743 function UVL._read_validator( self, values, validators )
745 values = ( type(values) == "table" and values or { values } )
746 for _, value in ipairs(values) do
749 if value:match("^exec:") then
750 validator = value:gsub("^exec:","")
751 elseif value:match("^lua:") then
752 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
753 elseif value:match("^regexp:") then
754 local pattern = value:gsub("^regexp:","")
755 validator = function( type, dtype, pack, sect, optn, ... )
756 local values = { ... }
757 for _, v in ipairs(values) do
759 pcall( string.match, v, pattern )
763 elseif not match then
765 'Value "%s" does not match pattern "%s"' % {
775 if not validators then
776 validators = { validator }
778 validators[#validators+1] = validator
789 -- Read a reference specification (XXX: We should validate external configs too...)
790 function UVL._read_reference( self, values )
792 values = ( type(values) == "table" and values or { values } )
794 for _, value in ipairs(values) do
795 local ref = util.split(value, ".")
797 if #ref == 2 or #ref == 3 then
798 local co = config( self, ref[1] )
799 if not co:config() then return false, co:errors() end
801 for k, v in pairs(co:config()) do
802 if v['.type'] == ref[2] then
804 if v['.anonymous'] == true then
805 return false, ERR.SME_INVREF('', value)
807 val[k] = k -- XXX: title/description would be nice
808 elseif v[ref[3]] then
809 val[v[ref[3]]] = v[ref[3]] -- XXX: dito
814 return false, ERR.SME_BADREF('', value)
821 -- Resolve given path
822 function UVL._resolve_function( self, value )
823 local path = util.split(value, ".")
826 local stat, mod = pcall(
827 require, table.concat(path, ".", 1, i)
831 for j=i+1, #path-1 do
832 if not type(mod) == "table" then
840 mod = type(mod) == "table" and mod[path[#path]] or nil
841 if type(mod) == "function" then
849 --- Object representation of an uvl item - base class.
850 uvlitem = util.class()
852 function uvlitem.cid(self)
853 if #self.cref == 1 then
856 local r = { unpack(self.cref) }
858 if c and c[r[2]] and c[r[2]]['.anonymous'] and c[r[2]]['.index'] then
859 r[2] = '@' .. c[r[2]]['.type'] ..
860 '[' .. tostring(c[r[2]]['.index']) .. ']'
862 return table.concat( r, '.' )
866 function uvlitem.sid(self)
867 return table.concat( self.sref, '.' )
870 function uvlitem.scheme(self, opt)
871 local s = self.s and self.s.packages
872 s = s and s[self.sref[1]]
873 if #self.sref == 4 or #self.sref == 3 then
874 s = s and s.variables
875 s = s and s[self.sref[2]]
876 s = s and s[self.sref[3]]
877 elseif #self.sref == 2 then
879 s = s and s[self.sref[2]]
889 function uvlitem.config(self, opt)
892 if #self.cref >= 2 and #self.cref <= 4 then
893 c = c and self.c[self.cref[2]] or nil
894 if #self.cref >= 3 then
895 c = c and c[self.cref[3]] or nil
906 function uvlitem.title(self)
907 return self:scheme() and self:scheme('title') or
908 self.cref[3] or self.cref[2] or self.cref[1]
911 function uvlitem.type(self)
912 if self.t == TYPE_CONFIG then
914 elseif self.t == TYPE_SECTION then
916 elseif self.t == TYPE_OPTION then
918 elseif self.t == TYPE_ENUM then
923 function uvlitem.error(self, ...)
925 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
926 self.e = errconst[#self.cref]( self )
929 return self.e:child( ... )
932 function uvlitem.errors(self)
936 function uvlitem.ok(self)
937 return not self:errors()
940 function uvlitem.parent(self)
943 elseif #self.cref == 3 or #self.cref == 4 then
944 return section( self.s, self.c, self.cref[1], self.cref[2] )
945 elseif #self.cref == 2 then
946 return config( self.s, self.c, self.cref[1] )
952 function uvlitem._loadconf(self, co, c, configdir)
953 co = co or self._configcache
956 co, err = uci.cursor(configdir):get_all(c)
959 self:error(ERR.UCILOAD(self, err))
962 self._configcache = co
968 --- Object representation of a scheme.
971 -- @name luci.uvl.scheme
973 --- Scheme instance constructor.
976 -- @param scheme Scheme instance
977 -- @param co Configuration data
978 -- @param c Configuration name
979 -- @return Config instance
980 scheme = util.class(uvlitem)
982 function scheme.__init__(self, scheme, co, c)
989 self.c = self:_loadconf(co, c, scheme.configdir)
994 --- Add an error to scheme.
995 -- @return Scheme error context
996 function scheme.error(self, ...)
997 if not self.e then self.e = ERR.SCHEME( self ) end
998 return self.e:child( ... )
1001 --- Get an associated config object.
1002 -- @return Config instance
1003 function scheme.config(self)
1004 local co = config( self.s, self.cref[1] )
1010 --- Get all section objects associated with this scheme.
1011 -- @return Table containing all associated luci.uvl.section instances
1012 function scheme.sections(self)
1014 if self.s.packages[self.sref[1]].sections then
1015 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1017 self.s, self.c, self.cref[1], self.cref[2], o
1024 --- Get an associated section object.
1025 -- @param s Section to select
1026 -- @return Section instance
1027 function scheme.section(self, s)
1028 local so = section( self.s, self.c, self.cref[1], s )
1035 --- Object representation of a config.
1038 -- @name luci.uvl.config
1040 --- Config instance constructor.
1043 -- @param scheme Scheme instance
1044 -- @param co Configuration data
1045 -- @param c Configuration name
1046 -- @return Config instance
1047 config = util.class(uvlitem)
1049 function config.__init__(self, scheme, co, c)
1055 self.c = self:_loadconf(co, c, scheme.configdir)
1057 self.t = TYPE_CONFIG
1060 --- Get all section objects associated with this config.
1061 -- @return Table containing all associated luci.uvl.section instances
1062 function config.sections(self)
1064 if self.s.packages[self.sref[1]].sections then
1065 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1067 self.s, self.c, self.cref[1], self.cref[2], o
1074 --- Get an associated section object.
1075 -- @param s Section to select
1076 -- @return Section instance
1077 function config.section(self, s)
1078 local so = section( self.s, self.c, self.cref[1], s )
1085 --- Object representation of a scheme/config section.
1088 -- @name luci.uvl.section
1090 --- Section instance constructor.
1093 -- @param scheme Scheme instance
1094 -- @param co Configuration data
1095 -- @param c Configuration name
1096 -- @param s Section name
1097 -- @return Section instance
1098 section = util.class(uvlitem)
1100 function section.__init__(self, scheme, co, c, s)
1101 self.cref = { c, s }
1102 self.sref = { c, co and co[s] and co[s]['.type'] or s }
1103 self.c = self:_loadconf(co, c, scheme.configdir)
1105 self.t = TYPE_SECTION
1108 --- Get all option objects associated with this section.
1109 -- @return Table containing all associated luci.uvl.option instances
1110 function section.variables(self)
1112 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
1114 self.s.packages[self.sref[1]].variables[self.sref[2]]
1117 self.s, self.c, self.cref[1], self.cref[2], o
1124 --- Get an associated option object.
1125 -- @param o Option to select
1126 -- @return Option instance
1127 function section.option(self, o)
1128 local oo = option( self.s, self.c, self.cref[1], self.cref[2], o )
1135 --- Object representation of a scheme/config option.
1138 -- @name luci.uvl.option
1140 --- Section instance constructor.
1143 -- @param scheme Scheme instance
1144 -- @param co Configuration data
1145 -- @param c Configuration name
1146 -- @param s Section name
1147 -- @param o Option name
1148 -- @return Option instance
1149 option = util.class(uvlitem)
1151 function option.__init__(self, scheme, co, c, s, o)
1152 self.cref = { c, s, o }
1153 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1154 self.c = self:_loadconf(co, c, scheme.configdir)
1156 self.t = TYPE_OPTION
1159 --- Get the value of this option.
1160 -- @return The associated configuration value
1161 function option.value(self)
1162 local v = self:config() or self:scheme('default')
1163 if v and self:scheme('multival') then
1164 v = util.split( v, "%s+", nil, true )
1169 --- Get the associated section information in scheme.
1170 -- @return Table containing the scheme properties
1171 function option.section(self)
1172 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1175 --- Construct an enum object instance from given or default value.
1176 -- @param v Value to select
1177 -- @return Enum instance for selected value
1178 function option.enum(self, val)
1181 self.cref[1], self.cref[2], self.cref[3],
1187 --- Object representation of a enum value.
1190 -- @name luci.uvl.enum
1192 --- Section instance constructor.
1195 -- @param scheme Scheme instance
1196 -- @param co Configuration data
1197 -- @param c Configuration name
1198 -- @param s Section name
1199 -- @param o Enum name
1200 -- @param v Enum value
1201 -- @return Enum value instance
1202 enum = util.class(option)
1204 function enum.__init__(self, scheme, co, c, s, o, v)
1205 self.cref = { c, s, o, v }
1206 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1207 self.c = self:_loadconf(co, c, scheme.configdir)