libs/uci: implement get_bool() to retrive uci variables as boolean value

This commit is contained in:
Jo-Philipp Wich 2009-01-19 13:39:54 +00:00
parent c1eb7cf481
commit b204c9ec8d

View file

@ -60,7 +60,7 @@ local Cursor = getmetatable(cursor())
function Cursor.apply(self, configlist, command) function Cursor.apply(self, configlist, command)
configlist = self:_affected(configlist) configlist = self:_affected(configlist)
local reloadcmd = "/sbin/luci-reload " .. table.concat(configlist, " ") local reloadcmd = "/sbin/luci-reload " .. table.concat(configlist, " ")
return command and reloadcmd or os.execute(reloadcmd .. " >/dev/null 2>&1") return command and reloadcmd or os.execute(reloadcmd .. " >/dev/null 2>&1")
end end
@ -72,7 +72,7 @@ end
-- returns a boolean whether to delete the current section (optional) -- returns a boolean whether to delete the current section (optional)
function Cursor.delete_all(self, config, stype, comparator) function Cursor.delete_all(self, config, stype, comparator)
local del = {} local del = {}
if type(comparator) == "table" then if type(comparator) == "table" then
local tbl = comparator local tbl = comparator
comparator = function(section) comparator = function(section)
@ -80,11 +80,11 @@ function Cursor.delete_all(self, config, stype, comparator)
if section[k] ~= v then if section[k] ~= v then
return false return false
end end
end end
return true return true
end end
end end
local function helper (section) local function helper (section)
if not comparator or comparator(section) then if not comparator or comparator(section) then
@ -135,6 +135,16 @@ function Cursor.tset(self, config, section, values)
return stat return stat
end end
--- Get a boolean option and return it's value as true or false.
-- @param config UCI config
-- @param section UCI section name
-- @param option UCI option
-- @return Boolean
function Cursor.get_bool(self, ...)
local val = self:get(...)
return ( val == "1" or val == "true" or val == "yes" or val == "on" )
end
--- Get an option or list and return values as table. --- Get an option or list and return values as table.
-- @param config UCI config -- @param config UCI config
-- @param section UCI section name -- @param section UCI section name
@ -177,7 +187,7 @@ function Cursor._affected(self, configlist)
local function _resolve_deps(name) local function _resolve_deps(name)
local reload = {name} local reload = {name}
local deps = {} local deps = {}
c:foreach("ucitrack", name, c:foreach("ucitrack", name,
function(section) function(section)
if section.affects then if section.affects then
@ -186,16 +196,16 @@ function Cursor._affected(self, configlist)
end end
end end
end) end)
for i, dep in ipairs(deps) do for i, dep in ipairs(deps) do
for j, add in ipairs(_resolve_deps(dep)) do for j, add in ipairs(_resolve_deps(dep)) do
reload[#reload+1] = add reload[#reload+1] = add
end end
end end
return reload return reload
end end
-- Collect initscripts -- Collect initscripts
for j, config in ipairs(configlist) do for j, config in ipairs(configlist) do
for i, e in ipairs(_resolve_deps(config)) do for i, e in ipairs(_resolve_deps(config)) do
@ -204,7 +214,7 @@ function Cursor._affected(self, configlist)
end end
end end
end end
return reloadlist return reloadlist
end end