Changeset 103

Show
Ignore:
Timestamp:
20/01/09 12:23:13 (4 years ago)
Author:
cyrus
Message:

Merge bugfixes and optimizations from mainline

Location:
trunk/luci/libs
Files:
10 modified

Legend:

Unmodified
Added
Removed
  • trunk/luci/libs/cbi/luasrc/cbi.lua

    r4 r103  
    6060        local upldir = "/lib/uci/upload/" 
    6161        local cbidir = luci.util.libpath() .. "/model/cbi/" 
    62          
    63         assert(luci.fs.stat(cbimap) or luci.fs.stat(cbidir..cbimap..".lua"),  
     62 
     63        assert(luci.fs.stat(cbimap) or luci.fs.stat(cbidir..cbimap..".lua"), 
    6464         "Model not found!") 
    65            
     65 
    6666        local func, err = loadfile(cbimap) 
    6767        if not func then 
     
    450450function Compound.parse(self, ...) 
    451451        local cstate, state = 0, 0 
    452          
     452 
    453453        for k, child in ipairs(self.children) do 
    454454                cstate = child:parse(...) 
    455455                state = (not state or cstate < state) and cstate or state 
    456456        end 
    457          
     457 
    458458        return state 
    459459end 
     
    473473        transitor = transitor or self.transistor_linear 
    474474        local state = {node=node, name=name, transitor=transitor} 
    475          
     475 
    476476        assert(instanceof(node, Node), "Invalid node") 
    477477        assert(not self.nodes[name], "Duplicate entry") 
    478          
     478 
    479479        self.nodes[name] = state 
    480480        self:append(state) 
    481          
     481 
    482482        return state 
    483483end 
     
    502502        local active = self:getactive() 
    503503        assert(active, "Invalid state") 
    504          
     504 
    505505        local cstate = active.node:parse() 
    506506        self.active = active.transistor(self, active.node, cstate) 
    507          
     507 
    508508        if not self.active then 
    509509                return FORM_DONE 
     
    519519 
    520520function Delegator.getactive(self) 
    521         return self:get(Map.formvalue(self, "cbi.delegated")  
    522                 or (self.children[1] and self.children[1].name))  
     521        return self:get(Map.formvalue(self, "cbi.delegated") 
     522                or (self.children[1] and self.children[1].name)) 
    523523end 
    524524 
     
    808808        datasource.config = "table" 
    809809        self.data = data 
    810          
     810 
    811811        datasource.formvalue = Map.formvalue 
    812812        datasource.formvaluetable = Map.formvaluetable 
     
    816816                return data[section] and data[section][option] 
    817817        end 
    818          
     818 
    819819        function datasource.submitstate(self) 
    820820                return Map.formvalue(self, "cbi.submit") 
     
    14961496        local valid = {} 
    14971497        for i, v in ipairs(value) do 
    1498                 if luci.util.contains(self.vallist, v) then 
     1498                if luci.util.contains(self.keylist, v) then 
    14991499                        table.insert(valid, v) 
    15001500                end 
  • trunk/luci/libs/cbi/luasrc/view/cbi/tblsection.htm

    r68 r103  
    2929                        <tr class="cbi-section-table-titles"> 
    3030                        <%- if not self.anonymous then -%> 
    31                                 <th>&#160;</th> 
     31                                <%- if self.sectionhead then -%> 
     32                                        <th class="cbi-section-table-cell"><%=self.sectionhead%></th> 
     33                                <%- else -%> 
     34                                        <th>&#160;</th> 
     35                                <%- end -%> 
    3236                        <%- end -%> 
    3337                        <%- for i, k in pairs(self.children) do if not k.optional then -%> 
     
    4347                        <tr class="cbi-section-table-descr"> 
    4448                        <%- if not self.anonymous then -%> 
    45                                 <th></th> 
     49                                <%- if self.sectiondesc then -%> 
     50                                        <th class="cbi-section-table-cell"><%=self.sectiondesc%></th> 
     51                                <%- else -%> 
     52                                        <th></th> 
     53                                <%- end -%> 
    4654                        <%- end -%> 
    4755                        <%- for i, k in pairs(self.children) do if not k.optional then -%> 
  • trunk/luci/libs/cbi/luasrc/view/cbi/ucisection.htm

    r68 r103  
    1313 
    1414-%> 
     15 
     16<%- 
     17        if type(self.hidden) == "table" then 
     18                for k, v in pairs(self.hidden) do 
     19-%> 
     20        <input type="hidden" id="cbid.<%=self.config%>.<%=section%>.<%=k%>" name="cbid.<%=self.config%>.<%=section%>.<%=k%>" value="<%=luci.util.pcdata(v)%>" /> 
     21<%- 
     22                end 
     23        end 
     24%> 
    1525 
    1626<% self:render_children(section, scope or {}) %> 
  • trunk/luci/libs/core/luasrc/fs.lua

    r4 r103  
    5555 
    5656--- Checks wheather the given path exists and points to a regular file. 
    57 -- @param filename      String containing the path of the file to read 
     57-- @param filename      String containing the path of the file to test 
    5858-- @return                      Boolean indicating wheather given path points to regular file 
    5959function isfile(filename) 
    6060        return posix.stat(filename, "type") == "regular" 
     61end 
     62 
     63--- Checks wheather the given path exists and points to a directory. 
     64-- @param dirname       String containing the path of the directory to test 
     65-- @return                      Boolean indicating wheather given path points to directory 
     66function isdirectory(dirname) 
     67        return posix.stat(dirname, "type") == "directory" 
    6168end 
    6269 
     
    130137end 
    131138 
     139--- Set the last modification time  of given file path in Unix epoch format. 
     140-- @param path  String containing the path of the file or directory to read 
     141-- @param mtime Last modification timestamp 
     142-- @param atime Last accessed timestamp 
     143-- @return              0 in case of success nil on error 
     144-- @return              String containing the error description (if any) 
     145-- @return              Number containing the os specific errno (if any) 
     146utime = posix.utime 
     147 
    132148--- Return the last element - usually the filename - from the given path with 
    133149-- the directory component stripped. 
  • trunk/luci/libs/core/luasrc/ip.lua

    r4 r103  
    6666 
    6767        elseif type(x) == "table" then 
    68                 list = x 
     68                list = { unpack(x) } 
    6969        end 
    7070 
  • trunk/luci/libs/json/luasrc/json.lua

    r4 r103  
    500500        ['{'] = Decoder.parse_object 
    501501} 
     502 
     503 
     504--- Create a new Active JSON-Decoder. 
     505-- @class       function 
     506-- @name        ActiveDecoder 
     507-- @param   customnull  Use luci.json.null instead of nil for decoding null 
     508-- @return  Active JSON-Decoder 
     509ActiveDecoder = util.class(Decoder) 
     510 
     511function ActiveDecoder.__init__(self, source, customnull) 
     512        Decoder.__init__(self, customnull) 
     513        self.source = source 
     514        self.chunk = nil 
     515        getmetatable(self).__call = self.get 
     516end 
     517 
     518 
     519--- Fetches one JSON-object from given source 
     520-- @return Decoded object 
     521function ActiveDecoder.get(self) 
     522        local chunk, src_err, object 
     523        if not self.chunk then 
     524                chunk, src_err = self.source() 
     525        else 
     526                chunk = self.chunk 
     527        end 
     528 
     529        self.chunk, object = self:dispatch(chunk, src_err, true) 
     530        return object 
     531end 
     532 
     533 
     534function ActiveDecoder.fetch(self) 
     535        local chunk, src_err = self.source() 
     536        assert(chunk or not src_err, src_err) 
     537        return chunk 
     538end 
  • trunk/luci/libs/uci/luasrc/model/uci.lua

    r4 r103  
    6262function Cursor.delete_all(self, config, stype, comparator) 
    6363        local del = {} 
    64          
     64 
    6565        if type(comparator) == "table" then 
    6666                local tbl = comparator 
     
    7070                                        return false 
    7171                                end 
    72                         end  
     72                        end 
    7373                        return true 
    7474                end 
    7575        end 
    76          
     76 
    7777        local function helper (section) 
    7878 
     
    125125end 
    126126 
     127--- Get a boolean option and return it's value as true or false. 
     128-- @param config        UCI config 
     129-- @param section       UCI section name 
     130-- @param option        UCI option 
     131-- @return                      Boolean 
     132function Cursor.get_bool(self, ...) 
     133        local val = self:get(...) 
     134        return ( val == "1" or val == "true" or val == "yes" or val == "on" ) 
     135end 
     136 
    127137--- Get an option or list and return values as table. 
    128138-- @param config        UCI config 
  • trunk/luci/libs/uvl/luasrc/uvl.lua

    r4 r103  
    6868-- @name                        UVL 
    6969-- @param schemedir     Path to the scheme directory (optional) 
     70-- @param configdir     Override config directory (optional) 
    7071-- @return                      Instance object 
    7172UVL = util.class() 
    7273 
    73 function UVL.__init__( self, schemedir ) 
     74function UVL.__init__( self, schemedir, configdir ) 
    7475        self.schemedir  = schemedir or default_schemedir 
     76        self.configdir  = configdir 
    7577        self.packages   = { } 
    7678        self.beenthere  = { } 
     
    947949end 
    948950 
    949 function uvlitem._loadconf(self, co, c) 
     951function uvlitem._loadconf(self, co, c, configdir) 
    950952        co = co or self._configcache 
    951953        if not co then 
    952954                local err 
    953                 co, err = uci.cursor():get_all(c) 
     955                co, err = uci.cursor(configdir):get_all(c) 
    954956 
    955957                if err then 
     
    984986        self.cref = { c } 
    985987        self.sref = { c } 
    986         self.c    = self:_loadconf(co, c) 
     988        self.c    = self:_loadconf(co, c, scheme.configdir) 
    987989        self.s    = scheme 
    988990        self.t    = TYPE_SCHEME 
     
    10481050                c, co = co, nil 
    10491051        end 
    1050  
    10511052        self.cref = { c } 
    10521053        self.sref = { c } 
    1053         self.c    = self:_loadconf(co, c) 
     1054        self.c    = self:_loadconf(co, c, scheme.configdir) 
    10541055        self.s    = scheme 
    10551056        self.t    = TYPE_CONFIG 
     
    10991100        self.cref = { c, s } 
    11001101        self.sref = { c, co and co[s] and co[s]['.type'] or s } 
    1101         self.c    = self:_loadconf(co, c) 
     1102        self.c    = self:_loadconf(co, c, scheme.configdir) 
    11021103        self.s    = scheme 
    11031104        self.t    = TYPE_SECTION 
     
    11501151        self.cref = { c, s, o } 
    11511152        self.sref = { c, co and co[s] and co[s]['.type'] or s, o } 
    1152         self.c    = self:_loadconf(co, c) 
     1153        self.c    = self:_loadconf(co, c, scheme.configdir) 
    11531154        self.s    = scheme 
    11541155        self.t    = TYPE_OPTION 
     
    12031204        self.cref = { c, s, o, v } 
    12041205        self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v } 
    1205         self.c    = self:_loadconf(co, c) 
     1206        self.c    = self:_loadconf(co, c, scheme.configdir) 
    12061207        self.s    = scheme 
    12071208        self.t    = TYPE_ENUM 
  • trunk/luci/libs/uvl/root/usr/bin/uvl

    r4 r103  
    201201 
    202202        local uvl = luci.uvl.UVL( 
    203                 type(options.schemedir) == "string" and options.schemedir 
     203                type(options.schemedir) == "string" and options.schemedir, 
     204                type(options.configdir) == "string" and options.configdir 
    204205        ) 
    205206 
  • trunk/luci/libs/web/luasrc/dispatcher.lua

    r16 r103  
    167167        if c and c.leaf then 
    168168                for j=n+1, #request do 
    169                         table.insert(args, request[j]) 
     169                        args[#args+1] = request[j] 
     170                        freq[#freq+1] = request[j] 
    170171                end 
    171172        end 
     
    247248        end 
    248249 
    249         if c and (c.index or type(c.target) == "function") then 
     250        local target = nil 
     251        if c then 
     252                if type(c.target) == "function" then 
     253                        target = c.target 
     254                elseif type(c.target) == "table" then 
     255                        target = c.target.target 
     256                end 
     257        end 
     258 
     259        if c and (c.index or type(target) == "function") then 
    250260                ctx.dispatched = c 
    251261                ctx.requested = ctx.requested or ctx.dispatched 
     
    254264        if c and c.index then 
    255265                local tpl = require "luci.template" 
    256                 tpl.render "indexer" 
    257                 return true 
    258         end 
    259  
    260         if c and type(c.target) == "function" then 
     266                if util.copcall(tpl.render, "indexer", {}) then 
     267                        return true 
     268                end 
     269        end 
     270 
     271        if type(target) == "function" then 
    261272                util.copcall(function() 
    262                         local oldenv = getfenv(c.target) 
     273                        local oldenv = getfenv(target) 
    263274                        local module = require(c.module) 
    264275                        local env = setmetatable({}, {__index= 
     
    268279                        end}) 
    269280 
    270                         setfenv(c.target, env) 
     281                        setfenv(target, env) 
    271282                end) 
    272283 
    273                 c.target(unpack(args)) 
     284                if type(c.target) == "table" then 
     285                        target(c.target, unpack(args)) 
     286                else 
     287                        target(unpack(args)) 
     288                end 
    274289        else 
    275290                error404() 
     
    518533end 
    519534 
     535 
     536local function _call(self, ...) 
     537        if #self.argv > 0 then  
     538                return getfenv()[self.name](unpack(self.argv), ...) 
     539        else 
     540                return getfenv()[self.name](...) 
     541        end 
     542end 
     543 
    520544--- Create a function-call dispatching target. 
    521545-- @param       name    Target function of local controller 
    522546-- @param       ...             Additional parameters passed to the function 
    523547function call(name, ...) 
    524         local argv = {...} 
    525         return function(...) 
    526                 if #argv > 0 then  
    527                         return getfenv()[name](unpack(argv), ...) 
    528                 else 
    529                         return getfenv()[name](...) 
    530                 end 
    531         end 
     548        return {type = "call", argv = {...}, name = name, target = _call} 
     549end 
     550 
     551 
     552local _template = function(self, ...) 
     553        require "luci.template".render(self.view) 
    532554end 
    533555 
     
    535557-- @param       name    Template to be rendered 
    536558function template(name) 
    537         return function() 
    538                 require("luci.template") 
    539                 luci.template.render(name) 
    540         end 
     559        return {type = "template", view = name, target = _template} 
     560end 
     561 
     562 
     563local function _cbi(self, ...) 
     564        local cbi = require "luci.cbi" 
     565        local tpl = require "luci.template" 
     566        local http = require "luci.http" 
     567 
     568        local config = self.config or {} 
     569        local maps = cbi.load(self.model, ...) 
     570 
     571        local state = nil 
     572 
     573        for i, res in ipairs(maps) do 
     574                if config.autoapply then 
     575                        res.autoapply = config.autoapply 
     576                end 
     577                local cstate = res:parse() 
     578                if not state or cstate < state then 
     579                        state = cstate 
     580                end 
     581        end 
     582 
     583        if config.on_valid_to and state and state > 0 and state < 2 then 
     584                http.redirect(config.on_valid_to) 
     585                return 
     586        end 
     587 
     588        if config.on_changed_to and state and state > 1 then 
     589                http.redirect(config.on_changed_to) 
     590                return 
     591        end 
     592 
     593        if config.on_success_to and state and state > 0 then 
     594                http.redirect(config.on_success_to) 
     595                return 
     596        end 
     597 
     598        if config.state_handler then 
     599                if not config.state_handler(state, maps) then 
     600                        return 
     601                end 
     602        end 
     603 
     604        local pageaction = true 
     605        local cancelaction = false 
     606        http.header("X-CBI-State", state or 0) 
     607        luci.template.render("cbi/header", {state = state}) 
     608        for i, res in ipairs(maps) do 
     609                res:render() 
     610                if res.pageaction == false then 
     611                        pageaction = false 
     612                end 
     613                if res.cancelaction == true then 
     614                        cancelaction = true 
     615                end 
     616        end 
     617        luci.template.render("cbi/footer", {pageaction=pageaction, cancelaction=cancelaction, state = state, autoapply = config.autoapply}) 
    541618end 
    542619 
     
    544621-- @param       model   CBI model to be rendered 
    545622function cbi(model, config) 
    546         config = config or {} 
    547         return function(...) 
    548                 require("luci.cbi") 
    549                 require("luci.template") 
    550                 local http = require "luci.http" 
    551  
    552                 maps = luci.cbi.load(model, ...) 
    553  
    554                 local state = nil 
    555  
    556                 for i, res in ipairs(maps) do 
    557                         if config.autoapply then 
    558                                 res.autoapply = config.autoapply 
    559                         end 
    560                         local cstate = res:parse() 
    561                         if not state or cstate < state then 
    562                                 state = cstate 
    563                         end 
    564                 end 
    565  
    566                 if config.on_valid_to and state and state > 0 and state < 2 then 
    567                         luci.http.redirect(config.on_valid_to) 
    568                         return 
    569                 end 
    570  
    571                 if config.on_changed_to and state and state > 1 then 
    572                         luci.http.redirect(config.on_changed_to) 
    573                         return 
    574                 end 
    575  
    576                 if config.on_success_to and state and state > 0 then 
    577                         luci.http.redirect(config.on_success_to) 
    578                         return 
    579                 end 
    580  
    581                 if config.state_handler then 
    582                         if not config.state_handler(state, maps) then 
    583                                 return 
    584                         end 
    585                 end 
    586  
    587                 local pageaction = true 
    588                 local cancelaction = false 
    589                 http.header("X-CBI-State", state or 0) 
    590                 luci.template.render("cbi/header", {state = state}) 
    591                 for i, res in ipairs(maps) do 
    592                         res:render() 
    593                         if res.pageaction == false then 
    594                                 pageaction = false 
    595                         end 
    596                         if res.cancelaction == true then 
    597                                 cancelaction = true 
    598                         end 
    599                 end 
    600                 luci.template.render("cbi/footer", {pageaction=pageaction, cancelaction=cancelaction, state = state, autoapply = config.autoapply}) 
    601         end 
     623        return {type = "cbi", config = config, model = model, target = _cbi} 
     624end 
     625 
     626 
     627local function _arcombine(self, ...) 
     628        local argv = {...} 
     629        local target = #argv > 0 and self.targets[2] or self.targets[1] 
     630        setfenv(target.target, self.env) 
     631        target:target(unpack(argv)) 
     632end 
     633 
     634--- Create a combined dispatching target for non argv and argv requests. 
     635-- @param trg1  Overview Target 
     636-- @param trg2  Detail Target 
     637function arcombine(trg1, trg2) 
     638        return {type = "arcombine", env = getfenv(), target = _arcombine, targets = {trg1, trg2}} 
     639end 
     640 
     641 
     642local function _form(self, ...) 
     643        local cbi = require "luci.cbi" 
     644        local tpl = require "luci.template" 
     645        local http = require "luci.http" 
     646 
     647        local maps = luci.cbi.load(self.model, ...) 
     648        local state = nil 
     649 
     650        for i, res in ipairs(maps) do 
     651                local cstate = res:parse() 
     652                if not state or cstate < state then 
     653                        state = cstate 
     654                end 
     655        end 
     656 
     657        http.header("X-CBI-State", state or 0) 
     658        tpl.render("header") 
     659        for i, res in ipairs(maps) do 
     660                res:render() 
     661        end 
     662        tpl.render("footer") 
    602663end 
    603664 
     
    605666-- @param       model   CBI form model tpo be rendered 
    606667function form(model) 
    607         return function(...) 
    608                 require("luci.cbi") 
    609                 require("luci.template") 
    610                 local http = require "luci.http" 
    611  
    612                 maps = luci.cbi.load(model, ...) 
    613  
    614                 local state = nil 
    615  
    616                 for i, res in ipairs(maps) do 
    617                         local cstate = res:parse() 
    618                         if not state or cstate < state then 
    619                                 state = cstate 
    620                         end 
    621                 end 
    622  
    623                 http.header("X-CBI-State", state or 0) 
    624                 luci.template.render("header") 
    625                 for i, res in ipairs(maps) do 
    626                         res:render() 
    627                 end 
    628                 luci.template.render("footer") 
    629         end 
    630 end 
     668        return {type = "cbi", model = model, target = _form} 
     669end