* luci/libs: uvl: implement get_scheme() and get_dependencies()
authorJo-Philipp Wich <jow@openwrt.org>
Sun, 24 Aug 2008 14:20:20 +0000 (14:20 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Sun, 24 Aug 2008 14:20:20 +0000 (14:20 +0000)
libs/uvl/luasrc/uvl.lua
libs/uvl/luasrc/uvl/dependencies.lua

index 292a3ba0d86b3dbb67e476472e54708cfb3924d1..a9359d0c35efb2eca370df2e3dd593cbe21bc9a2 100644 (file)
@@ -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"] = { };
                                        }
index ced275e6b38d94d364e6051a3b6e70799a8005ae..fc407a985912a1302db967bb0d414e7497681e74 100644 (file)
@@ -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