require("luci.fs")
require("luci.util")
-local uci = require("luci.model.uci")
+require("luci.model.uci")
require("luci.uvl.errors")
require("luci.uvl.datatypes")
require("luci.uvl.validation")
require("luci.uvl.dependencies")
-local cursor = uci.cursor()
TYPE_SCHEME = 0x00
TYPE_CONFIG = 0x01
end
function uvlitem.scheme(self, opt)
- local s = self._scheme
-
- if not s then
- s = self.s and self.s.packages and self.s.packages[self.sref[1]]
- if #self.sref == 2 then
- s = s and s.sections and s.sections[self.sref[2]]
- elseif #self.sref > 2 then
- s = s and s.variables and s.variables[self.sref[2]]
- and s.variables[self.sref[2]][self.sref[3]]
- end
- self._scheme = s
+ local s
+
+ if #self.sref == 4 or #self.sref == 3 then
+ s = self.s and self.s.packages
+ s = s and s[self.sref[1]]
+ s = s and s.variables
+ s = s and s[self.sref[2]]
+ s = s and s[self.sref[3]]
+ elseif #self.sref == 2 then
+ s = self.s and self.s.packages
+ s = s and s[self.sref[1]]
+ s = s and s.sections
+ s = s and s[self.sref[2]]
+ else
+ s = self.s and self.s.packages
+ s = s and s[self.sref[1]]
end
- return opt and (s and s[opt]) or s
+ if s and opt then
+ return s[opt]
+ elseif s then
+ return s
+ end
end
function uvlitem.config(self, opt)
- local c = self._config
+ local c
- if not c then
+ if #self.cref == 4 or #self.cref == 3 then
+ c = self.c and self.c[self.cref[2]] or nil
+ c = c and c[self.cref[3]] or nil
+ elseif #self.cref == 2 then
+ c = self.c and self.c[self.cref[2]] or nil
+ else
c = self.c
- if #self.cref > 1 then
- c = c and self.c[self.cref[2]]
- end
- if #self.cref > 2 then
- c = c and c[self.cref[3]]
- end
- self._config = c
end
- return opt and (c and c[opt]) or c
+ if c and opt then
+ return c[opt]
+ elseif c then
+ return c
+ end
end
function uvlitem.title(self)
end
function uvlitem.type(self)
- local _t = {
- [TYPE_CONFIG] = 'config',
- [TYPE_SECTION] = 'section',
- [TYPE_OPTION] = 'option',
- [TYPE_ENUM] = 'enum'
- }
-
- return _t[self.t]
+ if self.t == luci.uvl.TYPE_CONFIG then
+ return 'config'
+ elseif self.t == luci.uvl.TYPE_SECTION then
+ return 'section'
+ elseif self.t == luci.uvl.TYPE_OPTION then
+ return 'option'
+ elseif self.t == luci.uvl.TYPE_ENUM then
+ return 'enum'
+ end
end
function uvlitem.error(self, ...)
if self.p then
return self.p
elseif #self.cref == 3 or #self.cref == 4 then
- return section( self.s, self.c, self.cref[1], self.cref[2] )
+ return luci.uvl.section( self.s, self.c, self.cref[1], self.cref[2] )
elseif #self.cref == 2 then
- return config( self.s, self.c, self.cref[1] )
+ return luci.uvl.config( self.s, self.c, self.cref[1] )
else
return nil
end
end
--- Shared cache
-uvlitem._ucicache = {}
-
function uvlitem._loadconf(self, co, c)
- co = co or self._ucicache[c]
if not co then
- local err
- co, err = cursor:get_all(c)
+ local uci, err = luci.model.uci.cursor(), nil
+ co, err = uci:get_all(c)
if err then
self:error(ERR.UCILOAD(self, err))
end
-
- self._ucicache[c] = co
end
return co
end
self.sref = { c }
self.c = self:_loadconf(co, c)
self.s = scheme
- self.t = TYPE_SCHEME
+ self.t = luci.uvl.TYPE_SCHEME
end
--- Add an error to scheme.
--- Get an associated config object.
-- @return Config instance
function scheme.config(self)
- local co = config( self.s, self.cref[1] )
+ local co = luci.uvl.config( self.s, self.cref[1] )
co.p = self
return co
local v = { }
if self.s.packages[self.sref[1]].sections then
for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
- table.insert( v, option(
+ table.insert( v, luci.uvl.option(
self.s, self.c, self.cref[1], self.cref[2], o
) )
end
-- @param s Section to select
-- @return Section instance
function scheme.section(self, s)
- local so = section( self.s, self.c, self.cref[1], s )
+ local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
so.p = self
return so
self.sref = { c }
self.c = self:_loadconf(co, c)
self.s = scheme
- self.t = TYPE_CONFIG
+ self.t = luci.uvl.TYPE_CONFIG
end
--- Get all section objects associated with this config.
self.sref = { c, co and co[s] and co[s]['.type'] or s }
self.c = self:_loadconf(co, c)
self.s = scheme
- self.t = TYPE_SECTION
+ self.t = luci.uvl.TYPE_SECTION
end
--- Get all option objects associated with this section.
self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
self.c = self:_loadconf(co, c)
self.s = scheme
- self.t = TYPE_OPTION
+ self.t = luci.uvl.TYPE_OPTION
end
--- Get the value of this option.
self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
self.c = self:_loadconf(co, c)
self.s = scheme
- self.t = TYPE_ENUM
+ self.t = luci.uvl.TYPE_ENUM
end
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 v in r:gmatch("[^.]+") do
- table.insert(ref, (v:gsub( "%$(.+)", vars )))
+ for i, v in ipairs(luci.util.split(r,".")) do
+ table.insert(ref, (v:gsub( "%$(.+)", function(n) return vars[n] end )))
end
-
- if #ref < 2 then
- table.insert(ref, 1, s or '$section')
- end
- if #ref < 3 then
- table.insert(ref, 1, c or '$config')
+
+ 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