you may not use this file except in compliance with the License.
You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
+ http://www.apache.org/licenses/LICENSE-2.0
$Id$
-- @class module
-- @cstyle instance
-local fs = require "luci.fs"
+local fs = require "nixio.fs"
local uci = require "luci.model.uci"
local util = require "luci.util"
+local nutil = require "nixio.util"
local table = require "table"
local string = require "string"
-- @class function
-- @name UVL
-- @param schemedir Path to the scheme directory (optional)
+-- @param configdir Override config directory (optional)
-- @return Instance object
UVL = util.class()
-function UVL.__init__( self, schemedir )
+function UVL.__init__( self, schemedir, configdir )
self.schemedir = schemedir or default_schemedir
+ self.configdir = configdir
self.packages = { }
self.beenthere = { }
self.depseen = { }
end
elseif option:scheme() then
+ if not nodeps then
+ local ok, err = dependencies.check( self, option )
+ if not ok then
+ if not err:is_all(
+ ERR.ERR_OPT_REQUIRED,
+ ERR.ERR_DEP_NOTEQUAL,
+ ERR.ERR_DEP_NOVALUE
+ ) then
+ option:error(err)
+ return false, option:errors()
+ else
+ return true
+ end
+ end
+ end
+
if option:scheme('required') and not option:value() then
return false, option:error(ERR.OPT_REQUIRED(option))
return false, option:error(ERR.OPT_DATATYPE(option, dt))
end
end
- end
- if not nodeps then
- local ok, err = dependencies.check( self, option )
- if not ok then
- option:error(err)
+ val = ( type(val) == "table" and val or { val } )
+ for _, v in ipairs(val) do
+ if option:scheme('minlength') then
+ if #v < option:scheme('minlength') then
+ return false, option:error(ERR.OPT_RANGE(option))
+ end
+ end
+
+ if option:scheme('maxlength') then
+ if #v > option:scheme('maxlength') then
+ return false, option:error(ERR.OPT_RANGE(option))
+ end
+ end
+
+ local w = tonumber(v)
+
+ if option:scheme('minimum') then
+ if not w or w < option:scheme('minimum') then
+ return false, option:error(ERR.OPT_RANGE(option))
+ end
+ end
+
+ if option:scheme('maximum') then
+ if not w or w > option:scheme('maximum') then
+ return false, option:error(ERR.OPT_RANGE(option))
+ end
+ end
end
end
local bc = "%s/bytecode/%s.lua" %{ self.schemedir, shm }
if not fs.access(bc) then
- local files = fs.glob(self.schemedir .. '/*/' .. shm)
+ local files = nutil.consume((fs.glob(self.schemedir .. '/*/' .. shm)))
- if files then
+ if #files > 0 then
local ok, err
- for i, file in ipairs( files ) do
+ for _, file in ipairs(files) do
if not fs.access(file) then
return false, so:error(ERR.SME_READ(so,file))
end
s.named = s.named or false
end
-
- -- Step 2: get all variables
+-- Step 2: get all variables
function UVL._parse_var(self, scheme, k, v)
local ok, err = _req( TYPE_OPTION, k, v, { "name", "section" } )
if err then error(scheme:error(err)) end
t.valueof = type(v2) == "table" and v2 or {v2}
elseif k == "required" then
t[k] = _bool(v2)
+ elseif k == "minlength" or k == "maxlength" or
+ k == "minimum" or k == "maximum"
+ then
+ t[k] = tonumber(v2)
else
t[k] = t[k] or v2
end
if not t.values then
t.values = { [v.value] = v.title or v.value }
+ t.valuelist = { {value = v.value, title = v.title} }
else
t.values[v.value] = v.title or v.value
+ t.valuelist[#t.valuelist + 1] = {value = v.value, title = v.title}
end
if not t.enum_depends then
local c = self.c
if c and c[r[2]] and c[r[2]]['.anonymous'] and c[r[2]]['.index'] then
r[2] = '@' .. c[r[2]]['.type'] ..
- '[' .. tostring(c[r[2]]['.index']) .. ']'
+ '[' .. tostring(c[r[2]]['.index']) .. ']'
end
return table.concat( r, '.' )
end
if #self.cref >= 3 then
c = c and c[self.cref[3]] or nil
end
- end
+ end
if c and opt then
return c[opt]
end
end
-function uvlitem._loadconf(self, co, c)
+function uvlitem._loadconf(self, co, c, configdir)
co = co or self._configcache
if not co then
local err
- co, err = uci.cursor():get_all(c)
+ co, err = uci.cursor(configdir):get_all(c)
if err then
self:error(ERR.UCILOAD(self, err))
end
-
+
self._configcache = co
end
return co
self.cref = { c }
self.sref = { c }
- self.c = self:_loadconf(co, c)
+ self.c = self:_loadconf(co, c, scheme.configdir)
self.s = scheme
self.t = TYPE_SCHEME
end
if not c then
c, co = co, nil
end
-
self.cref = { c }
self.sref = { c }
- self.c = self:_loadconf(co, c)
+ self.c = self:_loadconf(co, c, scheme.configdir)
self.s = scheme
self.t = TYPE_CONFIG
end
function section.__init__(self, scheme, co, c, s)
self.cref = { c, s }
self.sref = { c, co and co[s] and co[s]['.type'] or s }
- self.c = self:_loadconf(co, c)
+ self.c = self:_loadconf(co, c, scheme.configdir)
self.s = scheme
self.t = TYPE_SECTION
end
function option.__init__(self, scheme, co, c, s, o)
self.cref = { c, s, o }
self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
- self.c = self:_loadconf(co, c)
+ self.c = self:_loadconf(co, c, scheme.configdir)
self.s = scheme
self.t = TYPE_OPTION
end
function enum.__init__(self, scheme, co, c, s, o, v)
self.cref = { c, s, o, v }
self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
- self.c = self:_loadconf(co, c)
+ self.c = self:_loadconf(co, c, scheme.configdir)
self.s = scheme
self.t = TYPE_ENUM
end