* luci/libs: uvl: implement get_scheme() and get_dependencies()

This commit is contained in:
Jo-Philipp Wich 2008-08-24 14:20:20 +00:00
parent 606f7dc313
commit 23101e7137
2 changed files with 78 additions and 10 deletions

View file

@ -74,11 +74,68 @@ function UVL.__init__( self, schemedir )
self.packages = { } self.packages = { }
self.beenthere = { } self.beenthere = { }
self.uci = luci.model.uci self.uci = luci.model.uci
self.dep = luci.uvl.dependencies
self.log = luci.uvl.loghelper self.log = luci.uvl.loghelper
self.datatypes = luci.uvl.datatypes self.datatypes = luci.uvl.datatypes
end end
--- Parse given scheme and return the scheme tree.
-- @param scheme Name of the scheme to parse
-- @return Table containing the parsed scheme or nil on error
-- @return String containing the reason for errors (if any)
function UVL.get_scheme( self, scheme )
if not self.packages[scheme] then
local ok, err = pcall( self.read_scheme, self, scheme )
if not ok then
return nil, self.log.scheme_error( scheme, err )
end
end
return self.packages[scheme], nil
end
--- Return a table containing the dependencies of specified section or option.
-- @param config Name of the configuration or parsed scheme object
-- @param section Type of the section
-- @param option Name of the option (optional)
-- @return Table containing the dependencies or nil on error
-- @return String containing the reason for errors (if any)
function UVL.get_dependencies( self, config, section, option )
config = ( type(config) == "string" and self:get_scheme(config) or config )
local deps = { }
local dt
if not config.sections[section] then return deps end
if option and config.variables[section][option] then
dt = config.variables[section][option].depends
else
dt = config.sections[section].depends
end
if dt then
for _, d in ipairs(dt) do
local sdeps = { }
for k, v in pairs(d) do
local r = self.dep._parse_reference( k )
if r then
sdeps[r] = v
else
return nil, string.format(
'Ambiguous dependency reference "%s" for object ' ..
'"%s.%s%s" given',
k, config.name, section,
option and '.' .. option or ''
)
end
end
table.insert( deps, sdeps )
end
end
return deps
end
--- Validate given configuration, section or option. --- Validate given configuration, section or option.
-- @param config Name of the configuration to validate -- @param config Name of the configuration to validate
-- @param section Name of the section to validate (optional) -- @param section Name of the section to validate (optional)
@ -423,6 +480,7 @@ function UVL._read_scheme_parts( self, scheme, schemes )
self.packages[r[1]] = self.packages[r[1]] =
self.packages[r[1]] or { self.packages[r[1]] or {
["name"] = r[1];
["sections"] = { }; ["sections"] = { };
["variables"] = { }; ["variables"] = { };
} }

View file

@ -19,21 +19,31 @@ module( "luci.uvl.dependencies", package.seeall )
function _parse_reference( r, c, s, o ) function _parse_reference( r, c, s, o )
local ref = { } local ref = { }
local vars = { local vars = {
config = c, config = ( c or '$config' ),
section = s, section = ( s or '$section' ),
option = o option = ( o or '$option' )
} }
for i, v in ipairs(luci.util.split(r,".")) do for i, v in ipairs(luci.util.split(r,".")) do
table.insert( ref, (v:gsub( "%$(.+)", function(n) return vars[n] end )) ) table.insert(ref, (v:gsub( "%$(.+)", function(n) return vars[n] end )))
end end
if #ref == 1 and c and s then if c or s then
ref = { c, s, ref[1] } if #ref == 1 and c and s then
elseif #ref == 2 and c then ref = { c, s, ref[1] }
ref = { c, unpack(ref) } elseif #ref == 2 and c then
elseif #ref ~= 3 then ref = { c, unpack(ref) }
ref = nil elseif #ref ~= 3 then
ref = nil
end
else
if #ref == 1 then
ref = { '$config', '$section', ref[1] }
elseif #ref == 2 then
ref = { '$config', unpack(ref) }
elseif #ref ~= 3 then
ref = nil
end
end end
return ref return ref