Debian Codegraph @

bashets/bashets.lua

raw symbols SCIP
1 ------------------------------------------------------------------------
2 -- Bashets - use your shellscript's output in Awesome3 widgets
3 --
4 -- @author Anton Lobov <[email protected]>
5 -- @copyright 2010-2012 Anton Lobov
6 --
7 -- @contributor Victor Unegbu (fixed issue with formatting)
8 --
9 -- @license GPLv3
10 -- @release 0.6.3
11 ------------------------------------------------------------------------
12
13 -- Grab only needed enviroment
14 local awful = require("awful")
15 local string = string
16 local io = io
17 local table = table
18 local pairs = pairs
19 local timer = timer
20 local type = type
21 local image = image
22 local capi = {oocairo = oocairo, timer = timer, dbus = dbus}
23 local tonumber = tonumber
24 local print = print
25 local error = error
26
27 --- Bashets module
28 local bashets = {}
29
30 -- Default paths
31 local script_path = "/usr/share/awesome/bashets/"
32 local tmp_folder = "/tmp/"
33
34 -- Utility functions table
35 local util = {}
36
37 -- Timer data
38 local timerdata = {}
39 local timers = {}
40
41 -- External mode data
42 local ewidgets = {}
43
44 -- Some default values
45 local defaults = {}
46 defaults.update_time = 1
47 defaults.file_update_time = 2
48 defaults.format_string = "$1"
49 defaults.separator = " "
50 defaults.updater = "runner.sh"
51
52 -- State variable
53 local is_running = false
54
55 -- # Utility functions
56
57 --- Split string by separator into table
58 -- @param str String to split
59 -- @param sep Separator to use
60 function util.split(str, sep)
61 if sep == nil then
62 return {str}
63 end
64
65 local parts = {} --parts array
66 local first = 1
67 local ostart, oend = string.find(str, sep, first, true) --regexp disabled search
68
69 while ostart do
70 local part = string.sub(str, first, ostart - 1)
71 table.insert(parts, part)
72 first = oend + 1
73 ostart, oend = string.find(str, sep, first, true)
74 end
75
76 local part = string.sub(str, first)
77 table.insert(parts, part)
78
79 return parts
80 end
81
82 function util.tmpkey(script)
83 -- Replace all slashes with empty string so that /home/user1/script.sh
84 -- and /home/user2/script.sh will have different temporary files
85 local tmpname = string.gsub(script, '/', '')
86
87 -- Replace all spaces with dots so that "script.sh arg1"
88 -- and "script.sh arg2" will have different temporary files
89 tmpname = string.gsub(tmpname, '%s+', '.')
90
91 return tmpname
92 end
93
94 function util.tmpname(script)
95 -- Generated script-parameter unique temporary file path
96 local file = tmp_folder .. util.tmpkey(script) .. '.bashets.out'
97
98 return file
99 end
100
101 function util.fullpath(script)
102 if string.find(script, '^/') == nil then
103 script = script_path .. script
104 end
105
106 return script
107 end
108
109 --- Execute a command and write it's output to temporary file
110 -- @param script Script to execute
111 -- @param file File for script output
112 function util.execfile(script, file)
113 -- Spawn command and redirect it's output to file
114 awful.util.spawn_with_shell(script .. " > " .. file)
115 end
116
117
118 function util.readstring(str, sep)
119 if sep == nil then
120 return str
121 else
122 parts = util.split(str, sep);
123 return parts
124 end
125 end
126
127 --- Read temporary file to a table or string
128 -- @param file File to be read
129 -- @param israw If true, return raw string, not table
130 function util.readfile(file, sep)
131 local fh = io.input(file)
132 local str = fh:read("*all");
133 io.close(fh)
134
135 return util.readstring(str, sep)
136 end
137
138 --- Read script output to a table or string
139 -- @param script Script to execute
140 -- @param israw If true, return raw string, not table
141 function util.readshell(script, sep)
142 local str = awful.util.pread(script)
143
144 return util.readstring(str, sep)
145 end
146
147 --- Format script output with user defined format string
148 -- @param output sep-separated string of values
149 -- @param format Format string
150 -- @param sep Separator of values in string
151 function util.format(parts, format, sep)
152 -- For each part with number "k" replace corresponding "$k" variable in format string
153 for k,part in pairs(parts) do
154 local part = string.gsub(part, "%%", "%1%1") --percent fix for next gsub (bug found in Wicked)
155 part = awful.util.escape(part) --escape XML entities for correct Pango markup
156 format = string.gsub(format, "$" .. k, part)
157 end
158
159 return format
160 end
161
162 --- Add function to corresponding timer object (Awesome >= 3.4 timer API)
163 -- @param updtime Update time for widget, also dispatch time for timer
164 -- @param func Function to dispatch
165 function util.add_to_timings(updtime, func, force_new)
166 local found = false
167
168 -- Search for an existing timer at the same period
169 for k,tmr in pairs(timerdata) do
170 if tmr[1] == updtime and (force_new == nil or force_new == false) then
171 table.insert(timerdata[k][2], func)
172 found = true
173 end
174 end
175
176 -- Add a new timer for period if not found
177 if not found then
178 table.insert(timerdata, {updtime, {func}})
179 end
180 end
181
182 --- Create timer table to define timers for multiple widget updates
183 function util.create_timers_table()
184 -- Parse table with timer data
185 for _,tmr in pairs(timerdata) do
186 -- Create timer for the period
187 local t
188 if capi.timer ~= nil then
189 t = capi.timer {timeout = tmr[1]}
190 else
191 t = timer {timeout = tmr[1]}
192 end
193 -- Function to call all dispatched functions
194 local f = function()
195 for _, func in pairs(tmr[2]) do
196 func()
197 end
198 end
199
200 if t.add_signal ~= nil then
201 t:add_signal("timeout", f)
202 else
203 t:connect_signal("timeout", f)
204 end
205 table.insert(timers, t)
206 end
207 end
208
209 function util.update_widget_field(widget, valuess)
210 -- print(widget.type)
211 if widget.type == "imagebox" then --imagebox (old API)
212 widget["image"] = image(valuess)
213 elseif widget.set_image ~= nil then --imagebox (new API)
214 --widget["image"] = capi.oocairo.image_surface_create_from_png(valuess)
215 --widget:set_image(capi.oocairo.image_surface_create_from_png(valuess))
216 widget:set_image(valuess)
217 elseif widget.type == "textbox" then --textbox (old API)
218 widget["text"] = valuess
219 elseif widget.set_markup ~= nil then --textbox (new API)
220 widget:set_markup(valuess)
221 --elseif widget["widget"] ~= nil then
222 elseif widget.set_value ~= nil then --progressbar
223 widget:set_value(tonumber(valuess))
224 elseif widget.add_value ~= nil then --graph
225 widget:add_value(tonumber(valuess))
226 end
227 --end
228 end
229
230 --- Update widget from values
231 function util.update_widget(widget, values, format)
232 if widget ~= nil then
233 if type(values) == "table" then
234 util.update_widget_field(widget, util.format(values, format))
235 else
236 util.update_widget_field(widget, values)
237 end
238 end
239 end
240
241 -- # Setter functions
242
243 --- Set path for scripts
244 -- @param path Path to set
245 function bashets.set_script_path(path)
246 script_path = path
247 end
248
249 --- Set path for temporary files
250 -- @param path Path to set
251 function bashets.set_temporary_path(path)
252 tmp_folder = path
253 end
254
255 --- Set default values
256 -- @param defs Table with defaults
257 function bashets.set_defaults(defs)
258 if type(defs) == "table" then
259 if defs.update_time ~= nil then
260 defaults.update_time = defs.update_time
261 end
262 if defs.file_update_time ~= nil then
263 defaults.file_update_time = defs.file_update_time
264 end
265 if defs.format_string ~= nil then
266 defaults.format_string = defs.format_string
267 end
268 if defs.updater ~= nil then
269 defaults.updater = defs.updater
270 end
271
272 defaults.separator = defs.separator --now could be nil
273 end
274 end
275
276
277 -- # Acting functions
278
279 --- Start widget updates
280 function bashets.start()
281 -- Create timers table if not initialized or empty
282 if (not timers) or table.maxn(timers) == 0 then
283 util.create_timers_table()
284 end
285 -- Start all timers
286 for _, tmr in pairs(timers) do
287 tmr:start()
288 end
289
290 -- Kill all externals (if some were here from previous launch)
291 --awful.util.spawn_with_shell('killall ' .. defaults.updater)
292
293 -- Run all externals
294 for _, wgt in pairs(ewidgets) do
295 awful.util.spawn_with_shell(wgt.cmd)
296 end
297 is_running = true
298 end
299
300 --- Stop widget updates
301 function bashets.stop()
302 -- Stop all timers
303 for _, tmr in pairs(timers) do
304 tmr:stop()
305 end
306
307 -- Kill all externals
308 awful.util.spawn_with_shell('killall ' .. defaults.updater)
309
310 is_running = false
311 end
312
313 --- Check whether updates are running
314 function bashets.get_running()
315 return is_running
316 end
317
318 --- Toggle updates
319 function bashets.toggle()
320 if is_running then
321 bashets.start()
322 else
323 bashets.stop()
324 end
325 end
326
327 --- Shedule function for timed execution
328 -- @param func Function to run
329 -- @param updatime Update time (optional)
330 function bashets.schedule(func, updtime)
331 updtime = updtime or defaults.update_time
332 if func ~= nil then
333 util.add_to_timings(updtime, func)
334 end
335 end
336
337 -- # Widget registration functions
338
339 --- General widget-callback registration function. For internal use, but if you need it, feel free to call it :)
340 -- @param data_provider Function that returns table of values
341 -- @param widget Widget to update, if null, nothing is updated
342 -- @param callback Callback to call after the update of values, if null, nothing is called
343 -- @param format Format string for widget //N.B.: it is not checked for null
344 function bashets.schedule_w(data_provider, widget, callback, format, updtime)
345 if callback ~= nil and type(callback) == "function" then
346 if widget ~= nil then
347 bashets.schedule(function()
348 local data = data_provider()
349 util.update_widget(widget, data, format)
350 callback(data)
351 end, updtime)
352 else
353 bashets.schedule(function()
354 local data = data_provider()
355 callback(data)
356 end, updtime)
357 end
358 elseif widget ~= nil then
359 bashets.schedule(function()
360 local data = data_provider()
361 util.update_widget(widget, data, format)
362 end, updtime)
363 end
364 end
365
366 --- External script registration. Script will pass data by calling external_w through dbus
367 function bashets.schedule_e(script, widget, callback, format, updtime, sep)
368 local ascript = util.fullpath(script)
369 local key = util.tmpkey(ascript)
370
371 ewidgets[key] = {}
372 ewidgets[key].widget = widget
373 ewidgets[key].callback = callback
374 ewidgets[key].format = format
375 ewidgets[key].separator = sep
376 ewidgets[key].cmd = util.fullpath(defaults.updater) .. " \"" .. ascript .. "\" " .. key .. " " ..updtime
377 --print(ewidgets[key].cmd)
378
379 -- awful.util.spawn_with_shell()
380 end
381
382 function bashets.external_w(raw_data, key)
383
384 local callback = ewidgets[key].callback or nil
385 local widget = ewidgets[key].widget or nil
386 local format = ewidgets[key].format or defaults.format_string
387 local data = util.readstring(raw_data, ewidgets[key].separator)
388
389 if callback ~= nil and type(callback) == "function" then
390 if widget ~= nil then
391 util.update_widget(widget, data, format)
392 callback(data)
393 else
394 callback(data)
395 end
396 elseif widget ~= nil then
397 util.update_widget(widget, data, format)
398 end
399 end
400
401 function bashets.register_d(bus, iface, widget, callback, format)
402 if capi.dbus then
403 bus = bus or "session"
404 capi.dbus.add_match(bus, "interface='" .. iface .. "'")
405
406 capi.dbus.connect_signal(iface, function (...)
407 local argums = {...}
408 local data = {}
409 for _,v in pairs(argums) do
410 if type(v) == "table" then
411 for _,iv in pairs(v) do
412 table.insert(data, iv)
413 end
414 else
415 table.insert(data, v)
416 end
417 end
418
419 if callback ~= nil and type(callback) == "function" then
420 if widget ~= nil then
421 util.update_widget(widget, data, format)
422 callback(data)
423 else
424 callback(data)
425 end
426 elseif widget ~= nil then
427 util.update_widget(widget, data, format)
428 end
429 end)
430 else
431 error("bashets: You are trying to employ dbus, but no dbus api detected in your build of awesome")
432 end
433 end
434
435
436
437 --- Register script for text widget
438 -- @param object Object to be a data provider (function or string with a path to file/shellscript)
439 -- @param options Options table with following content (all fields are optional):
440 --
441 -- widget Widget to update
442 -- callback Function to execute after the update (should have a single parameter which represents table with data)
443 -- NOTE: you can use the widget without a callback, the callback without a widget, or both
444 --
445 -- update_time Widget update time (in seconds)
446 -- separator Output separator (string, could be null)
447 -- format User-defined format string (any valid Pango markup with variables)
448 --
449 -- async Perform script execution through a temporary file (true/false)
450 -- file_update_time Temporary file content update time (in seconds)
451 --
452 -- read_file Read file instead of executing it
453 function bashets.register(object, options)
454 -- Set optional variables
455 options = options or {}
456 local updtime = options.update_time or defaults.update_time
457 local fltime = options.file_update_time or defaults.file_update_time
458 local format = options.format or defaults.format_string
459 local sep = options.separator or defaults.separator
460 local callback = options.callback
461 local async = options.async or false
462 local isdbus = options.dbus or false
463 local busname = options.busname or "session"
464 local readfile = options.read_file or false
465 local widget = options.widget
466 local external = options.external or false
467
468 if type(object) == "function" then --We have a function as a data provider
469 -- Do it first time
470 local data = func()
471 util.update_widget(widget, data, format)
472
473 -- Schedule it for timed execution
474 bashets.schedule_w(func, widget, callback, format, updtime)
475
476 elseif readfile then --We have a text file as a data provider
477 -- Do it first time
478 local data = util.readfile(object, sep)
479 util.update_widget(widget, data, format)
480
481 -- Schedule it for timed execution
482 bashets.schedule_w(function() return util.readfile(object, sep) end, widget, callback, format, updtime)
483
484 elseif async then --Script could hang Awesome, we need to run it
485 local script = util.fullpath(object) --through a temporary file
486 local tmpfile = util.tmpname(script)
487
488 -- Create temporary file if not exists
489 fl = io.open(tmpfile, "w")
490 io.close(fl)
491
492 -- Do it first time
493 util.execfile(script, tmpfile)
494 local data = util.readfile(tmpfile, sep)
495 util.update_widget(widget, data, format)
496
497 -- Schedule it for timed execution
498 bashets.schedule(function() util.execfile(script, tmpfile) end, fltime)
499 bashets.schedule_w(function() return util.readfile(tmpfile, sep) end, widget, callback, format, updtime)
500
501 elseif external then -- Script provides external data through dbus
502 local script = util.fullpath(object)
503 bashets.schedule_e(script, widget, callback, format, updtime, sep)
504 elseif isdbus then -- Data is fetched through a message bus
505 bashets.register_d(busname, object, widget, callback, format)
506 --print("registered with bus name \"" .. busname .. "\" and object \"" .. object .. "\"")
507 else -- Fast script that can be read through pread
508 local script = util.fullpath(object)
509
510 -- Do it first time
511 local data = util.readshell(script, sep)
512 util.update_widget(widget, data, format)
513
514 -- Schedule it for timed execution
515 bashets.schedule_w(function() return util.readshell(script, sep) end, widget, callback, format, updtime)
516 end
517
518 end
519
520 return bashets