* CBI: updates
[project/luci.git] / src / ffluci / util.lua
1 --[[
2 FFLuCI - Utility library
3
4 Description:
5 Several common useful Lua functions
6
7 FileId:
8 $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
16
17 http://www.apache.org/licenses/LICENSE-2.0
18
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24
25 ]]--
26
27 module("ffluci.util", package.seeall)
28
29
30 -- Lua simplified Python-style OO class support emulation
31 function class(base)
32 local class = {}
33
34 local create = function(class, ...)
35 local inst = {}
36 setmetatable(inst, {__index = class})
37
38 if inst.__init__ then
39 local stat, err = pcall(inst.__init__, inst, ...)
40 if not stat then
41 error(err)
42 end
43 end
44
45 return inst
46 end
47
48 local classmeta = {__call = create}
49
50 if base then
51 classmeta.__index = base
52 end
53
54 setmetatable(class, classmeta)
55 return class
56 end
57
58
59 -- Checks whether a table has an object "value" in it
60 function contains(table, value)
61 for k,v in pairs(table) do
62 if value == v then
63 return true
64 end
65 end
66 return false
67 end
68
69
70 -- Dumps a table to stdout (useful for testing and debugging)
71 function dumptable(t, i)
72 i = i or 0
73 for k,v in pairs(t) do
74 print(string.rep("\t", i) .. k, v)
75 if type(v) == "table" then
76 dumptable(v, i+1)
77 end
78 end
79 end
80
81
82 -- Escapes all occurences of c in s
83 function escape(s, c)
84 c = c or "\\"
85 return s:gsub(c, "\\" .. c)
86 end
87
88
89 -- Runs "command" and returns its output
90 function exec(command)
91 local pp = io.popen(command)
92 local data = pp:read("*a")
93 pp:close()
94
95 return data
96 end
97
98
99 -- Runs "command" and returns its output as a array of lines
100 function execl(command)
101 local pp = io.popen(command)
102 local line = ""
103 local data = {}
104
105 while true do
106 line = pp:read()
107 if (line == nil) then break end
108 table.insert(data, line)
109 end
110 pp:close()
111
112 return data
113 end
114
115
116 -- Populate obj in the scope of f as key
117 function extfenv(f, key, obj)
118 local scope = getfenv(f)
119 scope[key] = obj
120 end
121
122
123 -- Checks whether an object is an instanceof class
124 function instanceof(object, class)
125 local meta = getmetatable(object)
126 while meta and meta.__index do
127 if meta.__index == class then
128 return true
129 end
130 meta = getmetatable(meta.__index)
131 end
132 return false
133 end
134
135
136 -- Resets the scope of f doing a shallow copy of its scope into a new table
137 function resfenv(f)
138 local scope = getfenv(f)
139 setfenv(f, {})
140 updfenv(f, scope)
141 end
142
143
144 -- Returns the Haserl unique sessionid
145 function sessionid()
146 return ENV.SESSIONID
147 end
148
149 -- Updates the scope of f with "extscope"
150 function updfenv(f, extscope)
151 local scope = getfenv(f)
152 for k, v in pairs(extscope) do
153 scope[k] = v
154 end
155 end
156
157
158 -- Validates a variable
159 function validate(value, cast_number, cast_int, valid)
160 if cast_number or cast_int then
161 value = tonumber(value)
162 end
163
164 if cast_int and not(value % 1 == 0) then
165 value = nil
166 end
167
168
169 if type(valid) == "function" then
170 value = valid(value)
171 elseif type(valid) == "table" then
172 if not contains(valid, value) then
173 value = nil
174 end
175 end
176
177 return value
178 end
179
180
181 -- Returns the filename of the calling script
182 function __file__()
183 return debug.getinfo(2, 'S').source:sub(2)
184 end