From 23101e71374383bdd1bae09166c2581daaa44b22 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Sun, 24 Aug 2008 14:20:20 +0000 Subject: [PATCH] * luci/libs: uvl: implement get_scheme() and get_dependencies() --- libs/uvl/luasrc/uvl.lua | 58 ++++++++++++++++++++++++++++ libs/uvl/luasrc/uvl/dependencies.lua | 30 +++++++++----- 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/libs/uvl/luasrc/uvl.lua b/libs/uvl/luasrc/uvl.lua index 292a3ba0d..a9359d0c3 100644 --- a/libs/uvl/luasrc/uvl.lua +++ b/libs/uvl/luasrc/uvl.lua @@ -74,11 +74,68 @@ function UVL.__init__( self, schemedir ) self.packages = { } self.beenthere = { } self.uci = luci.model.uci + self.dep = luci.uvl.dependencies self.log = luci.uvl.loghelper self.datatypes = luci.uvl.datatypes 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. -- @param config Name of the configuration to validate -- @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]] or { + ["name"] = r[1]; ["sections"] = { }; ["variables"] = { }; } diff --git a/libs/uvl/luasrc/uvl/dependencies.lua b/libs/uvl/luasrc/uvl/dependencies.lua index ced275e6b..fc407a985 100644 --- a/libs/uvl/luasrc/uvl/dependencies.lua +++ b/libs/uvl/luasrc/uvl/dependencies.lua @@ -19,21 +19,31 @@ module( "luci.uvl.dependencies", package.seeall ) function _parse_reference( r, c, s, o ) local ref = { } local vars = { - config = c, - section = s, - option = o + config = ( c or '$config' ), + section = ( s or '$section' ), + option = ( o or '$option' ) } 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 - if #ref == 1 and c and s then - ref = { c, s, ref[1] } - elseif #ref == 2 and c then - ref = { c, unpack(ref) } - elseif #ref ~= 3 then - ref = nil + if c or s then + if #ref == 1 and c and s then + ref = { c, s, ref[1] } + elseif #ref == 2 and c then + ref = { c, unpack(ref) } + 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 return ref -- 2.25.1