require("ffluci.i18n")
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
- error("Unable to load CBI map: " .. cbimap)
+ error(err)
return nil
end
Node.__init__(self, ...)
self.config = config
self.template = "cbi/map"
+ self.uci = ffluci.model.uci.Session()
end
function Map.parse(self)
- self.ucidata = ffluci.model.uci.show(self.config)
+ self.ucidata = self.uci:show(self.config)
if not self.ucidata then
error("Unable to read UCI data: " .. self.config)
else
end
function Map.render(self)
- self.ucidata = ffluci.model.uci.show(self.config)
+ self.ucidata = self.uci:show(self.config)
if not self.ucidata then
error("Unable to read UCI data: " .. self.config)
else
end
end
+function Map.set(self, section, option, value)
+ return self.uci:set(self.config, section, option, value)
+end
+
--[[
AbstractSection
]]--
end
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
self.list = {}
end
-function ListValue.addValue(self, key, val)
+function ListValue.add_value(self, key, val)
val = val or key
self.list[key] = val
end
\ No newline at end of file
-- Returns the content of file
function readfile(filename)
- local fp = io.open(filename)
+ local fp, err = io.open(filename)
if fp == nil then
- error("Unable to open file for reading: " .. filename)
+ error(err)
end
local data = fp:read("*a")
-- Returns the content of file as array of lines
function readfilel(filename)
- local fp = io.open(filename)
+ local fp, err = io.open(filename)
local line = ""
local data = {}
if fp == nil then
- error("Unable to open file for reading: " .. filename)
+ error(err)
end
while true do
-- Writes given data to a file
function writefile(filename, data)
- local fp = io.open(filename, "w")
+ local fp, err = io.open(filename, "w")
if fp == nil then
- error("Unable to open file for writing: " .. filename)
+ error(err)
end
fp:write(data)
fp:close()
end
end
return e
+end
+
+-- Alias for lfs.mkdir
+function mkdir(...)
+ return lfs.mkdir(...)
end
\ No newline at end of file
]]--
module("ffluci.model.uci", package.seeall)
require("ffluci.util")
+require("ffluci.fs")
+-- The OS uci command
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"
-function add(config, section_type)
- return _uci("add " .. _path(config) .. " " .. _path(section_type))
+function Session.add(self, config, section_type)
+ return self:_uci("add " .. _path(config) .. " " .. _path(section_type))
+end
+
+function add(...)
+ return default:add(...)
end
-- Wrapper for "uci changes"
-function changes(config)
- return _uci3("changes " .. _path(config))
+function Session.changes(self, config)
+ return self:_uci3("changes " .. _path(config))
+end
+
+function change(...)
+ return default:change(...)
end
-- Wrapper for "uci commit"
-function commit(config)
- return _uci2("commit " .. _path(config))
+function Session.commit(self, config)
+ return self:_uci2("commit " .. _path(config))
+end
+
+function commit(...)
+ return default:commit(...)
end
-- Wrapper for "uci get"
-function get(config, section, option)
- return _uci("get " .. _path(config, section, option))
+function Session.get(self, config, section, option)
+ return self:_uci("get " .. _path(config, section, option))
+end
+
+function get(...)
+ return default:get(...)
end
-- Wrapper for "uci revert"
-function revert(config)
- return _uci2("revert " .. _path(config))
+function Session.revert(self, config)
+ return self:_uci2("revert " .. _path(config))
+end
+
+function revert(...)
+ return self:revert(...)
end
-- Wrapper for "uci show"
-function show(config)
- return _uci3("show " .. _path(config))
+function Session.show(self, config)
+ return self:_uci3("show " .. _path(config))
+end
+
+function show(...)
+ return default:show(...)
end
-- Wrapper for "uci set"
-function set(config, section, option, value)
- return _uci2("set " .. _path(config, section, option, value))
+function Session.set(self, config, section, option, value)
+ return self:_uci2("set " .. _path(config, section, option, value))
+end
+
+function set(...)
+ return default:set(...)
end
-- Internal functions --
-function _uci(cmd)
- local res = ffluci.util.exec(ucicmd .. " 2>/dev/null " .. cmd)
+function Session._uci(self, cmd)
+ local res = ffluci.util.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
if res:len() == 0 then
return nil
end
end
-function _uci2(cmd)
- local res = ffluci.util.exec(ucicmd .. " 2>&1 " .. cmd)
+function Session._uci2(self, cmd)
+ local res = ffluci.util.exec(self.ucicmd .. " 2>&1 " .. cmd)
if res:len() > 0 then
return false, res
end
end
-function _uci3(cmd)
- local res = ffluci.util.execl(ucicmd .. " 2>&1 " .. cmd)
+function Session._uci3(self, cmd)
+ local res = ffluci.util.execl(self.ucicmd .. " 2>&1 " .. cmd)
if res[1]:sub(1, ucicmd:len() + 1) == ucicmd .. ":" then
return nil, res[1]
end
scope = scope or getfenv(2)
local s, t = pcall(Template, name)
if not s then
- error("Unable to load template: " .. name)
+ error(t)
else
t:render(scope, ...)
end
-- Compile and build
local sourcefile = viewdir .. name .. ".htm"
- local compiledfile = viewdir .. name .. ".lua"
+ local compiledfile = viewdir .. name .. ".lua"
+ local err
if compiler_mode == "file" then
local tplmt = ffluci.fs.mtime(sourcefile)
or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
local compiled = compile(ffluci.fs.readfile(sourcefile))
ffluci.fs.writefile(compiledfile, compiled)
- self.template = loadstring(compiled)
+ self.template, err = loadstring(compiled)
else
- self.template = loadfile(compiledfile)
+ self.template, err = loadfile(compiledfile)
end
elseif compiler_mode == "none" then
- self.template = loadfile(self.compiledfile)
+ self.template, err = loadfile(self.compiledfile)
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
-- If we have no valid template throw error, otherwise cache the template
if not self.template then
- error("Unable to load template: " .. name)
+ error(err)
else
self.cache[name] = self.template
end
setmetatable(inst, {__index = class})
if inst.__init__ then
- inst:__init__(...)
+ local stat, err = pcall(inst.__init__, inst, ...)
+ if not stat then
+ error(err)
+ end
end
return inst
end
+-- Returns the Haserl unique sessionid
+function sessionid()
+ return ENV.SESSIONID
+end
+
-- Updates the scope of f with "extscope"
function updfenv(f, extscope)
local scope = getfenv(f)