[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: Fixed:
- Deprecate the use of `io.popen` in following widgets: - 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 * bat_freebsd, mem_freebsd, net_freebsd
* volume, gmail, mdir, mpd * volume, gmail, mdir, mpd
- [mpd] Lua 5.3 compatibility (for real this time); also correct a typo - [mpd] Lua 5.3 compatibility (for real this time); also correct a typo

View File

@ -4,57 +4,37 @@
---------------------------------------------------------------- ----------------------------------------------------------------
-- environment -- environment
local io = { popen = io.popen, open = io.open } local type = type
local assert = assert local tonumber = tonumber
local setmetatable = setmetatable local io = { open = io.open }
-- sysfs prefix for hwmon devices local helpers = require"vicious.helpers"
local sys_hwmon = "/sys/class/hwmon/" local spawn = require"vicious.spawn"
-- 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
-- hwmontemp: provides name-indexed temps from /sys/class/hwmon -- hwmontemp: provides name-indexed temps from /sys/class/hwmon
-- vicious.widgets.hwmontemp -- vicious.widgets.hwmontemp
local hwmontemp_linux = {} return helpers.setasyncall{
async = function (format, warg, callback)
function worker(format, warg) if type(warg) ~= "table" or type(warg[1]) ~= "string" then
assert(type(warg) == "table", "invalid hwmontemp argument: must be a table") return callback{}
name = warg[1] end
local input = warg[2]
if not warg[2] then if type(input) == "number" then
input = 1 input = ("temp%d_input"):format(input)
else else
input = warg[2] input = "temp1_input"
end 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")
f:close()
return { temp / 1000 }
end
return setmetatable(hwmontemp_linux, { __call = function(_, ...) return worker(...) end })
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()
else
callback{}
end
end)
end }
-- vim: ts=4:sw=4:expandtab -- vim: ts=4:sw=4:expandtab