Misc: Get rid of the legacy defaults.lua globals (#9546)

* This removes support for the following deprecated constants: `DTAP_ZONE_FLIPPING`, `DTAP_ZONE_BOOKMARK`, `DCREREADER_CONFIG_DEFAULT_FONT_GAMMA`
* The "Advanced settings" panel now highlights modified values in bold (think about:config in Firefox ;)).
* LuaData: Isolate global table lookup shenanigans, and fix a few issues in unused-in-prod codepaths.
* CodeStyle: Require module locals for Lua/C modules, too.
* ScreenSaver: Actually garbage collect our widget on close (ScreenSaver itself is not an instantiated object).
* DateTimeWidget: Code cleanups to ensure child widgets can be GC'ed.
This commit is contained in:
NiLuJe
2022-09-28 01:10:50 +02:00
committed by GitHub
parent 46f729c248
commit 62059f8d68
74 changed files with 1030 additions and 1050 deletions

View File

@@ -5,6 +5,7 @@ Handles append-mostly data such as KOReader's bookmarks and dictionary search hi
local LuaSettings = require("luasettings")
local dbg = require("dbg")
local dump = require("dump")
local lfs = require("libs/libkoreader-lfs")
local logger = require("logger")
local util = require("util")
@@ -28,36 +29,54 @@ function LuaData:open(file_path, o) -- luacheck: ignore 312
local new = {file=file_path, data={}}
-- some magic to allow for self-describing function names
local _local = {}
_local.__index = _local
setmetatable(_G, _local)
_local[self.name.."Entry"] = function(table)
-- Some magic to allow for self-describing function names:
-- We'll use data_env both as the environment when loading the data, *and* its metatable,
-- *and* as the target of its index lookup metamethod.
-- Its NameEntry field is a function responsible for actually storing the data in the right place in the LuaData object.
-- It gets called via __index lookup in the global scope (i.e., the env) when Lua tries to resolve
-- the global NameEntry function calls in our stored data.
-- NOTE: We could also make the metatable's __index field point to a function, and handle the lookup ourselves inside it,
-- but using an empty env with loadfile is not a bad idea to begin with anyway ;).
local data_env = {}
data_env.__index = data_env
setmetatable(data_env, data_env)
data_env[self.name.."Entry"] = function(table)
if table.index then
-- we've got a deleted setting, overwrite with nil
if not table.data then new.data[table.index] = nil end
new.data[table.index] = new.data[table.index] or {}
local size = util.tableSize(table.data)
if size == 1 then
for key, value in pairs(table.data) do
new.data[table.index][key] = value
-- We've got a deleted setting, overwrite with nil and be done with it.
if not table.data then
new.data[table.index] = nil
return
end
if type(table.data) == "table" then
new.data[table.index] = new.data[table.index] or {}
local size = util.tableSize(table.data)
if size == 1 then
-- It's an incremental array element, insert it in the array at its proper index
for key, value in pairs(table.data) do
new.data[table.index][key] = value
end
else
-- It's a complex table, just replace the whole thing
new.data[table.index] = table.data
end
else
new.data[table.index] = table.data
end
-- we've got it all at once
else
-- It's an untagged blob, use it as-is
new.data = table
end
end
local ok = false
local ok, err
if lfs.attributes(new.file, "mode") == "file" then
ok = pcall(dofile, new.file)
ok, err = loadfile(new.file, "t", data_env)
if ok then
logger.dbg("data is read from ", new.file)
logger.dbg("data is read from", new.file)
ok()
else
logger.dbg(new.file, " is invalid, remove.")
logger.dbg(new.file, "is invalid, removed.", err)
os.remove(new.file)
end
end
@@ -65,11 +84,13 @@ function LuaData:open(file_path, o) -- luacheck: ignore 312
for i=1, self.max_backups, 1 do
local backup_file = new.file..".old."..i
if lfs.attributes(backup_file, "mode") == "file" then
if pcall(dofile, backup_file) then
logger.dbg("data is read from ", backup_file)
ok, err = loadfile(backup_file, "t", data_env)
if ok then
logger.dbg("data is read from", backup_file)
ok()
break
else
logger.dbg(backup_file, " is invalid, remove.")
logger.dbg(backup_file, "is invalid, removed.", err)
os.remove(backup_file)
end
end
@@ -109,10 +130,9 @@ function LuaData:addTableItem(table_name, value)
}
end
local _orig_removeTableItem = LuaSettings.removeTableItem
--- Removes index from table.
function LuaData:removeTableItem(key, index)
_orig_removeTableItem(self, key, index)
LuaSettings.removeTableItem(self, key, index)
self:flush()
return self
end
@@ -123,6 +143,7 @@ function LuaData:append(data)
local f_out = io.open(self.file, "a")
if f_out ~= nil then
os.setlocale('C', 'numeric')
-- NOTE: This is a function call, with a table as its single argument. Parentheses are elided.
f_out:write(self.name.."Entry")
f_out:write(dump(data))
f_out:write("\n")
@@ -145,17 +166,17 @@ function LuaData:flush()
if lfs.attributes(self.file, "mode") == "file" then
for i=1, self.max_backups, 1 do
if lfs.attributes(self.file..".old."..i, "mode") == "file" then
logger.dbg("LuaData: Rename ", self.file .. ".old." .. i, " to ", self.file .. ".old." .. i+1)
logger.dbg("LuaData: Rename", self.file .. ".old." .. i, "to", self.file .. ".old." .. i+1)
os.rename(self.file, self.file .. ".old." .. i+1)
else
break
end
end
logger.dbg("LuaData: Rename ", self.file, " to ", self.file .. ".old.1")
logger.dbg("LuaData: Rename", self.file, "to", self.file .. ".old.1")
os.rename(self.file, self.file .. ".old.1")
end
logger.dbg("LuaData: Write to ", self.file)
logger.dbg("LuaData: Write to", self.file)
local f_out = io.open(self.file, "w")
if f_out ~= nil then
os.setlocale('C', 'numeric')