Added module for system abstraction

Rewrote readfile and exec functions
Moved some orpahned example file out of the way
This commit is contained in:
Steven Barth 2008-03-12 20:52:28 +00:00
parent 93a98dd13b
commit ffe39ec1ba
9 changed files with 124 additions and 22 deletions

View file

@ -5,7 +5,8 @@ FILES = ffluci/config.lua
CFILES = ffluci/util.lua ffluci/http.lua \ CFILES = ffluci/util.lua ffluci/http.lua \
ffluci/fs.lua ffluci/i18n.lua ffluci/model/uci.lua \ ffluci/fs.lua ffluci/i18n.lua ffluci/model/uci.lua \
ffluci/template.lua ffluci/dispatcher.lua ffluci/menu.lua ffluci/init.lua ffluci/template.lua ffluci/dispatcher.lua ffluci/menu.lua \
ffluci/init.lua ffluci/sys.lua
DIRECTORIES = dist/ffluci/model dist/ffluci/controller/public dist/ffluci/controller/admin dist/ffluci/i18n dist/ffluci/view DIRECTORIES = dist/ffluci/model dist/ffluci/controller/public dist/ffluci/controller/admin dist/ffluci/i18n dist/ffluci/view

View file

@ -1,8 +1,22 @@
@charset "utf-8";
body { body {
font-family: Verdana, Arial, sans-serif; font-family: Verdana, Arial, sans-serif;
background-color: #aaaaaa; background-color: #aaaaaa;
} }
h1 {
margin: 0%;
font-size: 1.4em;
font-weight: bold;
}
h2 {
margin: 0%;
font-size: 1.2em;
font-weight: bold;
}
#header { #header {
padding: 0.2em; padding: 0.2em;
height: 4.5em; height: 4.5em;
@ -24,11 +38,12 @@ body {
} }
#content { #content {
margin-left: 10em; margin-left: 14em;
margin-right: 10em; margin-right: 14em;
display: block; display: block;
position: relative; position: relative;
padding: 2px; padding: 2px;
font-size: 0.8em;
} }
.headerlogo { .headerlogo {

View file

@ -31,14 +31,36 @@ require("lfs")
-- Returns the content of file -- Returns the content of file
function readfile(filename) function readfile(filename)
local fp = io.open(filename) local fp = io.open(filename)
if fp == nil then if fp == nil then
error("Unable to open file for reading: " .. filename) error("Unable to open file for reading: " .. filename)
end end
local data = fp:read("*a") local data = fp:read("*a")
fp:close() fp:close()
return data return data
end end
-- Returns the content of file as array of lines
function readfilel(filename)
local fp = io.open(filename)
local line = ""
local data = {}
if fp == nil then
error("Unable to open file for reading: " .. filename)
end
while true do
line = fp:read()
if (line == nil) then break end
table.insert(data, line)
end
fp:close()
return data
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 = io.open(filename, "w")

1
src/ffluci/i18n/index.en Normal file
View file

@ -0,0 +1 @@
hello = "Hello"

View file

@ -96,7 +96,7 @@ function _uci2(cmd)
end end
function _uci3(cmd) function _uci3(cmd)
local res = ffluci.util.exec(ucicmd .. " 2>&1 " .. cmd, true) local res = ffluci.util.execl(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

39
src/ffluci/sys.lua Normal file
View file

@ -0,0 +1,39 @@
--[[
FFLuCI - System library
Description:
Utilities for interaction with the Linux system
FileId:
$Id$
License:
Copyright 2008 Steven Barth <steven@midlink.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--
module("ffluci.sys", package.seeall)
require("ffluci.fs")
-- Returns the hostname
function hostname()
return ffluci.fs.readfilel("/proc/sys/kernel/hostname")[1]
end
-- Returns the load average
function loadavg()
local loadavg = ffluci.fs.readfilel("/proc/loadavg")[1]
return loadavg:match("^(.-) (.-) (.-) (.-) (.-)$")
end

View file

@ -26,6 +26,26 @@ limitations under the License.
module("ffluci.util", package.seeall) module("ffluci.util", package.seeall)
-- Lua OO class support emulation
function class(base)
local clsobj = {}
local metatable = {__index = clsobj}
function clsobj.new()
local inst = {}
setmetatable(inst, metatable)
return inst
end
if base then
setmetatable(clsobj, {__index = base})
end
return clsobj
end
-- Checks whether a table has an object "value" in it -- Checks whether a table has an object "value" in it
function contains(table, value) function contains(table, value)
for k,v in pairs(table) do for k,v in pairs(table) do
@ -57,24 +77,26 @@ end
-- Runs "command" and returns its output -- Runs "command" and returns its output
function exec(command, return_array) function exec(command)
local pp = io.popen(command) local pp = io.popen(command)
local data = nil local data = pp:read("*a")
pp:close()
if return_array then return data
local line = "" end
data = {}
-- Runs "command" and returns its output as a array of lines
while true do function execl(command)
line = pp:read() local pp = io.popen(command)
if (line == nil) then break end local line = ""
table.insert(data, line) local data = {}
end
pp:close() while true do
else line = pp:read()
data = pp:read("*a") if (line == nil) then break end
pp:close() table.insert(data, line)
end end
pp:close()
return data return data
end end

View file

@ -1,4 +1,6 @@
<% <%
require("ffluci.sys")
local load1, load5, load15 = ffluci.sys.loadavg()
local req = require("ffluci.dispatcher").request local req = require("ffluci.dispatcher").request
local menu = require("ffluci.menu").get()[req.category] local menu = require("ffluci.menu").get()[req.category]
require("ffluci.i18n").loadc("default") require("ffluci.i18n").loadc("default")
@ -16,8 +18,8 @@ require("ffluci.http").htmlheader()
<div class="whitetext smalltext right"> <div class="whitetext smalltext right">
OpenWRT Kamikaze<br /> OpenWRT Kamikaze<br />
Freifunk Firmware 2.0-dev<br /> Freifunk Firmware 2.0-dev<br />
Load average: 1.00 2.00 3.00<br /> <%:load Last%>: <%=load1%> <%=load5%> <%=load15%><br />
1.2.3.4 - host1 <%:hostname Hostname%>: <%=ffluci.sys.hostname()%>
</div> </div>
<div> <div>
<span class="headertitle">Freifunk Kamikaze</span><br /> <span class="headertitle">Freifunk Kamikaze</span><br />