From ffe39ec1babf58fef087bd580d7ad0725d9b3389 Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Wed, 12 Mar 2008 20:52:28 +0000 Subject: Added module for system abstraction Rewrote readfile and exec functions Moved some orpahned example file out of the way --- Makefile | 3 +- contrib/media/cascade.css | 19 +++++++++-- examples/ffluci/i18n/example-simpleview.de | 6 ++++ src/ffluci/fs.lua | 22 ++++++++++++ src/ffluci/i18n/example-simpleview.de | 6 ---- src/ffluci/i18n/index.en | 1 + src/ffluci/model/uci.lua | 2 +- src/ffluci/sys.lua | 39 +++++++++++++++++++++ src/ffluci/util.lua | 54 +++++++++++++++++++++--------- src/ffluci/view/header.htm | 6 ++-- 10 files changed, 130 insertions(+), 28 deletions(-) create mode 100644 examples/ffluci/i18n/example-simpleview.de delete mode 100644 src/ffluci/i18n/example-simpleview.de create mode 100644 src/ffluci/i18n/index.en create mode 100644 src/ffluci/sys.lua diff --git a/Makefile b/Makefile index 2d732ff083..a2d4dd1e4b 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ FILES = ffluci/config.lua CFILES = ffluci/util.lua ffluci/http.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 diff --git a/contrib/media/cascade.css b/contrib/media/cascade.css index 143b93ba29..49e51f1c82 100644 --- a/contrib/media/cascade.css +++ b/contrib/media/cascade.css @@ -1,8 +1,22 @@ +@charset "utf-8"; + body { font-family: Verdana, Arial, sans-serif; background-color: #aaaaaa; } +h1 { + margin: 0%; + font-size: 1.4em; + font-weight: bold; +} + +h2 { + margin: 0%; + font-size: 1.2em; + font-weight: bold; +} + #header { padding: 0.2em; height: 4.5em; @@ -24,11 +38,12 @@ body { } #content { - margin-left: 10em; - margin-right: 10em; + margin-left: 14em; + margin-right: 14em; display: block; position: relative; padding: 2px; + font-size: 0.8em; } .headerlogo { diff --git a/examples/ffluci/i18n/example-simpleview.de b/examples/ffluci/i18n/example-simpleview.de new file mode 100644 index 0000000000..db2bee0cf7 --- /dev/null +++ b/examples/ffluci/i18n/example-simpleview.de @@ -0,0 +1,6 @@ +descr = [[Dies ist das Simple View-Beispiel.
+Dieses Template ist: ffluci/view/example-simpleview/index.htm und gehoert +zur Aktion "index".
+Diese Uebersetzung ist: ffluci/i18n/example-simpleview.de]] + +lan = "Die LAN IP-Adresse des Routers lautet:" \ No newline at end of file diff --git a/src/ffluci/fs.lua b/src/ffluci/fs.lua index 5a1cc6b351..55da9b8dce 100644 --- a/src/ffluci/fs.lua +++ b/src/ffluci/fs.lua @@ -31,14 +31,36 @@ require("lfs") -- Returns the content of file function readfile(filename) local fp = io.open(filename) + if fp == nil then error("Unable to open file for reading: " .. filename) end + local data = fp:read("*a") fp:close() return data 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 function writefile(filename, data) local fp = io.open(filename, "w") diff --git a/src/ffluci/i18n/example-simpleview.de b/src/ffluci/i18n/example-simpleview.de deleted file mode 100644 index db2bee0cf7..0000000000 --- a/src/ffluci/i18n/example-simpleview.de +++ /dev/null @@ -1,6 +0,0 @@ -descr = [[Dies ist das Simple View-Beispiel.
-Dieses Template ist: ffluci/view/example-simpleview/index.htm und gehoert -zur Aktion "index".
-Diese Uebersetzung ist: ffluci/i18n/example-simpleview.de]] - -lan = "Die LAN IP-Adresse des Routers lautet:" \ No newline at end of file diff --git a/src/ffluci/i18n/index.en b/src/ffluci/i18n/index.en new file mode 100644 index 0000000000..71255873ab --- /dev/null +++ b/src/ffluci/i18n/index.en @@ -0,0 +1 @@ +hello = "Hello" \ No newline at end of file diff --git a/src/ffluci/model/uci.lua b/src/ffluci/model/uci.lua index 492367ce28..9b9fcb0935 100644 --- a/src/ffluci/model/uci.lua +++ b/src/ffluci/model/uci.lua @@ -96,7 +96,7 @@ function _uci2(cmd) end 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 return nil, res[1] end diff --git a/src/ffluci/sys.lua b/src/ffluci/sys.lua new file mode 100644 index 0000000000..048f6d3751 --- /dev/null +++ b/src/ffluci/sys.lua @@ -0,0 +1,39 @@ +--[[ +FFLuCI - System library + +Description: +Utilities for interaction with the Linux system + +FileId: +$Id$ + +License: +Copyright 2008 Steven Barth + +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 \ No newline at end of file diff --git a/src/ffluci/util.lua b/src/ffluci/util.lua index 07cbb8000c..3004f552e6 100644 --- a/src/ffluci/util.lua +++ b/src/ffluci/util.lua @@ -26,6 +26,26 @@ limitations under the License. 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 function contains(table, value) for k,v in pairs(table) do @@ -57,24 +77,26 @@ end -- Runs "command" and returns its output -function exec(command, return_array) +function exec(command) local pp = io.popen(command) - local data = nil + local data = pp:read("*a") + pp:close() - if return_array then - local line = "" - data = {} - - while true do - line = pp:read() - if (line == nil) then break end - table.insert(data, line) - end - pp:close() - else - data = pp:read("*a") - pp:close() - end + return data +end + +-- Runs "command" and returns its output as a array of lines +function execl(command) + local pp = io.popen(command) + local line = "" + local data = {} + + while true do + line = pp:read() + if (line == nil) then break end + table.insert(data, line) + end + pp:close() return data end diff --git a/src/ffluci/view/header.htm b/src/ffluci/view/header.htm index 2c0836be5b..44826f059a 100644 --- a/src/ffluci/view/header.htm +++ b/src/ffluci/view/header.htm @@ -1,4 +1,6 @@ <% +require("ffluci.sys") +local load1, load5, load15 = ffluci.sys.loadavg() local req = require("ffluci.dispatcher").request local menu = require("ffluci.menu").get()[req.category] require("ffluci.i18n").loadc("default") @@ -16,8 +18,8 @@ require("ffluci.http").htmlheader()
OpenWRT Kamikaze
Freifunk Firmware 2.0-dev
- Load average: 1.00 2.00 3.00
- 1.2.3.4 - host1 + <%:load Last%>: <%=load1%> <%=load5%> <%=load15%>
+ <%:hostname Hostname%>: <%=ffluci.sys.hostname()%>
Freifunk Kamikaze
-- cgit v1.2.3