Revert UVL optimizations

This commit is contained in:
Steven Barth 2008-09-09 12:44:51 +00:00
parent 8dbf29e86e
commit 68ecfaf698
2 changed files with 76 additions and 60 deletions

View file

@ -23,13 +23,12 @@ module( "luci.uvl", package.seeall )
require("luci.fs") require("luci.fs")
require("luci.util") require("luci.util")
local uci = require("luci.model.uci") require("luci.model.uci")
require("luci.uvl.errors") require("luci.uvl.errors")
require("luci.uvl.datatypes") require("luci.uvl.datatypes")
require("luci.uvl.validation") require("luci.uvl.validation")
require("luci.uvl.dependencies") require("luci.uvl.dependencies")
local cursor = uci.cursor()
TYPE_SCHEME = 0x00 TYPE_SCHEME = 0x00
TYPE_CONFIG = 0x01 TYPE_CONFIG = 0x01
@ -832,37 +831,48 @@ function uvlitem.sid(self)
end end
function uvlitem.scheme(self, opt) function uvlitem.scheme(self, opt)
local s = self._scheme local s
if not s then if #self.sref == 4 or #self.sref == 3 then
s = self.s and self.s.packages and self.s.packages[self.sref[1]] s = self.s and self.s.packages
if #self.sref == 2 then s = s and s[self.sref[1]]
s = s and s.sections and s.sections[self.sref[2]] s = s and s.variables
elseif #self.sref > 2 then s = s and s[self.sref[2]]
s = s and s.variables and s.variables[self.sref[2]] s = s and s[self.sref[3]]
and s.variables[self.sref[2]][self.sref[3]] elseif #self.sref == 2 then
end s = self.s and self.s.packages
self._scheme = s 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 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 end
function uvlitem.config(self, opt) 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 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 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 end
function uvlitem.title(self) function uvlitem.title(self)
@ -871,14 +881,15 @@ function uvlitem.title(self)
end end
function uvlitem.type(self) function uvlitem.type(self)
local _t = { if self.t == luci.uvl.TYPE_CONFIG then
[TYPE_CONFIG] = 'config', return 'config'
[TYPE_SECTION] = 'section', elseif self.t == luci.uvl.TYPE_SECTION then
[TYPE_OPTION] = 'option', return 'section'
[TYPE_ENUM] = 'enum' elseif self.t == luci.uvl.TYPE_OPTION then
} return 'option'
elseif self.t == luci.uvl.TYPE_ENUM then
return _t[self.t] return 'enum'
end
end end
function uvlitem.error(self, ...) function uvlitem.error(self, ...)
@ -902,28 +913,22 @@ function uvlitem.parent(self)
if self.p then if self.p then
return self.p return self.p
elseif #self.cref == 3 or #self.cref == 4 then 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 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 else
return nil return nil
end end
end end
-- Shared cache
uvlitem._ucicache = {}
function uvlitem._loadconf(self, co, c) function uvlitem._loadconf(self, co, c)
co = co or self._ucicache[c]
if not co then if not co then
local err local uci, err = luci.model.uci.cursor(), nil
co, err = cursor:get_all(c) co, err = uci:get_all(c)
if err then if err then
self:error(ERR.UCILOAD(self, err)) self:error(ERR.UCILOAD(self, err))
end end
self._ucicache[c] = co
end end
return co return co
end end
@ -952,7 +957,7 @@ function scheme.__init__(self, scheme, co, c)
self.sref = { c } self.sref = { c }
self.c = self:_loadconf(co, c) self.c = self:_loadconf(co, c)
self.s = scheme self.s = scheme
self.t = TYPE_SCHEME self.t = luci.uvl.TYPE_SCHEME
end end
--- Add an error to scheme. --- Add an error to scheme.
@ -965,7 +970,7 @@ end
--- Get an associated config object. --- Get an associated config object.
-- @return Config instance -- @return Config instance
function scheme.config(self) 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 co.p = self
return co return co
@ -977,7 +982,7 @@ function scheme.sections(self)
local v = { } local v = { }
if self.s.packages[self.sref[1]].sections then if self.s.packages[self.sref[1]].sections then
for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do 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 self.s, self.c, self.cref[1], self.cref[2], o
) ) ) )
end end
@ -989,7 +994,7 @@ end
-- @param s Section to select -- @param s Section to select
-- @return Section instance -- @return Section instance
function scheme.section(self, s) 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 so.p = self
return so return so
@ -1019,7 +1024,7 @@ function config.__init__(self, scheme, co, c)
self.sref = { c } self.sref = { c }
self.c = self:_loadconf(co, c) self.c = self:_loadconf(co, c)
self.s = scheme self.s = scheme
self.t = TYPE_CONFIG self.t = luci.uvl.TYPE_CONFIG
end end
--- Get all section objects associated with this config. --- Get all section objects associated with this config.
@ -1067,7 +1072,7 @@ function section.__init__(self, scheme, co, c, s)
self.sref = { c, co and co[s] and co[s]['.type'] or 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)
self.s = scheme self.s = scheme
self.t = TYPE_SECTION self.t = luci.uvl.TYPE_SECTION
end end
--- Get all option objects associated with this section. --- Get all option objects associated with this section.
@ -1118,7 +1123,7 @@ function option.__init__(self, scheme, co, c, s, o)
self.sref = { c, co and co[s] and co[s]['.type'] or 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)
self.s = scheme self.s = scheme
self.t = TYPE_OPTION self.t = luci.uvl.TYPE_OPTION
end end
--- Get the value of this option. --- Get the value of this option.
@ -1171,5 +1176,5 @@ function enum.__init__(self, scheme, co, c, s, o, v)
self.sref = { c, co and co[s] and co[s]['.type'] or 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)
self.s = scheme self.s = scheme
self.t = TYPE_ENUM self.t = luci.uvl.TYPE_ENUM
end end

View file

@ -21,20 +21,31 @@ local ERR = luci.uvl.errors
function _parse_reference( r, c, s, o ) function _parse_reference( r, c, s, o )
local ref = { } local ref = { }
local vars = { local vars = {
config = c, config = ( c or '$config' ),
section = s, section = ( s or '$section' ),
option = o option = ( o or '$option' )
} }
for v in r:gmatch("[^.]+") do for i, v in ipairs(luci.util.split(r,".")) do
table.insert(ref, (v:gsub( "%$(.+)", vars ))) table.insert(ref, (v:gsub( "%$(.+)", function(n) return vars[n] end )))
end end
if #ref < 2 then if c or s then
table.insert(ref, 1, s or '$section') 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
if #ref < 3 then
table.insert(ref, 1, c or '$config')
end end
return ref return ref