mirror of https://git.openwrt.org/project/luci.git
* CBI: updates
* UCI: Introduced Session class, supporting different change file paths * util: introduced sessionid() function * general: Changed several error messages to OS native ones
This commit is contained in:
parent
ef01ff75db
commit
06a385370c
|
@ -41,10 +41,10 @@ function load(cbimap)
|
||||||
require("ffluci.i18n")
|
require("ffluci.i18n")
|
||||||
|
|
||||||
local cbidir = ffluci.fs.dirname(ffluci.util.__file__()) .. "model/cbi/"
|
local cbidir = ffluci.fs.dirname(ffluci.util.__file__()) .. "model/cbi/"
|
||||||
local func = loadfile(cbidir..cbimap..".lua")
|
local func, err = loadfile(cbidir..cbimap..".lua")
|
||||||
|
|
||||||
if not func then
|
if not func then
|
||||||
error("Unable to load CBI map: " .. cbimap)
|
error(err)
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -102,10 +102,11 @@ function Map.__init__(self, config, ...)
|
||||||
Node.__init__(self, ...)
|
Node.__init__(self, ...)
|
||||||
self.config = config
|
self.config = config
|
||||||
self.template = "cbi/map"
|
self.template = "cbi/map"
|
||||||
|
self.uci = ffluci.model.uci.Session()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Map.parse(self)
|
function Map.parse(self)
|
||||||
self.ucidata = ffluci.model.uci.show(self.config)
|
self.ucidata = self.uci:show(self.config)
|
||||||
if not self.ucidata then
|
if not self.ucidata then
|
||||||
error("Unable to read UCI data: " .. self.config)
|
error("Unable to read UCI data: " .. self.config)
|
||||||
else
|
else
|
||||||
|
@ -115,7 +116,7 @@ function Map.parse(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Map.render(self)
|
function Map.render(self)
|
||||||
self.ucidata = ffluci.model.uci.show(self.config)
|
self.ucidata = self.uci:show(self.config)
|
||||||
if not self.ucidata then
|
if not self.ucidata then
|
||||||
error("Unable to read UCI data: " .. self.config)
|
error("Unable to read UCI data: " .. self.config)
|
||||||
else
|
else
|
||||||
|
@ -136,6 +137,10 @@ function Map.section(self, class, ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Map.set(self, section, option, value)
|
||||||
|
return self.uci:set(self.config, section, option, value)
|
||||||
|
end
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
AbstractSection
|
AbstractSection
|
||||||
]]--
|
]]--
|
||||||
|
@ -262,7 +267,7 @@ function AbstractValue.validate(self, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
function AbstractValue.write(self, value)
|
function AbstractValue.write(self, value)
|
||||||
return ffluci.model.uci.set(self.config, self.section, self.option, value)
|
return self.map:set(self.section, self.option, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -296,7 +301,7 @@ function ListValue.__init__(self, ...)
|
||||||
self.list = {}
|
self.list = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
function ListValue.addValue(self, key, val)
|
function ListValue.add_value(self, key, val)
|
||||||
val = val or key
|
val = val or key
|
||||||
self.list[key] = val
|
self.list[key] = val
|
||||||
end
|
end
|
|
@ -37,10 +37,10 @@ end
|
||||||
|
|
||||||
-- Returns the content of file
|
-- Returns the content of file
|
||||||
function readfile(filename)
|
function readfile(filename)
|
||||||
local fp = io.open(filename)
|
local fp, err = io.open(filename)
|
||||||
|
|
||||||
if fp == nil then
|
if fp == nil then
|
||||||
error("Unable to open file for reading: " .. filename)
|
error(err)
|
||||||
end
|
end
|
||||||
|
|
||||||
local data = fp:read("*a")
|
local data = fp:read("*a")
|
||||||
|
@ -50,12 +50,12 @@ end
|
||||||
|
|
||||||
-- Returns the content of file as array of lines
|
-- Returns the content of file as array of lines
|
||||||
function readfilel(filename)
|
function readfilel(filename)
|
||||||
local fp = io.open(filename)
|
local fp, err = io.open(filename)
|
||||||
local line = ""
|
local line = ""
|
||||||
local data = {}
|
local data = {}
|
||||||
|
|
||||||
if fp == nil then
|
if fp == nil then
|
||||||
error("Unable to open file for reading: " .. filename)
|
error(err)
|
||||||
end
|
end
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
|
@ -70,9 +70,9 @@ end
|
||||||
|
|
||||||
-- Writes given data to a file
|
-- Writes given data to a file
|
||||||
function writefile(filename, data)
|
function writefile(filename, data)
|
||||||
local fp = io.open(filename, "w")
|
local fp, err = io.open(filename, "w")
|
||||||
if fp == nil then
|
if fp == nil then
|
||||||
error("Unable to open file for writing: " .. filename)
|
error(err)
|
||||||
end
|
end
|
||||||
fp:write(data)
|
fp:write(data)
|
||||||
fp:close()
|
fp:close()
|
||||||
|
@ -97,4 +97,9 @@ function dir(path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return e
|
return e
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Alias for lfs.mkdir
|
||||||
|
function mkdir(...)
|
||||||
|
return lfs.mkdir(...)
|
||||||
end
|
end
|
|
@ -28,55 +28,101 @@ limitations under the License.
|
||||||
]]--
|
]]--
|
||||||
module("ffluci.model.uci", package.seeall)
|
module("ffluci.model.uci", package.seeall)
|
||||||
require("ffluci.util")
|
require("ffluci.util")
|
||||||
|
require("ffluci.fs")
|
||||||
|
|
||||||
|
-- The OS uci command
|
||||||
ucicmd = "uci"
|
ucicmd = "uci"
|
||||||
|
|
||||||
|
-- Session class
|
||||||
|
Session = ffluci.util.class()
|
||||||
|
|
||||||
|
-- Session constructor
|
||||||
|
function Session.__init__(self, path, uci)
|
||||||
|
uci = uci or ucicmd
|
||||||
|
if path then
|
||||||
|
self.ucicmd = uci .. " -P " .. path
|
||||||
|
else
|
||||||
|
self.ucicmd = uci
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- The default Session
|
||||||
|
local default = Session()
|
||||||
|
|
||||||
-- Wrapper for "uci add"
|
-- Wrapper for "uci add"
|
||||||
function add(config, section_type)
|
function Session.add(self, config, section_type)
|
||||||
return _uci("add " .. _path(config) .. " " .. _path(section_type))
|
return self:_uci("add " .. _path(config) .. " " .. _path(section_type))
|
||||||
|
end
|
||||||
|
|
||||||
|
function add(...)
|
||||||
|
return default:add(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Wrapper for "uci changes"
|
-- Wrapper for "uci changes"
|
||||||
function changes(config)
|
function Session.changes(self, config)
|
||||||
return _uci3("changes " .. _path(config))
|
return self:_uci3("changes " .. _path(config))
|
||||||
|
end
|
||||||
|
|
||||||
|
function change(...)
|
||||||
|
return default:change(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Wrapper for "uci commit"
|
-- Wrapper for "uci commit"
|
||||||
function commit(config)
|
function Session.commit(self, config)
|
||||||
return _uci2("commit " .. _path(config))
|
return self:_uci2("commit " .. _path(config))
|
||||||
|
end
|
||||||
|
|
||||||
|
function commit(...)
|
||||||
|
return default:commit(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Wrapper for "uci get"
|
-- Wrapper for "uci get"
|
||||||
function get(config, section, option)
|
function Session.get(self, config, section, option)
|
||||||
return _uci("get " .. _path(config, section, option))
|
return self:_uci("get " .. _path(config, section, option))
|
||||||
|
end
|
||||||
|
|
||||||
|
function get(...)
|
||||||
|
return default:get(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Wrapper for "uci revert"
|
-- Wrapper for "uci revert"
|
||||||
function revert(config)
|
function Session.revert(self, config)
|
||||||
return _uci2("revert " .. _path(config))
|
return self:_uci2("revert " .. _path(config))
|
||||||
|
end
|
||||||
|
|
||||||
|
function revert(...)
|
||||||
|
return self:revert(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Wrapper for "uci show"
|
-- Wrapper for "uci show"
|
||||||
function show(config)
|
function Session.show(self, config)
|
||||||
return _uci3("show " .. _path(config))
|
return self:_uci3("show " .. _path(config))
|
||||||
|
end
|
||||||
|
|
||||||
|
function show(...)
|
||||||
|
return default:show(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Wrapper for "uci set"
|
-- Wrapper for "uci set"
|
||||||
function set(config, section, option, value)
|
function Session.set(self, config, section, option, value)
|
||||||
return _uci2("set " .. _path(config, section, option, value))
|
return self:_uci2("set " .. _path(config, section, option, value))
|
||||||
|
end
|
||||||
|
|
||||||
|
function set(...)
|
||||||
|
return default:set(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Internal functions --
|
-- Internal functions --
|
||||||
|
|
||||||
function _uci(cmd)
|
function Session._uci(self, cmd)
|
||||||
local res = ffluci.util.exec(ucicmd .. " 2>/dev/null " .. cmd)
|
local res = ffluci.util.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
|
||||||
|
|
||||||
if res:len() == 0 then
|
if res:len() == 0 then
|
||||||
return nil
|
return nil
|
||||||
|
@ -85,8 +131,8 @@ function _uci(cmd)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function _uci2(cmd)
|
function Session._uci2(self, cmd)
|
||||||
local res = ffluci.util.exec(ucicmd .. " 2>&1 " .. cmd)
|
local res = ffluci.util.exec(self.ucicmd .. " 2>&1 " .. cmd)
|
||||||
|
|
||||||
if res:len() > 0 then
|
if res:len() > 0 then
|
||||||
return false, res
|
return false, res
|
||||||
|
@ -95,8 +141,8 @@ function _uci2(cmd)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function _uci3(cmd)
|
function Session._uci3(self, cmd)
|
||||||
local res = ffluci.util.execl(ucicmd .. " 2>&1 " .. cmd)
|
local res = ffluci.util.execl(self.ucicmd .. " 2>&1 " .. cmd)
|
||||||
if res[1]:sub(1, ucicmd:len() + 1) == ucicmd .. ":" then
|
if res[1]:sub(1, ucicmd:len() + 1) == ucicmd .. ":" then
|
||||||
return nil, res[1]
|
return nil, res[1]
|
||||||
end
|
end
|
||||||
|
|
|
@ -128,7 +128,7 @@ function render(name, scope, ...)
|
||||||
scope = scope or getfenv(2)
|
scope = scope or getfenv(2)
|
||||||
local s, t = pcall(Template, name)
|
local s, t = pcall(Template, name)
|
||||||
if not s then
|
if not s then
|
||||||
error("Unable to load template: " .. name)
|
error(t)
|
||||||
else
|
else
|
||||||
t:render(scope, ...)
|
t:render(scope, ...)
|
||||||
end
|
end
|
||||||
|
@ -165,7 +165,8 @@ function Template.__init__(self, name)
|
||||||
|
|
||||||
-- Compile and build
|
-- Compile and build
|
||||||
local sourcefile = viewdir .. name .. ".htm"
|
local sourcefile = viewdir .. name .. ".htm"
|
||||||
local compiledfile = viewdir .. name .. ".lua"
|
local compiledfile = viewdir .. name .. ".lua"
|
||||||
|
local err
|
||||||
|
|
||||||
if compiler_mode == "file" then
|
if compiler_mode == "file" then
|
||||||
local tplmt = ffluci.fs.mtime(sourcefile)
|
local tplmt = ffluci.fs.mtime(sourcefile)
|
||||||
|
@ -176,25 +177,22 @@ function Template.__init__(self, name)
|
||||||
or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
|
or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
|
||||||
local compiled = compile(ffluci.fs.readfile(sourcefile))
|
local compiled = compile(ffluci.fs.readfile(sourcefile))
|
||||||
ffluci.fs.writefile(compiledfile, compiled)
|
ffluci.fs.writefile(compiledfile, compiled)
|
||||||
self.template = loadstring(compiled)
|
self.template, err = loadstring(compiled)
|
||||||
else
|
else
|
||||||
self.template = loadfile(compiledfile)
|
self.template, err = loadfile(compiledfile)
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif compiler_mode == "none" then
|
elseif compiler_mode == "none" then
|
||||||
self.template = loadfile(self.compiledfile)
|
self.template, err = loadfile(self.compiledfile)
|
||||||
|
|
||||||
elseif compiler_mode == "memory" then
|
elseif compiler_mode == "memory" then
|
||||||
self.template = loadstring(compile(ffluci.fs.readfile(sourcefile)))
|
self.template, err = loadstring(compile(ffluci.fs.readfile(sourcefile)))
|
||||||
|
|
||||||
else
|
|
||||||
error("Invalid compiler mode: " .. compiler_mode)
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If we have no valid template throw error, otherwise cache the template
|
-- If we have no valid template throw error, otherwise cache the template
|
||||||
if not self.template then
|
if not self.template then
|
||||||
error("Unable to load template: " .. name)
|
error(err)
|
||||||
else
|
else
|
||||||
self.cache[name] = self.template
|
self.cache[name] = self.template
|
||||||
end
|
end
|
||||||
|
|
|
@ -36,7 +36,10 @@ function class(base)
|
||||||
setmetatable(inst, {__index = class})
|
setmetatable(inst, {__index = class})
|
||||||
|
|
||||||
if inst.__init__ then
|
if inst.__init__ then
|
||||||
inst:__init__(...)
|
local stat, err = pcall(inst.__init__, inst, ...)
|
||||||
|
if not stat then
|
||||||
|
error(err)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return inst
|
return inst
|
||||||
|
@ -138,6 +141,11 @@ function resfenv(f)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- Returns the Haserl unique sessionid
|
||||||
|
function sessionid()
|
||||||
|
return ENV.SESSIONID
|
||||||
|
end
|
||||||
|
|
||||||
-- Updates the scope of f with "extscope"
|
-- Updates the scope of f with "extscope"
|
||||||
function updfenv(f, extscope)
|
function updfenv(f, extscope)
|
||||||
local scope = getfenv(f)
|
local scope = getfenv(f)
|
||||||
|
|
Loading…
Reference in New Issue