* luci/libs: uvl: Major rewrite of internal element handling, reworked error model, dropped loghelper and get_dependencies(), introduced luci.uvl.errors api

This commit is contained in:
Jo-Philipp Wich 2008-08-27 16:44:07 +00:00
parent 86a3d500d1
commit aa94931fa0
6 changed files with 768 additions and 487 deletions

File diff suppressed because it is too large Load diff

View file

@ -16,6 +16,8 @@ $Id$
module( "luci.uvl.dependencies", package.seeall )
local ERR = luci.uvl.errors
function _parse_reference( r, c, s, o )
local ref = { }
local vars = {
@ -49,121 +51,148 @@ function _parse_reference( r, c, s, o )
return ref
end
function _serialize_dependency( dep, v )
local str
for k, v in luci.util.spairs( dep,
function(a,b)
a = ( type(dep[a]) ~= "boolean" and "_" or "" ) .. a
b = ( type(dep[b]) ~= "boolean" and "_" or "" ) .. b
return a < b
end
) do
str = ( str and str .. " and " or "" ) .. k ..
( type(v) ~= "boolean" and "=" .. v or "" )
end
return str
end
function check( self, object, nodeps )
local derr = ERR.DEPENDENCY(object)
if not self.beenthere[object:cid()] then
self.beenthere[object:cid()] = true
else
return false, "Recursive dependency for '" .. object:sid() .. "' found"
return false, derr:child(ERR.DEP_RECURSIVE(object))
end
local item = object.type == luci.uvl.TYPE_SECTION
and object:section() or object:option()
if object:scheme('depends') then
local ok = true
local valid = false
if item.depends then
local ok = false
local valid, err = false,
string.format( 'In dependency check for %s "%s":',
( object.type == luci.uvl.TYPE_SECTION and "section" or "option" ),
object:cid() )
for _, dep in ipairs(item.depends) do
for _, dep in ipairs(object:scheme('depends')) do
local subcondition = true
for k, v in pairs(dep) do
-- XXX: better error
local ref = _parse_reference( k, unpack(object.cref) )
if not ref then
return false, "Ambiguous dependency reference '" .. k ..
"' for object '" .. object:sid() .. "' given"
return false, derr:child(ERR.SME_BADDEP(object,k))
end
local option = luci.uvl.option(
self, object.config,
object.config[ref[2]]
and object.config[ref[2]]['.type']
or object.sref[2],
ref[1], ref[2], ref[3]
)
local option = luci.uvl.option( self, object.c, unpack(ref) )
valid, err2 = self:_validate_option( option, true )
valid, err = self:_validate_option( option, true )
if valid then
if not (
( type(v) == "boolean" and object.config[ref[2]][ref[3]] ) or
( ref[3] and object.config[ref[2]][ref[3]] ) == v
( type(v) == "boolean" and option:value() ) or
( ref[3] and option:value() ) == v
) then
subcondition = false
err = err .. "\n" ..
self.log.dump_dependency( dep, ref, v )
local depstr = _serialize_dependency( dep, v )
derr:child(
type(v) == "boolean"
and ERR.DEP_NOVALUE(option, depstr)
or ERR.DEP_NOTEQUAL(option, {depstr, v})
)
break
end
else
subcondition = false
err = err .. "\n" ..
self.log.dump_dependency( dep, ref, nil, err2 )
local depstr = _serialize_dependency( dep, v )
derr:child(ERR.DEP_NOTVALID(option, depstr):child(err))
break
end
end
if subcondition then
return true
ok = true
break
else
ok = false
end
end
return false, err
if not ok then
return false, derr
end
else
return true
end
if item.type == "enum" and item.enum_depends[object:value()] then
local ok = false
local valid, err = false,
string.format( 'In dependency check for enum value "%s.%s":',
object:cid(), object:value() )
if object:scheme("type") == "enum" and
object:scheme("enum_depends")[object:value()]
then
local ok = true
local valid = false
local enum = object:enum()
local eerr = ERR.DEP_BADENUM(enum)
for _, dep in ipairs(item.enum_depends[object:value()]) do
for _, dep in ipairs(enum:scheme('enum_depends')[object:value()]) do
local subcondition = true
for k, v in pairs(dep) do
-- XXX: better error
local ref = _parse_reference( k, unpack(object.cref) )
if not ref then
return false, "Ambiguous dependency reference '" .. k ..
"' for enum '" .. object:sid() .. "." ..
object:value() .. "' given"
return false, derr:child(eerr:child(ERR.SME_BADDEP(enum,k)))
end
local option = luci.uvl.option(
self, object.config,
object.config[ref[2]]
and object.config[ref[2]]['.type']
or object.sref[2],
ref[1], ref[2], ref[3]
)
local option = luci.uvl.option( self, object.c, unpack(ref) )
valid, err2 = self:_validate_option( option, true )
valid, err = self:_validate_option( option, true )
if valid then
if not (
( type(v) == "boolean" and object.config[ref[2]][ref[3]] ) or
( ref[3] and object.config[ref[2]][ref[3]] ) == v
( ref[3] and object:config() ) == v
) then
subcondition = false
err = err .. "\n" ..
self.log.dump_dependency( dep, ref, v )
local depstr = _serialize_dependency( dep, v )
eerr:child(
type(v) == "boolean"
and ERR.DEP_NOVALUE(option, depstr)
or ERR.DEP_NOTEQUAL(option, {depstr, v})
)
break
end
else
subcondition = false
err = err .. "\n" ..
self.log.dump_dependency( dep, ref, nil, err2 )
local depstr = _serialize_dependency( dep, v )
eerr:child(ERR.DEP_NOTVALID(option, depstr):child(err))
break
end
end
if subcondition then
return true
else
ok = false
end
end
return false, err
if not ok then
return false, derr:child(eerr)
end
end
return true

View file

@ -0,0 +1,146 @@
--[[
UCI Validation Layer - Error handling
(c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
(c) 2008 Steven Barth <steven@midlink.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
$Id$
]]--
module( "luci.uvl.errors", package.seeall )
require("luci.util")
ERRCODES = {
{ 'UCILOAD', 'Unable to load config "%p"' },
{ 'SCHEME', 'Error in scheme "%p":\n%c' },
{ 'CONFIG', 'Error in config "%p":\n%c' },
{ 'SECTION', 'Error in section "%p.%s":\n%c' },
{ 'OPTION', 'Error in option "%p.%s.%o":\n%c' },
{ 'REFERENCE', 'Option "%p.%s.%o" has invalid reference specification "%1":\n%c' },
{ 'DEPENDENCY', 'In dependency check for %t "%i":\n%c' },
{ 'SME_FIND', 'Can not find scheme "%p" in "%1"' },
{ 'SME_READ', 'Can not access file "%1"' },
{ 'SME_REQFLD', 'Missing required scheme field "%1" in "%i"' },
{ 'SME_INVREF', 'Illegal reference "%1" to an anonymous section' },
{ 'SME_BADREF', 'Malformed reference in "%1"' },
{ 'SME_BADDEP', 'Malformed dependency specification "%1" in "%i"' },
{ 'SME_BADVAL', 'Malformed validator specification "%1" in "%i"' },
{ 'SME_ERRVAL', 'External validator "%1" failed: %2' },
{ 'SME_VBADPACK', 'Variable "%o" in scheme "%p" references unknown package "%1"' },
{ 'SME_VBADSECT', 'Variable "%o" in scheme "%p" references unknown section "%1"' },
{ 'SME_EBADPACK', 'Enum "%v" in scheme "%p" references unknown package "%1"' },
{ 'SME_EBADSECT', 'Enum "%v" in scheme "%p" references unknown section "%1"' },
{ 'SME_EBADOPT', 'Enum "%v" in scheme "%p" references unknown option "%1"' },
{ 'SME_EBADTYPE', 'Enum "%v" in scheme "%p" references non-enum option "%p.%s.%o"' },
{ 'SME_EBADDEF', 'Enum "%v" in scheme "%p" redeclares the default value of "%p.%s.%o"' },
{ 'SECT_UNKNOWN', 'Section "%p.%s" not found in scheme' },
{ 'SECT_REQUIRED', 'Required section "%p.%S" not found in config' },
{ 'SECT_UNIQUE', 'Unique section "%p.%S" occurs multiple times in config' },
{ 'SECT_NAMED', 'The section of type "%p.%S" is stored anonymously in config but must be named' },
{ 'SECT_NOTFOUND', 'Section "%p.%s" not found in config' },
{ 'OPT_UNKNOWN', 'Option "%1" not found in scheme' },
{ 'OPT_REQUIRED', 'Required option "%i" has no value' },
{ 'OPT_BADVALUE', 'Value "%1" of option "%i" is not defined in %t { %2 }' },
{ 'OPT_INVVALUE', 'Value "%1" of given option "%i" does not validate as datatype "%2"' },
{ 'OPT_NOTLIST', 'Option "%i" is defined as list but stored as plain value' },
{ 'OPT_DATATYPE', 'Option "%i" has unknown datatype "%1"' },
{ 'OPT_NOTFOUND', 'Option "%p.%s.%o" not found in config' },
{ 'DEP_NOTEQUAL', 'Dependency (%1) failed:\nOption "%i" is not eqal "%2"' },
{ 'DEP_NOVALUE', 'Dependency (%1) failed:\nOption "%i" has no value' },
{ 'DEP_NOTVALID', 'Dependency (%1) failed:\n%c' },
{ 'DEP_RECURSIVE', 'Recursive dependency for option "%i" detected' },
{ 'DEP_BADENUM', 'In dependency check for enum value "%i":\n%c' }
}
-- build error constants
for i, v in ipairs(ERRCODES) do
luci.uvl.errors[v[1]] = function(...)
return error(i, ...)
end
end
error = luci.util.class()
function error.__init__(self, code, pso, args)
self.code = code
self.args = ( type(args) == "table" and args or { args } )
if luci.util.instanceof( pso, luci.uvl.uvlitem ) then
self.stype = pso.sref[2]
self.package, self.section, self.option, self.value = unpack(pso.cref)
self.object = pso
else
pso = ( type(pso) == "table" and pso or { pso } )
if pso[2] then
local uci = luci.model.uci.cursor()
self.stype = uci:get(pso[1], pso[2]) or pso[2]
end
self.package, self.section, self.option, self.value = unpack(pso)
end
end
function error.child(self, err)
if not self.childs then
self.childs = { err }
else
table.insert( self.childs, err )
end
return self
end
function error.string(self,pad)
pad = pad or " "
local str = ERRCODES[self.code][2]
:gsub("\n", "\n"..pad)
:gsub("%%i", self:cid())
:gsub("%%I", self:sid())
:gsub("%%p", self.package or '<nil>')
:gsub("%%s", self.section or '<nil>')
:gsub("%%S", self.stype or '<nil>')
:gsub("%%o", self.option or '<nil>')
:gsub("%%v", self.value or '<nil>')
:gsub("%%t", self.object and self.object:type() or '<nil>' )
:gsub("%%T", self.object and self.object:title() or '<nil>' )
:gsub("%%([1-9])", function(n) error(n) return self.args[tonumber(n)] or '<nil>' end)
:gsub("%%c",
function()
local s = ""
for _, err in ipairs(self.childs or {}) do
s = s .. err:string(pad.." ") .. "\n" .. pad
end
return s
end
)
return (str:gsub("%s+$",""))
end
function error.cid(self)
return self.object and self.object:cid() or self.package ..
( self.section and '.' .. self.section or '' ) ..
( self.option and '.' .. self.option or '' ) ..
( self.value and '.' .. self.value or '' )
end
function error.sid(self)
return self.object and self.object:sid() or self.package ..
( self.stype and '.' .. self.stype or '' ) ..
( self.option and '.' .. self.option or '' ) ..
( self.value and '.' .. self.value or '' )
end

View file

@ -1,81 +0,0 @@
--[[
UCI Validation Layer - Logging utilities
(c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
(c) 2008 Steven Barth <steven@midlink.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
$Id$
]]--
module( "luci.uvl.loghelper", package.seeall )
function config_error( config, message )
return string.format(
'Error in config "%s":\n%s',
config, message or "Unknown error"
)
end
function section_error( section, message )
return string.format(
'Error in section "%s":\n%s',
section:cid(), message or "Unknown error"
)
end
function validator_error( option, message )
return string.format(
'External validator in option "%s" failed:\n%s',
option:cid(), message or "Unknown error"
)
end
function scheme_error( scheme, message )
return string.format(
'Error while loading scheme "%s":\n%s',
scheme, message:gsub("^.-:.-: ","")
)
end
function dump_dependency( dep, ref, v, e )
local str = nil
for k, v in luci.util.spairs( dep,
function(a,b)
a = ( type(dep[a]) ~= "boolean" and "_" or "" ) .. a
b = ( type(dep[b]) ~= "boolean" and "_" or "" ) .. b
return a < b
end
) do
str = ( str and str .. " and " or "Dependency (" ) .. k ..
( type(v) ~= "boolean" and "=" .. v or "" )
end
str = string.format(
'%s) failed:\n\t%s',
str, e and e:gsub("\n","\n\t") or string.format(
'Option "%s" %s',
table.concat( ref, "." ), (
type(v) == "boolean"
and "has no value" or 'is not equal "' .. v .. '"'
)
)
)
return str
end
function id( c, s, o )
if type(c) == "table" then
c, s, o = unpack(c)
end
return c .. ( s and '.' .. s or '' ) .. ( o and '.' .. o or '' )
end

View file

@ -19,6 +19,7 @@ module( "luci.uvl.validation", package.seeall )
require("luci.fs")
require("luci.sys")
local ERR = luci.uvl.errors
function _exec( bin, args )
local cmd, output = "", nil
@ -39,13 +40,13 @@ function _exec( bin, args )
end
function check( self, object )
local item = object:option()
if item.validators then
for _, val in ipairs(item.validators) do
if object:scheme('validators') then
for _, val in ipairs(object:scheme('validators')) do
local ok, err = false, nil
local args = {
item.type, unpack(object.cref), item.datatype, object:value()
object:scheme('type'),
object.cref[1], object.cref[2], object.cref[3],
object:scheme('datatype'), object:value()
}
if type(val) == "function" then
@ -56,7 +57,7 @@ function check( self, object )
end
if not ok then
return false, err
return false, ERR.SME_ERRVAL(object, {tostring(val), err})
end
end
end

View file

@ -123,7 +123,7 @@ else
end
os.exit( 0 )
else
if not options.silent then print( err ) end
if not options.silent then print( err and err:string() or "Unknown error" ) end
os.exit( 1 )
end
end