util.luadoc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. ---[[
  2. LuCI utility functions.
  3. module "luci.util"
  4. ]]
  5. ---[[
  6. Create a Class object (Python-style object model).
  7. The class object can be instantiated by calling itself.
  8. Any class functions or shared parameters can be attached to this object.
  9. Attaching a table to the class object makes this table shared between
  10. all instances of this class. For object parameters use the __init__ function.
  11. Classes can inherit member functions and values from a base class.
  12. Class can be instantiated by calling them. All parameters will be passed
  13. to the __init__ function of this class - if such a function exists.
  14. The __init__ function must be used to set any object parameters that are not shared
  15. with other objects of this class. Any return values will be ignored.
  16. @class function
  17. @name class
  18. @param base The base class to inherit from (optional)
  19. @return A class object
  20. @see instanceof
  21. @see clone
  22. ]]
  23. ---[[
  24. Test whether the given object is an instance of the given class.
  25. @class function
  26. @name instanceof
  27. @param object Object instance
  28. @param class Class object to test against
  29. @return Boolean indicating whether the object is an instance
  30. @see class
  31. @see clone
  32. ]]
  33. ---[[
  34. Create a new or get an already existing thread local store associated with
  35. the current active coroutine. A thread local store is private a table object
  36. whose values can't be accessed from outside of the running coroutine.
  37. @class function
  38. @name threadlocal
  39. @return Table value representing the corresponding thread local store
  40. ]]
  41. ---[[
  42. Write given object to stderr.
  43. @class function
  44. @name perror
  45. @param obj Value to write to stderr
  46. @return Boolean indicating whether the write operation was successful
  47. ]]
  48. ---[[
  49. Recursively dumps a table to stdout, useful for testing and debugging.
  50. @class function
  51. @name dumptable
  52. @param t Table value to dump
  53. @param maxdepth Maximum depth
  54. @return Always nil
  55. ]]
  56. ---[[
  57. Create valid XML PCDATA from given string.
  58. @class function
  59. @name pcdata
  60. @param value String value containing the data to escape
  61. @return String value containing the escaped data
  62. ]]
  63. ---[[
  64. Strip HTML tags from given string.
  65. @class function
  66. @name striptags
  67. @param value String containing the HTML text
  68. @return String with HTML tags stripped of
  69. ]]
  70. ---[[
  71. Splits given string on a defined separator sequence and return a table
  72. containing the resulting substrings. The optional max parameter specifies
  73. the number of bytes to process, regardless of the actual length of the given
  74. string. The optional last parameter, regex, specifies whether the separator
  75. sequence is interpreted as regular expression.
  76. @class function
  77. @name split
  78. @param str String value containing the data to split up
  79. @param pat String with separator pattern (optional, defaults to "\n")
  80. @param max Maximum times to split (optional)
  81. @param regex Boolean indicating whether to interpret the separator
  82. -- pattern as regular expression (optional, default is false)
  83. @return Table containing the resulting substrings
  84. ]]
  85. ---[[
  86. Remove leading and trailing whitespace from given string value.
  87. @class function
  88. @name trim
  89. @param str String value containing whitespace padded data
  90. @return String value with leading and trailing space removed
  91. ]]
  92. ---[[
  93. Count the occurences of given substring in given string.
  94. @class function
  95. @name cmatch
  96. @param str String to search in
  97. @param pattern String containing pattern to find
  98. @return Number of found occurences
  99. ]]
  100. ---[[
  101. Return a matching iterator for the given value. The iterator will return
  102. one token per invocation, the tokens are separated by whitespace. If the
  103. input value is a table, it is transformed into a string first. A nil value
  104. will result in a valid interator which aborts with the first invocation.
  105. @class function
  106. @name imatch
  107. @param val The value to scan (table, string or nil)
  108. @return Iterator which returns one token per call
  109. ]]
  110. ---[[
  111. Parse certain units from the given string and return the canonical integer
  112. value or 0 if the unit is unknown. Upper- or lower case is irrelevant.
  113. Recognized units are:
  114. -- o "y" - one year (60*60*24*366)
  115. o "m" - one month (60*60*24*31)
  116. o "w" - one week (60*60*24*7)
  117. o "d" - one day (60*60*24)
  118. o "h" - one hour (60*60)
  119. o "min" - one minute (60)
  120. o "kb" - one kilobyte (1024)
  121. o "mb" - one megabyte (1024*1024)
  122. o "gb" - one gigabyte (1024*1024*1024)
  123. o "kib" - one si kilobyte (1000)
  124. o "mib" - one si megabyte (1000*1000)
  125. o "gib" - one si gigabyte (1000*1000*1000)
  126. @class function
  127. @name parse_units
  128. @param ustr String containing a numerical value with trailing unit
  129. @return Number containing the canonical value
  130. ]]
  131. ---[[
  132. Appends numerically indexed tables or single objects to a given table.
  133. @class function
  134. @name append
  135. @param src Target table
  136. @param ... Objects to insert
  137. @return Target table
  138. ]]
  139. ---[[
  140. Combines two or more numerically indexed tables and single objects into one table.
  141. @class function
  142. @name combine
  143. @param tbl1 Table value to combine
  144. @param tbl2 Table value to combine
  145. @param ... More tables to combine
  146. @return Table value containing all values of given tables
  147. ]]
  148. ---[[
  149. Checks whether the given table contains the given value.
  150. @class function
  151. @name contains
  152. @param table Table value
  153. @param value Value to search within the given table
  154. @return Boolean indicating whether the given value occurs within table
  155. ]]
  156. ---[[
  157. Update values in given table with the values from the second given table.
  158. Both table are - in fact - merged together.
  159. @class function
  160. @name update
  161. @param t Table which should be updated
  162. @param updates Table containing the values to update
  163. @return Always nil
  164. ]]
  165. ---[[
  166. Retrieve all keys of given associative table.
  167. @class function
  168. @name keys
  169. @param t Table to extract keys from
  170. @return Sorted table containing the keys
  171. ]]
  172. ---[[
  173. Clones the given object and return it's copy.
  174. @class function
  175. @name clone
  176. @param object Table value to clone
  177. @param deep Boolean indicating whether to do recursive cloning
  178. @return Cloned table value
  179. ]]
  180. ---[[
  181. Create a dynamic table which automatically creates subtables.
  182. @class function
  183. @name dtable
  184. @return Dynamic Table
  185. ]]
  186. ---[[
  187. Recursively serialize given data to lua code, suitable for restoring
  188. with loadstring().
  189. @class function
  190. @name serialize_data
  191. @param val Value containing the data to serialize
  192. @return String value containing the serialized code
  193. @see restore_data
  194. @see get_bytecode
  195. ]]
  196. ---[[
  197. Restore data previously serialized with serialize_data().
  198. @class function
  199. @name restore_data
  200. @param str String containing the data to restore
  201. @return Value containing the restored data structure
  202. @see serialize_data
  203. @see get_bytecode
  204. ]]
  205. ---[[
  206. Return the current runtime bytecode of the given data. The byte code
  207. will be stripped before it is returned.
  208. @class function
  209. @name get_bytecode
  210. @param val Value to return as bytecode
  211. @return String value containing the bytecode of the given data
  212. ]]
  213. ---[[
  214. Strips unnescessary lua bytecode from given string. Information like line
  215. numbers and debugging numbers will be discarded. Original version by
  216. Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html)
  217. @class function
  218. @name strip_bytecode
  219. @param code String value containing the original lua byte code
  220. @return String value containing the stripped lua byte code
  221. ]]
  222. ---[[
  223. Return a key, value iterator which returns the values sorted according to
  224. the provided callback function.
  225. @class function
  226. @name spairs
  227. @param t The table to iterate
  228. @param f A callback function to decide the order of elements
  229. @return Function value containing the corresponding iterator
  230. ]]
  231. ---[[
  232. Return a key, value iterator for the given table.
  233. The table pairs are sorted by key.
  234. @class function
  235. @name kspairs
  236. @param t The table to iterate
  237. @return Function value containing the corresponding iterator
  238. ]]
  239. ---[[
  240. Return a key, value iterator for the given table.
  241. The table pairs are sorted by value.
  242. @class function
  243. @name vspairs
  244. @param t The table to iterate
  245. @return Function value containing the corresponding iterator
  246. ]]
  247. ---[[
  248. Test whether the current system is operating in big endian mode.
  249. @class function
  250. @name bigendian
  251. @return Boolean value indicating whether system is big endian
  252. ]]
  253. ---[[
  254. Execute given commandline and gather stdout.
  255. @class function
  256. @name exec
  257. @param command String containing command to execute
  258. @return String containing the command's stdout
  259. ]]
  260. ---[[
  261. Return a line-buffered iterator over the output of given command.
  262. @class function
  263. @name execi
  264. @param command String containing the command to execute
  265. @return Iterator
  266. ]]
  267. ---[[
  268. Issue an ubus call.
  269. @class function
  270. @name ubus
  271. @param object String containing the ubus object to call
  272. @param method String containing the ubus method to call
  273. @param values Table containing the values to pass
  274. @return Table containin the ubus result
  275. ]]
  276. ---[[
  277. Convert data structure to JSON
  278. @class function
  279. @name serialize_json
  280. @param data The data to serialize
  281. @param writer A function to write a chunk of JSON data (optional)
  282. @return String containing the JSON if called without write callback
  283. ]]
  284. ---[[
  285. Returns the absolute path to LuCI base directory.
  286. @class function
  287. @name libpath
  288. @return String containing the directory path
  289. ]]
  290. ---[[
  291. This is a coroutine-safe drop-in replacement for Lua's "xpcall"-function
  292. @class function
  293. @name coxpcall
  294. @param f Lua function to be called protected
  295. @param err Custom error handler
  296. @param ... Parameters passed to the function
  297. @return A boolean whether the function call succeeded and the return
  298. -- values of either the function or the error handler
  299. ]]
  300. ---[[
  301. This is a coroutine-safe drop-in replacement for Lua's "pcall"-function
  302. @class function
  303. @name copcall
  304. @param f Lua function to be called protected
  305. @param ... Parameters passed to the function
  306. @return A boolean whether the function call succeeded and the returns
  307. -- values of the function or the error object
  308. ]]