Revert UVL optimizations
authorSteven Barth <steven@midlink.org>
Tue, 9 Sep 2008 12:44:51 +0000 (12:44 +0000)
committerSteven Barth <steven@midlink.org>
Tue, 9 Sep 2008 12:44:51 +0000 (12:44 +0000)
libs/uvl/luasrc/uvl.lua
libs/uvl/luasrc/uvl/dependencies.lua

index acbdccd..98e7dfb 100644 (file)
@@ -23,13 +23,12 @@ module( "luci.uvl", package.seeall )
 
 require("luci.fs")
 require("luci.util")
 
 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")
 
 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
 
 TYPE_SCHEME   = 0x00
 TYPE_CONFIG   = 0x01
@@ -832,37 +831,48 @@ function uvlitem.sid(self)
 end
 
 function uvlitem.scheme(self, opt)
 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
 
        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)
 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
                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
 
 function uvlitem.title(self)
 end
 
 function uvlitem.title(self)
@@ -871,14 +881,15 @@ function uvlitem.title(self)
 end
 
 function uvlitem.type(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, ...)
 end
 
 function uvlitem.error(self, ...)
@@ -902,28 +913,22 @@ function uvlitem.parent(self)
        if self.p then
                return self.p
        elseif #self.cref == 3 or #self.cref == 4 then
        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
        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
 
        else
                return nil
        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
-               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
 
                if err then
                        self:error(ERR.UCILOAD(self, err))
                end
-               
-               self._ucicache[c] = co
        end
        return co
 end
        end
        return co
 end
@@ -952,7 +957,7 @@ function scheme.__init__(self, scheme, co, c)
        self.sref = { c }
        self.c    = self:_loadconf(co, c)
        self.s    = scheme
        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.
 end
 
 --- Add an error to scheme.
@@ -965,7 +970,7 @@ end
 --- Get an associated config object.
 -- @return     Config instance
 function scheme.config(self)
 --- 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
              co.p = self
 
        return co
@@ -977,7 +982,7 @@ function scheme.sections(self)
        local v = { }
        if self.s.packages[self.sref[1]].sections then
                for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
        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
                                self.s, self.c, self.cref[1], self.cref[2], o
                        ) )
                end
@@ -989,7 +994,7 @@ end
 -- @param s    Section to select
 -- @return     Section instance
 function scheme.section(self, s)
 -- @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
              so.p = self
 
        return so
@@ -1019,7 +1024,7 @@ function config.__init__(self, scheme, co, c)
        self.sref = { c }
        self.c    = self:_loadconf(co, c)
        self.s    = scheme
        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.
 end
 
 --- 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.c    = self:_loadconf(co, c)
        self.s    = scheme
        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.
 end
 
 --- 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.c    = self:_loadconf(co, c)
        self.s    = scheme
        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.
 end
 
 --- 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.c    = self:_loadconf(co, c)
        self.s    = scheme
        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
 end
index ed5770f..3aa3c73 100644 (file)
@@ -21,20 +21,31 @@ local ERR = luci.uvl.errors
 function _parse_reference( r, c, s, o )
        local ref  = { }
        local vars = {
 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
        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
        end
 
        return ref