util.luadoc 11 KB

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