summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorManuel Munz <freifunk@somakoma.de>2010-12-12 15:01:39 +0000
committerManuel Munz <freifunk@somakoma.de>2010-12-12 15:01:39 +0000
commit99881d9cb66c49c0011360d54e8c9c14d271a859 (patch)
treefff5bf31857ef8516bdfd41ca66bdb3c932cbafa
parent80b5a4b6f88b8ecbcb27bfd062b37e249e78ced9 (diff)
libs/core: Create get_first() in uci model
-rw-r--r--libs/core/luasrc/model/uci.lua33
1 files changed, 31 insertions, 2 deletions
diff --git a/libs/core/luasrc/model/uci.lua b/libs/core/luasrc/model/uci.lua
index 46b0db8d82..270877677f 100644
--- a/libs/core/luasrc/model/uci.lua
+++ b/libs/core/luasrc/model/uci.lua
@@ -30,8 +30,9 @@ local table = require "table"
local setmetatable, rawget, rawset = setmetatable, rawget, rawset
-local error, pairs, ipairs, tostring = error, pairs, ipairs, tostring
-local require, getmetatable, type = require, getmetatable, type
+local require, getmetatable = require, getmetatable
+local error, pairs, ipairs = error, pairs, ipairs
+local type, tostring, tonumber = type, tostring, tonumber
--- LuCI UCI model library.
-- The typical workflow for UCI is: Get a cursor instance from the
@@ -167,6 +168,34 @@ function Cursor.get_list(self, config, section, option)
return nil
end
+--- Get the given option from the first section with the given type.
+-- @param config UCI config
+-- @param type UCI section type
+-- @param option UCI option (optional)
+-- @param default Default value (optional)
+-- @return UCI value
+function Cursor.get_first(self, conf, stype, opt, def)
+ local rv = def
+
+ self:foreach(conf, stype,
+ function(s)
+ local val = not opt and s['.name'] or s[opt]
+
+ if type(def) == "number" then
+ val = tonumber(val)
+ elseif type(def) == "boolean" then
+ val = (val == "1" or val == "true" or val == "enabled")
+ end
+
+ if val ~= nil then
+ rv = val
+ return false
+ end
+ end)
+
+ return rv
+end
+
--- Set given values as list.
-- @param config UCI config
-- @param section UCI section name