* luci/libs/uvl:
[project/luci.git] / libs / uvl / luasrc / uvl.lua
1 --[[
2
3 UCI Validation Layer - Main Library
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14
15 ]]--
16
17
18 --- UVL - UCI Validation Layer
19 -- @class module
20 -- @cstyle instance
21
22 module( "luci.uvl", package.seeall )
23
24 require("luci.fs")
25 require("luci.util")
26 require("luci.model.uci")
27 require("luci.uvl.errors")
28 require("luci.uvl.datatypes")
29 require("luci.uvl.validation")
30 require("luci.uvl.dependencies")
31
32
33 TYPE_SCHEME = 0x00
34 TYPE_CONFIG = 0x01
35 TYPE_SECTION = 0x02
36 TYPE_OPTION = 0x03
37 TYPE_ENUM = 0x04
38
39 --- Boolean; default true;
40 -- treat sections found in config but not in scheme as error
41 STRICT_UNKNOWN_SECTIONS = true
42
43 --- Boolean; default true;
44 -- treat options found in config but not in scheme as error
45 STRICT_UNKNOWN_OPTIONS = true
46
47 --- Boolean; default true;
48 -- treat failed external validators as error
49 STRICT_EXTERNAL_VALIDATORS = true
50
51 --- Boolean; default true;
52 -- treat list values stored as options like errors
53 STRICT_LIST_TYPE = true
54
55
56 local default_schemedir = "/lib/uci/schema"
57 local default_savedir = "/tmp/.uvl"
58 local ERR = luci.uvl.errors
59
60
61 --- Object constructor
62 -- @class function
63 -- @name UVL
64 -- @param schemedir Path to the scheme directory (optional)
65 -- @return Instance object
66 UVL = luci.util.class()
67
68 function UVL.__init__( self, schemedir )
69 self.schemedir = schemedir or default_schemedir
70 self.packages = { }
71 self.beenthere = { }
72 self.depseen = { }
73 self.uci = luci.model.uci
74 self.err = luci.uvl.errors
75 self.dep = luci.uvl.dependencies
76 self.datatypes = luci.uvl.datatypes
77 end
78
79
80 --- Parse given scheme and return the scheme tree.
81 -- @param scheme Name of the scheme to parse
82 -- @return Table containing the parsed scheme or nil on error
83 -- @return String containing the reason for errors (if any)
84 function UVL.get_scheme( self, scheme )
85 if not self.packages[scheme] then
86 local ok, err = self:read_scheme( scheme )
87 if not ok then
88 return nil, err
89 end
90 end
91 return self.packages[scheme], nil
92 end
93
94 --- Validate given configuration, section or option.
95 -- @param config Name of the configuration to validate
96 -- @param section Name of the section to validate (optional)
97 -- @param option Name of the option to validate (optional)
98 -- @return Boolean indicating whether the given config validates
99 -- @return String containing the reason for errors (if any)
100 function UVL.validate( self, config, section, option )
101 if config and section and option then
102 return self:validate_option( config, section, option )
103 elseif config and section then
104 return self:validate_section( config, section )
105 elseif config then
106 return self:validate_config( config )
107 end
108 end
109
110 --- Validate given configuration.
111 -- @param config Name of the configuration to validate
112 -- @return Boolean indicating whether the given config validates
113 -- @return String containing the reason for errors (if any)
114 function UVL.validate_config( self, config, uci )
115
116 if not self.packages[config] then
117 local ok, err = self:read_scheme(config)
118 if not ok then
119 return false, err
120 end
121 end
122
123 local co = luci.uvl.config( self, uci or config, uci and config )
124 local sc = { }
125
126 self.beenthere = { }
127 self.depseen = { }
128
129 if not co:config() then
130 return false, co:errors()
131 end
132
133 local function _uci_foreach( type, func )
134 for k, v in pairs(co:config()) do
135 if v['.type'] == type then
136 sc[type] = sc[type] + 1
137 local ok, err = func( k, v )
138 if not ok then co:error(err) end
139 end
140 end
141 end
142
143 for k, v in pairs( self.packages[config].sections ) do
144 sc[k] = 0
145 _uci_foreach( k,
146 function(s)
147 return self:_validate_section( co:section(s) )
148 end
149 )
150 end
151
152 if STRICT_UNKNOWN_SECTIONS then
153 for k, v in pairs(co:config()) do
154 local so = co:section(k)
155 if not self.beenthere[so:cid()] then
156 co:error(ERR.SECT_UNKNOWN(so))
157 end
158 end
159 end
160
161 for _, k in ipairs(luci.util.keys(sc)) do
162 local so = co:section(k)
163 if so:scheme('required') and sc[k] == 0 then
164 co:error(ERR.SECT_REQUIRED(so))
165 elseif so:scheme('unique') and sc[k] > 1 then
166 co:error(ERR.SECT_UNIQUE(so))
167 end
168 end
169
170 return co:ok(), co:errors()
171 end
172
173 --- Validate given config section.
174 -- @param config Name of the configuration to validate
175 -- @param section Name of the section to validate
176 -- @return Boolean indicating whether the given config validates
177 -- @return String containing the reason for errors (if any)
178 function UVL.validate_section( self, config, section, uci )
179
180 if not self.packages[config] then
181 local ok, err = self:read_scheme( config )
182 if not ok then
183 return false, err
184 end
185 end
186
187 local co = luci.uvl.config( self, uci or config, uci and config )
188 local so = co:section( section )
189
190 self.beenthere = { }
191 self.depseen = { }
192
193 if not co:config() then
194 return false, co:errors()
195 end
196
197 if so:config() then
198 return self:_validate_section( so )
199 else
200 return false, ERR.SECT_NOTFOUND(so)
201 end
202 end
203
204 --- Validate given config option.
205 -- @param config Name of the configuration to validate
206 -- @param section Name of the section to validate
207 -- @param option Name of the option to validate
208 -- @return Boolean indicating whether the given config validates
209 -- @return String containing the reason for errors (if any)
210 function UVL.validate_option( self, config, section, option, uci )
211
212 if not self.packages[config] then
213 local ok, err = self:read_scheme( config )
214 if not ok then
215 return false, err
216 end
217 end
218
219 local co = luci.uvl.config( self, uci or config, uci and config )
220 local so = co:section( section )
221 local oo = so:option( option )
222
223 if not co:config() then
224 return false, co:errors()
225 end
226
227 if so:config() and oo:config() then
228 return self:_validate_option( oo )
229 else
230 return false, ERR.OPT_NOTFOUND(oo)
231 end
232 end
233
234
235 function UVL._validate_section( self, section )
236
237 self.beenthere[section:cid()] = true
238
239 if section:config() then
240 if section:scheme('named') == true and
241 section:config('.anonymous') == true
242 then
243 return false, ERR.SECT_NAMED(section)
244 end
245
246 for _, v in ipairs(section:variables()) do
247 local ok, err = self:_validate_option( v )
248 if not ok and (
249 v:scheme('required') or v:scheme('type') == "enum" or (
250 not err:is(ERR.ERR_DEP_NOTEQUAL) and
251 not err:is(ERR.ERR_DEP_NOVALUE)
252 )
253 ) then
254 section:error(err)
255 end
256 end
257
258 local ok, err = luci.uvl.dependencies.check( self, section )
259 if not ok then
260 section:error(err)
261 end
262 else
263 return false, ERR.SECT_NOTFOUND(section)
264 end
265
266 if STRICT_UNKNOWN_OPTIONS and not section:scheme('dynamic') then
267 for k, v in pairs(section:config()) do
268 local oo = section:option(k)
269 if k:sub(1,1) ~= "." and not self.beenthere[oo:cid()] then
270 section:error(ERR.OPT_UNKNOWN(oo))
271 end
272 end
273 end
274
275 return section:ok(), section:errors()
276 end
277
278 function UVL._validate_option( self, option, nodeps )
279
280 self.beenthere[option:cid()] = true
281
282 if not option:scheme() and not option:parent():scheme('dynamic') then
283 if STRICT_UNKNOWN_OPTIONS then
284 return false, option:error(ERR.OPT_UNKNOWN(option))
285 else
286 return true
287 end
288
289 elseif option:scheme() then
290 if option:scheme('required') and not option:value() then
291 return false, option:error(ERR.OPT_REQUIRED(option))
292
293 elseif option:value() then
294 local val = option:value()
295
296 if option:scheme('type') == "reference" or
297 option:scheme('type') == "enum"
298 then
299 local scheme_values = option:scheme('values') or { }
300 local config_values = ( type(val) == "table" and val or { val } )
301 for _, v in ipairs(config_values) do
302 if not scheme_values[v] then
303 return false, option:error( ERR.OPT_BADVALUE(
304 option, { v, luci.util.serialize_data(
305 luci.util.keys(scheme_values)
306 ) }
307 ) )
308 end
309 end
310 elseif option:scheme('type') == "list" then
311 if type(val) ~= "table" and STRICT_LIST_TYPE then
312 return false, option:error(ERR.OPT_NOTLIST(option))
313 end
314 end
315
316 if option:scheme('datatype') then
317 local dt = option:scheme('datatype')
318
319 if self.datatypes[dt] then
320 val = ( type(val) == "table" and val or { val } )
321 for i, v in ipairs(val) do
322 if not self.datatypes[dt]( v ) then
323 return false, option:error(
324 ERR.OPT_INVVALUE(option, { v, dt })
325 )
326 end
327 end
328 else
329 return false, option:error(ERR.OPT_DATATYPE(option, dt))
330 end
331 end
332 end
333
334 if not nodeps then
335 local ok, err = luci.uvl.dependencies.check( self, option )
336 if not ok then
337 option:error(err)
338 end
339 end
340
341 local ok, err = luci.uvl.validation.check( self, option )
342 if not ok and STRICT_EXTERNAL_VALIDATORS then
343 return false, option:error(err)
344 end
345 end
346
347 return option:ok(), option:errors()
348 end
349
350 --- Find all parts of given scheme and construct validation tree.
351 -- This is normally done on demand, so you don't have to call this function
352 -- by yourself.
353 -- @param scheme Name of the scheme to parse
354 -- @param alias Create an alias for the loaded scheme
355 function UVL.read_scheme( self, scheme, alias )
356
357 local so = luci.uvl.scheme( self, scheme )
358 local bc = "%s/bytecode/%s.lua" %{ self.schemedir, scheme }
359
360 if not luci.fs.access(bc) then
361 local schemes = { }
362 local files = luci.fs.glob(self.schemedir .. '/*/' .. scheme)
363
364 if files then
365 for i, file in ipairs( files ) do
366 if not luci.fs.access(file) then
367 return false, so:error(ERR.SME_READ(so,file))
368 end
369
370 local uci = luci.model.uci.cursor( luci.fs.dirname(file), default_savedir )
371
372 local sd, err = uci:get_all( luci.fs.basename(file) )
373
374 if not sd then
375 return false, ERR.UCILOAD(so, err)
376 end
377
378 table.insert( schemes, sd )
379 end
380
381 local ok, err = self:_read_scheme_parts( so, schemes )
382 if ok and alias then self.packages[alias] = self.packages[scheme] end
383 return ok, err
384 else
385 return false, so:error(ERR.SME_FIND(so, self.schemedir))
386 end
387 else
388 local sc = loadfile(bc)
389 if sc then
390 self.packages[scheme] = sc()
391 return true
392 else
393 return false, so:error(ERR.SME_READ(so,file))
394 end
395 end
396 end
397
398 -- Process all given parts and construct validation tree
399 function UVL._read_scheme_parts( self, scheme, schemes )
400
401 -- helper function to check for required fields
402 local function _req( t, n, c, r )
403 for i, v in ipairs(r) do
404 if not c[v] then
405 local p, o = scheme:sid(), nil
406
407 if t == TYPE_SECTION then
408 o = section( scheme, nil, p, n )
409 elseif t == TYPE_OPTION then
410 o = option( scheme, nil, p, '(nil)', n )
411 elseif t == TYPE_ENUM then
412 o = enum( scheme, nil, p, '(nil)', '(nil)', n )
413 end
414
415 return false, ERR.SME_REQFLD(o,v)
416 end
417 end
418 return true
419 end
420
421 -- helper function to validate references
422 local function _ref( c, t )
423 local k, n
424 if c == TYPE_SECTION then
425 k = "package"
426 n = 1
427 elseif c == TYPE_OPTION then
428 k = "section"
429 n = 2
430 elseif c == TYPE_ENUM then
431 k = "variable"
432 n = 3
433 end
434
435 local r = luci.util.split( t[k], "." )
436 r[1] = ( #r[1] > 0 and r[1] or scheme:sid() )
437
438 if #r ~= n then
439 return false, ERR.SME_BADREF(scheme, k)
440 end
441
442 return r
443 end
444
445 -- helper function to read bools
446 local function _bool( v )
447 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
448 end
449
450
451 local ok, err
452
453 -- Step 0: get package meta information
454 for i, conf in ipairs( schemes ) do
455 for k, v in pairs( conf ) do
456 if v['.type'] == 'package' then
457 self.packages[scheme:sid()] =
458 self.packages[scheme:sid()] or {
459 ["name"] = scheme:sid();
460 ["sections"] = { };
461 ["variables"] = { };
462 }
463
464 for k, v2 in pairs(v) do
465 if k == "title" or k == "description" then
466 self.packages[scheme:sid()][k] = v2
467 end
468 end
469 end
470 end
471 end
472
473 -- Step 1: get all sections
474 for i, conf in ipairs( schemes ) do
475 for k, v in pairs( conf ) do
476 if v['.type'] == 'section' then
477
478 ok, err = _req( TYPE_SECTION, k, v, { "name", "package" } )
479 if err then return false, scheme:error(err) end
480
481 local r, err = _ref( TYPE_SECTION, v )
482 if err then return false, scheme:error(err) end
483
484 self.packages[r[1]] =
485 self.packages[r[1]] or {
486 ["name"] = r[1];
487 ["sections"] = { };
488 ["variables"] = { };
489 }
490
491 local p = self.packages[r[1]]
492 p.sections[v.name] = p.sections[v.name] or { }
493 p.variables[v.name] = p.variables[v.name] or { }
494
495 local s = p.sections[v.name]
496 local so = scheme:section(v.name)
497
498 for k, v2 in pairs(v) do
499 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
500 if k == "depends" then
501 s.depends = self:_read_dependency( v2, s.depends )
502 if not s.depends then
503 return false, scheme:error(
504 ERR.SME_BADDEP(so, luci.util.serialize_data(s.depends))
505 )
506 end
507 elseif k == "dynamic" or k == "unique" or
508 k == "required" or k == "named"
509 then
510 s[k] = _bool(v2)
511 else
512 s[k] = v2
513 end
514 end
515 end
516
517 s.dynamic = s.dynamic or false
518 s.unique = s.unique or false
519 s.required = s.required or false
520 s.named = s.named or false
521 end
522 end
523 end
524
525 -- Step 2: get all variables
526 for i, conf in ipairs( schemes ) do
527 for k, v in pairs( conf ) do
528 if v['.type'] == "variable" then
529
530 ok, err = _req( TYPE_OPTION, k, v, { "name", "section" } )
531 if err then return false, scheme:error(err) end
532
533 local r, err = _ref( TYPE_OPTION, v )
534 if err then return false, scheme:error(err) end
535
536 local p = self.packages[r[1]]
537 if not p then
538 return false, scheme:error(
539 ERR.SME_VBADPACK({scheme:sid(), '', v.name}, r[1])
540 )
541 end
542
543 local s = p.variables[r[2]]
544 if not s then
545 return false, scheme:error(
546 ERR.SME_VBADSECT({scheme:sid(), '', v.name}, r[2])
547 )
548 end
549
550 s[v.name] = s[v.name] or { }
551
552 local t = s[v.name]
553 local so = scheme:section(r[2])
554 local to = so:option(v.name)
555
556 for k, v2 in pairs(v) do
557 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
558 if k == "depends" then
559 t.depends = self:_read_dependency( v2, t.depends )
560 if not t.depends then
561 return false, scheme:error(so:error(
562 ERR.SME_BADDEP(to, luci.util.serialize_data(v2))
563 ))
564 end
565 elseif k == "validator" then
566 t.validators = self:_read_validator( v2, t.validators )
567 if not t.validators then
568 return false, scheme:error(so:error(
569 ERR.SME_BADVAL(to, luci.util.serialize_data(v2))
570 ))
571 end
572 elseif k == "valueof" then
573 local values, err = self:_read_reference( v2 )
574 if err then
575 return false, scheme:error(so:error(
576 ERR.REFERENCE(to, luci.util.serialize_data(v2)):child(err)
577 ))
578 end
579 t.type = "reference"
580 t.values = values
581 elseif k == "required" then
582 t[k] = _bool(v2)
583 else
584 t[k] = t[k] or v2
585 end
586 end
587 end
588
589 t.type = t.type or "variable"
590 t.datatype = t.datatype or "string"
591 t.required = t.required or false
592 end
593 end
594 end
595
596 -- Step 3: get all enums
597 for i, conf in ipairs( schemes ) do
598 for k, v in pairs( conf ) do
599 if v['.type'] == "enum" then
600
601 ok, err = _req( TYPE_ENUM, k, v, { "value", "variable" } )
602 if err then return false, scheme:error(err) end
603
604 local r, err = _ref( TYPE_ENUM, v )
605 if err then return false, scheme:error(err) end
606
607 local p = self.packages[r[1]]
608 if not p then
609 return false, scheme:error(
610 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
611 )
612 end
613
614 local s = p.variables[r[2]]
615 if not s then
616 return false, scheme:error(
617 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
618 )
619 end
620
621 local t = s[r[3]]
622 if not t then
623 return false, scheme:error(
624 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
625 )
626 end
627
628
629 local so = scheme:section(r[2])
630 local oo = so:option(r[3])
631 local eo = oo:enum(v.value)
632
633 if t.type ~= "enum" then
634 return false, scheme:error(ERR.SME_EBADTYPE(eo))
635 end
636
637 if not t.values then
638 t.values = { [v.value] = v.title or v.value }
639 else
640 t.values[v.value] = v.title or v.value
641 end
642
643 if not t.enum_depends then
644 t.enum_depends = { }
645 end
646
647 if v.default then
648 if t.default then
649 return false, scheme:error(ERR.SME_EBADDEF(eo))
650 end
651 t.default = v.value
652 end
653
654 if v.depends then
655 t.enum_depends[v.value] = self:_read_dependency(
656 v.depends, t.enum_depends[v.value]
657 )
658
659 if not t.enum_depends[v.value] then
660 return false, scheme:error(so:error(oo:error(
661 ERR.SME_BADDEP(eo, luci.util.serialize_data(v.depends))
662 )))
663 end
664 end
665 end
666 end
667 end
668
669 return self
670 end
671
672 -- Read a dependency specification
673 function UVL._read_dependency( self, values, deps )
674 local expr = "%$?[a-zA-Z0-9_]+"
675 if values then
676 values = ( type(values) == "table" and values or { values } )
677 for _, value in ipairs(values) do
678 local parts = luci.util.split( value, "%s*,%s*", nil, true )
679 local condition = { }
680 for i, val in ipairs(parts) do
681 local k, v = unpack(luci.util.split(val, "%s*=%s*", nil, true))
682
683 if k and (
684 k:match("^"..expr.."%."..expr.."%."..expr.."$") or
685 k:match("^"..expr.."%."..expr.."$") or
686 k:match("^"..expr.."$")
687 ) then
688 condition[k] = v or true
689 else
690 return nil
691 end
692 end
693
694 if not deps then
695 deps = { condition }
696 else
697 table.insert( deps, condition )
698 end
699 end
700 end
701
702 return deps
703 end
704
705 -- Read a validator specification
706 function UVL._read_validator( self, values, validators )
707 if values then
708 values = ( type(values) == "table" and values or { values } )
709 for _, value in ipairs(values) do
710 local validator
711
712 if value:match("^exec:") then
713 validator = value:gsub("^exec:","")
714 elseif value:match("^lua:") then
715 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
716 elseif value:match("^regexp:") then
717 local pattern = value:gsub("^regexp:","")
718 validator = function( type, dtype, pack, sect, optn, ... )
719 local values = { ... }
720 for _, v in ipairs(values) do
721 local ok, match =
722 luci.util.copcall( string.match, v, pattern )
723
724 if not ok then
725 return false, match
726 elseif not match then
727 return false,
728 'Value "%s" does not match pattern "%s"' % {
729 v, pattern
730 }
731 end
732 end
733 return true
734 end
735 end
736
737 if validator then
738 if not validators then
739 validators = { validator }
740 else
741 table.insert( validators, validator )
742 end
743 else
744 return nil
745 end
746 end
747
748 return validators
749 end
750 end
751
752 -- Read a reference specification (XXX: We should validate external configs too...)
753 function UVL._read_reference( self, values )
754 local val = { }
755 values = ( type(values) == "table" and values or { values } )
756
757 for _, value in ipairs(values) do
758 local ref = luci.util.split(value, ".")
759
760 if #ref == 2 or #ref == 3 then
761 local co = luci.uvl.config( self, ref[1] )
762 if not co:config() then return false, co:errors() end
763
764 for k, v in pairs(co:config()) do
765 if v['.type'] == ref[2] then
766 if #ref == 2 then
767 if v['.anonymous'] == true then
768 return false, ERR.SME_INVREF('', value)
769 end
770 val[k] = k -- XXX: title/description would be nice
771 elseif v[ref[3]] then
772 val[v[ref[3]]] = v[ref[3]] -- XXX: dito
773 end
774 end
775 end
776 else
777 return false, ERR.SME_BADREF('', value)
778 end
779 end
780
781 return val, nil
782 end
783
784 -- Resolve given path
785 function UVL._resolve_function( self, value )
786 local path = luci.util.split(value, ".")
787
788 for i=1, #path-1 do
789 local stat, mod = luci.util.copcall(
790 require, table.concat(path, ".", 1, i)
791 )
792
793 if stat and mod then
794 for j=i+1, #path-1 do
795 if not type(mod) == "table" then
796 break
797 end
798 mod = mod[path[j]]
799 if not mod then
800 break
801 end
802 end
803 mod = type(mod) == "table" and mod[path[#path]] or nil
804 if type(mod) == "function" then
805 return mod
806 end
807 end
808 end
809 end
810
811
812 --- Object representation of an uvl item - base class.
813 uvlitem = luci.util.class()
814
815 function uvlitem.cid(self)
816 return table.concat( self.cref, '.' )
817 end
818
819 function uvlitem.sid(self)
820 return table.concat( self.sref, '.' )
821 end
822
823 function uvlitem.scheme(self, opt)
824 local s
825
826 if #self.sref == 4 or #self.sref == 3 then
827 s = self.s and self.s.packages
828 s = s and s[self.sref[1]]
829 s = s and s.variables
830 s = s and s[self.sref[2]]
831 s = s and s[self.sref[3]]
832 elseif #self.sref == 2 then
833 s = self.s and self.s.packages
834 s = s and s[self.sref[1]]
835 s = s and s.sections
836 s = s and s[self.sref[2]]
837 else
838 s = self.s and self.s.packages
839 s = s and s[self.sref[1]]
840 end
841
842 if s and opt then
843 return s[opt]
844 elseif s then
845 return s
846 end
847 end
848
849 function uvlitem.config(self, opt)
850 local c
851
852 if #self.cref == 4 or #self.cref == 3 then
853 c = self.c and self.c[self.cref[2]] or nil
854 c = c and c[self.cref[3]] or nil
855 elseif #self.cref == 2 then
856 c = self.c and self.c[self.cref[2]] or nil
857 else
858 c = self.c
859 end
860
861 if c and opt then
862 return c[opt]
863 elseif c then
864 return c
865 end
866 end
867
868 function uvlitem.title(self)
869 return self:scheme() and self:scheme('title') or
870 self.cref[3] or self.cref[2] or self.cref[1]
871 end
872
873 function uvlitem.type(self)
874 if self.t == luci.uvl.TYPE_CONFIG then
875 return 'config'
876 elseif self.t == luci.uvl.TYPE_SECTION then
877 return 'section'
878 elseif self.t == luci.uvl.TYPE_OPTION then
879 return 'option'
880 elseif self.t == luci.uvl.TYPE_ENUM then
881 return 'enum'
882 end
883 end
884
885 function uvlitem.error(self, ...)
886 if not self.e then
887 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
888 self.e = errconst[#self.cref]( self )
889 end
890
891 return self.e:child( ... )
892 end
893
894 function uvlitem.errors(self)
895 return self.e
896 end
897
898 function uvlitem.ok(self)
899 return not self:errors()
900 end
901
902 function uvlitem.parent(self)
903 if self.p then
904 return self.p
905 elseif #self.cref == 3 or #self.cref == 4 then
906 return luci.uvl.section( self.s, self.c, self.cref[1], self.cref[2] )
907 elseif #self.cref == 2 then
908 return luci.uvl.config( self.s, self.c, self.cref[1] )
909 else
910 return nil
911 end
912 end
913
914 function uvlitem._loadconf(self, co, c)
915 if not co then
916 local uci, err = luci.model.uci.cursor(), nil
917 co, err = uci:get_all(c)
918
919 if err then
920 self:error(ERR.UCILOAD(self, err))
921 end
922 end
923 return co
924 end
925
926
927 --- Object representation of a scheme.
928 -- @class scheme
929 -- @cstyle instance
930 -- @name luci.uvl.scheme
931
932 --- Scheme instance constructor.
933 -- @class function
934 -- @name scheme
935 -- @param scheme Scheme instance
936 -- @param co Configuration data
937 -- @param c Configuration name
938 -- @return Config instance
939 scheme = luci.util.class(uvlitem)
940
941 function scheme.__init__(self, scheme, co, c)
942 if not c then
943 c, co = co, nil
944 end
945
946 self.cref = { c }
947 self.sref = { c }
948 self.c = self:_loadconf(co, c)
949 self.s = scheme
950 self.t = luci.uvl.TYPE_SCHEME
951 end
952
953 --- Add an error to scheme.
954 -- @return Scheme error context
955 function scheme.error(self, ...)
956 if not self.e then self.e = ERR.SCHEME( self ) end
957 return self.e:child( ... )
958 end
959
960 --- Get an associated config object.
961 -- @return Config instance
962 function scheme.config(self)
963 local co = luci.uvl.config( self.s, self.cref[1] )
964 co.p = self
965
966 return co
967 end
968
969 --- Get all section objects associated with this scheme.
970 -- @return Table containing all associated luci.uvl.section instances
971 function scheme.sections(self)
972 local v = { }
973 if self.s.packages[self.sref[1]].sections then
974 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
975 table.insert( v, luci.uvl.option(
976 self.s, self.c, self.cref[1], self.cref[2], o
977 ) )
978 end
979 end
980 return v
981 end
982
983 --- Get an associated section object.
984 -- @param s Section to select
985 -- @return Section instance
986 function scheme.section(self, s)
987 local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
988 so.p = self
989
990 return so
991 end
992
993
994 --- Object representation of a config.
995 -- @class config
996 -- @cstyle instance
997 -- @name luci.uvl.config
998
999 --- Config instance constructor.
1000 -- @class function
1001 -- @name config
1002 -- @param scheme Scheme instance
1003 -- @param co Configuration data
1004 -- @param c Configuration name
1005 -- @return Config instance
1006 config = luci.util.class(uvlitem)
1007
1008 function config.__init__(self, scheme, co, c)
1009 if not c then
1010 c, co = co, nil
1011 end
1012
1013 self.cref = { c }
1014 self.sref = { c }
1015 self.c = self:_loadconf(co, c)
1016 self.s = scheme
1017 self.t = luci.uvl.TYPE_CONFIG
1018 end
1019
1020 --- Get all section objects associated with this config.
1021 -- @return Table containing all associated luci.uvl.section instances
1022 function config.sections(self)
1023 local v = { }
1024 if self.s.packages[self.sref[1]].sections then
1025 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1026 table.insert( v, luci.uvl.option(
1027 self.s, self.c, self.cref[1], self.cref[2], o
1028 ) )
1029 end
1030 end
1031 return v
1032 end
1033
1034 --- Get an associated section object.
1035 -- @param s Section to select
1036 -- @return Section instance
1037 function config.section(self, s)
1038 local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
1039 so.p = self
1040
1041 return so
1042 end
1043
1044
1045 --- Object representation of a scheme/config section.
1046 -- @class module
1047 -- @cstyle instance
1048 -- @name luci.uvl.section
1049
1050 --- Section instance constructor.
1051 -- @class function
1052 -- @name section
1053 -- @param scheme Scheme instance
1054 -- @param co Configuration data
1055 -- @param c Configuration name
1056 -- @param s Section name
1057 -- @return Section instance
1058 section = luci.util.class(uvlitem)
1059
1060 function section.__init__(self, scheme, co, c, s)
1061 self.cref = { c, s }
1062 self.sref = { c, co and co[s] and co[s]['.type'] or s }
1063 self.c = self:_loadconf(co, c)
1064 self.s = scheme
1065 self.t = luci.uvl.TYPE_SECTION
1066 end
1067
1068 --- Get all option objects associated with this section.
1069 -- @return Table containing all associated luci.uvl.option instances
1070 function section.variables(self)
1071 local v = { }
1072 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
1073 for o, _ in pairs(
1074 self.s.packages[self.sref[1]].variables[self.sref[2]]
1075 ) do
1076 table.insert( v, luci.uvl.option(
1077 self.s, self.c, self.cref[1], self.cref[2], o
1078 ) )
1079 end
1080 end
1081 return v
1082 end
1083
1084 --- Get an associated option object.
1085 -- @param o Option to select
1086 -- @return Option instance
1087 function section.option(self, o)
1088 local oo = luci.uvl.option( self.s, self.c, self.cref[1], self.cref[2], o )
1089 oo.p = self
1090
1091 return oo
1092 end
1093
1094
1095 --- Object representation of a scheme/config option.
1096 -- @class module
1097 -- @cstyle instance
1098 -- @name luci.uvl.option
1099
1100 --- Section instance constructor.
1101 -- @class function
1102 -- @name option
1103 -- @param scheme Scheme instance
1104 -- @param co Configuration data
1105 -- @param c Configuration name
1106 -- @param s Section name
1107 -- @param o Option name
1108 -- @return Option instance
1109 option = luci.util.class(uvlitem)
1110
1111 function option.__init__(self, scheme, co, c, s, o)
1112 self.cref = { c, s, o }
1113 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1114 self.c = self:_loadconf(co, c)
1115 self.s = scheme
1116 self.t = luci.uvl.TYPE_OPTION
1117 end
1118
1119 --- Get the value of this option.
1120 -- @return The associated configuration value
1121 function option.value(self)
1122 local v = self:config()
1123 if v and self:scheme('multival') then
1124 v = luci.util.split( v, "%s+", nil, true )
1125 end
1126 return v
1127 end
1128
1129 --- Get the associated section information in scheme.
1130 -- @return Table containing the scheme properties
1131 function option.section(self)
1132 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1133 end
1134
1135 --- Construct an enum object instance from given or default value.
1136 -- @param v Value to select
1137 -- @return Enum instance for selected value
1138 function option.enum(self, val)
1139 return enum(
1140 self.s, self.c,
1141 self.cref[1], self.cref[2], self.cref[3],
1142 val or self:value()
1143 )
1144 end
1145
1146
1147 --- Object representation of a enum value.
1148 -- @class module
1149 -- @cstyle instance
1150 -- @name luci.uvl.enum
1151
1152 --- Section instance constructor.
1153 -- @class function
1154 -- @name enum
1155 -- @param scheme Scheme instance
1156 -- @param co Configuration data
1157 -- @param c Configuration name
1158 -- @param s Section name
1159 -- @param o Enum name
1160 -- @param v Enum value
1161 -- @return Enum value instance
1162 enum = luci.util.class(option)
1163
1164 function enum.__init__(self, scheme, co, c, s, o, v)
1165 self.cref = { c, s, o, v }
1166 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1167 self.c = self:_loadconf(co, c)
1168 self.s = scheme
1169 self.t = luci.uvl.TYPE_ENUM
1170 end