require("luci.fs")
require("luci.util")
-require("luci.model.uci")
+local 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
-
- 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
+ local s = self._scheme
- if s and opt then
- return s[opt]
- elseif s then
- return s
+ if not s then
+ s = self.s and self.s.packages and s[self.sref[1]]
+ if #self.sref > 1 then
+ s = s and s.sections and s[self.sref[2]]
+ end
+ if #self.sref > 2 then
+ s = s and s[self.sref[2]] and s[self.sref[3]]
+ end
+ self._scheme = s
end
+
+ return opt and (s and s[opt]) or s
end
function uvlitem.config(self, opt)
- local c
+ local c = self._config
- 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
+ if not c then
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
- if c and opt then
- return c[opt]
- elseif c then
- return c
- end
+ return opt and (c and c[opt]) or c
end
function uvlitem.title(self)
end
function uvlitem.type(self)
- 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
+ local _t = {
+ [TYPE_CONFIG] = 'config',
+ [TYPE_SECTION] = 'section',
+ [TYPE_OPTION] = 'option',
+ [TYPE_ENUM] = 'enum'
+ }
+
+ return _t[self.t]
end
function uvlitem.error(self, ...)
if self.p then
return self.p
elseif #self.cref == 3 or #self.cref == 4 then
- return luci.uvl.section( self.s, self.c, self.cref[1], self.cref[2] )
+ return section( self.s, self.c, self.cref[1], self.cref[2] )
elseif #self.cref == 2 then
- return luci.uvl.config( self.s, self.c, self.cref[1] )
+ return 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 uci, err = luci.model.uci.cursor(), nil
- co, err = uci:get_all(c)
+ local co, err = cursor: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 = luci.uvl.TYPE_SCHEME
+ self.t = TYPE_SCHEME
end
--- Add an error to scheme.
--- Get an associated config object.
-- @return Config instance
function scheme.config(self)
- local co = luci.uvl.config( self.s, self.cref[1] )
+ local co = 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, luci.uvl.option(
+ table.insert( v, 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 = luci.uvl.section( self.s, self.c, self.cref[1], s )
+ local so = 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 = luci.uvl.TYPE_CONFIG
+ self.t = 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 = luci.uvl.TYPE_SECTION
+ self.t = 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 = luci.uvl.TYPE_OPTION
+ self.t = 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 = luci.uvl.TYPE_ENUM
+ self.t = TYPE_ENUM
end
function _parse_reference( r, c, s, o )
local ref = { }
local vars = {
- config = ( c or '$config' ),
- section = ( s or '$section' ),
- option = ( o or '$option' )
+ config = c,
+ section = s,
+ option = o
}
- for i, v in ipairs(luci.util.split(r,".")) do
- table.insert(ref, (v:gsub( "%$(.+)", function(n) return vars[n] end )))
+ for v in r:gmatch("[^.]+") do
+ table.insert(ref, (v:gsub( "%$(.+)", vars )))
end
-
- 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
+
+ if #ref < 2 then
+ table.insert(ref, 1, s or '$section')
+ end
+ if #ref < 3 then
+ table.insert(ref, 1, c or '$config')
end
return ref