2
0

util.luadoc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. 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 number indicating the first index at which the given value occurs
  155. -- within table or false.
  156. ]]
  157. ---[[
  158. Update values in given table with the values from the second given table.
  159. Both table are - in fact - merged together.
  160. @class function
  161. @name update
  162. @param t Table which should be updated
  163. @param updates Table containing the values to update
  164. @return Always nil
  165. ]]
  166. ---[[
  167. Retrieve all keys of given associative table.
  168. @class function
  169. @name keys
  170. @param t Table to extract keys from
  171. @return Sorted table containing the keys
  172. ]]
  173. ---[[
  174. Clones the given object and return it's copy.
  175. @class function
  176. @name clone
  177. @param object Table value to clone
  178. @param deep Boolean indicating whether to do recursive cloning
  179. @return Cloned table value
  180. ]]
  181. ---[[
  182. Create a dynamic table which automatically creates subtables.
  183. @class function
  184. @name dtable
  185. @return Dynamic Table
  186. ]]
  187. ---[[
  188. Recursively serialize given data to lua code, suitable for restoring
  189. with loadstring().
  190. @class function
  191. @name serialize_data
  192. @param val Value containing the data to serialize
  193. @return String value containing the serialized code
  194. @see restore_data
  195. @see get_bytecode
  196. ]]
  197. ---[[
  198. Restore data previously serialized with serialize_data().
  199. @class function
  200. @name restore_data
  201. @param str String containing the data to restore
  202. @return Value containing the restored data structure
  203. @see serialize_data
  204. @see get_bytecode
  205. ]]
  206. ---[[
  207. Return the current runtime bytecode of the given data. The byte code
  208. will be stripped before it is returned.
  209. @class function
  210. @name get_bytecode
  211. @param val Value to return as bytecode
  212. @return String value containing the bytecode of the given data
  213. ]]
  214. ---[[
  215. Strips unnescessary lua bytecode from given string. Information like line
  216. numbers and debugging numbers will be discarded. Original version by
  217. Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html)
  218. @class function
  219. @name strip_bytecode
  220. @param code String value containing the original lua byte code
  221. @return String value containing the stripped lua byte code
  222. ]]
  223. ---[[
  224. Return a key, value iterator which returns the values sorted according to
  225. the provided callback function.
  226. @class function
  227. @name spairs
  228. @param t The table to iterate
  229. @param f A callback function to decide the order of elements
  230. @return Function value containing the corresponding iterator
  231. ]]
  232. ---[[
  233. Return a key, value iterator for the given table.
  234. The table pairs are sorted by key.
  235. @class function
  236. @name kspairs
  237. @param t The table to iterate
  238. @return Function value containing the corresponding iterator
  239. ]]
  240. ---[[
  241. Return a key, value iterator for the given table.
  242. The table pairs are sorted by value.
  243. @class function
  244. @name vspairs
  245. @param t The table to iterate
  246. @return Function value containing the corresponding iterator
  247. ]]
  248. ---[[
  249. Test whether the current system is operating in big endian mode.
  250. @class function
  251. @name bigendian
  252. @return Boolean value indicating whether system is big endian
  253. ]]
  254. ---[[
  255. Execute given commandline and gather stdout.
  256. @class function
  257. @name exec
  258. @param command String containing command to execute
  259. @return String containing the command's stdout
  260. ]]
  261. ---[[
  262. Return a line-buffered iterator over the output of given command.
  263. @class function
  264. @name execi
  265. @param command String containing the command to execute
  266. @return Iterator
  267. ]]
  268. ---[[
  269. Issue an ubus call.
  270. @class function
  271. @name ubus
  272. @param object String containing the ubus object to call
  273. @param method String containing the ubus method to call
  274. @param values Table containing the values to pass
  275. @return Table containin the ubus result
  276. ]]
  277. ---[[
  278. Convert data structure to JSON
  279. @class function
  280. @name serialize_json
  281. @param data The data to serialize
  282. @param writer A function to write a chunk of JSON data (optional)
  283. @return String containing the JSON if called without write callback
  284. ]]
  285. ---[[
  286. Returns the absolute path to LuCI base directory.
  287. @class function
  288. @name libpath
  289. @return String containing the directory path
  290. ]]
  291. ---[[
  292. This is a coroutine-safe drop-in replacement for Lua's "xpcall"-function
  293. @class function
  294. @name coxpcall
  295. @param f Lua function to be called protected
  296. @param err Custom error handler
  297. @param ... Parameters passed to the function
  298. @return A boolean whether the function call succeeded and the return
  299. -- values of either the function or the error handler
  300. ]]
  301. ---[[
  302. This is a coroutine-safe drop-in replacement for Lua's "pcall"-function
  303. @class function
  304. @name copcall
  305. @param f Lua function to be called protected
  306. @param ... Parameters passed to the function
  307. @return A boolean whether the function call succeeded and the returns
  308. -- values of the function or the error object
  309. ]]