From: Steven Barth Date: Sat, 22 Mar 2008 21:26:44 +0000 (+0000) Subject: * CBI updates X-Git-Tag: 0.8.0~1203 X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=ef01ff75db17f23a90757cf473778cfefe1ad120;p=project%2Fluci.git * CBI updates --- diff --git a/src/ffluci/cbi.lua b/src/ffluci/cbi.lua index 85b9f49e9d..0c2718ea8e 100644 --- a/src/ffluci/cbi.lua +++ b/src/ffluci/cbi.lua @@ -104,6 +104,26 @@ function Map.__init__(self, config, ...) self.template = "cbi/map" end +function Map.parse(self) + self.ucidata = ffluci.model.uci.show(self.config) + if not self.ucidata then + error("Unable to read UCI data: " .. self.config) + else + self.ucidata = self.ucidata[self.config] + end + Node.parse(self) +end + +function Map.render(self) + self.ucidata = ffluci.model.uci.show(self.config) + if not self.ucidata then + error("Unable to read UCI data: " .. self.config) + else + self.ucidata = self.ucidata[self.config] + end + Node.render(self) +end + function Map.section(self, class, ...) if instanceof(class, AbstractSection) then local obj = class(...) @@ -116,11 +136,6 @@ function Map.section(self, class, ...) end end -function Map.read(self) - self.ucidata = self.ucidata or ffluci.model.uci.show(self.config)[self.config] - return self.ucidata -end - --[[ AbstractSection ]]-- @@ -181,6 +196,15 @@ function TypedSection.__init__(self, ...) self.valid = nil end +function TypedSection.parse(self) + for k, v in pairs(self:ucisections()) do + for i, node in ipairs(self.children) do + node.section = k + node:parse() + end + end +end + function TypedSection.render_children(self, section) for k, node in ipairs(self.children) do node.section = section @@ -190,7 +214,7 @@ end function TypedSection.ucisections(self) local sections = {} - for k, v in pairs(self.map:read()) do + for k, v in pairs(self.map.ucidata) do if v[".type"] == self.sectiontype then sections[k] = v end @@ -217,14 +241,20 @@ function AbstractValue.__init__(self, option, ...) self.default = nil end - function AbstractValue.formvalue(self) - local key = "uci."..self.map.config.."."..self.section.."."..self.option + local key = "cbid."..self.map.config.."."..self.section.."."..self.option return ffluci.http.formvalue(key) end +function AbstractValue.parse(self) + local fvalue = self:validate(self:formvalue()) + if fvalue and not (fvalue == self:ucivalue()) then + self:write(fvalue) + end +end + function AbstractValue.ucivalue(self) - return self.map:read()[self.section][self.option] + return self.map.ucidata[self.section][self.option] end function AbstractValue.validate(self, value) @@ -232,7 +262,7 @@ function AbstractValue.validate(self, value) end function AbstractValue.write(self, value) - ffluci.model.uci.set(self.config, self.section, self.option, value) + return ffluci.model.uci.set(self.config, self.section, self.option, value) end diff --git a/src/ffluci/dispatcher.lua b/src/ffluci/dispatcher.lua index f3a62bb193..ee836043d2 100644 --- a/src/ffluci/dispatcher.lua +++ b/src/ffluci/dispatcher.lua @@ -116,7 +116,7 @@ end function error500(message) ffluci.http.status(500, "Internal Server Error") - if not pcall(ffluci.template.render, "error500") then + if not pcall(ffluci.template.render, "error500", {message=message}) then ffluci.http.textheader() print(message) end @@ -171,8 +171,13 @@ function cbi(request) i18n.loadc(request.module) - stat, map = pcall(cbi.load, path) + local stat, map = pcall(cbi.load, path) if stat then + local stat, err = pcall(map.parse, map) + if not stat then + disp.error500(err) + return + end tmpl.render("cbi/header") map:render() tmpl.render("cbi/footer") @@ -202,8 +207,13 @@ function dynamic(request) return end - stat, map = pcall(cbi.load, path) + local stat, map = pcall(cbi.load, path) if stat then + local stat, err = pcall(map.parse, map) + if not stat then + disp.error500(err) + return + end tmpl.render("cbi/header") map:render() tmpl.render("cbi/footer") diff --git a/src/ffluci/fs.lua b/src/ffluci/fs.lua index 55da9b8dce..fb9a9c1c95 100644 --- a/src/ffluci/fs.lua +++ b/src/ffluci/fs.lua @@ -28,6 +28,13 @@ module("ffluci.fs", package.seeall) require("lfs") +-- Checks whether a file exists +function isfile(filename) + local fp = io.open(path, "r") + if file then file:close() end + return file ~= nil +end + -- Returns the content of file function readfile(filename) local fp = io.open(filename) diff --git a/src/ffluci/util.lua b/src/ffluci/util.lua index b219be130e..8a526173d7 100644 --- a/src/ffluci/util.lua +++ b/src/ffluci/util.lua @@ -161,7 +161,7 @@ function validate(value, cast_number, cast_int, valid) if type(valid) == "function" then value = valid(value) elseif type(valid) == "table" then - if not ffluci.util.contains(valid, value) then + if not contains(valid, value) then value = nil end end diff --git a/src/ffluci/view/cbi/footer.htm b/src/ffluci/view/cbi/footer.htm index e8ef8fef5e..8fc212b88b 100644 --- a/src/ffluci/view/cbi/footer.htm +++ b/src/ffluci/view/cbi/footer.htm @@ -1,2 +1,3 @@ + <%+footer%> \ No newline at end of file diff --git a/src/ffluci/view/cbi/map.htm b/src/ffluci/view/cbi/map.htm index e6884f1045..83c377fb86 100644 --- a/src/ffluci/view/cbi/map.htm +++ b/src/ffluci/view/cbi/map.htm @@ -4,5 +4,4 @@
<% self:render_children() %>
- diff --git a/src/ffluci/view/error500.htm b/src/ffluci/view/error500.htm new file mode 100644 index 0000000000..8af22e8f20 --- /dev/null +++ b/src/ffluci/view/error500.htm @@ -0,0 +1,5 @@ +<%+header%> +

500 Internal Server Error

+

Sorry, the server encountered an unexpected error.

+<%=message%> +<%+footer%> \ No newline at end of file diff --git a/src/ffluci/view/header.htm b/src/ffluci/view/header.htm index 2af577a078..cd2a64b55c 100644 --- a/src/ffluci/view/header.htm +++ b/src/ffluci/view/header.htm @@ -9,10 +9,10 @@ require("ffluci.http").htmlheader() - - -FFLuCI -<% if addheaders then write(addheaders) end %> + + + + FFLuCI