util.luadoc 11 KB

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