[hwmontemp_linux] Deprecate io.popen

Note that all failures now return an empty table instead of { N/A },
since N/A indicates missing information from a successful attempt.
This commit is contained in:
Nguyễn Gia Phong 2019-06-22 03:03:19 +07:00
parent db446c35d9
commit c91167f954
2 changed files with 29 additions and 49 deletions

View File

@ -18,7 +18,7 @@ Added:
Fixed:
- Deprecate the use of `io.popen` in following widgets:
* wifi_linux, wifiiw_linux
* wifi_linux, wifiiw_linux, hwmontemp_linux
* bat_freebsd, mem_freebsd, net_freebsd
* volume, gmail, mdir, mpd
- [mpd] Lua 5.3 compatibility (for real this time); also correct a typo

View File

@ -4,57 +4,37 @@
----------------------------------------------------------------
-- environment
local io = { popen = io.popen, open = io.open }
local assert = assert
local setmetatable = setmetatable
local type = type
local tonumber = tonumber
local io = { open = io.open }
-- sysfs prefix for hwmon devices
local sys_hwmon = "/sys/class/hwmon/"
-- cache table for hwmon device names
local paths = {}
-- transparently caching hwmon device name lookup
function name_to_path(name)
if paths[name] then return paths[name] end
for sensor in io.popen("ls -1 " .. sys_hwmon):lines() do
local path = sys_hwmon .. sensor
local f = assert(io.open(path .. "/name", "r"))
local sname = f:read("*line")
f:close()
if sname == name then
paths[name] = path
return path
end
end
return nil
end
local helpers = require"vicious.helpers"
local spawn = require"vicious.spawn"
-- hwmontemp: provides name-indexed temps from /sys/class/hwmon
-- vicious.widgets.hwmontemp
local hwmontemp_linux = {}
function worker(format, warg)
assert(type(warg) == "table", "invalid hwmontemp argument: must be a table")
name = warg[1]
if not warg[2] then
input = 1
return helpers.setasyncall{
async = function (format, warg, callback)
if type(warg) ~= "table" or type(warg[1]) ~= "string" then
return callback{}
end
local input = warg[2]
if type(input) == "number" then
input = ("temp%d_input"):format(input)
else
input = warg[2]
input = "temp1_input"
end
local sensor = name_to_path(name)
if not sensor then return { "N/A" } end
local f = assert(io.open(("%s/temp%d_input"):format(sensor, input), "r"))
local temp = f:read("*line")
spawn.easy_async_with_shell(
"grep " .. warg[1] .. " -wl /sys/class/hwmon/*/name",
function (stdout, stderr, exitreason, exitcode)
if exitreason == "exit" and exitcode == 0 then
local f = io.open(stdout:gsub("name%s+", input), "r")
callback{ tonumber(f:read"*line") / 1000 }
f:close()
return { temp / 1000 }
else
callback{}
end
return setmetatable(hwmontemp_linux, { __call = function(_, ...) return worker(...) end })
end)
end }
-- vim: ts=4:sw=4:expandtab