libs/core: Add Pythonic string formatting syntax
[project/luci.git] / libs / core / luasrc / util.lua
1 --[[
2 LuCI - 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 --- LuCI utility functions.
28 module("luci.util", package.seeall)
29
30 --
31 -- Pythonic string formatting extension
32 --
33 getmetatable("").__mod = function(a, b)
34         if not b then
35                 return a
36         elseif type(b) == "table" then
37                 return a:format(unpack(b))
38         else
39                 return a:format(b)
40         end
41 end
42
43
44 --
45 -- Class helper routines
46 --
47
48 --- Create a Class object (Python-style object model).
49 -- The class object can be instantiated by calling itself.
50 -- Any class functions or shared parameters can be attached to this object.
51 -- Attaching a table to the class object makes this table shared between
52 -- all instances of this class. For object parameters use the __init__ function.
53 -- Classes can inherit member functions and values from a base class.
54 -- Class can be instantiated by calling them. All parameters will be passed
55 -- to the __init__ function of this class - if such a function exists.
56 -- The __init__ function must be used to set any object parameters that are not shared
57 -- with other objects of this class. Any return values will be ignored.
58 -- @param base  The base class to inherit from (optional)
59 -- @return              A class object
60 -- @see                 instanceof
61 -- @see                 clone
62 function class(base)
63         local class = {}
64
65         local create = function(class, ...)
66                 local inst = {}
67                 setmetatable(inst, {__index = class})
68
69                 if inst.__init__ then
70                         local stat, err = copcall(inst.__init__, inst, ...)
71                         if not stat then
72                                 error(err)
73                         end
74                 end
75
76                 return inst
77         end
78
79         local classmeta = {__call = create}
80
81         if base then
82                 classmeta.__index = base
83         end
84
85         setmetatable(class, classmeta)
86         return class
87 end
88
89 --- Test whether the given object is an instance of the given class.
90 -- @param object        Object instance
91 -- @param class         Class object to test against
92 -- @return                      Boolean indicating whether the object is an instance
93 -- @see                         class
94 -- @see                         clone
95 function instanceof(object, class)
96         local meta = getmetatable(object)
97         while meta and meta.__index do
98                 if meta.__index == class then
99                         return true
100                 end
101                 meta = getmetatable(meta.__index)
102         end
103         return false
104 end
105
106
107 --
108 -- Scope manipulation routines
109 --
110
111 --- Replace a function scope with a shallow copy of itself.
112 -- This is useful if you want to get rid of several unwanted side effects
113 -- while changing the scope of a certain Lua function.
114 -- @param f             Lua function
115 function resfenv(f)
116         setfenv(f, clone(getfenv(f)))
117 end
118
119 --- Store given object associated with given key in the scope of a function.
120 -- @param f             Lua function
121 -- @param key   String value containg the key of the object to store
122 -- @param obj   Object to store in the scope
123 -- @return              Always nil
124 -- @see updfenv
125 -- @see resfenv
126 function extfenv(f, key, obj)
127         local scope = getfenv(f)
128         scope[key] = obj
129 end
130
131 --- Extend the scope of a function with the contents of a table
132 -- @param f             Lua function
133 -- @param key   String value containg the key of the object to store
134 -- @param obj   Object to store in the scope
135 -- @return              Always nil
136 -- @see extfenv
137 -- @see resfenv
138 function updfenv(f, extscope)
139         update(getfenv(f), extscope)
140 end
141
142 --- Create a new or get an already existing thread local store associated with
143 -- the current active coroutine. A thread local store is private a table object
144 -- whose values can't be accessed from outside of the running coroutine.
145 -- @return      Table value representing the corresponding thread local store
146 function threadlocal()
147         local tbl = {}
148
149         local function get(self, key)
150                 local c = coroutine.running()
151                 local thread = coxpt[c] or c or 0
152                 if not rawget(self, thread) then
153                         return nil
154                 end
155                 return rawget(self, thread)[key]
156         end
157
158         local function set(self, key, value)
159                 local c = coroutine.running()
160                 local thread = coxpt[c] or c or 0
161                 if not rawget(self, thread) then
162                         rawset(self, thread, {})
163                 end
164                 rawget(self, thread)[key] = value
165         end
166
167         setmetatable(tbl, {__index = get, __newindex = set, __mode = "k"})
168
169         return tbl
170 end
171
172
173 --
174 -- Debugging routines
175 --
176
177 --- Write given object to stderr.
178 -- @param obj   Value to write to stderr
179 -- @return              Boolean indicating whether the write operation was successful
180 function perror(obj)
181         return io.stderr:write(tostring(obj) .. "\n")
182 end
183
184 --- Recursively dumps a table to stdout, useful for testing and debugging.
185 -- @param t     Table value to dump
186 -- @param i     Number of tabs to prepend to each line
187 -- @return      Always nil
188 function dumptable(t, i)
189         i = i or 0
190         for k,v in pairs(t) do
191                 print(string.rep("\t", i) .. tostring(k), tostring(v))
192                 if type(v) == "table" then
193                         dumptable(v, i+1)
194                 end
195         end
196 end
197
198
199 --
200 -- String and data manipulation routines
201 --
202
203 --- Escapes all occurrences of the given character in given string.
204 -- @param s     String value containing unescaped characters
205 -- @param c     String value with character to escape (optional, defaults to "\")
206 -- @return      String value with each occurrence of character escaped with "\"
207 function escape(s, c)
208         c = c or "\\"
209         return s:gsub(c, "\\" .. c)
210 end
211
212 --- Create valid XML PCDATA from given string.
213 -- @param value String value containing the data to escape
214 -- @return              String value containing the escaped data
215 function pcdata(value)
216         if not value then return end
217         value = tostring(value)
218         value = value:gsub("&", "&amp;")
219         value = value:gsub('"', "&quot;")
220         value = value:gsub("'", "&apos;")
221         value = value:gsub("<", "&lt;")
222         return value:gsub(">", "&gt;")
223 end
224
225 --- Strip HTML tags from given string.
226 -- @param value String containing the HTML text
227 -- @return      String with HTML tags stripped of
228 function striptags(s)
229         return pcdata(s:gsub("</?[A-Za-z][A-Za-z0-9:_%-]*[^>]*>", " "):gsub("%s+", " "))
230 end
231
232 --- Splits given string on a defined separator sequence and return a table
233 -- containing the resulting substrings. The optional max parameter specifies
234 -- the number of bytes to process, regardless of the actual length of the given
235 -- string. The optional last parameter, regex, specifies whether the separator
236 -- sequence is interpreted as regular expression.
237 -- @param str           String value containing the data to split up
238 -- @param pat           String with separator pattern (optional, defaults to "\n")
239 -- @param max           Maximum times to split (optional)
240 -- @param regex         Boolean indicating whether to interpret the separator
241 --                                      pattern as regular expression (optional, default is false)
242 -- @return                      Table containing the resulting substrings
243 function split(str, pat, max, regex)
244         pat = pat or "\n"
245         max = max or #str
246
247         local t = {}
248         local c = 1
249
250         if #str == 0 then
251                 return {""}
252         end
253
254         if #pat == 0 then
255                 return nil
256         end
257
258         if max == 0 then
259                 return str
260         end
261
262         repeat
263                 local s, e = str:find(pat, c, not regex)
264                 max = max - 1
265                 if s and max < 0 then
266                         table.insert(t, str:sub(c))
267                 else
268                         table.insert(t, str:sub(c, s and s - 1))
269                 end
270                 c = e and e + 1 or #str + 1
271         until not s or max < 0
272
273         return t
274 end
275
276 --- Remove leading and trailing whitespace from given string value.
277 -- @param str   String value containing whitespace padded data
278 -- @return              String value with leading and trailing space removed
279 function trim(str)
280         local s = str:gsub("^%s*(.-)%s*$", "%1")
281         return s
282 end
283
284 --- Parse certain units from the given string and return the canonical integer
285 -- value or 0 if the unit is unknown. Upper- or lower case is irrelevant.
286 -- Recognized units are:
287 --      o "y"   - one year   (60*60*24*366)
288 --  o "m"       - one month  (60*60*24*31)
289 --  o "w"       - one week   (60*60*24*7)
290 --  o "d"       - one day    (60*60*24)
291 --  o "h"       - one hour       (60*60)
292 --  o "min"     - one minute (60)
293 --  o "kb"  - one kilobyte (1024)
294 --  o "mb"      - one megabyte (1024*1024)
295 --  o "gb"      - one gigabyte (1024*1024*1024)
296 --  o "kib" - one si kilobyte (1000)
297 --  o "mib"     - one si megabyte (1000*1000)
298 --  o "gib"     - one si gigabyte (1000*1000*1000)
299 -- @param ustr  String containing a numerical value with trailing unit
300 -- @return              Number containing the canonical value
301 function parse_units(ustr)
302
303         local val = 0
304
305         -- unit map
306         local map = {
307                 -- date stuff
308                 y   = 60 * 60 * 24 * 366,
309                 m   = 60 * 60 * 24 * 31,
310                 w   = 60 * 60 * 24 * 7,
311                 d   = 60 * 60 * 24,
312                 h   = 60 * 60,
313                 min = 60,
314
315                 -- storage sizes
316                 kb  = 1024,
317                 mb  = 1024 * 1024,
318                 gb  = 1024 * 1024 * 1024,
319
320                 -- storage sizes (si)
321                 kib = 1000,
322                 mib = 1000 * 1000,
323                 gib = 1000 * 1000 * 1000
324         }
325
326         -- parse input string
327         for spec in ustr:lower():gmatch("[0-9%.]+[a-zA-Z]*") do
328
329                 local num = spec:gsub("[^0-9%.]+$","")
330                 local spn = spec:gsub("^[0-9%.]+", "")
331
332                 if map[spn] or map[spn:sub(1,1)] then
333                         val = val + num * ( map[spn] or map[spn:sub(1,1)] )
334                 else
335                         val = val + num
336                 end
337         end
338
339
340         return val
341 end
342
343 --- Combines two or more numerically indexed tables into one.
344 -- @param tbl1  Table value to combine
345 -- @param tbl2  Table value to combine
346 -- @param ...   More tables to combine
347 -- @return              Table value containing all values of given tables
348 function combine(...)
349         local result = {}
350         for i, a in ipairs(arg) do
351                 for j, v in ipairs(a) do
352                         table.insert(result, v)
353                 end
354         end
355         return result
356 end
357
358 --- Checks whether the given table contains the given value.
359 -- @param table Table value
360 -- @param value Value to search within the given table
361 -- @return              Boolean indicating whether the given value occurs within table
362 function contains(table, value)
363         for k, v in pairs(table) do
364                 if value == v then
365                         return k
366                 end
367         end
368         return false
369 end
370
371 --- Update values in given table with the values from the second given table.
372 -- Both table are - in fact - merged together.
373 -- @param t                     Table which should be updated
374 -- @param updates       Table containing the values to update
375 -- @return                      Always nil
376 function update(t, updates)
377         for k, v in pairs(updates) do
378                 t[k] = v
379         end
380 end
381
382 --- Retrieve all keys of given associative table.
383 -- @param t     Table to extract keys from
384 -- @return      Sorted table containing the keys
385 function keys(t)
386         local keys = { }
387         if t then
388                 for k, _ in kspairs(t) do
389                         table.insert( keys, k )
390                 end
391         end
392         return keys
393 end
394
395 --- Clones the given object and return it's copy.
396 -- @param object        Table value to clone
397 -- @param deep          Boolean indicating whether to do recursive cloning
398 -- @return                      Cloned table value
399 function clone(object, deep)
400         local copy = {}
401
402         for k, v in pairs(object) do
403                 if deep and type(v) == "table" then
404                         v = clone(v, deep)
405                 end
406                 copy[k] = v
407         end
408
409         setmetatable(copy, getmetatable(object))
410
411         return copy
412 end
413
414
415 --- Create a dynamic table which automatically creates subtables.
416 -- @return      Dynamic Table
417 function dtable()
418         return setmetatable({}, { __index =
419                 function(tbl, key)
420                         return rawget(tbl, key)
421                          or rawget(rawset(tbl, key, dtable()), key)
422                 end
423         })
424 end
425
426
427 -- Serialize the contents of a table value.
428 function _serialize_table(t)
429         local data = ""
430         for k, v in pairs(t) do
431                 k = serialize_data(k)
432                 v = serialize_data(v)
433                 data = data .. ( #data > 0 and ", " or "" ) ..
434                         '[' .. k .. '] = ' .. v
435         end
436         return data
437 end
438
439 --- Recursively serialize given data to lua code, suitable for restoring
440 -- with loadstring().
441 -- @param val   Value containing the data to serialize
442 -- @return              String value containing the serialized code
443 -- @see                 restore_data
444 -- @see                 get_bytecode
445 function serialize_data(val)
446         if val == nil then
447                 return "nil"
448         elseif type(val) == "number" then
449                 return val
450         elseif type(val) == "string" then
451                 return string.format("%q", val)
452         elseif type(val) == "boolean" then
453                 return val and "true" or "false"
454         elseif type(val) == "function" then
455                 return string.format("loadstring(%q)", get_bytecode(val))
456         elseif type(val) == "table" then
457                 return "{ " .. _serialize_table(val) .. " }"
458         else
459                 return '"[unhandled data type:' .. type(val) .. ']"'
460         end
461 end
462
463 --- Restore data previously serialized with serialize_data().
464 -- @param str   String containing the data to restore
465 -- @return              Value containing the restored data structure
466 -- @see                 serialize_data
467 -- @see                 get_bytecode
468 function restore_data(str)
469         return loadstring("return " .. str)()
470 end
471
472
473 --
474 -- Byte code manipulation routines
475 --
476
477 --- Return the current runtime bytecode of the given data. The byte code
478 -- will be stripped before it is returned.
479 -- @param val   Value to return as bytecode
480 -- @return              String value containing the bytecode of the given data
481 function get_bytecode(val)
482         local code
483
484         if type(val) == "function" then
485                 code = string.dump(val)
486         else
487                 code = string.dump( loadstring( "return " .. serialize_data(val) ) )
488         end
489
490         return code and strip_bytecode(code)
491 end
492
493 --- Strips unnescessary lua bytecode from given string. Information like line
494 -- numbers and debugging numbers will be discarded. Original version by
495 -- Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html)
496 -- @param code  String value containing the original lua byte code
497 -- @return              String value containing the stripped lua byte code
498 function strip_bytecode(code)
499         local version, format, endian, int, size, ins, num, lnum = code:byte(5, 12)
500         local subint
501         if endian == 1 then
502                 subint = function(code, i, l)
503                         local val = 0
504                         for n = l, 1, -1 do
505                                 val = val * 256 + code:byte(i + n - 1)
506                         end
507                         return val, i + l
508                 end
509         else
510                 subint = function(code, i, l)
511                         local val = 0
512                         for n = 1, l, 1 do
513                                 val = val * 256 + code:byte(i + n - 1)
514                         end
515                         return val, i + l
516                 end
517         end
518
519         local strip_function
520         strip_function = function(code)
521                 local count, offset = subint(code, 1, size)
522                 local stripped, dirty = string.rep("\0", size), offset + count
523                 offset = offset + count + int * 2 + 4
524                 offset = offset + int + subint(code, offset, int) * ins
525                 count, offset = subint(code, offset, int)
526                 for n = 1, count do
527                         local t
528                         t, offset = subint(code, offset, 1)
529                         if t == 1 then
530                                 offset = offset + 1
531                         elseif t == 4 then
532                                 offset = offset + size + subint(code, offset, size)
533                         elseif t == 3 then
534                                 offset = offset + num
535                         elseif t == 254 or t == 9 then
536                                 offset = offset + lnum
537                         end
538                 end
539                 count, offset = subint(code, offset, int)
540                 stripped = stripped .. code:sub(dirty, offset - 1)
541                 for n = 1, count do
542                         local proto, off = strip_function(code:sub(offset, -1))
543                         stripped, offset = stripped .. proto, offset + off - 1
544                 end
545                 offset = offset + subint(code, offset, int) * int + int
546                 count, offset = subint(code, offset, int)
547                 for n = 1, count do
548                         offset = offset + subint(code, offset, size) + size + int * 2
549                 end
550                 count, offset = subint(code, offset, int)
551                 for n = 1, count do
552                         offset = offset + subint(code, offset, size) + size
553                 end
554                 stripped = stripped .. string.rep("\0", int * 3)
555                 return stripped, offset
556         end
557
558         return code:sub(1,12) .. strip_function(code:sub(13,-1))
559 end
560
561
562 --
563 -- Sorting iterator functions
564 --
565
566 function _sortiter( t, f )
567         local keys = { }
568
569         for k, v in pairs(t) do
570                 table.insert( keys, k )
571         end
572
573         local _pos = 0
574         local _len = table.getn( keys )
575
576         table.sort( keys, f )
577
578         return function()
579                 _pos = _pos + 1
580                 if _pos <= _len then
581                         return keys[_pos], t[keys[_pos]]
582                 end
583         end
584 end
585
586 --- Return a key, value iterator which returns the values sorted according to
587 -- the provided callback function.
588 -- @param t     The table to iterate
589 -- @param f A callback function to decide the order of elements
590 -- @return      Function value containing the corresponding iterator
591 function spairs(t,f)
592         return _sortiter( t, f )
593 end
594
595 --- Return a key, value iterator for the given table.
596 -- The table pairs are sorted by key.
597 -- @param t     The table to iterate
598 -- @return      Function value containing the corresponding iterator
599 function kspairs(t)
600         return _sortiter( t )
601 end
602
603 --- Return a key, value iterator for the given table.
604 -- The table pairs are sorted by value.
605 -- @param t     The table to iterate
606 -- @return      Function value containing the corresponding iterator
607 function vspairs(t)
608         return _sortiter( t, function (a,b) return t[a] < t[b] end )
609 end
610
611
612 --
613 -- System utility functions
614 --
615
616 --- Test whether the current system is operating in big endian mode.
617 -- @return      Boolean value indicating whether system is big endian
618 function bigendian()
619         return string.byte(string.dump(function() end), 7) == 0
620 end
621
622 --- Execute given commandline and gather stdout.
623 -- @param command       String containing command to execute
624 -- @return                      String containing the command's stdout
625 function exec(command)
626         local pp   = io.popen(command)
627         local data = pp:read("*a")
628         pp:close()
629
630         return data
631 end
632
633 --- Return a line-buffered iterator over the output of given command.
634 -- @param command       String containing the command to execute
635 -- @return                      Iterator
636 function execi(command)
637         local pp = io.popen(command)
638
639         return pp and function()
640                 local line = pp:read()
641                 
642                 if not line then
643                         pp:close()
644                 end
645                 
646                 return line
647         end
648 end
649
650 -- Deprecated
651 function execl(command)
652         local pp   = io.popen(command)
653         local line = ""
654         local data = {}
655
656         while true do
657                 line = pp:read()
658                 if (line == nil) then break end
659                 table.insert(data, line)
660         end
661         pp:close()
662
663         return data
664 end
665
666 --- Returns the absolute path to LuCI base directory.
667 -- @return              String containing the directory path
668 function libpath()
669         return luci.fs.dirname(require("luci.debug").__file__)
670 end
671
672
673 --
674 -- Coroutine safe xpcall and pcall versions modified for Luci
675 -- original version:
676 -- coxpcall 1.13 - Copyright 2005 - Kepler Project (www.keplerproject.org)
677 --
678 -- Copyright © 2005 Kepler Project.
679 -- Permission is hereby granted, free of charge, to any person obtaining a
680 -- copy of this software and associated documentation files (the "Software"),
681 -- to deal in the Software without restriction, including without limitation
682 -- the rights to use, copy, modify, merge, publish, distribute, sublicense,
683 -- and/or sell copies of the Software, and to permit persons to whom the
684 -- Software is furnished to do so, subject to the following conditions:
685 --
686 -- The above copyright notice and this permission notice shall be
687 -- included in all copies or substantial portions of the Software.
688 --
689 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
690 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
691 -- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
692 -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
693 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
694 -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
695 -- OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
696
697 local performResume, handleReturnValue
698 local oldpcall, oldxpcall = pcall, xpcall
699 coxpt = {}
700 setmetatable(coxpt, {__mode = "kv"})
701
702 -- Identity function for copcall
703 local function copcall_id(trace, ...)
704   return ...
705 end
706
707 --- This is a coroutine-safe drop-in replacement for Lua's "xpcall"-function
708 -- @param f             Lua function to be called protected
709 -- @param err   Custom error handler
710 -- @param ...   Parameters passed to the function
711 -- @return              A boolean whether the function call succeeded and the return
712 --                              values of either the function or the error handler
713 function coxpcall(f, err, ...)
714         local res, co = oldpcall(coroutine.create, f)
715         if not res then
716                 local params = {...}
717                 local newf = function() return f(unpack(params)) end
718                 co = coroutine.create(newf)
719         end
720         local c = coroutine.running()
721         coxpt[co] = coxpt[c] or c or 0
722
723         return performResume(err, co, ...)
724 end
725
726 --- This is a coroutine-safe drop-in replacement for Lua's "pcall"-function
727 -- @param f             Lua function to be called protected
728 -- @param ...   Parameters passed to the function
729 -- @return              A boolean whether the function call succeeded and the returns
730 --                              values of the function or the error object
731 function copcall(f, ...)
732         return coxpcall(f, copcall_id, ...)
733 end
734
735 -- Handle return value of protected call
736 function handleReturnValue(err, co, status, ...)
737         if not status then
738                 return false, err(debug.traceback(co, (...)), ...)
739         end
740         if coroutine.status(co) == 'suspended' then
741                 return performResume(err, co, coroutine.yield(...))
742         else
743                 return true, ...
744         end
745 end
746
747 -- Resume execution of protected function call
748 function performResume(err, co, ...)
749         return handleReturnValue(err, co, coroutine.resume(co, ...))
750 end