mirror of
https://github.com/don-philipe/knorke.git
synced 2025-11-08 14:57:02 +01:00
changed widget lib handling, pyload widget now ready to use
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -39,3 +39,5 @@ luac.out
|
|||||||
*.x86_64
|
*.x86_64
|
||||||
*.hex
|
*.hex
|
||||||
|
|
||||||
|
# vim stuff
|
||||||
|
*.swp
|
||||||
|
|||||||
23
helpers.lua
23
helpers.lua
@@ -1,25 +1,10 @@
|
|||||||
-- some things copied from vicious' helper
|
-- helper functions
|
||||||
|
|
||||||
local rawget = rawget
|
|
||||||
local helpers = {}
|
local helpers = {}
|
||||||
|
|
||||||
-- {{{ Loader for knorke modules
|
-- substitutes parts from format_string of the form ${var} with the value from sub_table with the key "var"
|
||||||
function helpers.wrequire(table, key)
|
function helpers.sub_format_string(format_string, sub_table)
|
||||||
local module = rawget(table, key)
|
return string.gsub(format_string, "%${(%w+)}", sub_table)
|
||||||
return module or require(table._NAME .. "." .. key)
|
|
||||||
end
|
end
|
||||||
-- }}}
|
|
||||||
|
|
||||||
-- {{ Format a string with args
|
|
||||||
function helpers.format(format, args)
|
|
||||||
for var, val in pairs(args) do
|
|
||||||
format = format:gsub("$" .. (tonumber(var) and var or
|
|
||||||
var:gsub("[-+?*]", function(i) return "%"..i end)),
|
|
||||||
val)
|
|
||||||
end
|
|
||||||
|
|
||||||
return format
|
|
||||||
end
|
|
||||||
-- }}}
|
|
||||||
|
|
||||||
return helpers
|
return helpers
|
||||||
|
|||||||
261
init.lua
261
init.lua
@@ -1,259 +1,6 @@
|
|||||||
-- copied from vicious' init.lua
|
|
||||||
-- {{{ Setup environment
|
|
||||||
local type = type
|
|
||||||
local pairs = pairs
|
|
||||||
local tonumber = tonumber
|
|
||||||
local timer = (type(timer) == 'table' and timer or require("gears.timer"))
|
|
||||||
local os = { time = os.time }
|
|
||||||
local table = {
|
|
||||||
insert = table.insert,
|
|
||||||
remove = table.remove
|
|
||||||
}
|
|
||||||
|
|
||||||
local helpers = require("knorke.helpers")
|
|
||||||
|
|
||||||
-- knorke: widgets for the awesome window manager
|
|
||||||
local knorke = {}
|
|
||||||
knorke.widgets = require("knorke.widgets")
|
|
||||||
--knorke.contrib = require("knorke.contrib")
|
|
||||||
|
|
||||||
-- Initialize tables
|
|
||||||
local timers = {}
|
|
||||||
local registered = {}
|
|
||||||
local widget_cache = {}
|
|
||||||
-- }}}
|
|
||||||
|
|
||||||
|
|
||||||
-- {{{ Local functions
|
|
||||||
-- {{{ Update a widget
|
|
||||||
local function update(widget, reg, disablecache)
|
|
||||||
-- Check if there are any equal widgets
|
|
||||||
if reg == nil then
|
|
||||||
for w, i in pairs(registered) do
|
|
||||||
if w == widget then
|
|
||||||
for _, r in pairs(i) do
|
|
||||||
update(w, r, disablecache)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return
|
return
|
||||||
end
|
{
|
||||||
|
helpers = require("knorke.helpers");
|
||||||
local t = os.time()
|
pyload = require("knorke.pyload");
|
||||||
local data = {}
|
testwidget = require("knorke.testwidget");
|
||||||
|
|
||||||
-- Check for chached output newer than the last update
|
|
||||||
if widget_cache[reg.wtype] ~= nil then
|
|
||||||
local c = widget_cache[reg.wtype]
|
|
||||||
|
|
||||||
if (c.time == nil or c.time <= t-reg.timer) or disablecache then
|
|
||||||
c.time, c.data = t, reg.wtype(reg.format, reg.warg)
|
|
||||||
end
|
|
||||||
|
|
||||||
data = c.data
|
|
||||||
else
|
|
||||||
data = reg.wtype and reg.wtype(reg.format, reg.warg)
|
|
||||||
end
|
|
||||||
|
|
||||||
if type(data) == "table" then
|
|
||||||
if type(reg.format) == "string" then
|
|
||||||
data = helpers.format(reg.format, data)
|
|
||||||
elseif type(reg.format) == "function" then
|
|
||||||
data = reg.format(widget, data)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if widget.add_value ~= nil then
|
|
||||||
widget:add_value(tonumber(data) and tonumber(data)/100)
|
|
||||||
elseif widget.set_value ~= nil then
|
|
||||||
widget:set_value(tonumber(data) and tonumber(data)/100)
|
|
||||||
elseif widget.set_markup ~= nil then
|
|
||||||
widget:set_markup(data)
|
|
||||||
else
|
|
||||||
widget.text = data
|
|
||||||
end
|
|
||||||
|
|
||||||
return data
|
|
||||||
end
|
|
||||||
-- }}}
|
|
||||||
|
|
||||||
-- {{{ Register from reg object
|
|
||||||
local function regregister(reg)
|
|
||||||
if not reg.running then
|
|
||||||
if registered[reg.widget] == nil then
|
|
||||||
registered[reg.widget] = {}
|
|
||||||
table.insert(registered[reg.widget], reg)
|
|
||||||
else
|
|
||||||
local already = false
|
|
||||||
|
|
||||||
for w, i in pairs(registered) do
|
|
||||||
if w == reg.widget then
|
|
||||||
for _, v in pairs(i) do
|
|
||||||
if v == reg then
|
|
||||||
already = true
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if already then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if not already then
|
|
||||||
table.insert(registered[reg.widget], reg)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Start the timer
|
|
||||||
if reg.timer > 0 then
|
|
||||||
local tm = timers[reg.timer] and timers[reg.timer].timer
|
|
||||||
tm = tm or timer({ timeout = reg.timer })
|
|
||||||
if tm.connect_signal then
|
|
||||||
tm:connect_signal("timeout", reg.update)
|
|
||||||
else
|
|
||||||
tm:add_signal("timeout", reg.update)
|
|
||||||
end
|
|
||||||
if not timers[reg.timer] then
|
|
||||||
timers[reg.timer] = { timer = tm, refs = 1 }
|
|
||||||
else
|
|
||||||
timers[reg.timer].refs = timers[reg.timer].refs + 1
|
|
||||||
end
|
|
||||||
if not tm.started then
|
|
||||||
tm:start()
|
|
||||||
end
|
|
||||||
-- Initial update
|
|
||||||
reg.update()
|
|
||||||
end
|
|
||||||
reg.running = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- }}}
|
|
||||||
-- }}}
|
|
||||||
|
|
||||||
|
|
||||||
-- {{{ Global functions
|
|
||||||
-- {{{ Register a widget
|
|
||||||
function knorke.register(widget, wtype, format, timer, warg)
|
|
||||||
local widget = widget
|
|
||||||
local reg = {
|
|
||||||
-- Set properties
|
|
||||||
wtype = wtype,
|
|
||||||
format = format,
|
|
||||||
timer = timer,
|
|
||||||
warg = warg,
|
|
||||||
widget = widget,
|
|
||||||
}
|
}
|
||||||
-- Set functions
|
|
||||||
reg.update = function ()
|
|
||||||
update(widget, reg)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Default to 2s timer
|
|
||||||
if reg.timer == nil then
|
|
||||||
reg.timer = 2
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Register a reg object
|
|
||||||
regregister(reg)
|
|
||||||
|
|
||||||
-- Return a reg object for reuse
|
|
||||||
return reg
|
|
||||||
end
|
|
||||||
-- }}}
|
|
||||||
|
|
||||||
-- {{{ Unregister a widget
|
|
||||||
function knorke.unregister(widget, keep, reg)
|
|
||||||
if reg == nil then
|
|
||||||
for w, i in pairs(registered) do
|
|
||||||
if w == widget then
|
|
||||||
for _, v in pairs(i) do
|
|
||||||
reg = knorke.unregister(w, keep, v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return reg
|
|
||||||
end
|
|
||||||
|
|
||||||
if not keep then
|
|
||||||
for w, i in pairs(registered) do
|
|
||||||
if w == widget then
|
|
||||||
for k, v in pairs(i) do
|
|
||||||
if v == reg then
|
|
||||||
table.remove(registered[w], k)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if not reg.running then
|
|
||||||
return reg
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Disconnect from timer
|
|
||||||
local tm = timers[reg.timer]
|
|
||||||
if tm.timer.disconnect_signal then
|
|
||||||
tm.timer:disconnect_signal("timeout", reg.update)
|
|
||||||
else
|
|
||||||
tm.timer:remove_signal("timeout", reg.update)
|
|
||||||
end
|
|
||||||
reg.running = false
|
|
||||||
-- Stop the timer
|
|
||||||
tm.refs = tm.refs - 1
|
|
||||||
if tm.refs == 0 and tm.timer.started then
|
|
||||||
tm.timer:stop()
|
|
||||||
end
|
|
||||||
|
|
||||||
return reg
|
|
||||||
end
|
|
||||||
-- }}}
|
|
||||||
|
|
||||||
-- {{{ Enable caching of a widget type
|
|
||||||
function knorke.cache(wtype)
|
|
||||||
if wtype ~= nil then
|
|
||||||
if widget_cache[wtype] == nil then
|
|
||||||
widget_cache[wtype] = {}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- }}}
|
|
||||||
|
|
||||||
-- {{{ Force update of widgets
|
|
||||||
function knorke.force(wtable)
|
|
||||||
if type(wtable) == "table" then
|
|
||||||
for _, w in pairs(wtable) do
|
|
||||||
update(w, nil, true)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- }}}
|
|
||||||
|
|
||||||
-- {{{ Suspend all widgets
|
|
||||||
function knorke.suspend()
|
|
||||||
for w, i in pairs(registered) do
|
|
||||||
for _, v in pairs(i) do
|
|
||||||
knorke.unregister(w, true, v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- }}}
|
|
||||||
|
|
||||||
-- {{{ Activate a widget
|
|
||||||
function knorke.activate(widget)
|
|
||||||
for w, i in pairs(registered) do
|
|
||||||
if widget == nil or w == widget then
|
|
||||||
for _, v in pairs(i) do
|
|
||||||
regregister(v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- }}}
|
|
||||||
|
|
||||||
return knorke
|
|
||||||
|
|
||||||
-- }}}
|
|
||||||
|
|||||||
417
json.lua
Normal file
417
json.lua
Normal file
@@ -0,0 +1,417 @@
|
|||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- JSON4Lua: JSON encoding / decoding support for the Lua language.
|
||||||
|
-- json Module.
|
||||||
|
-- Author: Craig Mason-Jones
|
||||||
|
-- Homepage: http://github.com/craigmj/json4lua/
|
||||||
|
-- Version: 1.0.0
|
||||||
|
-- This module is released under the MIT License (MIT).
|
||||||
|
-- Please see LICENCE.txt for details.
|
||||||
|
--
|
||||||
|
-- USAGE:
|
||||||
|
-- This module exposes two functions:
|
||||||
|
-- json.encode(o)
|
||||||
|
-- Returns the table / string / boolean / number / nil / json.null value as a JSON-encoded string.
|
||||||
|
-- json.decode(json_string)
|
||||||
|
-- Returns a Lua object populated with the data encoded in the JSON string json_string.
|
||||||
|
--
|
||||||
|
-- REQUIREMENTS:
|
||||||
|
-- compat-5.1 if using Lua 5.0
|
||||||
|
--
|
||||||
|
-- CHANGELOG
|
||||||
|
-- 0.9.20 Introduction of local Lua functions for private functions (removed _ function prefix).
|
||||||
|
-- Fixed Lua 5.1 compatibility issues.
|
||||||
|
-- Introduced json.null to have null values in associative arrays.
|
||||||
|
-- json.encode() performance improvement (more than 50%) through table.concat rather than ..
|
||||||
|
-- Introduced decode ability to ignore /**/ comments in the JSON string.
|
||||||
|
-- 0.9.10 Fix to array encoding / decoding to correctly manage nil/null values in arrays.
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- Imports and dependencies
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
local math = require('math')
|
||||||
|
local string = require("string")
|
||||||
|
local table = require("table")
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- Module declaration
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
local json = {} -- Public namespace
|
||||||
|
local json_private = {} -- Private namespace
|
||||||
|
|
||||||
|
-- Public functions
|
||||||
|
|
||||||
|
-- Private functions
|
||||||
|
local decode_scanArray
|
||||||
|
local decode_scanComment
|
||||||
|
local decode_scanConstant
|
||||||
|
local decode_scanNumber
|
||||||
|
local decode_scanObject
|
||||||
|
local decode_scanString
|
||||||
|
local decode_scanWhitespace
|
||||||
|
local encodeString
|
||||||
|
local isArray
|
||||||
|
local isEncodable
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- PUBLIC FUNCTIONS
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
--- Encodes an arbitrary Lua object / variable.
|
||||||
|
-- @param v The Lua object / variable to be JSON encoded.
|
||||||
|
-- @return String containing the JSON encoding in internal Lua string format (i.e. not unicode)
|
||||||
|
function json.encode (v)
|
||||||
|
-- Handle nil values
|
||||||
|
if v==nil then
|
||||||
|
return "null"
|
||||||
|
end
|
||||||
|
|
||||||
|
local vtype = type(v)
|
||||||
|
|
||||||
|
-- Handle strings
|
||||||
|
if vtype=='string' then
|
||||||
|
return '"' .. json_private.encodeString(v) .. '"' -- Need to handle encoding in string
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Handle booleans
|
||||||
|
if vtype=='number' or vtype=='boolean' then
|
||||||
|
return tostring(v)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Handle tables
|
||||||
|
if vtype=='table' then
|
||||||
|
local rval = {}
|
||||||
|
-- Consider arrays separately
|
||||||
|
local bArray, maxCount = isArray(v)
|
||||||
|
if bArray then
|
||||||
|
for i = 1,maxCount do
|
||||||
|
table.insert(rval, json.encode(v[i]))
|
||||||
|
end
|
||||||
|
else -- An object, not an array
|
||||||
|
for i,j in pairs(v) do
|
||||||
|
if isEncodable(i) and isEncodable(j) then
|
||||||
|
table.insert(rval, '"' .. json_private.encodeString(i) .. '":' .. json.encode(j))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if bArray then
|
||||||
|
return '[' .. table.concat(rval,',') ..']'
|
||||||
|
else
|
||||||
|
return '{' .. table.concat(rval,',') .. '}'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Handle null values
|
||||||
|
if vtype=='function' and v==null then
|
||||||
|
return 'null'
|
||||||
|
end
|
||||||
|
|
||||||
|
assert(false,'encode attempt to encode unsupported type ' .. vtype .. ':' .. tostring(v))
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--- Decodes a JSON string and returns the decoded value as a Lua data structure / value.
|
||||||
|
-- @param s The string to scan.
|
||||||
|
-- @param [startPos] Optional starting position where the JSON string is located. Defaults to 1.
|
||||||
|
-- @param Lua object, number The object that was scanned, as a Lua table / string / number / boolean or nil,
|
||||||
|
-- and the position of the first character after
|
||||||
|
-- the scanned JSON object.
|
||||||
|
function json.decode(s, startPos)
|
||||||
|
startPos = startPos and startPos or 1
|
||||||
|
startPos = decode_scanWhitespace(s,startPos)
|
||||||
|
assert(startPos<=string.len(s), 'Unterminated JSON encoded object found at position in [' .. s .. ']')
|
||||||
|
local curChar = string.sub(s,startPos,startPos)
|
||||||
|
-- Object
|
||||||
|
if curChar=='{' then
|
||||||
|
return decode_scanObject(s,startPos)
|
||||||
|
end
|
||||||
|
-- Array
|
||||||
|
if curChar=='[' then
|
||||||
|
return decode_scanArray(s,startPos)
|
||||||
|
end
|
||||||
|
-- Number
|
||||||
|
if string.find("+-0123456789.e", curChar, 1, true) then
|
||||||
|
return decode_scanNumber(s,startPos)
|
||||||
|
end
|
||||||
|
-- String
|
||||||
|
if curChar==[["]] or curChar==[[']] then
|
||||||
|
return decode_scanString(s,startPos)
|
||||||
|
end
|
||||||
|
if string.sub(s,startPos,startPos+1)=='/*' then
|
||||||
|
return decode(s, decode_scanComment(s,startPos))
|
||||||
|
end
|
||||||
|
-- Otherwise, it must be a constant
|
||||||
|
return decode_scanConstant(s,startPos)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- The null function allows one to specify a null value in an associative array (which is otherwise
|
||||||
|
-- discarded if you set the value with 'nil' in Lua. Simply set t = { first=json.null }
|
||||||
|
function null()
|
||||||
|
return null -- so json.null() will also return null ;-)
|
||||||
|
end
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- Internal, PRIVATE functions.
|
||||||
|
-- Following a Python-like convention, I have prefixed all these 'PRIVATE'
|
||||||
|
-- functions with an underscore.
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
--- Scans an array from JSON into a Lua object
|
||||||
|
-- startPos begins at the start of the array.
|
||||||
|
-- Returns the array and the next starting position
|
||||||
|
-- @param s The string being scanned.
|
||||||
|
-- @param startPos The starting position for the scan.
|
||||||
|
-- @return table, int The scanned array as a table, and the position of the next character to scan.
|
||||||
|
function decode_scanArray(s,startPos)
|
||||||
|
local array = {} -- The return value
|
||||||
|
local stringLen = string.len(s)
|
||||||
|
assert(string.sub(s,startPos,startPos)=='[','decode_scanArray called but array does not start at position ' .. startPos .. ' in string:\n'..s )
|
||||||
|
startPos = startPos + 1
|
||||||
|
-- Infinite loop for array elements
|
||||||
|
repeat
|
||||||
|
startPos = decode_scanWhitespace(s,startPos)
|
||||||
|
assert(startPos<=stringLen,'JSON String ended unexpectedly scanning array.')
|
||||||
|
local curChar = string.sub(s,startPos,startPos)
|
||||||
|
if (curChar==']') then
|
||||||
|
return array, startPos+1
|
||||||
|
end
|
||||||
|
if (curChar==',') then
|
||||||
|
startPos = decode_scanWhitespace(s,startPos+1)
|
||||||
|
end
|
||||||
|
assert(startPos<=stringLen, 'JSON String ended unexpectedly scanning array.')
|
||||||
|
object, startPos = json.decode(s,startPos)
|
||||||
|
table.insert(array,object)
|
||||||
|
until false
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Scans a comment and discards the comment.
|
||||||
|
-- Returns the position of the next character following the comment.
|
||||||
|
-- @param string s The JSON string to scan.
|
||||||
|
-- @param int startPos The starting position of the comment
|
||||||
|
function decode_scanComment(s, startPos)
|
||||||
|
assert( string.sub(s,startPos,startPos+1)=='/*', "decode_scanComment called but comment does not start at position " .. startPos)
|
||||||
|
local endPos = string.find(s,'*/',startPos+2)
|
||||||
|
assert(endPos~=nil, "Unterminated comment in string at " .. startPos)
|
||||||
|
return endPos+2
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Scans for given constants: true, false or null
|
||||||
|
-- Returns the appropriate Lua type, and the position of the next character to read.
|
||||||
|
-- @param s The string being scanned.
|
||||||
|
-- @param startPos The position in the string at which to start scanning.
|
||||||
|
-- @return object, int The object (true, false or nil) and the position at which the next character should be
|
||||||
|
-- scanned.
|
||||||
|
function decode_scanConstant(s, startPos)
|
||||||
|
local consts = { ["true"] = true, ["false"] = false, ["null"] = nil }
|
||||||
|
local constNames = {"true","false","null"}
|
||||||
|
|
||||||
|
for i,k in pairs(constNames) do
|
||||||
|
if string.sub(s,startPos, startPos + string.len(k) -1 )==k then
|
||||||
|
return consts[k], startPos + string.len(k)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert(nil, 'Failed to scan constant from string ' .. s .. ' at starting position ' .. startPos)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Scans a number from the JSON encoded string.
|
||||||
|
-- (in fact, also is able to scan numeric +- eqns, which is not
|
||||||
|
-- in the JSON spec.)
|
||||||
|
-- Returns the number, and the position of the next character
|
||||||
|
-- after the number.
|
||||||
|
-- @param s The string being scanned.
|
||||||
|
-- @param startPos The position at which to start scanning.
|
||||||
|
-- @return number, int The extracted number and the position of the next character to scan.
|
||||||
|
function decode_scanNumber(s,startPos)
|
||||||
|
local endPos = startPos+1
|
||||||
|
local stringLen = string.len(s)
|
||||||
|
local acceptableChars = "+-0123456789.e"
|
||||||
|
while (string.find(acceptableChars, string.sub(s,endPos,endPos), 1, true)
|
||||||
|
and endPos<=stringLen
|
||||||
|
) do
|
||||||
|
endPos = endPos + 1
|
||||||
|
end
|
||||||
|
local stringValue = 'return ' .. string.sub(s,startPos, endPos-1)
|
||||||
|
local stringEval = loadstring(stringValue)
|
||||||
|
assert(stringEval, 'Failed to scan number [ ' .. stringValue .. '] in JSON string at position ' .. startPos .. ' : ' .. endPos)
|
||||||
|
return stringEval(), endPos
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Scans a JSON object into a Lua object.
|
||||||
|
-- startPos begins at the start of the object.
|
||||||
|
-- Returns the object and the next starting position.
|
||||||
|
-- @param s The string being scanned.
|
||||||
|
-- @param startPos The starting position of the scan.
|
||||||
|
-- @return table, int The scanned object as a table and the position of the next character to scan.
|
||||||
|
function decode_scanObject(s,startPos)
|
||||||
|
local object = {}
|
||||||
|
local stringLen = string.len(s)
|
||||||
|
local key, value
|
||||||
|
assert(string.sub(s,startPos,startPos)=='{','decode_scanObject called but object does not start at position ' .. startPos .. ' in string:\n' .. s)
|
||||||
|
startPos = startPos + 1
|
||||||
|
repeat
|
||||||
|
startPos = decode_scanWhitespace(s,startPos)
|
||||||
|
assert(startPos<=stringLen, 'JSON string ended unexpectedly while scanning object.')
|
||||||
|
local curChar = string.sub(s,startPos,startPos)
|
||||||
|
if (curChar=='}') then
|
||||||
|
return object,startPos+1
|
||||||
|
end
|
||||||
|
if (curChar==',') then
|
||||||
|
startPos = decode_scanWhitespace(s,startPos+1)
|
||||||
|
end
|
||||||
|
assert(startPos<=stringLen, 'JSON string ended unexpectedly scanning object.')
|
||||||
|
-- Scan the key
|
||||||
|
key, startPos = json.decode(s,startPos)
|
||||||
|
assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
|
||||||
|
startPos = decode_scanWhitespace(s,startPos)
|
||||||
|
assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
|
||||||
|
assert(string.sub(s,startPos,startPos)==':','JSON object key-value assignment mal-formed at ' .. startPos)
|
||||||
|
startPos = decode_scanWhitespace(s,startPos+1)
|
||||||
|
assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
|
||||||
|
value, startPos = json.decode(s,startPos)
|
||||||
|
object[key]=value
|
||||||
|
until false -- infinite loop while key-value pairs are found
|
||||||
|
end
|
||||||
|
|
||||||
|
-- START SoniEx2
|
||||||
|
-- Initialize some things used by decode_scanString
|
||||||
|
-- You know, for efficiency
|
||||||
|
local escapeSequences = {
|
||||||
|
["\\t"] = "\t",
|
||||||
|
["\\f"] = "\f",
|
||||||
|
["\\r"] = "\r",
|
||||||
|
["\\n"] = "\n",
|
||||||
|
["\\b"] = "\b"
|
||||||
|
}
|
||||||
|
setmetatable(escapeSequences, {__index = function(t,k)
|
||||||
|
-- skip "\" aka strip escape
|
||||||
|
return string.sub(k,2)
|
||||||
|
end})
|
||||||
|
-- END SoniEx2
|
||||||
|
|
||||||
|
--- Scans a JSON string from the opening inverted comma or single quote to the
|
||||||
|
-- end of the string.
|
||||||
|
-- Returns the string extracted as a Lua string,
|
||||||
|
-- and the position of the next non-string character
|
||||||
|
-- (after the closing inverted comma or single quote).
|
||||||
|
-- @param s The string being scanned.
|
||||||
|
-- @param startPos The starting position of the scan.
|
||||||
|
-- @return string, int The extracted string as a Lua string, and the next character to parse.
|
||||||
|
function decode_scanString(s,startPos)
|
||||||
|
assert(startPos, 'decode_scanString(..) called without start position')
|
||||||
|
local startChar = string.sub(s,startPos,startPos)
|
||||||
|
-- START SoniEx2
|
||||||
|
-- PS: I don't think single quotes are valid JSON
|
||||||
|
assert(startChar == [["]] or startChar == [[']],'decode_scanString called for a non-string')
|
||||||
|
--assert(startPos, "String decoding failed: missing closing " .. startChar .. " for string at position " .. oldStart)
|
||||||
|
local t = {}
|
||||||
|
local i,j = startPos,startPos
|
||||||
|
while string.find(s, startChar, j+1) ~= j+1 do
|
||||||
|
local oldj = j
|
||||||
|
i,j = string.find(s, "\\.", j+1)
|
||||||
|
local x,y = string.find(s, startChar, oldj+1)
|
||||||
|
if not i or x < i then
|
||||||
|
i,j = x,y-1
|
||||||
|
end
|
||||||
|
table.insert(t, string.sub(s, oldj+1, i-1))
|
||||||
|
if string.sub(s, i, j) == "\\u" then
|
||||||
|
local a = string.sub(s,j+1,j+4)
|
||||||
|
j = j + 4
|
||||||
|
local n = tonumber(a, 16)
|
||||||
|
assert(n, "String decoding failed: bad Unicode escape " .. a .. " at position " .. i .. " : " .. j)
|
||||||
|
-- math.floor(x/2^y) == lazy right shift
|
||||||
|
-- a % 2^b == bitwise_and(a, (2^b)-1)
|
||||||
|
-- 64 = 2^6
|
||||||
|
-- 4096 = 2^12 (or 2^6 * 2^6)
|
||||||
|
local x
|
||||||
|
if n < 0x80 then
|
||||||
|
x = string.char(n % 0x80)
|
||||||
|
elseif n < 0x800 then
|
||||||
|
-- [110x xxxx] [10xx xxxx]
|
||||||
|
x = string.char(0xC0 + (math.floor(n/64) % 0x20), 0x80 + (n % 0x40))
|
||||||
|
else
|
||||||
|
-- [1110 xxxx] [10xx xxxx] [10xx xxxx]
|
||||||
|
x = string.char(0xE0 + (math.floor(n/4096) % 0x10), 0x80 + (math.floor(n/64) % 0x40), 0x80 + (n % 0x40))
|
||||||
|
end
|
||||||
|
table.insert(t, x)
|
||||||
|
else
|
||||||
|
table.insert(t, escapeSequences[string.sub(s, i, j)])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.insert(t,string.sub(j, j+1))
|
||||||
|
assert(string.find(s, startChar, j+1), "String decoding failed: missing closing " .. startChar .. " at position " .. j .. "(for string at position " .. startPos .. ")")
|
||||||
|
return table.concat(t,""), j+2
|
||||||
|
-- END SoniEx2
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Scans a JSON string skipping all whitespace from the current start position.
|
||||||
|
-- Returns the position of the first non-whitespace character, or nil if the whole end of string is reached.
|
||||||
|
-- @param s The string being scanned
|
||||||
|
-- @param startPos The starting position where we should begin removing whitespace.
|
||||||
|
-- @return int The first position where non-whitespace was encountered, or string.len(s)+1 if the end of string
|
||||||
|
-- was reached.
|
||||||
|
function decode_scanWhitespace(s,startPos)
|
||||||
|
local whitespace=" \n\r\t"
|
||||||
|
local stringLen = string.len(s)
|
||||||
|
while ( string.find(whitespace, string.sub(s,startPos,startPos), 1, true) and startPos <= stringLen) do
|
||||||
|
startPos = startPos + 1
|
||||||
|
end
|
||||||
|
return startPos
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Encodes a string to be JSON-compatible.
|
||||||
|
-- This just involves back-quoting inverted commas, back-quotes and newlines, I think ;-)
|
||||||
|
-- @param s The string to return as a JSON encoded (i.e. backquoted string)
|
||||||
|
-- @return The string appropriately escaped.
|
||||||
|
|
||||||
|
local escapeList = {
|
||||||
|
['"'] = '\\"',
|
||||||
|
['\\'] = '\\\\',
|
||||||
|
['/'] = '\\/',
|
||||||
|
['\b'] = '\\b',
|
||||||
|
['\f'] = '\\f',
|
||||||
|
['\n'] = '\\n',
|
||||||
|
['\r'] = '\\r',
|
||||||
|
['\t'] = '\\t'
|
||||||
|
}
|
||||||
|
|
||||||
|
function json_private.encodeString(s)
|
||||||
|
local s = tostring(s)
|
||||||
|
return s:gsub(".", function(c) return escapeList[c] end) -- SoniEx2: 5.0 compat
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Determines whether the given Lua type is an array or a table / dictionary.
|
||||||
|
-- We consider any table an array if it has indexes 1..n for its n items, and no
|
||||||
|
-- other data in the table.
|
||||||
|
-- I think this method is currently a little 'flaky', but can't think of a good way around it yet...
|
||||||
|
-- @param t The table to evaluate as an array
|
||||||
|
-- @return boolean, number True if the table can be represented as an array, false otherwise. If true,
|
||||||
|
-- the second returned value is the maximum
|
||||||
|
-- number of indexed elements in the array.
|
||||||
|
function isArray(t)
|
||||||
|
-- Next we count all the elements, ensuring that any non-indexed elements are not-encodable
|
||||||
|
-- (with the possible exception of 'n')
|
||||||
|
local maxIndex = 0
|
||||||
|
for k,v in pairs(t) do
|
||||||
|
if (type(k)=='number' and math.floor(k)==k and 1<=k) then -- k,v is an indexed pair
|
||||||
|
if (not isEncodable(v)) then return false end -- All array elements must be encodable
|
||||||
|
maxIndex = math.max(maxIndex,k)
|
||||||
|
else
|
||||||
|
if (k=='n') then
|
||||||
|
if v ~= table.getn(t) then return false end -- False if n does not hold the number of elements
|
||||||
|
else -- Else of (k=='n')
|
||||||
|
if isEncodable(v) then return false end
|
||||||
|
end -- End of (k~='n')
|
||||||
|
end -- End of k,v not an indexed pair
|
||||||
|
end -- End of loop across all pairs
|
||||||
|
return true, maxIndex
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Determines whether the given Lua object / table / variable can be JSON encoded. The only
|
||||||
|
-- types that are JSON encodable are: string, boolean, number, nil, table and json.null.
|
||||||
|
-- In this implementation, all other types are ignored.
|
||||||
|
-- @param o The object to examine.
|
||||||
|
-- @return boolean True if the object should be JSON encoded, false if it should be ignored.
|
||||||
|
function isEncodable(o)
|
||||||
|
local t = type(o)
|
||||||
|
return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null)
|
||||||
|
end
|
||||||
|
|
||||||
|
return json
|
||||||
70
pyload.lua
Normal file
70
pyload.lua
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
-- TODO: support https
|
||||||
|
-- TODO: more info from api (see: https://github.com/pyload/pyload/blob/stable/module/Api.py)
|
||||||
|
-- TODO: provide icon
|
||||||
|
-- TODO: flashing on captcha request and open webinterface on click
|
||||||
|
local setmetatable = setmetatable
|
||||||
|
local io = { popen = io.popen }
|
||||||
|
|
||||||
|
local wibox = require("wibox")
|
||||||
|
|
||||||
|
local helpers = require("knorke.helpers")
|
||||||
|
local json = require("knorke.json")
|
||||||
|
|
||||||
|
local pyload = { mt = {} }
|
||||||
|
local thiswidget = wibox.widget.textbox()
|
||||||
|
|
||||||
|
-- all values in returned table are converted to string values
|
||||||
|
local function get_state(plhost, plport, pluser, plpasswd)
|
||||||
|
local pyload_state = {
|
||||||
|
["pause"] = "N/A", -- boolean
|
||||||
|
-- ["captcha"] = "N/A", -- boolean available in /json/status
|
||||||
|
["queue"] = "N/A", -- integer
|
||||||
|
["download"] = "N/A", -- boolean
|
||||||
|
["reconnect"] = "N/A", -- boolean
|
||||||
|
["active"] = "N/A", -- integer
|
||||||
|
["total"] = "N/A", -- integer
|
||||||
|
["speed"] = "N/A" -- float
|
||||||
|
}
|
||||||
|
|
||||||
|
-- first argument host, second port - with fallback to localhost:8000
|
||||||
|
local host = plhost or "127.0.0.1"
|
||||||
|
local port = plport or "8000"
|
||||||
|
local username = pluser or ""
|
||||||
|
local password = plpasswd or ""
|
||||||
|
|
||||||
|
local loginurl = "http://" .. host .. ":" .. port .. "/api/login"
|
||||||
|
local statusurl = "http://" .. host .. ":" .. port .. "/api/statusServer"
|
||||||
|
-- the /json/status url needs activated webinterface
|
||||||
|
|
||||||
|
-- the cookie from login will be passed to the secound curl command via pipe
|
||||||
|
local json_string = io.popen("curl --connect-timeout 1 -fsm 3 --data \"username=" .. username .. "&password=" .. password .. "\" --cookie-jar - " .. loginurl .. " | curl --connect-timeout 1 -fsm 3 --cookie - " .. statusurl)
|
||||||
|
for k, v in pairs(json.decode(json_string:read())) do
|
||||||
|
-- if k == "pause" then
|
||||||
|
pyload_state[k] = tostring(v)
|
||||||
|
end
|
||||||
|
json_string:close()
|
||||||
|
|
||||||
|
return pyload_state
|
||||||
|
end
|
||||||
|
|
||||||
|
local function update_data(args)
|
||||||
|
state = get_state(args.host, args.port, args.user, args.passwd)
|
||||||
|
thiswidget:set_text(helpers.sub_format_string(args.format, state))
|
||||||
|
end
|
||||||
|
|
||||||
|
local function setup_update(args)
|
||||||
|
mytimer = timer({timeout = tonumber(args.timeout)})
|
||||||
|
mytimer:connect_signal("timeout", function() update_data(args) end)
|
||||||
|
mytimer:start()
|
||||||
|
end
|
||||||
|
|
||||||
|
function pyload.new(args)
|
||||||
|
thiswidget.update = setup_update(args)
|
||||||
|
return thiswidget
|
||||||
|
end
|
||||||
|
|
||||||
|
function pyload.mt:__call(...)
|
||||||
|
return pyload.new(...)
|
||||||
|
end
|
||||||
|
|
||||||
|
return setmetatable(pyload, pyload.mt)
|
||||||
31
testwidget.lua
Normal file
31
testwidget.lua
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
-- minimal widget to show how it works
|
||||||
|
|
||||||
|
local setmetatable = setmetatable
|
||||||
|
local wibox = require("wibox")
|
||||||
|
|
||||||
|
local testwidget = { mt ={} }
|
||||||
|
local w = wibox.widget.textbox()
|
||||||
|
|
||||||
|
-- cares about getting data and outputting it
|
||||||
|
local function updatedata()
|
||||||
|
w:set_text(tostring(os.time()))
|
||||||
|
end
|
||||||
|
|
||||||
|
-- setup the update cycle an call the functions that update the data
|
||||||
|
local function setupupdate(t)
|
||||||
|
mytimer = timer({timeout = t})
|
||||||
|
mytimer:connect_signal("timeout", function() updatedata() end)
|
||||||
|
mytimer:start()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- create this widget
|
||||||
|
function testwidget.new(args)
|
||||||
|
w.update = setupupdate(tonumber(args.t))
|
||||||
|
return w
|
||||||
|
end
|
||||||
|
|
||||||
|
function testwidget.mt:__call(...)
|
||||||
|
return testwidget.new(...)
|
||||||
|
end
|
||||||
|
|
||||||
|
return setmetatable(testwidget, testwidget.mt)
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
local setmetatable = setmetatable
|
|
||||||
local wrequire = require("knorke.helpers").wrequire
|
|
||||||
|
|
||||||
local widgets = { _NAME = "knorke.widgets" }
|
|
||||||
|
|
||||||
-- Load modules at runtime as needed
|
|
||||||
return setmetatable(contrib, { __index = wrequire })
|
|
||||||
Reference in New Issue
Block a user