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 -- @param configdir Override config directory (optional)
71 -- @return Instance object
74 function UVL.__init__( self, schemedir, configdir )
75 self.schemedir = schemedir or default_schemedir
76 self.configdir = configdir
82 self.dep = dependencies
83 self.datatypes = datatypes
87 --- Parse given scheme and return the scheme tree.
88 -- @param scheme Name of the scheme to parse
89 -- @return Table containing the parsed scheme or nil on error
90 -- @return String containing the reason for errors (if any)
91 function UVL.get_scheme( self, scheme )
92 if not self.packages[scheme] then
93 local ok, err = self:read_scheme( scheme )
98 return self.packages[scheme], nil
101 --- Validate given configuration, section or option.
102 -- @param config Name of the configuration to validate
103 -- @param section Name of the section to validate (optional)
104 -- @param option Name of the option to validate (optional)
105 -- @return Boolean indicating whether the given config validates
106 -- @return String containing the reason for errors (if any)
107 function UVL.validate( self, config, section, option )
108 if config and section and option then
109 return self:validate_option( config, section, option )
110 elseif config and section then
111 return self:validate_section( config, section )
113 return self:validate_config( config )
117 --- Validate given configuration.
118 -- @param cfg Name of the configuration to validate
119 -- @return Boolean indicating whether the given config validates
120 -- @return String containing the reason for errors (if any)
121 function UVL.validate_config( self, cfg, uci )
123 if not self.packages[cfg] then
124 local ok, err = self:read_scheme(cfg)
130 local co = config( self, uci or cfg, uci and cfg )
136 if not co:config() then
137 return false, co:errors()
140 local function _uci_foreach( type, func )
141 for k, v in pairs(co:config()) do
142 if v['.type'] == type then
143 sc[type] = sc[type] + 1
144 local ok, err = func( k, v )
145 if not ok then co:error(err) end
150 for k, v in pairs( self.packages[cfg].sections ) do
154 return self:_validate_section( co:section(s) )
159 if STRICT_UNKNOWN_SECTIONS then
160 for k, v in pairs(co:config()) do
161 local so = co:section(k)
162 if not self.beenthere[so:cid()] then
163 co:error(ERR.SECT_UNKNOWN(so))
168 for _, k in ipairs(util.keys(sc)) do
169 local so = co:section(k)
170 if so:scheme('required') and sc[k] == 0 then
171 co:error(ERR.SECT_REQUIRED(so))
172 elseif so:scheme('unique') and sc[k] > 1 then
173 co:error(ERR.SECT_UNIQUE(so))
177 return co:ok(), co:errors()
180 --- Validate given config section.
181 -- @param config Name of the configuration to validate
182 -- @param section Name of the section to validate
183 -- @return Boolean indicating whether the given config validates
184 -- @return String containing the reason for errors (if any)
185 function UVL.validate_section( self, cfg, section, uci )
187 if not self.packages[cfg] then
188 local ok, err = self:read_scheme( cfg )
194 local co = config( self, uci or cfg, uci and cfg )
195 local so = co:section( section )
200 if not co:config() then
201 return false, co:errors()
205 return self:_validate_section( so )
207 return false, ERR.SECT_NOTFOUND(so)
211 --- Validate given config option.
212 -- @param config Name of the configuration to validate
213 -- @param section Name of the section to validate
214 -- @param option Name of the option to validate
215 -- @return Boolean indicating whether the given config validates
216 -- @return String containing the reason for errors (if any)
217 function UVL.validate_option( self, cfg, section, option, uci )
219 if not self.packages[cfg] then
220 local ok, err = self:read_scheme( cfg )
226 local co = config( self, uci or cfg, uci and cfg )
227 local so = co:section( section )
228 local oo = so:option( option )
230 if not co:config() then
231 return false, co:errors()
234 if so:config() and oo:config() then
235 return self:_validate_option( oo )
237 return false, ERR.OPT_NOTFOUND(oo)
242 function UVL._validate_section( self, section )
244 self.beenthere[section:cid()] = true
246 if section:config() then
247 if section:scheme('named') == true and
248 section:config('.anonymous') == true
250 return false, ERR.SECT_NAMED(section)
253 for _, v in ipairs(section:variables()) do
254 local ok, err = self:_validate_option( v )
256 v:scheme('required') or v:scheme('type') == "enum" or (
257 not err:is(ERR.ERR_DEP_NOTEQUAL) and
258 not err:is(ERR.ERR_DEP_NOVALUE)
265 local ok, err = dependencies.check( self, section )
270 return false, ERR.SECT_NOTFOUND(section)
273 if STRICT_UNKNOWN_OPTIONS and not section:scheme('dynamic') then
274 for k, v in pairs(section:config()) do
275 local oo = section:option(k)
276 if k:sub(1,1) ~= "." and not self.beenthere[oo:cid()] then
277 section:error(ERR.OPT_UNKNOWN(oo))
282 return section:ok(), section:errors()
285 function UVL._validate_option( self, option, nodeps )
287 self.beenthere[option:cid()] = true
289 if not option:scheme() and not option:parent():scheme('dynamic') then
290 if STRICT_UNKNOWN_OPTIONS then
291 return false, option:error(ERR.OPT_UNKNOWN(option))
296 elseif option:scheme() then
298 local ok, err = dependencies.check( self, option )
301 ERR.ERR_OPT_REQUIRED,
302 ERR.ERR_DEP_NOTEQUAL,
306 return false, option:errors()
313 if option:scheme('required') and not option:value() then
314 return false, option:error(ERR.OPT_REQUIRED(option))
316 elseif option:value() then
317 local val = option:value()
319 if option:scheme('type') == "reference" or
320 option:scheme('type') == "enum"
322 local scheme_values = option:scheme('values') or { }
323 local config_values = ( type(val) == "table" and val or { val } )
324 for _, v in ipairs(config_values) do
325 if not scheme_values[v] then
326 return false, option:error( ERR.OPT_BADVALUE(
327 option, { v, util.serialize_data(
328 util.keys(scheme_values)
333 elseif option:scheme('type') == "list" then
334 if type(val) ~= "table" and STRICT_LIST_TYPE then
335 return false, option:error(ERR.OPT_NOTLIST(option))
339 if option:scheme('datatype') then
340 local dt = option:scheme('datatype')
342 if self.datatypes[dt] then
343 val = ( type(val) == "table" and val or { val } )
344 for i, v in ipairs(val) do
345 if not self.datatypes[dt]( v ) then
346 return false, option:error(
347 ERR.OPT_INVVALUE(option, { v, dt })
352 return false, option:error(ERR.OPT_DATATYPE(option, dt))
356 val = ( type(val) == "table" and val or { val } )
357 for _, v in ipairs(val) do
358 if option:scheme('minlength') then
359 if #v < option:scheme('minlength') then
360 return false, option:error(ERR.OPT_RANGE(option))
364 if option:scheme('maxlength') then
365 if #v > option:scheme('maxlength') then
366 return false, option:error(ERR.OPT_RANGE(option))
370 local w = tonumber(v)
372 if option:scheme('minimum') then
373 if not w or w < option:scheme('minimum') then
374 return false, option:error(ERR.OPT_RANGE(option))
378 if option:scheme('maximum') then
379 if not w or w > option:scheme('maximum') then
380 return false, option:error(ERR.OPT_RANGE(option))
386 local ok, err = validation.check( self, option )
387 if not ok and STRICT_EXTERNAL_VALIDATORS then
388 return false, option:error(err)
392 return option:ok(), option:errors()
395 --- Find all parts of given scheme and construct validation tree.
396 -- This is normally done on demand, so you don't have to call this function
398 -- @param shm Name of the scheme to parse
399 -- @param alias Create an alias for the loaded scheme
400 function UVL.read_scheme( self, shm, alias )
402 local so = scheme( self, shm )
403 local bc = "%s/bytecode/%s.lua" %{ self.schemedir, shm }
405 if not fs.access(bc) then
406 local files = fs.glob(self.schemedir .. '/*/' .. shm)
410 for i, file in ipairs( files ) do
411 if not fs.access(file) then
412 return false, so:error(ERR.SME_READ(so,file))
415 local uci = uci.cursor( fs.dirname(file), default_savedir )
417 local sname = fs.basename(file)
418 local sd, err = uci:load( sname )
421 return false, ERR.UCILOAD(so, err)
424 ok, err = pcall(function()
425 uci:foreach(sname, "package", function(s)
426 self:_parse_package(so, s[".name"], s)
428 uci:foreach(sname, "section", function(s)
429 self:_parse_section(so, s[".name"], s)
431 uci:foreach(sname, "variable", function(s)
432 self:_parse_var(so, s[".name"], s)
434 uci:foreach(sname, "enum", function(s)
435 self:_parse_enum(so, s[".name"], s)
441 if ok and alias then self.packages[alias] = self.packages[shm] end
442 return ok and self, err
444 return false, so:error(ERR.SME_FIND(so, self.schemedir))
447 local sc = loadfile(bc)
449 self.packages[shm] = sc()
452 return false, so:error(ERR.SME_READ(so,bc))
457 -- helper function to check for required fields
458 local function _req( t, n, c, r )
459 for i, v in ipairs(r) do
461 local p, o = scheme:sid(), nil
463 if t == TYPE_SECTION then
464 o = section( scheme, nil, p, n )
465 elseif t == TYPE_OPTION then
466 o = option( scheme, nil, p, '(nil)', n )
467 elseif t == TYPE_ENUM then
468 o = enum( scheme, nil, p, '(nil)', '(nil)', n )
471 return false, ERR.SME_REQFLD(o,v)
477 -- helper function to validate references
478 local function _ref( c, t )
480 if c == TYPE_SECTION then
483 elseif c == TYPE_OPTION then
486 elseif c == TYPE_ENUM then
491 for o in t[k]:gmatch("[^.]+") do
494 r[1] = ( #r[1] > 0 and r[1] or scheme:sid() )
497 return false, ERR.SME_BADREF(scheme, k)
503 -- helper function to read bools
504 local function _bool( v )
505 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
508 -- Step 0: get package meta information
509 function UVL._parse_package(self, scheme, k, v)
510 local sid = scheme:sid()
511 local pkg = self.packages[sid] or {
518 pkg.description = v.description
520 self.packages[sid] = pkg
523 -- Step 1: get all sections
524 function UVL._parse_section(self, scheme, k, v)
525 local ok, err = _req( TYPE_SECTION, k, v, { "name", "package" } )
526 if err then error(scheme:error(err)) end
528 local r, err = _ref( TYPE_SECTION, v )
529 if err then error(scheme:error(err)) end
531 local p = self.packages[r[1]] or {
536 p.sections[v.name] = p.sections[v.name] or { }
537 p.variables[v.name] = p.variables[v.name] or { }
538 self.packages[r[1]] = p
540 local s = p.sections[v.name]
541 local so = scheme:section(v.name)
543 for k, v2 in pairs(v) do
544 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
545 if k == "depends" then
546 s.depends = self:_read_dependency( v2, s.depends )
547 if not s.depends then
548 return false, scheme:error(
549 ERR.SME_BADDEP(so, util.serialize_data(s.depends))
552 elseif k == "dynamic" or k == "unique" or
553 k == "required" or k == "named"
562 s.dynamic = s.dynamic or false
563 s.unique = s.unique or false
564 s.required = s.required or false
565 s.named = s.named or false
568 -- Step 2: get all variables
569 function UVL._parse_var(self, scheme, k, v)
570 local ok, err = _req( TYPE_OPTION, k, v, { "name", "section" } )
571 if err then error(scheme:error(err)) end
573 local r, err = _ref( TYPE_OPTION, v )
574 if err then error(scheme:error(err)) end
576 local p = self.packages[r[1]]
579 ERR.SME_VBADPACK({scheme:sid(), '', v.name}, r[1])
583 local s = p.variables[r[2]]
586 ERR.SME_VBADSECT({scheme:sid(), '', v.name}, r[2])
590 s[v.name] = s[v.name] or { }
593 local so = scheme:section(r[2])
594 local to = so:option(v.name)
596 for k, v2 in pairs(v) do
597 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
598 if k == "depends" then
599 t.depends = self:_read_dependency( v2, t.depends )
600 if not t.depends then
601 error(scheme:error(so:error(
602 ERR.SME_BADDEP(to, util.serialize_data(v2))
605 elseif k == "validator" then
606 t.validators = self:_read_validator( v2, t.validators )
607 if not t.validators then
608 error(scheme:error(so:error(
609 ERR.SME_BADVAL(to, util.serialize_data(v2))
612 elseif k == "valueof" then
613 local values, err = self:_read_reference( v2 )
615 error(scheme:error(so:error(
616 ERR.REFERENCE(to, util.serialize_data(v2)):child(err)
621 t.valueof = type(v2) == "table" and v2 or {v2}
622 elseif k == "required" then
624 elseif k == "minlength" or k == "maxlength" or
625 k == "minimum" or k == "maximum"
634 t.type = t.type or "variable"
635 t.datatype = t.datatype or "string"
636 t.required = t.required or false
639 -- Step 3: get all enums
640 function UVL._parse_enum(self, scheme, k, v)
641 local ok, err = _req( TYPE_ENUM, k, v, { "value", "variable" } )
642 if err then error(scheme:error(err)) end
644 local r, err = _ref( TYPE_ENUM, v )
645 if err then error(scheme:error(err)) end
647 local p = self.packages[r[1]]
650 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
654 local s = p.variables[r[2]]
657 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
664 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
669 local so = scheme:section(r[2])
670 local oo = so:option(r[3])
671 local eo = oo:enum(v.value)
673 if t.type ~= "enum" and t.type ~= "reference" then
674 error(scheme:error(ERR.SME_EBADTYPE(eo)))
678 t.values = { [v.value] = v.title or v.value }
679 t.valuelist = { {value = v.value, title = v.title} }
681 t.values[v.value] = v.title or v.value
682 t.valuelist[#t.valuelist + 1] = {value = v.value, title = v.title}
685 if not t.enum_depends then
691 error(scheme:error(ERR.SME_EBADDEF(eo)))
697 t.enum_depends[v.value] = self:_read_dependency(
698 v.depends, t.enum_depends[v.value]
701 if not t.enum_depends[v.value] then
702 error(scheme:error(so:error(oo:error(
703 ERR.SME_BADDEP(eo, util.serialize_data(v.depends))
709 -- Read a dependency specification
710 function UVL._read_dependency( self, values, deps )
711 local expr = "%$?[%w_]+"
713 values = ( type(values) == "table" and values or { values } )
714 for _, value in ipairs(values) do
715 local condition = { }
716 for val in value:gmatch("[^,]+") do
717 local k, e, v = val:match("%s*([%w$_.]+)%s*(=?)%s*(.*)")
720 k:match("^"..expr.."%."..expr.."%."..expr.."$") or
721 k:match("^"..expr.."%."..expr.."$") or
722 k:match("^"..expr.."$")
724 condition[k] = (e == '=') and v or true
733 deps[#deps+1] = condition
741 -- Read a validator specification
742 function UVL._read_validator( self, values, validators )
744 values = ( type(values) == "table" and values or { values } )
745 for _, value in ipairs(values) do
748 if value:match("^exec:") then
749 validator = value:gsub("^exec:","")
750 elseif value:match("^lua:") then
751 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
752 elseif value:match("^regexp:") then
753 local pattern = value:gsub("^regexp:","")
754 validator = function( type, dtype, pack, sect, optn, ... )
755 local values = { ... }
756 for _, v in ipairs(values) do
758 pcall( string.match, v, pattern )
762 elseif not match then
764 'Value "%s" does not match pattern "%s"' % {
774 if not validators then
775 validators = { validator }
777 validators[#validators+1] = validator
788 -- Read a reference specification (XXX: We should validate external configs too...)
789 function UVL._read_reference( self, values )
791 values = ( type(values) == "table" and values or { values } )
793 for _, value in ipairs(values) do
794 local ref = util.split(value, ".")
796 if #ref == 2 or #ref == 3 then
797 local co = config( self, ref[1] )
798 if not co:config() then return false, co:errors() end
800 for k, v in pairs(co:config()) do
801 if v['.type'] == ref[2] then
803 if v['.anonymous'] == true then
804 return false, ERR.SME_INVREF('', value)
806 val[k] = k -- XXX: title/description would be nice
807 elseif v[ref[3]] then
808 val[v[ref[3]]] = v[ref[3]] -- XXX: dito
813 return false, ERR.SME_BADREF('', value)
820 -- Resolve given path
821 function UVL._resolve_function( self, value )
822 local path = util.split(value, ".")
825 local stat, mod = pcall(
826 require, table.concat(path, ".", 1, i)
830 for j=i+1, #path-1 do
831 if not type(mod) == "table" then
839 mod = type(mod) == "table" and mod[path[#path]] or nil
840 if type(mod) == "function" then
848 --- Object representation of an uvl item - base class.
849 uvlitem = util.class()
851 function uvlitem.cid(self)
852 if #self.cref == 1 then
855 local r = { unpack(self.cref) }
857 if c and c[r[2]] and c[r[2]]['.anonymous'] and c[r[2]]['.index'] then
858 r[2] = '@' .. c[r[2]]['.type'] ..
859 '[' .. tostring(c[r[2]]['.index']) .. ']'
861 return table.concat( r, '.' )
865 function uvlitem.sid(self)
866 return table.concat( self.sref, '.' )
869 function uvlitem.scheme(self, opt)
870 local s = self.s and self.s.packages
871 s = s and s[self.sref[1]]
872 if #self.sref == 4 or #self.sref == 3 then
873 s = s and s.variables
874 s = s and s[self.sref[2]]
875 s = s and s[self.sref[3]]
876 elseif #self.sref == 2 then
878 s = s and s[self.sref[2]]
888 function uvlitem.config(self, opt)
891 if #self.cref >= 2 and #self.cref <= 4 then
892 c = c and self.c[self.cref[2]] or nil
893 if #self.cref >= 3 then
894 c = c and c[self.cref[3]] or nil
905 function uvlitem.title(self)
906 return self:scheme() and self:scheme('title') or
907 self.cref[3] or self.cref[2] or self.cref[1]
910 function uvlitem.type(self)
911 if self.t == TYPE_CONFIG then
913 elseif self.t == TYPE_SECTION then
915 elseif self.t == TYPE_OPTION then
917 elseif self.t == TYPE_ENUM then
922 function uvlitem.error(self, ...)
924 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
925 self.e = errconst[#self.cref]( self )
928 return self.e:child( ... )
931 function uvlitem.errors(self)
935 function uvlitem.ok(self)
936 return not self:errors()
939 function uvlitem.parent(self)
942 elseif #self.cref == 3 or #self.cref == 4 then
943 return section( self.s, self.c, self.cref[1], self.cref[2] )
944 elseif #self.cref == 2 then
945 return config( self.s, self.c, self.cref[1] )
951 function uvlitem._loadconf(self, co, c, configdir)
952 co = co or self._configcache
955 co, err = uci.cursor(configdir):get_all(c)
958 self:error(ERR.UCILOAD(self, err))
961 self._configcache = co
967 --- Object representation of a scheme.
970 -- @name luci.uvl.scheme
972 --- Scheme instance constructor.
975 -- @param scheme Scheme instance
976 -- @param co Configuration data
977 -- @param c Configuration name
978 -- @return Config instance
979 scheme = util.class(uvlitem)
981 function scheme.__init__(self, scheme, co, c)
988 self.c = self:_loadconf(co, c, scheme.configdir)
993 --- Add an error to scheme.
994 -- @return Scheme error context
995 function scheme.error(self, ...)
996 if not self.e then self.e = ERR.SCHEME( self ) end
997 return self.e:child( ... )
1000 --- Get an associated config object.
1001 -- @return Config instance
1002 function scheme.config(self)
1003 local co = config( self.s, self.cref[1] )
1009 --- Get all section objects associated with this scheme.
1010 -- @return Table containing all associated luci.uvl.section instances
1011 function scheme.sections(self)
1013 if self.s.packages[self.sref[1]].sections then
1014 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1016 self.s, self.c, self.cref[1], self.cref[2], o
1023 --- Get an associated section object.
1024 -- @param s Section to select
1025 -- @return Section instance
1026 function scheme.section(self, s)
1027 local so = section( self.s, self.c, self.cref[1], s )
1034 --- Object representation of a config.
1037 -- @name luci.uvl.config
1039 --- Config instance constructor.
1042 -- @param scheme Scheme instance
1043 -- @param co Configuration data
1044 -- @param c Configuration name
1045 -- @return Config instance
1046 config = util.class(uvlitem)
1048 function config.__init__(self, scheme, co, c)
1054 self.c = self:_loadconf(co, c, scheme.configdir)
1056 self.t = TYPE_CONFIG
1059 --- Get all section objects associated with this config.
1060 -- @return Table containing all associated luci.uvl.section instances
1061 function config.sections(self)
1063 if self.s.packages[self.sref[1]].sections then
1064 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1066 self.s, self.c, self.cref[1], self.cref[2], o
1073 --- Get an associated section object.
1074 -- @param s Section to select
1075 -- @return Section instance
1076 function config.section(self, s)
1077 local so = section( self.s, self.c, self.cref[1], s )
1084 --- Object representation of a scheme/config section.
1087 -- @name luci.uvl.section
1089 --- Section instance constructor.
1092 -- @param scheme Scheme instance
1093 -- @param co Configuration data
1094 -- @param c Configuration name
1095 -- @param s Section name
1096 -- @return Section instance
1097 section = util.class(uvlitem)
1099 function section.__init__(self, scheme, co, c, s)
1100 self.cref = { c, s }
1101 self.sref = { c, co and co[s] and co[s]['.type'] or s }
1102 self.c = self:_loadconf(co, c, scheme.configdir)
1104 self.t = TYPE_SECTION
1107 --- Get all option objects associated with this section.
1108 -- @return Table containing all associated luci.uvl.option instances
1109 function section.variables(self)
1111 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
1113 self.s.packages[self.sref[1]].variables[self.sref[2]]
1116 self.s, self.c, self.cref[1], self.cref[2], o
1123 --- Get an associated option object.
1124 -- @param o Option to select
1125 -- @return Option instance
1126 function section.option(self, o)
1127 local oo = option( self.s, self.c, self.cref[1], self.cref[2], o )
1134 --- Object representation of a scheme/config option.
1137 -- @name luci.uvl.option
1139 --- Section instance constructor.
1142 -- @param scheme Scheme instance
1143 -- @param co Configuration data
1144 -- @param c Configuration name
1145 -- @param s Section name
1146 -- @param o Option name
1147 -- @return Option instance
1148 option = util.class(uvlitem)
1150 function option.__init__(self, scheme, co, c, s, o)
1151 self.cref = { c, s, o }
1152 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1153 self.c = self:_loadconf(co, c, scheme.configdir)
1155 self.t = TYPE_OPTION
1158 --- Get the value of this option.
1159 -- @return The associated configuration value
1160 function option.value(self)
1161 local v = self:config() or self:scheme('default')
1162 if v and self:scheme('multival') then
1163 v = util.split( v, "%s+", nil, true )
1168 --- Get the associated section information in scheme.
1169 -- @return Table containing the scheme properties
1170 function option.section(self)
1171 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1174 --- Construct an enum object instance from given or default value.
1175 -- @param v Value to select
1176 -- @return Enum instance for selected value
1177 function option.enum(self, val)
1180 self.cref[1], self.cref[2], self.cref[3],
1186 --- Object representation of a enum value.
1189 -- @name luci.uvl.enum
1191 --- Section instance constructor.
1194 -- @param scheme Scheme instance
1195 -- @param co Configuration data
1196 -- @param c Configuration name
1197 -- @param s Section name
1198 -- @param o Enum name
1199 -- @param v Enum value
1200 -- @return Enum value instance
1201 enum = util.class(option)
1203 function enum.__init__(self, scheme, co, c, s, o, v)
1204 self.cref = { c, s, o, v }
1205 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1206 self.c = self:_loadconf(co, c, scheme.configdir)